From e6ca512af1c43d3dfe2aa62fd3c75494add7b2cc Mon Sep 17 00:00:00 2001 From: wangchaochaohu Date: Wed, 22 Jan 2020 22:51:36 +1400 Subject: [PATCH] refine convert type for numpy type (#22386) --- python/paddle/fluid/data_feeder.py | 10 ++++++++-- .../fluid/tests/unittests/test_fill_constant_op.py | 8 ++++++-- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/python/paddle/fluid/data_feeder.py b/python/paddle/fluid/data_feeder.py index 4848d23d27..b9016bb7d3 100644 --- a/python/paddle/fluid/data_feeder.py +++ b/python/paddle/fluid/data_feeder.py @@ -15,7 +15,7 @@ from __future__ import print_function from . import core -import numpy +import numpy as np import os import six from six.moves import zip, range, xrange @@ -47,6 +47,12 @@ def convert_dtype(dtype): return 'int64' elif dtype == core.VarDesc.VarType.UINT8: return 'uint8' + elif isinstance(dtype, type): + if dtype in [ + np.bool, np.float16, np.float32, np.float64, np.int8, np.int16, + np.int32, np.int64, np.uint8 + ]: + return dtype.__name__ else: if dtype in [ 'bool', 'float16', 'float32', 'float64', 'int8', 'int16', @@ -136,7 +142,7 @@ class DataToLoDTensorConverter(object): format(self.shape, shape)) def done(self): - arr = numpy.array(self.data, dtype=self.dtype) + arr = np.array(self.data, dtype=self.dtype) if self.shape: if len(arr.shape) != len(self.shape): try: diff --git a/python/paddle/fluid/tests/unittests/test_fill_constant_op.py b/python/paddle/fluid/tests/unittests/test_fill_constant_op.py index 2baec782c4..a9bfa14cc1 100644 --- a/python/paddle/fluid/tests/unittests/test_fill_constant_op.py +++ b/python/paddle/fluid/tests/unittests/test_fill_constant_op.py @@ -216,20 +216,24 @@ class TestFillConstantAPI(unittest.TestCase): out_5 = fluid.layers.fill_constant( shape=shape_tensor_int64, dtype="float32", value=1.1) + out_6 = fluid.layers.fill_constant( + shape=shape_tensor_int64, dtype=np.float32, value=1.1) + exe = fluid.Executor(place=fluid.CPUPlace()) - res_1, res_2, res_3, res_4, res_5 = exe.run( + res_1, res_2, res_3, res_4, res_5, res_6 = exe.run( fluid.default_main_program(), feed={ "shape_tensor_int32": np.array([1, 2]).astype("int32"), "shape_tensor_int64": np.array([1, 2]).astype("int64"), }, - fetch_list=[out_1, out_2, out_3, out_4, out_5]) + fetch_list=[out_1, out_2, out_3, out_4, out_5, out_6]) assert np.array_equal(res_1, np.full([1, 2], 1.1, dtype="float32")) assert np.array_equal(res_2, np.full([1, 2], 1.1, dtype="float32")) assert np.array_equal(res_3, np.full([1, 2], 1.1, dtype="float32")) assert np.array_equal(res_4, np.full([1, 2], 1.1, dtype="float32")) assert np.array_equal(res_5, np.full([1, 2], 1.1, dtype="float32")) + assert np.array_equal(res_6, np.full([1, 2], 1.1, dtype="float32")) class TestFillConstantOpError(unittest.TestCase): -- GitLab