|
| 1 | +/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserve. |
| 2 | +
|
| 3 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +you may not use this file except in compliance with the License. |
| 5 | +You may obtain a copy of the License at |
| 6 | +
|
| 7 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +
|
| 9 | +Unless required by applicable law or agreed to in writing, software |
| 10 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +See the License for the specific language governing permissions and |
| 13 | +limitations under the License. */ |
| 14 | + |
| 15 | +#include "FactorizationMachineLayer.h" |
| 16 | +#include <algorithm> |
| 17 | +#include <vector> |
| 18 | +#include "paddle/math/SparseMatrix.h" |
| 19 | +#include "paddle/utils/Logging.h" |
| 20 | +#include "paddle/utils/Stat.h" |
| 21 | + |
| 22 | +namespace paddle { |
| 23 | + |
| 24 | +REGISTER_LAYER(factorization_machine, FactorizationMachineLayer); |
| 25 | + |
| 26 | +bool FactorizationMachineLayer::init(const LayerMap& layerMap, |
| 27 | + const ParameterMap& parameterMap) { |
| 28 | + /* Initialize the basic parent class */ |
| 29 | + Layer::init(layerMap, parameterMap); |
| 30 | + |
| 31 | + factorSize_ = config_.factor_size(); |
| 32 | + |
| 33 | + /* initialize the latentVectors_ */ |
| 34 | + CHECK_EQ(inputLayers_.size(), 1UL); |
| 35 | + size_t inputSize = inputLayers_[0]->getSize(); |
| 36 | + CHECK_EQ(parameters_[0]->getSize(), inputSize * factorSize_); |
| 37 | + latentVectors_ = std::unique_ptr<Weight>( |
| 38 | + new Weight(inputSize, factorSize_, parameters_[0])); |
| 39 | + |
| 40 | + return true; |
| 41 | +} |
| 42 | + |
| 43 | +void FactorizationMachineLayer::forward(PassType passType) { |
| 44 | + Layer::forward(passType); |
| 45 | + |
| 46 | + const MatrixPtr& inputV = getInputValue(0); |
| 47 | + |
| 48 | + size_t batchSize = inputV->getHeight(); |
| 49 | + size_t outputSize = getSize(); |
| 50 | + size_t inputSize = inputLayers_[0]->getSize(); |
| 51 | + reserveOutput(batchSize, outputSize); |
| 52 | + |
| 53 | + MatrixPtr outV = getOutputValue(); |
| 54 | + |
| 55 | + Matrix::resizeOrCreate( |
| 56 | + latentVectorsSquare_, inputSize, factorSize_, false, useGpu_); |
| 57 | + Matrix::resizeOrCreate( |
| 58 | + inputMulFactor_, batchSize, factorSize_, false, useGpu_); |
| 59 | + Matrix::resizeOrCreate(tmpOut_, batchSize, factorSize_, false, useGpu_); |
| 60 | + |
| 61 | + REGISTER_TIMER_INFO("FmInputMulFactorTimer", getName().c_str()); |
| 62 | + inputMulFactor_->mul(*inputV, *latentVectors_->getW()); |
| 63 | + inputMulFactor_->square2(*tmpOut_); |
| 64 | + outV->sumRows(*tmpOut_, 0.5, 0); |
| 65 | + |
| 66 | + if (dynamic_cast<CpuSparseMatrix*>(inputV.get())) { |
| 67 | + Matrix::resizeOrCreateSparseMatrix(inputSquare_, |
| 68 | + inputV->getHeight(), |
| 69 | + inputV->getWidth(), |
| 70 | + inputV->getElementCnt(), |
| 71 | + inputV->getValueType()); |
| 72 | + inputSquare_->copyFrom(*inputV); |
| 73 | + (dynamic_cast<CpuSparseMatrix*>(inputSquare_.get()))->square2(); |
| 74 | + } else { |
| 75 | + Matrix::resizeOrCreate( |
| 76 | + inputSquare_, inputV->getHeight(), inputV->getWidth(), false, useGpu_); |
| 77 | + inputV->square2(*inputSquare_); |
| 78 | + } |
| 79 | + latentVectors_->getW()->square2(*latentVectorsSquare_); |
| 80 | + tmpOut_->mul(*inputSquare_, *latentVectorsSquare_); |
| 81 | + outV->sumRows(*tmpOut_, -0.5, 1.0); |
| 82 | + |
| 83 | + /* activation */ { |
| 84 | + REGISTER_TIMER_INFO("FmFwAtvTimer", getName().c_str()); |
| 85 | + forwardActivation(); |
| 86 | + } |
| 87 | +} |
| 88 | + |
| 89 | +void FactorizationMachineLayer::backward(const UpdateCallback& callback) { |
| 90 | + /* Do derivation */ { backwardActivation(); } |
| 91 | + |
| 92 | + const MatrixPtr& inputV = getInputValue(0); |
| 93 | + const MatrixPtr& oGrad = getOutputGrad(); |
| 94 | + |
| 95 | + Matrix::resizeOrCreate( |
| 96 | + tmpSum_, 1, latentVectors_->getW()->getHeight(), false, useGpu_); |
| 97 | + MatrixPtr tmpSumTrans = Matrix::create(tmpSum_->getRowBuf(0), |
| 98 | + latentVectors_->getW()->getHeight(), |
| 99 | + 1, |
| 100 | + false, |
| 101 | + useGpu_); |
| 102 | + |
| 103 | + /* Calculate the gradients of the latentVectors_ matrix */ |
| 104 | + if (latentVectors_->getWGrad()) { |
| 105 | + if (dynamic_cast<CpuSparseMatrix*>(inputV.get())) { |
| 106 | + Matrix::resizeOrCreateSparseMatrix(tmpInput_, |
| 107 | + inputV->getHeight(), |
| 108 | + inputV->getWidth(), |
| 109 | + inputV->getElementCnt()); |
| 110 | + |
| 111 | + CpuSparseMatrix* sparseInputV = |
| 112 | + dynamic_cast<CpuSparseMatrix*>(inputV.get()); |
| 113 | + CpuSparseMatrix* sparseInputSquare = |
| 114 | + dynamic_cast<CpuSparseMatrix*>(inputSquare_.get()); |
| 115 | + CpuSparseMatrix* sparseTmpInput = |
| 116 | + dynamic_cast<CpuSparseMatrix*>(tmpInput_.get()); |
| 117 | + sparseTmpInput->copyFrom(*sparseInputV); |
| 118 | + |
| 119 | + sparseTmpInput->rowScale(0, *sparseInputV, *oGrad); |
| 120 | + latentVectors_->getWGrad()->mul( |
| 121 | + *sparseTmpInput->getTranspose(), *inputMulFactor_, 1, 1); |
| 122 | + sparseTmpInput->rowScale(0, *sparseInputSquare, *oGrad); |
| 123 | + |
| 124 | + Matrix::resizeOrCreate(negOnes_, 1, inputV->getHeight(), false, useGpu_); |
| 125 | + negOnes_->zeroMem(); |
| 126 | + negOnes_->add(-1); |
| 127 | + tmpSum_->mul(*negOnes_, *sparseTmpInput, 1, 0); |
| 128 | + } else { |
| 129 | + Matrix::resizeOrCreate( |
| 130 | + tmpInput_, inputV->getHeight(), inputV->getWidth(), false, useGpu_); |
| 131 | + |
| 132 | + tmpInput_->rowScale(0, *inputV, *oGrad); |
| 133 | + latentVectors_->getWGrad()->mul( |
| 134 | + *tmpInput_->getTranspose(), *inputMulFactor_, 1, 1); |
| 135 | + tmpInput_->rowScale(0, *inputSquare_, *oGrad); |
| 136 | + |
| 137 | + tmpSum_->sumCols(*tmpInput_, -1, 0); |
| 138 | + } |
| 139 | + |
| 140 | + latentVectors_->getWGrad()->addRowScale( |
| 141 | + 0, *latentVectors_->getW(), *tmpSumTrans); |
| 142 | + |
| 143 | + /* Increasing the number of gradient */ |
| 144 | + latentVectors_->getParameterPtr()->incUpdate(callback); |
| 145 | + } |
| 146 | + |
| 147 | + /* Calculate the input layers gradient */ |
| 148 | + MatrixPtr inGrad = getInputGrad(0); |
| 149 | + if (inGrad != NULL) { |
| 150 | + inGrad->mul( |
| 151 | + *inputMulFactor_, *latentVectors_->getW()->getTranspose(), 1, 1); |
| 152 | + tmpSumTrans->sumRows(*latentVectorsSquare_, -1, 0); |
| 153 | + inGrad->addColScale(0, *inputV, *tmpSum_); |
| 154 | + inGrad->rowScale(0, *inGrad, *oGrad); |
| 155 | + } |
| 156 | +} |
| 157 | + |
| 158 | +} // namespace paddle |
0 commit comments