Skip to content

Commit 7097f8f

Browse files
committed
[DOCS] Improve Core API
1 parent 0e6e934 commit 7097f8f

File tree

10 files changed

+89
-82
lines changed

10 files changed

+89
-82
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
* [模型切分(modelPartitioner)](./docs/design/model_partitioner.md)
3838
* [异步控制(syncController)](./docs/design/sync_controller.md)
3939
* [定制函数(psFunc)](./docs/design/psfFunc.md)
40-
* [核心类的说明](./docs/apis/interface_api.md)
40+
* [核心接口类](./docs/apis/core_api.md)
4141

4242

4343
## Algorithm

README_en.md

+4-1
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,11 @@ We welcome everyone interested in machine learning to contribute code, create is
3333

3434
## Design
3535

36-
* [Interface API](./docs/apis/interface_api.md)
36+
* [Model Partitioner](./docs/design/model_partitioner.md)
37+
* [SyncController](./docs/design/sync_controller.md)
3738
* [psFunc](./docs/design/psfFunc.md)
39+
* [Core API](./docs/apis/core_api.md)
40+
3841

3942
## Algorithm
4043

angel-ps/mllib/src/main/java/com/tencent/angel/ml/factorizationmachines/FMLearner.scala

+25-20
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,11 @@ import scala.collection.mutable
3838
* @param maxP: max value of y
3939
* @param feaUsed: array of used feature of the input data
4040
*/
41-
class FMLearner(override val ctx: TaskContext, val minP: Double, val maxP: Double, val feaUsed:
42-
Array[Int]) extends MLLearner(ctx) {
41+
class FMLearner(override val ctx: TaskContext, val minP: Double, val maxP: Double, val feaUsed: Array[Int])
42+
extends MLLearner(ctx) {
43+
4344
val LOG: Log = LogFactory.getLog(classOf[FMLearner])
44-
val fmmodel = new FMModel(conf, ctx)
45+
val fmModel = new FMModel(conf, ctx)
4546

4647
val learnType = conf.get(MLConf.ML_FM_LEARN_TYPE, MLConf.DEFAULT_ML_FM_LEARN_TYPE)
4748
val feaNum: Int = conf.getInt(MLConf.ML_FEATURE_NUM, MLConf.DEFAULT_ML_FEATURE_NUM)
@@ -53,12 +54,11 @@ Array[Int]) extends MLLearner(ctx) {
5354
val reg2: Double = conf.getDouble(MLConf.ML_FM_REG2, MLConf.DEFAULT_ML_FM_REG2)
5455
val lr: Double = conf.getDouble(MLConf.ML_LEARN_RATE, MLConf.DEFAULT_ML_LEAR_RATE)
5556
val vStddev: Double = conf.getDouble(MLConf.ML_FM_V_STDDEV, MLConf.DEFAULT_ML_FM_V_INIT)
56-
// Put used feature indexes to vIndexs
57-
//val vIndexs = feaUsed.zipWithIndex.filter((p:(Int,Int))=>p._1!=0).map((p:(Int,Int))=>p._2).array
58-
val vIndexs = new RowIndex()
59-
feaUsed.zipWithIndex.filter((p:(Int, Int))=>p._1!=0).map((p:(Int, Int))=>vIndexs.addRowId(p._2))
60-
val feaUsedN = vIndexs.getRowsNumber
61-
LOG.info("vIndexs's row's number = " + vIndexs)
57+
58+
val vIndexes = new RowIndex()
59+
feaUsed.zipWithIndex.filter((p:(Int, Int))=>p._1!=0).map((p:(Int, Int))=>vIndexes.addRowId(p._2))
60+
val feaUsedN = vIndexes.getRowsNumber
61+
LOG.info("vIndexs's row's number = " + vIndexes)
6262

6363
/**
6464
* Train a Factorization machines Model
@@ -68,8 +68,7 @@ Array[Int]) extends MLLearner(ctx) {
6868
* @return : a learned model
6969
*/
7070
override
71-
def train(trainData: DataBlock[LabeledData], vali: DataBlock[LabeledData]):
72-
MLModel = {
71+
def train(trainData: DataBlock[LabeledData], vali: DataBlock[LabeledData]): MLModel = {
7372
val start = System.currentTimeMillis()
7473
LOG.info(s"learnType=$learnType, feaNum=$feaNum, rank=$rank, #trainData=${trainData.size}")
7574
LOG.info(s"reg0=$reg0, reg1=$reg1, reg2=$reg2, lr=$lr, vStev=$vStddev")
@@ -79,7 +78,7 @@ Array[Int]) extends MLLearner(ctx) {
7978
val initCost = System.currentTimeMillis() - beforeInit
8079
LOG.info(s"Init matrixes cost $initCost ms.")
8180

82-
globalMetrics.addMetrics(fmmodel.FM_OBJ, LossMetric(trainData.size()))
81+
globalMetrics.addMetrics(fmModel.FM_OBJ, LossMetric(trainData.size()))
8382

8483
while (ctx.getIteration < epochNum) {
8584
val startIter = System.currentTimeMillis()
@@ -90,7 +89,7 @@ Array[Int]) extends MLLearner(ctx) {
9089
val loss = evaluate(trainData, w0.get(0), w, v)
9190
val valiCost = System.currentTimeMillis() - startVali
9291

93-
globalMetrics.metrics(fmmodel.FM_OBJ, loss)
92+
globalMetrics.metrics(fmModel.FM_OBJ, loss)
9493
LOG.info(s"Epoch=${ctx.getIteration}, evaluate loss=${loss/trainData.size()}. " +
9594
s"trainCost=$iterCost, " +
9695
s"valiCost=$valiCost")
@@ -101,7 +100,7 @@ Array[Int]) extends MLLearner(ctx) {
101100
val end = System.currentTimeMillis()
102101
val cost = end - start
103102
LOG.info(s"FM Learner train cost $cost ms.")
104-
fmmodel
103+
fmModel
105104
}
106105

107106
/**
@@ -110,22 +109,24 @@ Array[Int]) extends MLLearner(ctx) {
110109
def initModels(): Unit = {
111110
if(ctx.getTaskId.getIndex == 0) {
112111
for (row <- 0 until feaNum) {
113-
fmmodel.v.update(new RandomNormal(fmmodel.v.getMatrixId(), row, 0.0, vStddev)).get()
112+
fmModel.v.update(new RandomNormal(fmModel.v.getMatrixId(), row, 0.0, vStddev)).get()
114113
}
115114
}
116115

117-
fmmodel.v.clock().get()
116+
fmModel.v.clock().get()
118117
}
119118

120119
/**
121120
* One iteration to train Factorization Machines
121+
*
122122
* @param dataBlock
123123
* @return
124124
*/
125-
def oneIteration(dataBlock: DataBlock[LabeledData]): (DenseDoubleVector,
126-
DenseDoubleVector, mutable.HashMap[Int, DenseDoubleVector]) = {
125+
def oneIteration(dataBlock: DataBlock[LabeledData]):
126+
(DenseDoubleVector, DenseDoubleVector, mutable.HashMap[Int, DenseDoubleVector]) = {
127+
127128
val startGet = System.currentTimeMillis()
128-
val (w0, w, v) = fmmodel.pullFromPS(vIndexs)
129+
val (w0, w, v) = fmModel.pullFromPS(vIndexes)
129130
val getCost = System.currentTimeMillis() - startGet
130131
LOG.info(s"Get matrixes cost $getCost ms.")
131132

@@ -154,7 +155,7 @@ Array[Int]) extends MLLearner(ctx) {
154155
v(update._1).plusBy(update._2, -1.0).timesBy(-1.0)
155156
}
156157

157-
fmmodel.pushToPS(w0.plusBy(_w0, -1.0).timesBy(-1.0).asInstanceOf[DenseDoubleVector],
158+
fmModel.pushToPS(w0.plusBy(_w0, -1.0).timesBy(-1.0).asInstanceOf[DenseDoubleVector],
158159
w.plusBy(_w, -1.0).timesBy(-1.0).asInstanceOf[DenseDoubleVector],
159160
v)
160161

@@ -163,6 +164,7 @@ Array[Int]) extends MLLearner(ctx) {
163164

164165
/**
165166
* Evaluate the objective value
167+
*
166168
* @param dataBlock
167169
* @param w0
168170
* @param w
@@ -188,6 +190,7 @@ Array[Int]) extends MLLearner(ctx) {
188190

189191
/**
190192
* Predict an instance
193+
*
191194
* @param x:feature vector of instance
192195
* @param y: label value of instance
193196
* @param w0: w0 mat of FM
@@ -220,6 +223,7 @@ Array[Int]) extends MLLearner(ctx) {
220223

221224
/**
222225
* \frac{\partial loss}{\partial x} = dm * \frac{\partial y}{\partial x}
226+
*
223227
* @param y: label of the instance
224228
* @param pre: predict value of the instance
225229
* @return : dm value
@@ -240,6 +244,7 @@ Array[Int]) extends MLLearner(ctx) {
240244

241245
/**
242246
* Update v mat
247+
*
243248
* @param x: a train instance
244249
* @param dm: dm value of the instance
245250
* @param v: v mat

docs/apis/MLLearner.md

+9-7
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
# MLLearner
2-
模型训练接口定义。
3-
## train
4-
- 定义:```def train(train: DataBlock[LabeledData], vali: DataBlock[LabeledData])```
52

3+
> 模型训练的核心类,理论上,Angel所有模型训练核心逻辑,都应该写在这个类中。通过这个类来实现和调用。它是Train的核心类。
64
7-
- 功能描述:使用算法对训练数据进行训练,得到模型
5+
## 功能
86

7+
* 不断读取DataBlock,训练得到MLModel模型
98

10-
- 参数:train: DataBlock[LabeledData] 训练数据集; vali: DataBlock[LabeledData] 验证数据集
9+
## 核心接口
1110

12-
13-
- 返回值:训练结果
11+
1. **train**
12+
- 定义:```def train(train: DataBlock[LabeledData], vali: DataBlock[LabeledData]):MLModel```
13+
- 功能描述:使用算法对训练数据进行训练,得到模型
14+
- 参数:train: DataBlock[LabeledData] 训练数据集; vali: DataBlock[LabeledData] 验证数据集
15+
- 返回值:MLModel

docs/apis/MLRunner.md

+33-36
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,49 @@
11
# MLRunner
2-
算法的启动类。它定义了启动Angel app和运行task的接口,需要应用程序根据需要实现这些接口。当然,它在封装了使用 [AngelClient](AngelClient.md)的使用。
32

4-
包括:启动Angel ps,加载和存储模型,和启动task等过程的默认实现。一般情况下,应用程序直接调用它们就可以了。
3+
> Angel算法的启动入口类。它定义了启动Angel app标准流程,封装了对 [AngelClient](AngelClient.md)的使用。
54
6-
## train
7-
- 定义:``` train(conf: Configuration)```
8-
- 功能描述:启动Angel app训练模型
9-
- 参数:conf: Configuration Angel任务相关配置和算法配置信息
10-
- 返回值:无
5+
## 功能
116

12-
## train(default implementation)
7+
* 启动Angel ps,加载和存储模型,和启动task等过程的默认实现
8+
* 一般情况下,应用程序直接调用它们就可以了。
139

14-
- **定义**```train(conf: Configuration, model: MLModel, taskClass: Class[_ <: BaseTask[_, _, _]]): Unit```
10+
## 核心方法
1511

16-
- **功能描述**:启动Angel app训练模型。该方法封装了具体的Angel ps/worker启动以及模型加载/存储过程,子类可直接引用
17-
- **参数**
18-
* conf: Configuration Angel任务相关配置和算法配置信息
19-
* model: MLModel 算法模型信息
20-
* taskClass: Class[_ <: TrainTask[_, _, _]] 表示算法运行过程的Task 类
21-
* 返回值:无
12+
1. **train**
13+
- 定义:``` train(conf: Configuration)```
14+
- 功能描述:启动Angel app训练模型
15+
- 参数:conf: Configuration Angel任务相关配置和算法配置信息
16+
- 返回值:无
2217

23-
## incTrain
24-
* **定义**```incTrain(conf: Configuration)```
25-
- **功能描述**:启动Angel app 并使用增量训练的方式更新一个旧模型
26-
- **参数**
27-
- conf: Configuration Angel任务相关配置和算法配置信息
28-
- **返回值**:无
18+
2. **train(default implementation)**
2919

30-
## predict
31-
- **定义**```predict(conf: Configuration)```
20+
- **定义**: ```train(conf: Configuration, model: MLModel, taskClass: Class[_ <: BaseTask[_, _, _]]): Unit```
3221

22+
- **功能描述**:启动Angel app训练模型。该方法封装了具体的Angel ps/worker启动以及模型加载/存储过程,子类可直接引用
3323

34-
- **功能描述**:启动Angel app,计算预测结果
24+
- **参数**
25+
* conf: Configuration Angel任务相关配置和算法配置信息
26+
* model: MLModel 算法模型信息
27+
* taskClass: Class[_ <: TrainTask[_, _, _]] 表示算法运行过程的Task 类
28+
* 返回值:无
3529

30+
3. **incTrain**
3631

37-
- **参数**:conf: Configuration Angel任务相关配置
32+
* **定义**:```incTrain(conf: Configuration)```
33+
- **功能描述**:使用增量训练的方式更新一个已有模型
34+
- **参数:**conf: Configuration Angel任务相关配置和算法配置信息
35+
- **返回值:**无
3836

37+
4. **predict**
3938

40-
- **返回值**:无
39+
- **定义**:```predict(conf: Configuration)```
40+
- **功能描述**:启动Angel app,计算预测结果
41+
- **参数**:conf: Configuration Angel任务相关配置
42+
- **返回值**:无
4143

42-
## predict(default implementation)
43-
- **定义:**```predict(conf: Configuration, model: MLModel, taskClass: Class[_ <: PredictTask[_, _, _]]): Unit```
44+
5. **predict(default implementation)**
4445

45-
46-
- **功能描述:**启动Angel app 并使用增量训练的方式更新一个旧模型。该方法封装了具体的Angel ps/worker启动以及模型加载/存储过程,子类可直接引用
47-
48-
49-
- **参数:**conf: Configuration Angel任务相关配置
50-
51-
52-
- **返回值:**
46+
- **定义:** ```predict(conf: Configuration, model: MLModel, taskClass: Class[_ <: PredictTask[_, _, _]]): Unit```
47+
- **功能描述:**启动Angel app 并使用增量训练的方式更新一个旧模型。该方法封装了具体的Angel ps/worker启动以及模型加载/存储过程,子类可直接引用
48+
- **参数:**conf: Configuration Angel任务相关配置
49+
- **返回值:**无

docs/apis/angel_client.md

-5
This file was deleted.

docs/apis/core_api.md

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# 核心接口类
2+
3+
---
4+
5+
![](../img/angel_class_diagram.png)
6+
7+
如上图所示,Angel的核心接口类,在Train的过程中,按照调用的顺序,分别为:
8+
9+
* [MLRunner](MLRunner.md)
10+
* [AngelClient](AngelClient.md)
11+
* [TrainTask](Task.md)
12+
* [DataBlock](DataBlock.md)
13+
* [MLLearner](MLLearner.md)
14+
* [PSModel](PSModel.md)
15+
* [MLModel](MLModel.md)
16+
17+
了解这些接口类的定义和功能,对于基于Angel实现高性能的机器学习算法,会有不错的帮助。

docs/apis/gpu_adpater.md

-2
This file was deleted.

docs/apis/interface_api.md

-10
This file was deleted.

docs/img/angel_class_diagram.png

320 KB
Loading

0 commit comments

Comments
 (0)