Skip to content

Commit b709af6

Browse files
committed
HuberTwoClassification only support one dimension
1 parent e63ad0a commit b709af6

File tree

1 file changed

+12
-19
lines changed

1 file changed

+12
-19
lines changed

paddle/gserver/layers/CostLayer.cpp

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -672,25 +672,22 @@ void HuberTwoClassification::forwardImp(Matrix& output,
672672
Matrix& target) {
673673
HuberCost::forwardImp(output, label, target);
674674
size_t numSamples = target.getHeight();
675-
size_t dim = output.getWidth();
676675
CHECK(label.ids);
677676
CHECK_EQ((*label.ids).getSize(), numSamples);
678677
CHECK_EQ(output.getHeight(), numSamples);
678+
CHECK_EQ(output.getWidth(), (size_t)1);
679679
CHECK_EQ(target.getWidth(), (size_t)1);
680680

681681
real* out = useGpu_ ? tmpCpuInput_[0].value->getData() : output.getData();
682682
int* lbl = useGpu_ ? tmpCpuInput_[1].ids->getData() : (*label.ids).getData();
683683
std::vector<real> cost(numSamples, 0);
684684
for (size_t i = 0; i < numSamples; ++i) {
685685
int y = 2 * lbl[i] - 1;
686-
for (size_t j = 0; j < dim; ++j) {
687-
int index = i * dim + j;
688-
real a = out[index] * y;
689-
if (a < -1)
690-
cost[i] += -4 * a;
691-
else if (a < 1)
692-
cost[i] += (1 - a) * (1 - a);
693-
}
686+
real a = out[i] * y;
687+
if (a < -1)
688+
cost[i] = -4 * a;
689+
else if (a < 1)
690+
cost[i] = (1 - a) * (1 - a);
694691
}
695692
target.copyFrom(cost.data(), numSamples);
696693
}
@@ -699,22 +696,18 @@ void HuberTwoClassification::backwardImp(Matrix& output,
699696
Argument& label,
700697
Matrix& outputG) {
701698
size_t numSamples = output.getHeight();
702-
size_t dim = output.getWidth();
703699
real* out = useGpu_ ? tmpCpuInput_[0].value->getData() : output.getData();
704700
int* lbl = useGpu_ ? tmpCpuInput_[1].ids->getData() : (*label.ids).getData();
705701
real* grad = useGpu_ ? tmpCpuInput_[0].grad->getData() : outputG.getData();
706702
for (size_t i = 0; i < numSamples; ++i) {
707703
int y = 2 * lbl[i] - 1;
708-
for (size_t j = 0; j < dim; ++j) {
709-
int index = i * dim + j;
710-
real a = out[index] * y;
711-
if (a < -1)
712-
grad[index] += -4 * y;
713-
else if (a < 1)
714-
grad[index] += -2 * (1 - a) * y;
715-
}
704+
real a = out[i] * y;
705+
if (a < -1)
706+
grad[i] += -4 * y;
707+
else if (a < 1)
708+
grad[i] += -2 * (1 - a) * y;
716709
}
717-
if (useGpu_) outputG.copyFrom(grad, numSamples * dim);
710+
if (useGpu_) outputG.copyFrom(grad, numSamples);
718711
}
719712
/**
720713
* This cost layer compute the sum of its input as loss.

0 commit comments

Comments
 (0)