提交 42f47d49 编写于 作者: Q qingqing01 提交者: GitHub

Merge pull request #2267 from pkuyym/fix-2265

Add scale factor for smoothL1 and smoothL1Bp
...@@ -217,10 +217,10 @@ void SmoothL1CostLayer::forwardImp(Matrix& output, ...@@ -217,10 +217,10 @@ void SmoothL1CostLayer::forwardImp(Matrix& output,
targetCpu->copyFrom(target); targetCpu->copyFrom(target);
outputCpu->copyFrom(output); outputCpu->copyFrom(output);
labelCpu->copyFrom(*label.value); labelCpu->copyFrom(*label.value);
targetCpu->smoothL1(*outputCpu, *labelCpu); targetCpu->smoothL1(*outputCpu, *labelCpu, 1.0);
target.copyFrom(*targetCpu); target.copyFrom(*targetCpu);
} else { } else {
target.smoothL1(output, *label.value); target.smoothL1(output, *label.value, 1.0);
} }
} }
...@@ -238,10 +238,10 @@ void SmoothL1CostLayer::backwardImp(Matrix& output, ...@@ -238,10 +238,10 @@ void SmoothL1CostLayer::backwardImp(Matrix& output,
outputGCpu->copyFrom(outputG); outputGCpu->copyFrom(outputG);
outputCpu->copyFrom(output); outputCpu->copyFrom(output);
labelCpu->copyFrom(*label.value); labelCpu->copyFrom(*label.value);
outputGCpu->smoothL1Bp(*outputCpu, *labelCpu); outputGCpu->smoothL1Bp(*outputCpu, *labelCpu, 1.0);
outputG.copyFrom(*outputGCpu); outputG.copyFrom(*outputGCpu);
} else { } else {
outputG.smoothL1Bp(output, *label.value); outputG.smoothL1Bp(output, *label.value, 1.0);
} }
} }
......
...@@ -3606,7 +3606,7 @@ void CpuMatrix::sumOfSquaresBp(Matrix& output, Matrix& label) { ...@@ -3606,7 +3606,7 @@ void CpuMatrix::sumOfSquaresBp(Matrix& output, Matrix& label) {
} }
} }
void CpuMatrix::smoothL1(Matrix& output, Matrix& label) { void CpuMatrix::smoothL1(Matrix& output, Matrix& label, real destScale) {
CHECK(output.useGpu_ == false && label.useGpu_ == false) CHECK(output.useGpu_ == false && label.useGpu_ == false)
<< "Matrix type are not equal"; << "Matrix type are not equal";
...@@ -3624,6 +3624,7 @@ void CpuMatrix::smoothL1(Matrix& output, Matrix& label) { ...@@ -3624,6 +3624,7 @@ void CpuMatrix::smoothL1(Matrix& output, Matrix& label) {
for (size_t i = 0; i < numSamples; ++i, out += dim, lbl += dim) { for (size_t i = 0; i < numSamples; ++i, out += dim, lbl += dim) {
for (size_t j = 0; j < dim; ++j) { for (size_t j = 0; j < dim; ++j) {
real absVal = std::fabs(out[j] - lbl[j]); real absVal = std::fabs(out[j] - lbl[j]);
cost[i] *= destScale;
if (absVal < 1.0) if (absVal < 1.0)
cost[i] += 0.5 * absVal * absVal; cost[i] += 0.5 * absVal * absVal;
else else
...@@ -3632,7 +3633,7 @@ void CpuMatrix::smoothL1(Matrix& output, Matrix& label) { ...@@ -3632,7 +3633,7 @@ void CpuMatrix::smoothL1(Matrix& output, Matrix& label) {
} }
} }
void CpuMatrix::smoothL1Bp(Matrix& output, Matrix& label) { void CpuMatrix::smoothL1Bp(Matrix& output, Matrix& label, real destScale) {
CHECK(output.useGpu_ == false && label.useGpu_ == false) CHECK(output.useGpu_ == false && label.useGpu_ == false)
<< "Matrix type are not equal"; << "Matrix type are not equal";
...@@ -3650,6 +3651,7 @@ void CpuMatrix::smoothL1Bp(Matrix& output, Matrix& label) { ...@@ -3650,6 +3651,7 @@ void CpuMatrix::smoothL1Bp(Matrix& output, Matrix& label) {
for (size_t i = 0; i < numSamples; ++i, out += dim, grad += dim, lbl += dim) { for (size_t i = 0; i < numSamples; ++i, out += dim, grad += dim, lbl += dim) {
for (size_t j = 0; j < dim; ++j) { for (size_t j = 0; j < dim; ++j) {
real val = out[j] - lbl[j]; real val = out[j] - lbl[j];
grad[j] *= destScale;
if (std::fabs(val) < 1) { if (std::fabs(val) < 1) {
grad[j] += val; grad[j] += val;
} else { } else {
......
...@@ -789,11 +789,11 @@ public: ...@@ -789,11 +789,11 @@ public:
LOG(FATAL) << "Not implemented"; LOG(FATAL) << "Not implemented";
} }
virtual void smoothL1(Matrix& output, Matrix& label) { virtual void smoothL1(Matrix& output, Matrix& label, real destScale) {
LOG(FATAL) << "Not implemented"; LOG(FATAL) << "Not implemented";
} }
virtual void smoothL1Bp(Matrix& outputV, Matrix& label) { virtual void smoothL1Bp(Matrix& outputV, Matrix& label, real destScale) {
LOG(FATAL) << "Not implemented"; LOG(FATAL) << "Not implemented";
} }
...@@ -1736,8 +1736,8 @@ public: ...@@ -1736,8 +1736,8 @@ public:
/// gradient of sumOfSquares. /// gradient of sumOfSquares.
void sumOfSquaresBp(Matrix& outputV, Matrix& label); void sumOfSquaresBp(Matrix& outputV, Matrix& label);
void smoothL1(Matrix& output, Matrix& label); void smoothL1(Matrix& output, Matrix& label, real destScale);
void smoothL1Bp(Matrix& output, Matrix& label); void smoothL1Bp(Matrix& output, Matrix& label, real destScale);
void tanh(Matrix& output); void tanh(Matrix& output);
void tanhDerivative(Matrix& output); void tanhDerivative(Matrix& output);
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册