Tensorflow报错: Shape must be rank 2 but is rank 1 for 'MatMul' (op: 'MatMul') with input shapes

不念不忘少年蓝@ 2023-12-13 06:24 106阅读 0赞
  1. import tensorflow as tf
  2. # 创建两个常量Tensor
  3. const1 = tf.constant([2, 2])
  4. const2 = tf.constant([4, 4])
  5. multiple = tf.matmul(const1, const2)
  6. # 尝试用print输出multiple的值
  7. print(multiple)

初学TF时矩阵乘法报错:

ValueError: Shape must be rank 2 but is rank 1 for ‘MatMul’ (op: ‘MatMul’) with input shapes: [2], [2].

解决方法:

矩阵相乘是对应的行和列相乘的结果,所以需要第一个矩阵的列数与第二个矩阵的行数相等。

只需令

  1. const1 = tf.constant([[2, 2]])
  2. const2 = tf.constant([[4],[4]])

发表评论

表情:
评论列表 (有 0 条评论,106人围观)

还没有评论,来说两句吧...

相关阅读