tensorflow报错:Shape must be rank 2 but is rank 3 for 'MatMul' (op: 'MatMul')
Table of Contents
tensorflow矩阵相乘,秩不同报错
在tensorflow中写了这样一句:
y_out = tf.matmul(outputs, W)
其中,outputs的shape为[16,336,400],W的shape为[400,1]
出现以下报错:
Shape must be rank 2 but is rank 3 for 'MatMul' (op: 'MatMul') with input shapes: [16,336,400], [400,1].
Numpy下同样的写法没有问题
1. import numpy as np
2.
3. A = np.array([[[1, 2, 3, 4],
4. [5, 6, 7, 8],
5. [9, 0, 1, 2]],
6. [[4, 3, 2, 1],
7. [8, 7, 6, 5],
8. [2, 1, 0, 9]]])
9. print(A)
10. print(A.shape)
11. print('---------------------------')
12.
13. B = np.array([[1], [2], [3], [4]])
14. print(B)
15. print(B.shape)
16. print('---------------------------')
17.
18. C = np.matmul(A, B)
19. print(C)
20. print(C.shape)
![](/static/blog/img/csdnimg/newCodeMoreWhite.png)
输出结果:
1. [[[1 2 3 4]
2. [5 6 7 8]
3. [9 0 1 2]]
4.
5. [[4 3 2 1]
6. [8 7 6 5]
7. [2 1 0 9]]]
8. (2, 3, 4)
9. ---------------------------
10. [[1]
11. [2]
12. [3]
13. [4]]
14. (4, 1)
15. ---------------------------
16. [[[30]
17. [70]
18. [20]]
19.
20. [[20]
21. [60]
22. [40]]]
23. (2, 3, 1)
![](/static/blog/img/csdnimg/newCodeMoreWhite.png)
解决办法
方案一
1. import numpy as np
2. import tensorflow as tf
3. sess = tf.Session()
4.
5. A = np.array([[[1, 2, 3, 4],
6. [5, 6, 7, 8],
7. [9, 0, 1, 2]],
8. [[4, 3, 2, 1],
9. [8, 7, 6, 5],
10. [2, 1, 0, 9]]])
11. B = np.array([[1], [2], [3], [4]])
12.
13. A = tf.cast(tf.convert_to_tensor(A), tf.int32) # shape=[2, 3, 4]
14. B = tf.cast(tf.convert_to_tensor(B), tf.int32) # shape=[4, 1]
15.
16. #-----------------------------------------修改部分(开始)-----------------------------------------
17. #要想让A和B进行tf.matmul操作,第一个维数必须一致。因此要把B先tile后转成[2, 4, 1]维
18. B_ = tf.tile(B, [2, 1])# B的第一维复制2倍,第二维复制1倍
19. B = tf.reshape(B_, [2, 4, 1])
20. # 或 更通用的改法:
21. #B_ = tf.tile(B, [tf.shape(A)[0], 1])
22. #B = tf.reshape(B_, [tf.shape(A)[0], tf.shape(B)[0], tf.shape(B)[1]])
23. #-----------------------------------------修改部分(结束)-----------------------------------------
24.
25. #此时就可以matmul了
26. C = tf.matmul(A, B)
27. print('C:',C.get_shape().as_list())
28. sess.run(C)
![](/static/blog/img/csdnimg/newCodeMoreWhite.png)
输出结果:
1. ('C:', [2, 3, 1])
2.
3. array([[[30],
4. [70],
5. [20]],
6.
7. [[20],
8. [60],
9. [40]]], dtype=int32)
方案二
1. import numpy as np
2. import tensorflow as tf
3. sess = tf.Session()
4.
5. A = np.array([[[1, 2, 3, 4],
6. [5, 6, 7, 8],
7. [9, 0, 1, 2]],
8. [[4, 3, 2, 1],
9. [8, 7, 6, 5],
10. [2, 1, 0, 9]]])
11. B = np.array([[1], [2], [3], [4]])
12.
13. A = tf.cast(tf.convert_to_tensor(A), tf.int32) # shape=[2, 3, 4]
14. B = tf.cast(tf.convert_to_tensor(B), tf.int32) # shape=[4, 1]
15.
16. #-----------------------------------------修改部分(开始)-----------------------------------------
17. #把A的前两个维度展为一个维度
18. A = tf.reshape(A, [-1, 4])
19.
20. #此时就可以matmul了
21. C = tf.matmul(A, B)
22. # print('C:',C.get_shape().as_list()) #结果: [6, 1]
23.
24. #再把C的前两个维度还原
25. C = tf.reshape(C, [2, 3, 1])
26.
27. #-----------------------------------------修改部分(结束)-----------------------------------------
28. print('C:',C.get_shape().as_list())
29. sess.run(C)
![](/static/blog/img/csdnimg/newCodeMoreWhite.png)
输出结果:
('C:', [2, 3, 1])
array([[[30],
[70],
[20]],
[[20],
[60],
[40]]], dtype=int32)
0 评论