diff --git a/python/paddle/fluid/dygraph/nn.py b/python/paddle/fluid/dygraph/nn.py index 7e0209b464a42ab99d0754b239bddafbbcb3276c..baa49e5f3e44d0e3397b6418f4cd85b8a61bb53b 100644 --- a/python/paddle/fluid/dygraph/nn.py +++ b/python/paddle/fluid/dygraph/nn.py @@ -2371,6 +2371,10 @@ class BilinearTensorProduct(layers.Layer): is_bias=True) def forward(self, x, y): + check_variable_and_dtype(x, 'x', ['float32', 'float64'], + 'BilinearTensorProduct') + check_variable_and_dtype(y, 'y', ['float32', 'float64'], + 'BilinearTensorProduct') self._inputs = {"X": x, "Y": y, "Weight": self.weight} if self.bias is not None: self._inputs["Bias"] = self.bias 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 0db669bc1f9508f90111d6c04e6730ef3433de29..ba9db2c104f18c3af969570e4e6c93e2d14c5c60 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 @@ -16,9 +16,25 @@ from __future__ import print_function import unittest import numpy as np +import paddle.fluid as fluid from op_test import OpTest +class TestDygraphBilinearTensorProductAPIError(unittest.TestCase): + def test_errors(self): + with fluid.program_guard(fluid.Program(), fluid.Program()): + layer = fluid.dygraph.nn.BilinearTensorProduct( + input1_dim=5, input2_dim=4, output_dim=1000) + # the input must be Variable. + x0 = fluid.create_lod_tensor( + np.array([-1, 3, 5, 5]), [[1, 1, 1, 1]], fluid.CPUPlace()) + self.assertRaises(TypeError, layer, x0) + # the input dtype must be float32 or float64 + 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) + + class TestBilinearTensorProductOp(OpTest): def setUp(self): self.op_type = "bilinear_tensor_product"