diff --git a/python/paddle/fluid/tests/unittests/test_bilinear_tensor_product_op.py b/python/paddle/fluid/tests/unittests/test_bilinear_tensor_product_op.py index 77416dd9826fb..350b6ca72f467 100644 --- a/python/paddle/fluid/tests/unittests/test_bilinear_tensor_product_op.py +++ b/python/paddle/fluid/tests/unittests/test_bilinear_tensor_product_op.py @@ -34,6 +34,17 @@ def test_errors(self): x1 = fluid.data(name='x1', shape=[-1, 5], dtype="float16") x2 = fluid.data(name='x2', shape=[-1, 4], dtype="float32") self.assertRaises(TypeError, layer, x1, x2) + # the dimensions of x and y must be 2 + paddle.enable_static() + x3 = paddle.static.data("", shape=[0], dtype="float32") + x4 = paddle.static.data("", shape=[0], dtype="float32") + self.assertRaises( + ValueError, + paddle.static.nn.bilinear_tensor_product, + x3, + x4, + 1000, + ) class TestBilinearTensorProductOp(OpTest): diff --git a/python/paddle/static/nn/common.py b/python/paddle/static/nn/common.py index ef49b5642a37c..07be93fb55bda 100644 --- a/python/paddle/static/nn/common.py +++ b/python/paddle/static/nn/common.py @@ -2568,7 +2568,12 @@ def bilinear_tensor_product( """ helper = LayerHelper('bilinear_tensor_product', **locals()) dtype = helper.input_dtype('x') - + if len(x.shape) != 2 or len(y.shape) != 2: + raise ValueError( + "Input x and y should be 2D tensor, but received x with the shape of {}, y with the shape of {}".format( + x.shape, y.shape + ) + ) param_shape = [size, x.shape[1], y.shape[1]] w = helper.create_parameter(