2 Star 0 Fork 1

licshire / angel

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
ftrl_fm_spark_en.md 4.74 KB
一键复制 编辑 原始数据 按行查看 历史
rachelsunrh 提交于 2019-08-16 17:55 . fix docs

Training Factorization Machine with FTRL on Spark on Angel

FM(Factorization Machine) is an algorithm based on matrix decomposition which can predict any real-valued vector.

Its main advantages include:

  • can handle highly sparse data;
  • linear computational complexity

FTRL (Follow-the-regularized-leader) is an optimization algorithm which is widely deployed by online learning. Employing FTRL is easy in Spark-on-Angel and you can train a model with billions, even ten billions, dimensions once you have enough machines.

Here, we will use FTRL Optimizer to update the parameters of FM.

If you are not familiar with how to programming on Spark-on-Angel, please first refer to Programming Guide for Spark-on-Angel;

Factorization Model

model

where is the dot of two k-dimension vector:

dot

model parameters: parameter , where n is the number of feature, represents feature i composed by k factors, k is a hyperparameter that determines the factorization.

Using the FTRL-FM


import com.tencent.angel.ml.math2.utils.RowType
import com.tencent.angel.spark.ml.online_learning.FtrlFM

// allocate a ftrl optimizer with (lambda1, lambda2, alpha, beta)
val optim = new FtrlFM(lambda1, lambda2, alpha, beta)
// initializing the model
optim.init(dim, factor)

There are four hyper-parameters for the FTRL optimizer, which are lambda1, lambda2, alpha and beta. We allocate a FTRL optimizer with these four hyper-parameters. The next step is to initialized a FtrlFM model. There are two matrixs for FtrlFM, including first and second, the first contains the z, n and w in which z and n are used to init or update parameter w in FM, the second contains the z, n and v in which z and n are used to init or update parameter v in FM. In the aboving code, we allocate first a sparse distributed matrix with 3 rows and dim columns, and allocate second a sparse distributed matrix with 3 * factor rows and dim columns.

set the dimension

In the scenaro of online learning, the index of features can be range from (int.min, int.max), which is usually generated by a hash function. In Spark-on-Angel, you can set the dim=-1 when your feature index range from (int.min, int.max) and rowType is sparse. If the feature index range from [0, n), you can set the dim=n.

Training with Spark

loading data

Using the interface of RDD to load data and parse them to vectors.

val data = sc.textFile(input).repartition(partNum)
      .map(s => (DataLoader.parseIntFloat(s, dim), DataLoader.parseLabel(s, false)))
      .map {
        f =>
          f._1.setY(f._2)
          f._1
      }

training model

val size = data.count()
for (epoch <- 1 to numEpoch) {
    val totalLoss = data.mapPartitions {
        case iterator =>
        // for each partition
          val loss = iterator
            .sliding(batchSize, batchSize)
            .zipWithIndex
            .map(f => optim.optimize(f._2, f_1.toArray)).sum
          Iterator.single(loss)
    }.sum()
    println(s"epoch=$epoch loss=${totalLoss / size}")
}

saving model

output = "hdfs://xxx"
optim.weight
optim.save(output + "/back")
optim.saveWeight(output)

Submit Command

source ./bin/spark-on-angel-env.sh
 
$SPARK_HOME/bin/spark-submit \
    --master yarn-cluster \
    --conf spark.yarn.allocation.am.maxMemory=55g \
    --conf spark.yarn.allocation.executor.maxMemory=55g \
    --conf spark.driver.maxResultSize=20g \
    --conf spark.kryoserializer.buffer.max=2000m\
    --conf spark.ps.jars=$SONA_ANGEL_JARS \
    --conf spark.ps.instances=1 \
    --conf spark.ps.cores=2 \
    --conf spark.ps.memory=5g \
    --conf spark.ps.log.level=INFO \
    --conf spark.offline.evaluate=200\
    --jars $SONA_SPARK_JARS  \
    --name "FTRLFM on Spark-on-Angel" \
    --driver-memory 5g \
    --num-executors 5 \
    --executor-cores 2 \
    --executor-memory 2g \
    --class org.apache.spark.angel.examples.oneline_learning.FtrlFMExample \
    ./lib/angelml-${SONA_version}.jar \
    input:$input modelPath:$model dim:$dim batchSize:$batchSize actionType:train factor:5

detail parameters

1
https://gitee.com/licshire/angel.git
git@gitee.com:licshire/angel.git
licshire
angel
angel
master

搜索帮助