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 77416dd9826fbb85c23dce6d597d6dc1d81fe2e7..350b6ca72f46748fc3ab2906712df6610a5fa06a 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 @@ class TestDygraphBilinearTensorProductAPIError(unittest.TestCase): 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 f70a19e65f6ded51fbdac6bd6d0a82e17560fdcf..41a2d944d25b846b2c3186712e8ce9fa27bc9474 100644 --- a/python/paddle/static/nn/common.py +++ b/python/paddle/static/nn/common.py @@ -2574,7 +2574,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(