关于PaddlePaddle OP 的tensor list作为输入的问题。
Created by: wangchaochaohu
修改reduce_sum类型检查的PR20063的时候发现之前的版本中input是list作为输入是正确的。 自己写了如下测试代码 结果正确
import paddle.fluid as fluid
import numpy as np
numpy_data1 = np.array([[1,2,3,4],[5,6,7,8]], dtype='float32')
numpy_data2 = np.array([[1,2,3,4],[5,6,7,8]], dtype='float32')
tensor_data1 = fluid.layers.assign(numpy_data1)
tensor_array1 = [tensor_data1]
result = fluid.layers.reduce_sum(tensor_array1)
exe = fluid.Executor(fluid.CPUPlace())
exe.run(fluid.default_startup_program())
all_result, = exe.run(fetch_list=[result])
print(all_result)
运行结果如下:
grep: warning: GREP_OPTIONS is deprecated; please use an alias or script
/usr/local/lib/python2.7/dist-packages/paddle/fluid/executor.py:732: UserWarning: The current program is empty.
warnings.warn(error_info)
[36.]
但是list长度为2的时候就会出现错误 错误来源是python 端的
import paddle.fluid as fluid
import numpy as np
numpy_data1 = np.array([[1,2,3,4],[5,6,7,8]], dtype='float32')
numpy_data2 = np.array([[1,2,3,4],[5,6,7,8]], dtype='float32')
tensor_data1 = fluid.layers.assign(numpy_data1)
tensor_array1 = [tensor_data1, tensor_data1]
result = fluid.layers.reduce_sum(tensor_array1)
exe = fluid.Executor(fluid.CPUPlace())
exe.run(fluid.default_startup_program())
all_result, = exe.run(fetch_list=[result])
print(all_result)
报错信息如下:
grep: warning: GREP_OPTIONS is deprecated; please use an alias or script
Traceback (most recent call last):
File "test3.py", line 9, in <module>
result = fluid.layers.reduce_sum(tensor_array1)
File "/usr/local/lib/python2.7/dist-packages/paddle/fluid/layers/nn.py", line 5079, in reduce_sum
'reduce_all': True if dim == None else False
File "/usr/local/lib/python2.7/dist-packages/paddle/fluid/layer_helper.py", line 43, in append_op
return self.main_program.current_block().append_op(*args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/paddle/fluid/framework.py", line 1910, in append_op
attrs=kwargs.get("attrs", None))
File "/usr/local/lib/python2.7/dist-packages/paddle/fluid/framework.py", line 1236, in __init__
% (in_proto.name, len(in_args)))
ValueError: Input X expects only one input, but 2 are given.
相同的道理 我们测试了其他一些OP
import paddle.fluid as fluid
import numpy as np
numpy_data1 = np.array([[1,2,3,4],[5,6,7,8]], dtype='float32')
numpy_data2 = np.array([[1,2,3,4],[5,6,7,8]], dtype='float32')
tensor_data1 = fluid.layers.assign(numpy_data1)
tensor_array1 = [tensor_data1]
result = fluid.layers.reduce_sum(tensor_array1)
exe = fluid.Executor(fluid.CPUPlace())
exe.run(fluid.default_startup_program())
all_result, = exe.run(fetch_list=[result])
print(all_result)
结果如下
grep: warning: GREP_OPTIONS is deprecated; please use an alias or script
/usr/local/lib/python2.7/dist-packages/paddle/fluid/executor.py:732: UserWarning: The current program is empty.
warnings.warn(error_info)
[[1. 2. 3. 4.]
[5. 6. 7. 8.]]
同理我们如果将 tensor_array的长度设置为2 我们还是会出现类似reduce 的报错信息
结论: 目前PaddlePaddle的OP如果python 端没有做一些类型检查或者python端没有使用关于Variable属性或者一些函数,从python 端传入的长度为1 的list (类型为Tensor)是可以得到正确的结果的。