elementwise_mul,shape不对
gaoxiupei1 发布于2020-01-16 19:15 浏览:74 回复:2
0
收藏
最后编辑于2020-01-18
import paddle.fluid as fluid
import numpy as np

train_data = [1, 2, 3, 4, 5]
x = fluid.layers.data(name='x', shape=[1], dtype='float32')
x_op = fluid.layers.Print(x, message='input x')
y = fluid.layers.data(name='y', shape=[1], dtype='float32')
w = fluid.layers.create_parameter(name='w', shape=[1], dtype='float32')
w_op = fluid.layers.Print(w, message='w')
out_put = fluid.layers.elementwise_mul(w, x)


# 准备运行网络
cpu = fluid.CPUPlace() # 定义运算设备,这里选择在CPU下训练
exe = fluid.Executor(cpu) # 创建执行器
exe.run(fluid.default_startup_program()) # 网络参数初始化

exe.run(feed={'x':np.array([train_data[1%5]]).astype('float32')})

错误信息:

Python Call Stacks (More useful to users):
------------------------------------------
File "/opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/paddle/fluid/framework.py", line 2488, in append_op
attrs=kwargs.get("attrs", None))
File "/opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/paddle/fluid/layer_helper.py", line 43, in append_op
return self.main_program.current_block().append_op(*args, **kwargs)
File "/opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/paddle/fluid/layers/nn.py", line 14055, in _elementwise_op
'use_mkldnn': use_mkldnn})
File "/opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/paddle/fluid/layers/nn.py", line 14452, in elementwise_mul
return _elementwise_op(LayerHelper('elementwise_mul', **locals()))
File "learn.py", line 10, in
out_put = fluid.layers.elementwise_mul(w, x)

----------------------
Error Message Summary:
----------------------
Error: ShapeError: the dimension of input X must greater than or equal to the one of input Y. But received: the shape of input X = [1], the dimension of input X = 1, the shape of input Y = [-1, 1], the dimension of input Y = 2
[Hint: Expected x_dim.size() >= y_dim.size(), but received x_dim.size():1 < y_dim.size():2.] at (/paddle/paddle/fluid/operators/elementwise/elementwise_op.h:61)
[operator < elementwise_mul > error]

收藏
点赞
0
个赞
共2条回复 最后由gaoxiupei1回复于2020-01-18 08:37
#3gaoxiupei1回复于2020-01-18 08:37:12

谢答,不过w,x的shape都是【1】,理论上来说,属于等于的情况,换一下确实可以,但问题还是没有说清楚

0
#2BadDay_回复于2020-01-17 23:32:46

w,x得反过来

out_put = fluid.layers.elementwise_mul(x, w)

0
TOP
切换版块