From dc1901f48e7e72179b0b2517f06372d26ea41d41 Mon Sep 17 00:00:00 2001 From: songyouwei Date: Fri, 10 Apr 2020 21:24:45 +0800 Subject: [PATCH] API(BilinearTensorProduct) error message enhancement (#23528) * err msg enhance for BilinearTensorProduct test=develop * rebase dev test=develop * add ut test=develop --- python/paddle/fluid/dygraph/nn.py | 4 ++++ .../unittests/test_bilinear_tensor_product_op.py | 16 ++++++++++++++++ 2 files changed, 20 insertions(+) diff --git a/python/paddle/fluid/dygraph/nn.py b/python/paddle/fluid/dygraph/nn.py index 7e0209b464a..baa49e5f3e4 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 0db669bc1f9..ba9db2c104f 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" -- GitLab