diff --git a/python/paddle/fluid/layers/tensor.py b/python/paddle/fluid/layers/tensor.py index e33b34cc9254b18a18c293fb3670203fecdeb38f..2d874b4806c9e1449a170017440c4b5038ff93bf 100644 --- a/python/paddle/fluid/layers/tensor.py +++ b/python/paddle/fluid/layers/tensor.py @@ -685,8 +685,9 @@ def fill_constant(shape, dtype, value, force_cpu=False, out=None, name=None): """ attrs = {'force_cpu': force_cpu} + dtype = convert_dtype(dtype) if not isinstance(value, Variable): - if convert_dtype(dtype) in ['int64', 'int32']: + if dtype in ['int64', 'int32']: attrs['str_value'] = str(int(value)) else: attrs['str_value'] = str(float(value)) @@ -697,7 +698,7 @@ def fill_constant(shape, dtype, value, force_cpu=False, out=None, name=None): out = _varbase_creator(dtype=dtype) if isinstance(value, Variable): - if convert_dtype(dtype) in ['int64', 'int32']: + if dtype in ['int64', 'int32']: attrs['str_value'] = str(int(value.numpy())) else: attrs['str_value'] = str(float(value.numpy())) @@ -712,6 +713,8 @@ def fill_constant(shape, dtype, value, force_cpu=False, out=None, name=None): helper = LayerHelper("fill_constant", **locals()) inputs = {} if isinstance(value, Variable): + if convert_dtype(value.dtype) != dtype: + value = cast(value, dtype) inputs['ValueTensor'] = value check_dtype(dtype, 'dtype', 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 3eb761f925a677dcbaa3d7e39221299013f84b33..aefc809bd5cb852d3fde95dff4550e506c5f1c12 100644 --- a/python/paddle/fluid/tests/unittests/test_fill_constant_op.py +++ b/python/paddle/fluid/tests/unittests/test_fill_constant_op.py @@ -269,18 +269,26 @@ class TestFillConstantAPI(unittest.TestCase): out_6 = fluid.layers.fill_constant( shape=shape_tensor_int64, dtype=np.float32, value=1.1) - val = fluid.layers.fill_constant(shape=[1], dtype=np.float32, value=1.1) + val1 = fluid.layers.fill_constant( + shape=[1], dtype=np.float32, value=1.1) + val2 = fluid.layers.fill_constant( + shape=[1], dtype=np.float64, value=1.1) out_7 = fluid.layers.fill_constant( - shape=shape_tensor_int64, dtype=np.float32, value=val) + shape=shape_tensor_int64, dtype=np.float32, value=val1) + + out_8 = fluid.layers.fill_constant( + shape=shape_tensor_int64, dtype=np.float32, value=val2) exe = fluid.Executor(place=fluid.CPUPlace()) - res_1, res_2, res_3, res_4, res_5, res_6, res_7 = exe.run( + res_1, res_2, res_3, res_4, res_5, res_6, res_7, res_8 = 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, out_6, out_7]) + fetch_list=[ + out_1, out_2, out_3, out_4, out_5, out_6, out_7, out_8 + ]) 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")) @@ -289,6 +297,31 @@ class TestFillConstantAPI(unittest.TestCase): 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")) assert np.array_equal(res_7, np.full([1, 2], 1.1, dtype="float32")) + assert np.array_equal(res_8, np.full([1, 2], 1.1, dtype="float32")) + + +class TestFillConstantImperative(unittest.TestCase): + def test_api(self): + with fluid.dygraph.guard(): + data1 = np.array([1, 2]).astype('int32') + data2 = np.array([1.1]).astype('float32') + shape = fluid.dygraph.to_variable(data1) + val = fluid.dygraph.to_variable(data2) + res1 = fluid.layers.fill_constant( + shape=[1, 2], dtype='float32', value=1.1) + res2 = fluid.layers.fill_constant( + shape=shape, dtype='float32', value=1.1) + res3 = fluid.layers.fill_constant( + shape=shape, dtype='float32', value=val) + assert np.array_equal( + res1.numpy(), np.full( + [1, 2], 1.1, dtype="float32")) + assert np.array_equal( + res2.numpy(), np.full( + [1, 2], 1.1, dtype="float32")) + assert np.array_equal( + res3.numpy(), np.full( + [1, 2], 1.1, dtype="float32")) class TestFillConstantOpError(unittest.TestCase): diff --git a/python/paddle/tensor/creation.py b/python/paddle/tensor/creation.py index 8b07bddfc15fc27310430d77c45e11d038ac6b59..02c908be347ab00ce29babd01f227e8367e259f2 100644 --- a/python/paddle/tensor/creation.py +++ b/python/paddle/tensor/creation.py @@ -248,7 +248,7 @@ def zeros(shape, dtype=None, name=None): # shape is a Tensor shape = paddle.fill_constant(shape=[2], dtype='int32', value=2) - data3 = paddle.ones(shape=shape, dtype='int32') + data3 = paddle.zeros(shape=shape, dtype='int32') # [[0 0] # [0 0]] """