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

Fix error of HSigmoidLoss #34719

Merged
merged 3 commits into from
Aug 10, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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