首页 炼丹房 帖子详情
elementwise_mul,shape不对
收藏
快速回复
炼丹房 问答新手上路 1442 3
elementwise_mul,shape不对
收藏
快速回复
炼丹房 问答新手上路 1442 3
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
收藏
回复
全部评论(3)
时间顺序
CChan
#2 回复于2020-01

w,x得反过来

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

0
回复
k
kduxqh
#3 回复于2020-01

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

0
回复
U
Up晖99
#4 回复于2021-08

请问楼主这个问题是怎么解决的呀

0
回复
在@后输入用户全名并按空格结束,可艾特全站任一用户