Skip to content

Commit

Permalink
Fix error of HSigmoidLoss (#34719)
Browse files Browse the repository at this point in the history
* Fix error of HSigmoidLoss

* update unittest

* update unittest
  • Loading branch information
linjieccc authored Aug 10, 2021
1 parent bf54534 commit 3f32b73
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
13 changes: 11 additions & 2 deletions paddle/fluid/operators/hierarchical_sigmoid_op.cc
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,17 @@ class HierarchicalSigmoidOp : public framework::OperatorWithKernel {
if (with_prefetch) {
OP_INOUT_CHECK(ctx->HasOutput("W_Out"), "Output", "W_Out", "hsigmoid");
}
const int64_t batch_size = ctx->GetInputDim("X")[0];
std::vector<int64_t> output_shape({batch_size, 1});
const int64_t input_dims = ctx->GetInputDim("X")[0];
const int64_t label_dims = ctx->GetInputDim("Label")[0];
PADDLE_ENFORCE_EQ(input_dims, label_dims,
platform::errors::InvalidArgument(
"The first dimension of "
"input and label is expected to be the same. "
"But received input's first dimension is %d; "
"label's first dimension is %d.",
input_dims, label_dims));

std::vector<int64_t> output_shape({input_dims, 1});
ctx->SetOutputDim("Out", framework::make_ddim(output_shape));
ctx->ShareLoD("X", /*->*/ "Out");
}
Expand Down
14 changes: 14 additions & 0 deletions python/paddle/fluid/tests/unittests/test_hsigmoid_op.py
Original file line number Diff line number Diff line change
Expand Up @@ -575,6 +575,20 @@ def test_errors(self):
weight,
path_code=path_code_int32)

# test paddle.nn.HSigmoidLoss
paddle.disable_static(self.place)
x_arr = np.array([], dtype=np.float32)
x = paddle.to_tensor(np.reshape(x_arr, (100000, 0)))
label = paddle.to_tensor(0, dtype='int64')
self.assertRaises(ValueError, paddle.nn.HSigmoidLoss, x, label)

# test paddle.nn.functional.hsigmoid_loss
x = paddle.to_tensor(np.reshape(x_arr, (10, 0)), dtype='float32')
label = paddle.to_tensor([], dtype='int64')
weight = paddle.to_tensor([], dtype='float32')
self.assertRaises(ValueError, F.hsigmoid_loss, x, label, 0, weight)
paddle.enable_static()

# test paddle.fluid.layers.hsigmoid
with program_guard(Program()):
label = fluid.data('label', [4, 1], 'int64')
Expand Down

0 comments on commit 3f32b73

Please sign in to comment.