Tensorflow报错: Shape must be rank 2 but is rank 1 for 'MatMul' (op: 'MatMul') with input shapes
import tensorflow as tf
# 创建两个常量Tensor
const1 = tf.constant([2, 2])
const2 = tf.constant([4, 4])
multiple = tf.matmul(const1, const2)
# 尝试用print输出multiple的值
print(multiple)
初学TF时矩阵乘法报错:
ValueError: Shape must be rank 2 but is rank 1 for ‘MatMul’ (op: ‘MatMul’) with input shapes: [2], [2].
解决方法:
矩阵相乘是对应的行和列相乘的结果,所以需要第一个矩阵的列数与第二个矩阵的行数相等。
只需令
const1 = tf.constant([[2, 2]])
const2 = tf.constant([[4],[4]])
还没有评论,来说两句吧...