未验证 提交 3f32b730 编写于 作者: L Linjie Chen 提交者: GitHub

Fix error of HSigmoidLoss (#34719)

* Fix error of HSigmoidLoss

* update unittest

* update unittest
上级 bf545344
......@@ -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");
}
......
......@@ -575,6 +575,20 @@ class TestHSigmoidLossAPI(unittest.TestCase):
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')
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册