Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Factorization Machine Layer #4859

Merged
merged 39 commits into from
Nov 27, 2017
Merged
Show file tree
Hide file tree
Changes from 20 commits
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
1644c72
Add framework of the factorization machine layer
will-am Oct 11, 2017
5e78c7a
Merge remote-tracking branch 'upstream/develop' into factorization_ma…
will-am Oct 11, 2017
28c9810
Merge remote-tracking branch 'upstream/develop' into factorization_ma…
will-am Oct 11, 2017
f504c8a
Remove unnecessary configs
will-am Oct 11, 2017
947b6a7
Implement factorization machine layer
will-am Oct 16, 2017
22c5d1f
Merge remote-tracking branch 'upstream/develop' into factorization_ma…
will-am Oct 16, 2017
2ce8f18
Fix tests for factorization machine layer
will-am Oct 17, 2017
b3cd679
Merge remote-tracking branch 'upstream/develop' into factorization_ma…
will-am Oct 17, 2017
86053e7
Reduce the input size in testing factorization machine
will-am Oct 17, 2017
0574915
Merge remote-tracking branch 'upstream/develop' into factorization_ma…
will-am Oct 17, 2017
9741ade
Change pow to square in factorization machine layer
will-am Oct 17, 2017
8654e8a
Fix dims in config parser for factorization machine layer
will-am Oct 17, 2017
a30d53b
Merge remote-tracking branch 'upstream/develop' into factorization_ma…
will-am Oct 17, 2017
4c72b06
Fix creation of tmp variable in factorization machine layer
will-am Oct 18, 2017
a8526f1
Merge remote-tracking branch 'upstream/develop' into factorization_ma…
will-am Oct 18, 2017
822ff38
Merge remote-tracking branch 'upstream/develop' into factorization_ma…
will-am Oct 23, 2017
601c1a3
Merge remote-tracking branch 'upstream/develop' into factorization_ma…
will-am Oct 24, 2017
d9062cd
Add sparse matrix support in factorization machine layer
will-am Oct 25, 2017
509ae79
Add rowScale for CpuSparseMatrix
will-am Oct 25, 2017
477e3eb
Merge remote-tracking branch 'upstream/develop' into add_rowScale_for…
will-am Oct 26, 2017
4172fc0
Add sparse input support for factorization machine layer
will-am Nov 1, 2017
f7941db
Merge remote-tracking branch 'upstream/develop' into factorization_ma…
will-am Nov 13, 2017
3ff683f
Merge branch 'add_rowScale_for_CpuSparseMatrix' into factorization_ma…
will-am Nov 14, 2017
e5135e8
Merge remote-tracking branch 'upstream/develop' into factorization_ma…
will-am Nov 14, 2017
7a1a586
Update variable names and docs for factorization machine layer
will-am Nov 16, 2017
d6e35ec
Merge remote-tracking branch 'upstream/develop' into factorization_ma…
will-am Nov 16, 2017
0b6afb5
Fix typo in factorization machine layer
will-am Nov 16, 2017
09f4f92
Add unitest for factorization machine layer with sparse input
will-am Nov 16, 2017
571ef90
Merge remote-tracking branch 'upstream/develop' into factorization_ma…
will-am Nov 16, 2017
5ee63bb
Merge remote-tracking branch 'upstream/develop' into factorization_ma…
will-am Nov 17, 2017
6a0cfd9
Merge remote-tracking branch 'upstream/develop' into factorization_ma…
will-am Nov 17, 2017
d5a6c81
Update docs for factorization machine layer
will-am Nov 20, 2017
5392a50
Merge remote-tracking branch 'upstream/develop' into factorization_ma…
will-am Nov 20, 2017
6fed6f2
Add support of sparse_binary_vector as input for fm layer
will-am Nov 20, 2017
13ec6f9
Merge remote-tracking branch 'upstream/develop' into factorization_ma…
will-am Nov 20, 2017
74a699a
change clone to resizeOrCreate in fm layer
will-am Nov 20, 2017
b80cdce
Merge remote-tracking branch 'upstream/develop' into factorization_ma…
will-am Nov 20, 2017
89e63b1
Merge remote-tracking branch 'upstream/develop' into factorization_ma…
will-am Nov 23, 2017
8a283db
Update docs for fm layer
will-am Nov 27, 2017
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 11 additions & 4 deletions doc/api/v2/config/layer.rst
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ img_conv

.. _api_v2.layer_context_projection:

context_projection
context_projection
------------------
.. autoclass:: paddle.v2.layer.context_projection
:noindex:
Expand All @@ -70,7 +70,7 @@ Image Pooling Layer
img_pool
--------
.. autoclass:: paddle.v2.layer.img_pool
:noindex:
:noindex:

