简体   繁体   English

广播带有动态形状的tf.matmul

[英]Broadcast tf.matmul with dynamic shapes

I would like to broadcast a tf.matmul operation between two tensors of ranks 2 and 3, one of which contains an "unknown" shaped dimension (basically a 'None' value in a particular dimension). 我想在等级2和3的两个张量之间广播tf.matmul操作,其中一个张量包含“未知”形状的维(基本上是特定维中的“无”值)。

The problem is that with dynamic dimensions tf.reshape and tf.broadcast_to don't seem to work. 问题在于动态尺寸tf.reshapetf.broadcast_to似乎不起作用。

x = tf.placeholder(shape=[None, 5, 10], dtype=tf.float32)
w = tf.ones([10, 20])
y = x @ w
with tf.Session() as sess:
  r1 = sess.run(y, feed_dict={x: np.ones([3, 5, 10])})
  r2 = sess.run(y, feed_dict={x: np.ones([7, 5, 10])})

Take the above code as an example. 以上面的代码为例。 In this case I'm feeding two different batches of 3 and 7 elements each. 在这种情况下,我要分别喂食两批分别为3和7的元素。 I would like r1 and r2 to be the result of matrix-multiplying w by each of the 3 or 7 elements from these batches. 我希望r1r2是矩阵w与这些批处理中3或7个元素中的每个元素相乘的结果。 Therefore the resulting shapes for r1 and r2 respectively would be (3, 5, 20) and (7, 5, 20), but instead I'm getting: 因此, r1r2的最终形状分别为( r2 )和(7、5、20),但是我得到的是:

ValueError: Shape must be rank 2 but is rank 3 for 'matmul' (op: 'MatMul') with input shapes: [?,5,10], [10,20]. ValueError:形状必须为2级,但输入形状为[?,5,10],[10,20]的“ matmul”(操作:“ MatMul”)为3级。

w can be expanded to a rank-3 tensor with a batch-size equal to that of the input. w可以扩展为rank-3张量,其批大小等于输入的大小。 Then, the matmul operation can be performed 然后,可以执行matmul操作

x = tf.placeholder(shape=[None, 5, 10], dtype=tf.float32)
w = tf.ones([10, 20])

number_batches = tf.shape(x)[0]
w = tf.tile(tf.expand_dims(w, 0), [number_batches, 1, 1])
y = x @ w
with tf.Session() as sess:
  print(sess.run(y, feed_dict={x: np.ones([2, 5, 10])}))
  print(sess.run(y, feed_dict={x: np.ones([3, 5, 10])}))

live code here 现场代码在这里

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM