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

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下同样的写法没有问题

import numpy as np

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]]])
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)

输出结果:

[[[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)

解决办法


方案一

import numpy as np
import tensorflow as tf
sess = 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)

输出结果:

('C:', [2, 3, 1])

array([[[30],
        [70],
        [20]],

       [[20],
        [60],
        [40]]], dtype=int32)

方案二

import numpy as np
import tensorflow as tf
sess = 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的前两个维度展为一个维度
A = tf.reshape(A, [-1, 4])

#此时就可以matmul了
C = tf.matmul(A, B)
# print('C:',C.get_shape().as_list())  #结果: [6, 1]

#再把C的前两个维度还原
C = tf.reshape(C, [2, 3, 1])

#-----------------------------------------修改部分(结束)-----------------------------------------
print('C:',C.get_shape().as_list())
sess.run(C)

输出结果:

('C:', [2, 3, 1])
array([[[30],
        [70],
        [20]],

       [[20],
        [60],
        [40]]], dtype=int32)

  • 5
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值