spp
---
Expand Down Expand Up @@ -104,7 +104,7 @@ sum_to_one_norm
---------------
.. autoclass:: paddle.v2.layer.sum_to_one_norm
:noindex:

cross_channel_norm
------------------
.. autoclass:: paddle.v2.layer.cross_channel_norm
Expand All @@ -114,7 +114,7 @@ row_l2_norm
-----------
.. autoclass:: paddle.v2.layer.row_l2_norm
:noindex:

Recurrent Layers
================

Expand Down Expand Up @@ -400,6 +400,13 @@ multiplex
.. autoclass:: paddle.v2.layer.multiplex
:noindex:

Factorization Machine Layer
============================

factorization_machine
---------------------
.. autoclass:: paddle.v2.layer.factorization_machine
:noindex:

Slicing and Joining Layers
==========================
Expand Down
140 changes: 140 additions & 0 deletions paddle/gserver/layers/FactorizationMachineLayer.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserve.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License. */

#include "FactorizationMachineLayer.h"
#include <algorithm>
#include <vector>
#include "paddle/math/SparseMatrix.h"
#include "paddle/utils/Logging.h"
#include "paddle/utils/Stat.h"

namespace paddle {

REGISTER_LAYER(factorization_machine, FactorizationMachineLayer);

bool FactorizationMachineLayer::init(const LayerMap& layerMap,
const ParameterMap& parameterMap) {
/* Initialize the basic parent class */
Layer::init(layerMap, parameterMap);

factorSize_ = config_.factor_size();

/* initialize the latentVectors_ */
CHECK_EQ(inputLayers_.size(), 1UL);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

35 ~ 40 行不要在 init 里面做,移到 forward 里面。

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

已改

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

已改

size_t height = inputLayers_[0]->getSize();
CHECK_EQ(parameters_[0]->getSize(), height * factorSize_);
latentVectors_ =
std::unique_ptr<Weight>(new Weight(height, factorSize_, parameters_[0]));

v2_ = Matrix::create(height, factorSize_, false, useGpu_);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

v2_ 这个命名不可读,请使用有意义更可读的名字。

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

已改为latentVectorsSquare_


return true;
}

void FactorizationMachineLayer::forward(PassType passType) {
Layer::forward(passType);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

不支持GPU上运行请加检查并提示错误。


const MatrixPtr& inputV = getInputValue(0);

size_t batchSize = inputV->getHeight();
size_t size = getSize();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what is getSize mean? I cannot validate this snippet of code without your comment.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

getSize returns the output size of this layer.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

请按照上面 @dzhwinter 的comment,为变量起一个更有意义的名字。

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

已改为outputSize

reserveOutput(batchSize, size);

MatrixPtr outV = getOutputValue();

Matrix::resizeOrCreate(tmpMul_, batchSize, factorSize_, false, useGpu_);
Matrix::resizeOrCreate(tmpOut_, batchSize, factorSize_, false, useGpu_);

REGISTER_TIMER_INFO("FwMulTimer", getName().c_str());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

如果要使用 REGISTER_TIMER_INFO 第一个参数是 Timer的名字,这里是从 FC copy过来的吧,请把名字改一下。

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

已改

tmpMul_->mul(*inputV, *latentVectors_->getW());
tmpMul_->square2(*tmpOut_);
outV->sumRows(*tmpOut_, 0.5, 0);

x2_ = inputV->clone(0, 0, useGpu_);
if (dynamic_cast<CpuSparseMatrix*>(x2_.get())) {
x2_->copyFrom(*inputV);
(dynamic_cast<CpuSparseMatrix*>(x2_.get()))->square2();
} else {
inputV->square2(*x2_);
}
latentVectors_->getW()->square2(*v2_);
tmpOut_->mul(*x2_, *v2_);
outV->sumRows(*tmpOut_, -0.5, 1.0);

/* activation */ {
REGISTER_TIMER_INFO("FwAtvTimer", getName().c_str());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

请改一下Timer的名字。

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

已改

forwardActivation();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FM 层可以加非线性激活吗?如果原理上不可以(我记得不可以,可以再确认下),这里可以删掉。如果允许,就保留。

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

可以加非线性的激活~

Copy link
Contributor

@lcy-seso lcy-seso Nov 27, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里算的只是二阶交叉项,你的意思是如果我在二阶交叉项使用非线性激活A,一阶项使用非线性激活B,这样也可以吗 ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

虽然没有看到这样用的,但理论上应该是可以的~

}
}

void FactorizationMachineLayer::backward(const UpdateCallback& callback) {
/* Do derivation */ {
REGISTER_TIMER_INFO("BpAvtTimer", getName().c_str());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

请注意改一下Timer的名字。

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

已改

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_);
if (dynamic_cast<CpuSparseMatrix*>(inputV.get())) {
CpuSparseMatrix* inputV_s = dynamic_cast<CpuSparseMatrix*>(inputV.get());
CpuSparseMatrix* x2_s = dynamic_cast<CpuSparseMatrix*>(x2_.get());
CpuSparseMatrix* tmpIn_s = dynamic_cast<CpuSparseMatrix*>(tmpIn.get());
tmpIn_s->copyFrom(*inputV_s);
tmpIn_s->rowScale(0, *inputV_s, *oGrad);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

inputV_s x2_s tmpIn_s inputV_s 这些命名的风格不统一,请按照layers里面的风格进行统一。并且,这些变量的命名不可读,请考虑使用有意义的名字。

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

已改为sparseInputV, sparseInputSquare, sparseTmpInput

latentVectors_->getWGrad()->mul(*tmpIn_s->getTranspose(), *tmpMul_, 1, 1);
tmpIn_s->rowScale(0, *x2_s, *oGrad);

MatrixPtr ones = Matrix::create(1, inputV->getHeight(), false, useGpu_);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

把临时变量ones变成员变量。

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

已改

ones->zeroMem();
ones->add(-1);
tmpSum->mul(*ones, *tmpIn_s, 1, 0);
} else {
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);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tmpSum_T请修改一下变量的命名风格。

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

已改为tmpSumTrans

inGrad->addColScale(0, *inputV, *tmpSum);
inGrad->rowScale(0, *inGrad, *oGrad);
}
}

} // namespace paddle
71 changes: 71 additions & 0 deletions paddle/gserver/layers/FactorizationMachineLayer.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserve.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License. */

