在python学习过程中使用矩阵相乘函数发现无法实现向量和矩阵相乘。
报错ValueError: Shape must be rank 2 but is rank 1 for ‘MatMul’ (op: ‘MatMul’) with input shapes: [2], [2,1].
查找原因发现相乘的两个矩阵格式不对。
import tensorflow as tf
x = tf.constant([1.0,2.0])
w = tf.constant([[3.0],[4.0]])
y = tf.matmul(x,w)
print(y)
with tf.Session() as sess:
print( sess.run(y) )
把x加个[ ]就可以了x = tf.constant([[1.0,2.0]])