parent
3f874143fe
commit
1644c72acc
@ -0,0 +1,65 @@
|
||||
/* 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);
|
||||
size_t height = inputLayers_[0]->getSize();
|
||||
latentVectors_.reset(new Weight(height, factorSize_, parameters_[0]));
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void FactorizationMachineLayer::forward(PassType passType) {
|
||||
Layer::forward(passType);
|
||||
|
||||
auto input = getInput(0);
|
||||
|
||||
int batchSize = input.getBatchSize();
|
||||
int size = getSize();
|
||||
reserveOutput(batchSize, size);
|
||||
|
||||
MatrixPtr outV = getOutputValue();
|
||||
|
||||
/* activation */ {
|
||||
REGISTER_TIMER_INFO("FwAtvTimer", getName().c_str());
|
||||
forwardActivation();
|
||||
}
|
||||
}
|
||||
|
||||
void FactorizationMachineLayer::backward(const UpdateCallback& callback) {
|
||||
/* Do derivation */ {
|
||||
REGISTER_TIMER_INFO("BpAvtTimer", getName().c_str());
|
||||
backwardActivation();
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace paddle
|
@ -0,0 +1,59 @@
|
||||
/* 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]
|
||||
*
|
||||
* The config file api is factorization_machine.
|
||||
*/
|
||||
|
||||
class FactorizationMachineLayer : public Layer {
|
||||
protected:
|
||||
/// The latent vectors, shape: (size, factorSize_)
|
||||
std::unique_ptr<Weight> latentVectors_;
|
||||
/// The hyperparameter that defines the dimensionality of the factorization
|
||||
size_t factorSize_;
|
||||
|
||||
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
|
@ -0,0 +1,39 @@
|
||||
type: "nn"
|
||||
layers {
|
||||
name: "data"
|
||||
type: "data"
|
||||
size: 1024
|
||||
active_type: ""
|
||||
}
|
||||
layers {
|
||||
name: "__factorization_machine_0__"
|
||||
type: "factorization_machine"
|
||||
size: 1
|
||||
active_type: ""
|
||||
inputs {
|
||||
input_layer_name: "data"
|
||||
input_parameter_name: "___factorization_machine_0__.w0"
|
||||
}
|
||||
factor_size: 10
|
||||
}
|
||||
parameters {
|
||||
name: "___factorization_machine_0__.w0"
|
||||
size: 10240
|
||||
initial_mean: 0.0
|
||||
initial_std: 0.03125
|
||||
dims: 1024
|
||||
dims: 1
|
||||
initial_strategy: 0
|
||||
initial_smart: true
|
||||
}
|
||||
input_layer_names: "data"
|
||||
output_layer_names: "__factorization_machine_0__"
|
||||
sub_models {
|
||||
name: "root"
|
||||
layer_names: "data"
|
||||
layer_names: "__factorization_machine_0__"
|
||||
input_layer_names: "data"
|
||||
output_layer_names: "__factorization_machine_0__"
|
||||
is_recurrent_layer_group: false
|
||||
}
|
||||
|
@ -0,0 +1,9 @@
|
||||
from paddle.trainer_config_helpers import *
|
||||
|
||||
settings(batch_size=1000, learning_rate=1e-5)
|
||||
|
||||
data = data_layer(name='data', size=1024)
|
||||
|
||||
fm = factorization_machine(input=data, factor_size=10)
|
||||
|
||||
outputs(fm)
|
Loading…
Reference in new issue