#pragma once

#include "Layer.h"
#include "paddle/math/Matrix.h"
#include "paddle/utils/ThreadLocal.h"

namespace paddle {
/**
* @brief The Factorization Machine models pairwise (order-2) feature
* interactions as inner product of the learned latent vectors corresponding
* to each input feature.
*
* The Factorization Machine can effectively capture feature interactions
* especially when the input is sparse. While in principle FM can model higher
* order feature interaction, in practice usually only order-2 feature
* interactions are considered. The Factorization Machine Layer here only
* computes the order-2 interations with the formula:
*
* \f[
* y = \sum_{i=1}^{n-1}\sum_{j=i+1}^n\langle v_i, v_j \rangle x_i x_j
* \f]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can cite the inference paper here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

已加

*
* The config file api is factorization_machine.
*/

class FactorizationMachineLayer : public Layer {
protected:
/// 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_;
/// The hyperparameter that defines the dimensionality of the factorization
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:
explicit FactorizationMachineLayer(const LayerConfig& config)
: Layer(config) {}
~FactorizationMachineLayer() {}

bool init(const LayerMap& layerMap,
const ParameterMap& parameterMap) override;

void forward(PassType passType) override;
void backward(const UpdateCallback& callback = nullptr) override;
};

} // namespace paddle
18 changes: 18 additions & 0 deletions paddle/gserver/tests/test_LayerGrad.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2427,6 +2427,24 @@ TEST(Layer, ScaleSubRegionLayer) {
}
}

void testFactorizationMachineLayer(InputType type, bool useGpu) {
const int FACTOR_SIZE = 10;
TestConfig config;
config.layerConfig.set_type("factorization_machine");
config.layerConfig.set_factor_size(FACTOR_SIZE);
config.layerConfig.set_size(1);
config.biasSize = 0;
config.inputDefs.push_back({type, "layer_0", 128, 1280});
config.layerConfig.add_inputs();
testLayerGrad(config, "factorization_machine", 16, false, useGpu, false);
Copy link
Contributor

@lcy-seso lcy-seso Nov 14, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

SparseMatrix 作为输时请添加单测。

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

已加

}

TEST(Layer, FactorizationMachineLayer) {
for (auto useGpu : {false, true}) {
testFactorizationMachineLayer(INPUT_DATA, useGpu);
}
}

