Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
BaiXuePrincess
Paddle
提交
947b6a77
P
Paddle
项目概览
BaiXuePrincess
/
Paddle
与 Fork 源项目一致
Fork自
PaddlePaddle / Paddle
通知
1
Star
1
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
Paddle
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
提交
947b6a77
编写于
10月 17, 2017
作者:
W
wangmeng28
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Implement factorization machine layer
上级
f504c8a8
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
73 addition
and
6 deletion
+73
-6
paddle/gserver/layers/FactorizationMachineLayer.cpp
paddle/gserver/layers/FactorizationMachineLayer.cpp
+58
-4
paddle/gserver/layers/FactorizationMachineLayer.h
paddle/gserver/layers/FactorizationMachineLayer.h
+12
-0
paddle/gserver/tests/test_LayerGrad.cpp
paddle/gserver/tests/test_LayerGrad.cpp
+3
-2
未找到文件。
paddle/gserver/layers/FactorizationMachineLayer.cpp
浏览文件 @
947b6a77
...
@@ -33,7 +33,10 @@ bool FactorizationMachineLayer::init(const LayerMap& layerMap,
...
@@ -33,7 +33,10 @@ bool FactorizationMachineLayer::init(const LayerMap& layerMap,
/* initialize the latentVectors_ */
/* initialize the latentVectors_ */
CHECK_EQ
(
inputLayers_
.
size
(),
1UL
);
CHECK_EQ
(
inputLayers_
.
size
(),
1UL
);
size_t
height
=
inputLayers_
[
0
]
->
getSize
();
size_t
height
=
inputLayers_
[
0
]
->
getSize
();
latentVectors_
.
reset
(
new
Weight
(
height
,
factorSize_
,
parameters_
[
0
]));
latentVectors_
=
std
::
unique_ptr
<
Weight
>
(
new
Weight
(
height
,
factorSize_
,
parameters_
[
0
]));
v2_
=
latentVectors_
->
getW
()
->
clone
(
0
,
0
,
useGpu_
);
return
true
;
return
true
;
}
}
...
@@ -41,14 +44,28 @@ bool FactorizationMachineLayer::init(const LayerMap& layerMap,
...
@@ -41,14 +44,28 @@ bool FactorizationMachineLayer::init(const LayerMap& layerMap,
void
FactorizationMachineLayer
::
forward
(
PassType
passType
)
{
void
FactorizationMachineLayer
::
forward
(
PassType
passType
)
{
Layer
::
forward
(
passType
);
Layer
::
forward
(
passType
);
auto
input
=
getInput
(
0
);
const
MatrixPtr
&
inputV
=
getInputValue
(
0
);
int
batchSize
=
input
.
getBatchSize
();
size_t
batchSize
=
inputV
->
getHeight
();
in
t
size
=
getSize
();
size_
t
size
=
getSize
();
reserveOutput
(
batchSize
,
size
);
reserveOutput
(
batchSize
,
size
);
MatrixPtr
outV
=
getOutputValue
();
MatrixPtr
outV
=
getOutputValue
();
Matrix
::
resizeOrCreate
(
tmpMul_
,
batchSize
,
factorSize_
,
false
,
useGpu_
);
Matrix
::
resizeOrCreate
(
tmpOut_
,
batchSize
,
factorSize_
,
false
,
useGpu_
);
REGISTER_TIMER_INFO
(
"FwMulTimer"
,
getName
().
c_str
());
tmpMul_
->
mul
(
*
inputV
,
*
latentVectors_
->
getW
());
tmpOut_
->
pow2
(
*
tmpMul_
,
2
);
outV
->
sumRows
(
*
tmpOut_
,
0.5
,
0
);
x2_
=
inputV
->
clone
(
0
,
0
,
useGpu_
);
x2_
->
pow2
(
*
inputV
,
2
);
v2_
->
pow2
(
*
latentVectors_
->
getW
(),
2
);
tmpOut_
->
mul
(
*
x2_
,
*
v2_
);
outV
->
sumRows
(
*
tmpOut_
,
-
0.5
,
1.0
);
/* activation */
{
/* activation */
{
REGISTER_TIMER_INFO
(
"FwAtvTimer"
,
getName
().
c_str
());
REGISTER_TIMER_INFO
(
"FwAtvTimer"
,
getName
().
c_str
());
forwardActivation
();
forwardActivation
();
...
@@ -60,6 +77,43 @@ void FactorizationMachineLayer::backward(const UpdateCallback& callback) {
...
@@ -60,6 +77,43 @@ void FactorizationMachineLayer::backward(const UpdateCallback& callback) {
REGISTER_TIMER_INFO
(
"BpAvtTimer"
,
getName
().
c_str
());
REGISTER_TIMER_INFO
(
"BpAvtTimer"
,
getName
().
c_str
());
backwardActivation
();
backwardActivation
();
}
}
const
MatrixPtr
&
inputV
=
getInputValue
(
0
);
const
MatrixPtr
&
oGrad
=
getOutputGrad
();
MatrixPtr
tmpSum
=
Matrix
::
create
(
1
,
latentVectors_
->
getW
()
->
getHeight
(),
false
,
useGpu_
);
MatrixPtr
tmpSum_T
=
Matrix
::
create
(
tmpSum
->
getRowBuf
(
0
),
latentVectors_
->
getW
()
->
getHeight
(),
1
,
false
,
useGpu_
);
/* Calculate the gradients of the latentVectors_ matrix */
if
(
latentVectors_
->
getWGrad
())
{
MatrixPtr
tmpIn
=
inputV
->
clone
(
0
,
0
,
useGpu_
);
tmpIn
->
rowScale
(
0
,
*
inputV
,
*
oGrad
);
latentVectors_
->
getWGrad
()
->
mul
(
*
tmpIn
->
getTranspose
(),
*
tmpMul_
,
1
,
1
);
tmpIn
->
rowScale
(
0
,
*
x2_
,
*
oGrad
);
tmpSum
->
sumCols
(
*
tmpIn
,
-
1
,
0
);
latentVectors_
->
getWGrad
()
->
addRowScale
(
0
,
*
latentVectors_
->
getW
(),
*
tmpSum_T
);
/* Increasing the number of gradient */
latentVectors_
->
getParameterPtr
()
->
incUpdate
(
callback
);
}
/* Calculate the input layers gradient */
MatrixPtr
inGrad
=
getInputGrad
(
0
);
if
(
inGrad
!=
NULL
)
{
MatrixPtr
latentVectors_T
=
latentVectors_
->
getW
()
->
getTranspose
();
inGrad
->
mul
(
*
tmpMul_
,
*
latentVectors_T
,
1
,
1
);
tmpSum_T
->
sumRows
(
*
v2_
,
-
1
,
0
);
inGrad
->
addColScale
(
0
,
*
inputV
,
*
tmpSum
);
inGrad
->
rowScale
(
0
,
*
inGrad
,
*
oGrad
);
}
}
}
}
// namespace paddle
}
// namespace paddle
paddle/gserver/layers/FactorizationMachineLayer.h
浏览文件 @
947b6a77
...
@@ -40,10 +40,22 @@ namespace paddle {
...
@@ -40,10 +40,22 @@ namespace paddle {
class
FactorizationMachineLayer
:
public
Layer
{
class
FactorizationMachineLayer
:
public
Layer
{
protected:
protected:
/// The latent vectors, shape: (size, factorSize_)
/// The latent vectors, shape: (size, factorSize_)
/// Each row of the latentVectors_ matrix is the latent vector
/// corresponding to one input feature dimension
std
::
unique_ptr
<
Weight
>
latentVectors_
;
std
::
unique_ptr
<
Weight
>
latentVectors_
;
/// The hyperparameter that defines the dimensionality of the factorization
/// The hyperparameter that defines the dimensionality of the factorization
size_t
factorSize_
;
size_t
factorSize_
;
private:
/// The result of input matrix * letent vector matrix that will be used in
/// both forward and backward step
MatrixPtr
tmpMul_
;
MatrixPtr
tmpOut_
;
/// Store the square values of the letent vectors matrix
MatrixPtr
v2_
;
/// Store the square values of input matrix
MatrixPtr
x2_
;
public:
public:
explicit
FactorizationMachineLayer
(
const
LayerConfig
&
config
)
explicit
FactorizationMachineLayer
(
const
LayerConfig
&
config
)
:
Layer
(
config
)
{}
:
Layer
(
config
)
{}
...
...
paddle/gserver/tests/test_LayerGrad.cpp
浏览文件 @
947b6a77
...
@@ -2363,8 +2363,9 @@ void testFactorizationMachineLayer(InputType type, bool useGpu) {
...
@@ -2363,8 +2363,9 @@ void testFactorizationMachineLayer(InputType type, bool useGpu) {
TestConfig
config
;
TestConfig
config
;
config
.
layerConfig
.
set_type
(
"factorization_machine"
);
config
.
layerConfig
.
set_type
(
"factorization_machine"
);
config
.
layerConfig
.
set_factor_size
(
FACTOR_SIZE
);
config
.
layerConfig
.
set_factor_size
(
FACTOR_SIZE
);
config
.
biasSize
=
1
;
config
.
layerConfig
.
set_size
(
1
);
config
.
inputDefs
.
push_back
({
type
,
"layer_0"
,
8192
,
0
});
config
.
biasSize
=
0
;
config
.
inputDefs
.
push_back
({
type
,
"layer_0"
,
1024
,
10240
});
config
.
layerConfig
.
add_inputs
();
config
.
layerConfig
.
add_inputs
();
testLayerGrad
(
config
,
"factorization_machine"
,
16
,
false
,
useGpu
,
false
);
testLayerGrad
(
config
,
"factorization_machine"
,
16
,
false
,
useGpu
,
false
);
}
}
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录