【发布时间】:2018-05-24 04:05:56
【问题描述】:
所以,我想将矩阵与矩阵相乘。当我尝试使用矩阵的数组时,它可以工作:
import tensorflow as tf
x = tf.placeholder(tf.float32, [None, 3])
W = tf.Variable(tf.ones([3, 3]))
y = tf.matmul(x, W)
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
curr_y = sess.run(y, feed_dict={x: [[1,2,3],[0,4,5]]})
print curr_y
所以数组的批量大小为 2,形状为 3x1。所以我可以将形状为 3x3 的矩阵与数组 3x1 相乘。 但是当我再次有一个形状为 3x3 的矩阵时,但这次是一个矩阵而不是一个形状为 3x2、批量大小为 2 的数组,它不起作用。
但是,如果我尝试将矩阵与矩阵相乘。它不起作用。
import tensorflow as tf
x = tf.placeholder(tf.float32, [None, 3,3])
W = tf.Variable(tf.ones([3, 3]))
y = tf.matmul(x, W)
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
curr_y = sess.run(y, feed_dict={x: [[[1,2,3],[1,2,3]],[[1,1,4],[0,4,5]]]})
print curr_y
########编辑ValueError: Shape 必须是 2 级,但 'MatMul' 是 3 级(操作: 'MatMul') 输入形状:[?,3,3], [3,3]。
对不起,我想要做的是,将一个矩阵与一批矩阵或数组进行 matmul。所以我不想做
y = tf.matmul(x, W)
其实我也想做
y = tf.matmul(W, x)
【问题讨论】:
标签: python matrix tensorflow