tensorflow报错:Shape must be rank 2 but is rank 3 for 'MatMul' (op: 'MatMul')

来源:互联网 发布:好玩的java源代码 编辑:程序博客网 时间:2024/05/19 13:56

tensorflow矩阵相乘,秩不同报错

在tensorflow中写了这样一句:

[python] view plain copy
print?
  1. y_out = tf.matmul(outputs, W)  
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下同样的写法没有问题

[python] view plain copy
print?
  1. import numpy as np  
  2.   
  3. A = np.array([[[1234],  
  4.         [5678],  
  5.         [9012]],  
  6.         [[4321],  
  7.         [8765],  
  8.                [2109]]])  
  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)  
import numpy as npA = np.array([[[1, 2, 3, 4],        [5, 6, 7, 8],        [9, 0, 1, 2]],        [[4, 3, 2, 1],        [8, 7, 6, 5],               [2, 1, 0, 9]]])print(A)print(A.shape)print('---------------------------')B = np.array([[1], [2], [3], [4]])print(B)print(B.shape)print('---------------------------')C = np.matmul(A, B)print(C)print(C.shape)

输出结果:

[python] view plain copy
print?
  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. (234)  
  9. —————————  
  10. [[1]  
  11.  [2]  
  12.  [3]  
  13.  [4]]  
  14. (41)  
  15. —————————  
  16. [[[30]  
  17.   [70]  
  18.   [20]]  
  19.   
  20.  [[20]  
  21.   [60]  
  22.   [40]]]  
  23. (231)  
[[[1 2 3 4]  [5 6 7 8]  [9 0 1 2]] [[4 3 2 1]  [8 7 6 5]  [2 1 0 9]]]
(2, 3, 4)---------------------------[[1] [2] [3] [4]](4, 1)---------------------------[[[30] [70] [20]] [[20] [60] [40]]](2, 3, 1)

解决办法

[python] view plain copy
print?
  1. import numpy as np  
  2. import tensorflow as tf  
  3. sess = tf.Session()  
  4.   
  5. A = np.array([[[1234],  
  6.                [5678],  
  7.                [9012]],  
  8.               [[4321],  
  9.                [8765],  
  10.                [2109]]])  
  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, [21])# B的第一维复制2倍,第二维复制1倍  
  19. B = tf.reshape(B_, [241])  
  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)  
import numpy as npimport tensorflow as tfsess = tf.Session()A = np.array([[[1, 2, 3, 4],               [5, 6, 7, 8],               [9, 0, 1, 2]],              [[4, 3, 2, 1],               [8, 7, 6, 5],               [2, 1, 0, 9]]])B = np.array([[1], [2], [3], [4]])A = tf.cast(tf.convert_to_tensor(A), tf.int32) # shape=[2, 3, 4]B = tf.cast(tf.convert_to_tensor(B), tf.int32) # shape=[4, 1]
#-----------------------------------------修改部分(开始)-----------------------------------------#要想让A和B进行tf.matmul操作,第一个维数必须一致。因此要把B先tile后转成[2, 4, 1]维B_ = tf.tile(B, [2, 1])# B的第一维复制2倍,第二维复制1倍B = tf.reshape(B_, [2, 4, 1])# 或 更通用的改法:#B_ = tf.tile(B, [tf.shape(A)[0], 1])#B = tf.reshape(B_, [tf.shape(A)[0], tf.shape(B)[0], tf.shape(B)[1]])#-----------------------------------------修改部分(结束)-----------------------------------------#此时就可以matmul了C = tf.matmul(A, B)print(‘C:’,C.get_shape().as_list())sess.run(C)

输出结果:

[python] view plain copy
print?
  1. (‘C:’, [231])  
  2.   
  3. array([[[30],  
  4.         [70],  
  5.         [20]],  
  6.   
  7.        [[20],  
  8.         [60],  
  9.         [40]]], dtype=int32)  
('C:', [2, 3, 1])array([[[30],        [70],        [20]],       [[20],        [60],        [40]]], dtype=int32)


转自博客:http://blog.csdn.net/blythe0107/article/details/74171870

原创粉丝点击