Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add scale factor for smoothL1 and smoothL1Bp #2265

Closed
pkuyym opened this issue May 25, 2017 · 0 comments · Fixed by #2267
Closed

Add scale factor for smoothL1 and smoothL1Bp #2265

pkuyym opened this issue May 25, 2017 · 0 comments · Fixed by #2267
Assignees

Comments

@pkuyym
Copy link
Contributor

pkuyym commented May 25, 2017

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant