为什么我得到这个ValueError? [英] Why am I getting this ValueError?
问题描述
所以这里是我的代码,充分了解: https://hastebin.com/qigimomika.py
。
所以基本上我有一个问题,以下行:
有点上下文:
def weight_variable(shape):
initial = tensorflow.truncated_normal(shape,stddev = 0.01)
return tensorflow.Variable(initial)
def bias_variable(shape):
initial = tensorflow.constant(0.01,shape = shape)
return tensorflow.Variable(initial)
w_layer1 = weight_variable([4,32])
b_layer1 = bias_variable([32])
input_layer = tensorflow.placeholder(float,[4 ])
产生错误的行:
h_layer1 = tensorflow.add(tensorflow.matmul(input_layer,w_layer1),b_layer1)
当我运行整个代码(以上)时,它会产生以下内容ValueError
ValueError:Shape必须是2级,但是'MatMul'的排名1(op:'MatMul')
具有输入形状:[4],[4,32]。
现在我的问题:会发生什么以及如何避免这种情况?
感谢您的关注
编辑 :谢谢我的解决方案:我将input_layer更改为:
input_layer = tensorflow.placeholder(float,[1,4])
问题是我的第一个数组是张量流1([4])和我的第二个数组2([4,32])。所以我添加了这一行:
state = [state]
由此声明输入是:
output_layer.eval(feed_dict = {input_layer:state})
状态最初是[1,2,3,4](等级1 ),现在是[[1,2,3,4]](等级2)。
谢谢
EDIT2: 确定自上次编辑以来,我改变了很多。我失去了记录的变化。如果您希望看到我的代码这里。我知道这是凌乱的他妈的。现在我只是很开心,狗屎正在工作:D,你将无法理解我的代码,这是一个完整的混乱,但我只想记录当前的状态。一个大的 谢谢 到阿里·阿巴西 :D。
在这里,我们是因为你知道 MatMul
操作是经典的矩阵乘法运算,所以如果我们要乘以两个矩阵, M1
和 M2
,形状分别为 AxB
和 BxC
当我们想要乘法和结果时,具有相同的形状 B
M1 x M2
生成矩阵 R
,形状为 AxC
。
所以在你的情况下,你尝试将形状 4x1
和 4x32
乘以两个矩阵,所以它会引发形状问题的错误,你应该转置第一张张,然后你有:
1x4
MatMul 4 x32
导致 1x32
矩阵。
您的代码在这里:
h_layer1 = tensorflow.add(tensorflow.matmul(input_layer,w_layer1),b_layer1)
使用如下:
h_layer1 = tensorflow.add(tensorflow.matmul(tf.transpose(input_layer),w_layer1), b_layer1)
有关更详细的答案,您可以在每个阶段打印张量的形状,如:
print h_layer1.get_shape()
看到形状,那么你可以修改你的形状和投入。
祝你好运。
So here is my code for a full understanding: https://hastebin.com/qigimomika.py .
So basically I have a problem with the following lines:
a bit context:
def weight_variable(shape):
initial = tensorflow.truncated_normal(shape, stddev=0.01)
return tensorflow.Variable(initial)
def bias_variable(shape):
initial = tensorflow.constant(0.01, shape=shape)
return tensorflow.Variable(initial)
w_layer1 = weight_variable([4, 32])
b_layer1 = bias_variable([32])
input_layer = tensorflow.placeholder("float", [4])
the line which produces the error:
h_layer1 = tensorflow.add(tensorflow.matmul(input_layer, w_layer1),b_layer1)
When I run the whole code (which is above) it produces the following ValueError
ValueError: Shape must be rank 2 but is rank 1 for 'MatMul' (op: 'MatMul')
with input shapes: [4], [4,32].
Now my question: What happens and how can I avoid this ?
Thanks for your attention
EDIT: thanks to Prune and Ali Abbasi.
My solution: I changed the input_layer to:
input_layer = tensorflow.placeholder("float", [1, 4])
The problem was that my first array was tensorflow rank 1 ([4]) and my second array rank 2 ([4, 32]). So I added this line:
state = [state]
whereby state the input is:
output_layer.eval(feed_dict={input_layer : state})
state was initially [1, 2, 3, 4] (rank 1), now it is [[1, 2, 3, 4]] (rank 2).
thanks
EDIT2: Ok I changed a lot since the last EDIT. I got lost of the changes to record them. In case you want to see my code here it is. I know it's messy as fuck. For now I am just soo happy that shit is working :"D. You will not be able to understand my code it's a total mess but I just wanted to document the current state. A big thanks to Ali Abbasi :D.
Here we are since you know MatMul
operations is classic Matrix Multiplication operation so if we want to multiply two matrices, M1
and M2
, with shape AxB
and BxC
respectively, we should have same shapes B
when we want to multiplication and in result:
M1 x M2
results in a matrix R
with shape AxC
.
So in your case you try to multiplication two matrices with shape 4x1
and 4x32
, so it throws an error of shape problem, you should transpose the first tensor, then you have:
1x4
MatMul 4x32
result in 1x32
matrix.
Your code here:
h_layer1 = tensorflow.add(tensorflow.matmul(input_layer, w_layer1),b_layer1)
Use like this:
h_layer1 = tensorflow.add(tensorflow.matmul(tf.transpose(input_layer), w_layer1),b_layer1)
For more detailed answer, you can print the shapes of tensors in each stage like:
print h_layer1.get_shape()
And see the shapes, then you can modify your shapes and inputs.
Good Luck.
这篇关于为什么我得到这个ValueError?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!