├── LICENSE
├── Makefile
├── README.md
├── bin
└── .gitignore
├── fm_predict.cpp
├── fm_train.cpp
└── src
├── FTRL
├── ftrl_model.h
├── ftrl_predictor.h
└── ftrl_trainer.h
├── Frame
├── Makefile
├── pc_frame.cpp
├── pc_frame.h
├── pc_task.h
├── test_main.cpp
└── test_task.h
├── Sample
└── fm_sample.h
└── Utils
├── utils.cpp
└── utils.h
/LICENSE:
--------------------------------------------------------------------------------
1 | MIT License
2 |
3 | Copyright (c) 2021 Zhang Wei
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE.
22 |
--------------------------------------------------------------------------------
/Makefile:
--------------------------------------------------------------------------------
1 | all:
2 | g++ -O3 fm_train.cpp src/Frame/pc_frame.cpp src/Utils/utils.cpp -I . -std=c++0x -o bin/fm_train_softmax -lpthread
3 | g++ -O3 fm_predict.cpp src/Frame/pc_frame.cpp src/Utils/utils.cpp -I . -std=c++0x -o bin/fm_predict_softmax -lpthread
4 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # alphaFM_softmax
2 | ## 前言:
3 | * alphaFM_softmax是[alphaFM](https://github.com/CastellanZhang/alphaFM)的多分类版本。
4 |
5 | * 算法原理见我的博客文章:http://castellanzhang.github.io/2016/10/16/fm_ftrl_softmax/
6 |
7 | * 当将dim参数设置为1,1,0时,alphaFM_softmax就退化成标准的softmax的FTRL训练工具。
8 |
9 | ## 安装方法:
10 | 直接在根目录make即可,编译后会在bin目录下生成两个可执行文件。如果编译失败,请升级gcc版本。
11 | ## 输入文件格式:
12 | 类似于libsvm格式,但更加灵活:特征编号不局限于整数也可以是字符串;特征值可以是整数或浮点数(特征值最好做归一化处理,否则可能会导致结果为nan),
13 | 特征值为0的项可以省略不写;label必须是1到k(假设类别数为k)。举例如下:
14 | `1 sex:1 age:0.3 f1:1 f3:0.9`
15 | `3 sex:0 age:0.7 f2:0.4 f5:0.8 f8:1`
16 | `2 sex:0 age:0.2 f2:0.6 f8:1`
17 | `...`
18 | ## 模型文件格式:
19 | 假定v的维度为f,类别数为k,类似于alphaFM的格式,只是feature_name之后的部分从1段变成了k段
20 | 第一行是bias的参数:
21 | `bias w_1 w_n_1 w_z_1 w_2 w_n_2 w_z_2 ... w_k w_n_k w_z_k`
22 | 其他行的格式为:
23 | `feature_name w_1 v1_1 v2_1 ... vf_1 w_n_1 w_z_1 v_n1_1 v_n2_1 ... v_nf_1 v_z1_1 v_z2_1 ... v_zf_1 w_2 v1_2 v2_2 ... vf_2 w_n_2 w_z_2 v_n1_2 v_n2_2 ... v_nf_2 v_z1_2 v_z2_2 ... v_zf_2 ... ... w_k v1_k v2_k ... vf_k w_n_k w_z_k v_n1_k v_n2_k ... v_nf_k v_z1_k v_z2_k ... v_zf_k`
24 | ## 预测结果格式:
25 | `label score_1 score_2 ... score_k`
26 | label为输入数据的类别标注值(1到k),score_i为样本属于类别i的预测概率值。
27 |
28 | ## 参数说明:
29 | ### fm_train_softmax的参数:
30 | 和alphaFM基本一致,多了一个-cn参数,少了一个-fvs参数
31 | -m \: 设置模型文件的输出路径。
32 | -cn \: 设置类别数。
33 | -dim \: k0为1表示使用偏置w0参数,0表示不使用;k1为1表示使用w参数,为0表示不使用;k2为v的维度,可以是0。 default:1,1,8
34 | -init_stdev \: v的初始化使用均值为0的高斯分布,stdev为标准差。 default:0.1
35 | -w_alpha \: w0和w的FTRL超参数alpha。 default:0.05
36 | -w_beta \: w0和w的FTRL超参数beta。 default:1.0
37 | -w_l1 \: w0和w的L1正则。 default:0.1
38 | -w_l2 \: w0和w的L2正则。 default:5.0
39 | -v_alpha \: v的FTRL超参数alpha。 default:0.05
40 | -v_beta \: v的FTRL超参数beta。 default:1.0
41 | -v_l1 \: v的L1正则。 default:0.1
42 | -v_l2 \: v的L2正则。 default:5.0
43 | -core \: 计算线程数。 default:1
44 | -im \: 上次模型的路径,用于初始化模型参数。如果是第一次训练则不用设置此参数。
45 | ### fm_predict_softmax的参数:
46 | 比alphaFM多了一个-cn参数
47 | -m \: 模型文件路径。
48 | -cn \: 设置类别数。
49 | -dim \: v的维度。 default:8
50 | -core \: 计算线程数。 default:1
51 | -out \: 输出文件路径。
52 | ## 计算速度:
53 | ### 我的实验结果:
54 | 本地1000万的样本,200万的特征维度,7个类别,v的维度为12,2.10GHz的CPU,开10个线程,非缺省参数如下:
55 | `-cn 7 -dim 1,1,12 -w_l1 0.05 -v_l1 0.05 -init_stdev 0.001 -w_alpha 0.01 -v_alpha 0.01 -core 10`
56 | 训练时间大概半个多小时。
57 |
58 |
--------------------------------------------------------------------------------
/bin/.gitignore:
--------------------------------------------------------------------------------
1 | # Ignore everything in this directory
2 | *
3 | # Except this file !.gitignore
4 | !.gitignore
5 |
--------------------------------------------------------------------------------
/fm_predict.cpp:
--------------------------------------------------------------------------------
1 | #include
2 | #include