提交 53946283 编写于 作者: P peterzhang2029

bug fix when using hsigmoid with gpu

上级 cda3a774
...@@ -75,10 +75,10 @@ void HierarchicalSigmoidLayer::forward(PassType passType) { ...@@ -75,10 +75,10 @@ void HierarchicalSigmoidLayer::forward(PassType passType) {
if (useGpu_) { if (useGpu_) {
Matrix::resizeOrCreate(cpuOutput_, Matrix::resizeOrCreate(cpuOutput_,
output_.value->getHeight(), output_.value->getHeight(),
output_.value->getWidth(), output_.value->getWidth(),
/* trans */ false, /* trans */ false,
false); false);
IVector::resizeOrCreate(cpuLabel_, label->getSize(), false); IVector::resizeOrCreate(cpuLabel_, label->getSize(), false);
cpuLabel_->copyFrom(*label); cpuLabel_->copyFrom(*label);
cpuOutput_->copyFrom(*output_.value); cpuOutput_->copyFrom(*output_.value);
...@@ -90,10 +90,10 @@ void HierarchicalSigmoidLayer::forward(PassType passType) { ...@@ -90,10 +90,10 @@ void HierarchicalSigmoidLayer::forward(PassType passType) {
if (biases_.get() != NULL) { if (biases_.get() != NULL) {
if (useGpu_) { if (useGpu_) {
Matrix::resizeOrCreate(cpuBias_, Matrix::resizeOrCreate(cpuBias_,
1, 1,
numClasses_ - 1, numClasses_ - 1,
/* trans */ false, /* trans */ false,
false); false);
cpuBias_->copyFrom(*biases_->getW()); cpuBias_->copyFrom(*biases_->getW());
} else { } else {
cpuBias_ = biases_->getW(); cpuBias_ = biases_->getW();
...@@ -104,15 +104,15 @@ void HierarchicalSigmoidLayer::forward(PassType passType) { ...@@ -104,15 +104,15 @@ void HierarchicalSigmoidLayer::forward(PassType passType) {
MatrixPtr input = getInputValue(i); MatrixPtr input = getInputValue(i);
if (useGpu_) { if (useGpu_) {
Matrix::resizeOrCreate(cpuInput_, Matrix::resizeOrCreate(cpuInput_,
input->getHeight(), input->getHeight(),
input->getWidth(), input->getWidth(),
/* trans */ false, /* trans */ false,
false); false);
Matrix::resizeOrCreate(cpuWeight_, Matrix::resizeOrCreate(cpuWeight_,
weights_[i]->getW()->getHeight(), weights_[i]->getW()->getHeight(),
weights_[i]->getW()->getWidth(), weights_[i]->getW()->getWidth(),
/* trans */ false, /* trans */ false,
false); false);
cpuInput_->copyFrom(*input); cpuInput_->copyFrom(*input);
cpuWeight_->copyFrom(*weights_[i]->getW()); cpuWeight_->copyFrom(*weights_[i]->getW());
} else { } else {
...@@ -129,8 +129,7 @@ void HierarchicalSigmoidLayer::forward(PassType passType) { ...@@ -129,8 +129,7 @@ void HierarchicalSigmoidLayer::forward(PassType passType) {
*cpuOutput_, *cpuOutput_,
-1); // scaleSum -1); // scaleSum
preOutput_.value->softrelu(*preOutput_.value); preOutput_.value->softrelu(*preOutput_.value);
MatrixPtr sum = MatrixPtr sum = Matrix::create(batchSize, 1, /* trans= */ false, false);
Matrix::create(batchSize, 1, /* trans= */ false, false);
preOutput_.value->rowSum(*sum); preOutput_.value->rowSum(*sum);
cpuOutput_->add(*sum); cpuOutput_->add(*sum);
if (useGpu_) { if (useGpu_) {
...@@ -156,16 +155,15 @@ void HierarchicalSigmoidLayer::backward(const UpdateCallback& callback) { ...@@ -156,16 +155,15 @@ void HierarchicalSigmoidLayer::backward(const UpdateCallback& callback) {
MatrixPtr biases_grad = biases_->getWGrad(); MatrixPtr biases_grad = biases_->getWGrad();
if (useGpu_) { if (useGpu_) {
Matrix::resizeOrCreate(cpuBias_, Matrix::resizeOrCreate(cpuBias_,
1, 1,
numClasses_ - 1, numClasses_ - 1,
/* trans */ false, /* trans */ false,
false); false);
cpuBias_->copyFrom(*biases_grad); cpuBias_->copyFrom(*biases_grad);
} else { } else {
cpuBias_ = biases_grad; cpuBias_ = biases_grad;
} }
preOutput_.grad->addByBitCodeBackward( preOutput_.grad->addByBitCodeBackward(numClasses_, *cpuLabel_, *cpuBias_);
numClasses_, *cpuLabel_, *cpuBias_);
if (useGpu) { if (useGpu) {
biases_grad->copyFrom(*cpuBias_); biases_grad->copyFrom(*cpuBias_);
} else { } else {
...@@ -182,15 +180,15 @@ void HierarchicalSigmoidLayer::backward(const UpdateCallback& callback) { ...@@ -182,15 +180,15 @@ void HierarchicalSigmoidLayer::backward(const UpdateCallback& callback) {
MatrixPtr weights_grad = weights_[i]->getWGrad(); MatrixPtr weights_grad = weights_[i]->getWGrad();
if (useGpu_) { if (useGpu_) {
Matrix::resizeOrCreate(cpuInput_, Matrix::resizeOrCreate(cpuInput_,
input->getHeight(), input->getHeight(),
input->getWidth(), input->getWidth(),
/* trans */ false, /* trans */ false,
false); false);
Matrix::resizeOrCreate(cpuWeightGrad_, Matrix::resizeOrCreate(cpuWeightGrad_,
weights_grad->getHeight(), weights_grad->getHeight(),
weights_grad->getWidth(), weights_grad->getWidth(),
/* trans */ false, /* trans */ false,
false); false);
cpuInput_->copyFrom(*input); cpuInput_->copyFrom(*input);
cpuWeightGrad_->copyFrom(*weights_grad); cpuWeightGrad_->copyFrom(*weights_grad);
} else { } else {
...@@ -213,15 +211,15 @@ void HierarchicalSigmoidLayer::backward(const UpdateCallback& callback) { ...@@ -213,15 +211,15 @@ void HierarchicalSigmoidLayer::backward(const UpdateCallback& callback) {
if (inputGrad) { if (inputGrad) {
if (useGpu_) { if (useGpu_) {
Matrix::resizeOrCreate(cpuInputGrad_, Matrix::resizeOrCreate(cpuInputGrad_,
inputGrad->getHeight(), inputGrad->getHeight(),
inputGrad->getWidth(), inputGrad->getWidth(),
/* trans */ false, /* trans */ false,
false); false);
Matrix::resizeOrCreate(cpuWeight_, Matrix::resizeOrCreate(cpuWeight_,
weights_[i]->getW()->getHeight(), weights_[i]->getW()->getHeight(),
weights_[i]->getW()->getWidth(), weights_[i]->getW()->getWidth(),
/* trans */ false, /* trans */ false,
false); false);
cpuInputGrad_->copyFrom(*inputGrad); cpuInputGrad_->copyFrom(*inputGrad);
cpuWeight_->copyFrom(*weights_[i]->getW()); cpuWeight_->copyFrom(*weights_[i]->getW());
} else { } else {
......
...@@ -89,7 +89,6 @@ protected: ...@@ -89,7 +89,6 @@ protected:
MatrixPtr cpuBias_; MatrixPtr cpuBias_;
MatrixPtr cpuOutput_; MatrixPtr cpuOutput_;
IVectorPtr cpuLabel_; IVectorPtr cpuLabel_;
}; };
} // namespace paddle } // namespace paddle
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册