int main(int argc, char** argv) {
testing::InitGoogleTest(&argc, argv);
initMain(argc, argv);
Expand Down
3 changes: 3 additions & 0 deletions proto/ModelConfig.proto
Original file line number Diff line number Diff line change
Expand Up @@ -540,6 +540,9 @@ message LayerConfig {

// for switch order layer
optional ReshapeConfig reshape_conf = 59;

// for factorization machine layer
optional uint32 factor_size = 60;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

为什么不能复用 Layer 的size,而新定义这个字段。

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Layer的size是输出的维度,而这个是内部使用的隐变量(factor)的维度

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

要是用size感觉会有歧义

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

抱歉,我理解错误。忽略。

}

message EvaluatorConfig {
Expand Down
15 changes: 15 additions & 0 deletions python/paddle/trainer/config_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -3832,6 +3832,21 @@ def __init__(self, name, inputs, value, **xargs):
image_conf.channels)


@config_layer('factorization_machine')
class FactorizationMachineLayer(LayerBase):
def __init__(self, name, inputs, factor_size, **xargs):
super(FactorizationMachineLayer, self).__init__(
name, 'factorization_machine', size=1, inputs=inputs, **xargs)
config_assert(
len(self.inputs) == 1,
'factorization machine layer must have one and only one input.')
self.config.factor_size = factor_size
input_layer = self.get_input_layer(0)
psize = input_layer.size * factor_size
dims = [input_layer.size, factor_size]
self.create_input_parameter(0, psize, dims)


# Deprecated, use a new layer specific class instead
@config_func
def Layer(name, type, **xargs):
Expand Down
57 changes: 57 additions & 0 deletions python/paddle/trainer_config_helpers/layers.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@
'resize_layer',
'sub_seq_layer',
'scale_sub_region_layer',
'factorization_machine',
]


Expand Down Expand Up @@ -260,6 +261,8 @@ class LayerType(object):

SCALE_SUB_REGION_LAYER = 'scale_sub_region'

FACTORIZATION_MACHINE = 'factorization_machine'

@staticmethod
def is_layer_type(type_name):
"""
Expand Down Expand Up @@ -7142,3 +7145,57 @@ def scale_sub_region_layer(input, indices, value, name=None):
parents=[input, indices],
num_filters=input.num_filters,
size=input.size)


@wrap_name_default()
@wrap_act_default(act=LinearActivation())
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里只可以使用非线性激活函数吧。如果从原理上不能使用非线性激活,就把激活写死,不要让用户来设置了。

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

可以用非线性的~

@wrap_param_attr_default()
@layer_support()
def factorization_machine(input,
factor_size,
act=None,
name=None,
param_attr=None,
layer_attr=None):
"""
The Factorization Machine models pairwise feature interactions as inner
product of the learned latent vectors corresponding to each input feature.
The Factorization Machine can effectively capture feature interactions
especially when the input is sparse. In practice, usually order 2 feature
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • usually order 2 feature --> this implementation only consider the 2-order feature interactions.
  • 请在注释中增加一下对FM层实现所参考的原论文的引用。

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

已加

interactions are considered using Factorization Machine with the formula:
.. math::
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

line 7166 之前空一行。
line 7167 之后空一行,否则公式无法正常显示。

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

已加

y = \sum_{i=1}^{n-1}\sum_{j=i+1}^n\langle v_i, v_j \rangle x_i x_j
Note:
X is the input vector with size n. V is the factor matrix. Each row of V
is the latent vector corresponding to each input dimesion. The size of
each latent vector is k.
.. code-block:: python
factor_machine = factorization_machine(input=input_layer, factor_size=10)
Copy link
Contributor

@lcy-seso lcy-seso Nov 14, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

7172 行之前空一行,
7173 行之后空一行。

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

已改

:param input: The input layer.
:type input: LayerOutput
:param factor_size: The hyperparameter that defines the dimensionality of
the latent vector size
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

句末加上句号。

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

好的

:type context_len: int
:param act: Activation Type. Default is linear activation.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

原理上这里可以使用非线性激活吗?应该不可以吧。

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

可以的~

:type act: BaseActivation
:param param_attr: The Parameter Attribute. If None, the latent vectors will
be initialized smartly. It's better to set it by
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

作为注释,还是解释一下 “be initialized smartly” 到底是怎样初始化的。

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

好的

yourself.
:type param_attr: ParameterAttribute
:param layer_attr: Extra Layer config.
:type layer_attr: ExtraLayerAttribute|None
:return: LayerOutput object.
:rtype: LayerOutput
Copy link
Contributor

@lcy-seso lcy-seso Nov 14, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

请在comment和示例代码中注明这一层本身并不是 FM,只是完成二阶特征组合部分。需要和其它层配置使用,在simple code 部分给出一个完整的示例。

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

已加

"""
assert isinstance(input, LayerOutput)
assert factor_size > 0, "the factor_size must be greater than 0."

Layer(
inputs=[Input(input.name, **param_attr.attr)],
name=name,
factor_size=factor_size,
type=LayerType.FACTORIZATION_MACHINE,
active_type=act.name,
**ExtraLayerAttribute.to_kwargs(layer_attr))
return LayerOutput(
name, LayerType.FACTORIZATION_MACHINE, input, activation=act, size=1)
Loading