Add scale factor for smoothL1 and smoothL1Bp
Created by: pkuyym
Whether to add costs or gradients to destination Matrix should be decided by user. Current implement is:
void CpuMatrix::smoothL1(Matrix& output, Matrix& label) {
CHECK(output.useGpu_ == false && label.useGpu_ == false)
<< "Matrix type are not equal";
size_t numSamples = getHeight();
size_t dim = output.getWidth();
CHECK_EQ(label.getHeight(), numSamples);
CHECK_EQ(output.getHeight(), numSamples);
CHECK_EQ(label.getWidth(), dim);
CHECK_EQ(getWidth(), (size_t)1);
real* cost = getData();
real* out = output.getData();
real* lbl = label.getData();
for (size_t i = 0; i < numSamples; ++i, out += dim, lbl += dim) {
for (size_t j = 0; j < dim; ++j) {
real absVal = std::fabs(out[j] - lbl[j]);
if (absVal < 1.0)
cost[i] += 0.5 * absVal * absVal;
else
cost[i] += absVal - 0.5;
}
}
}
void CpuMatrix::smoothL1Bp(Matrix& output, Matrix& label) {
CHECK(output.useGpu_ == false && label.useGpu_ == false)
<< "Matrix type are not equal";
size_t numSamples = getHeight();
size_t dim = output.getWidth();
CHECK_EQ(label.getHeight(), numSamples);
CHECK_EQ(output.getHeight(), numSamples);
CHECK_EQ(label.getWidth(), dim);
CHECK_EQ(getWidth(), dim);
real* out = output.getData();
real* lbl = label.getData();
real* grad = getData();
for (size_t i = 0; i < numSamples; ++i, out += dim, grad += dim, lbl += dim) {
for (size_t j = 0; j < dim; ++j) {
real val = out[j] - lbl[j];
if (std::fabs(val) < 1) {
grad[j] += val;
} else {
grad[j] += (real(0) < val) - (val < real(0));
}
}
}
}
As we can see, current implement adds cost and gradient to caller object leaving no other choice. A solution is adding a scale factor to caller object.