简体   繁体   English

如何将[Batch_Size,A,B]张量乘以[Batch_Size,B]张量以产生[Batch_Size,A]张量?

[英]How to multiply [Batch_Size, A, B] tensor by [Batch_Size, B] tensor to yield [Batch_Size, A] tensor?

Question as in title — how do I multiply two tensors of those shapes to yield a tensor of that shape? 标题中的问题 - 如何将这些形状的两个张量相乘以产生该形状的张量?

[Batch_Size, A, B] x [Batch_Size, B] = [Batch_Size, A]

tf.matmul() requires the tensors be of the same rank. tf.matmul()要求张量具有相同的等级。

import tensorflow as tf

tensor1 = tf.zeros([10, 64, 64])
tensor2 = tf.zeros([10, 64])

# dot = tf.matmul(tensor1, tensor2)  # NOT THIS
dot = tf.einsum('ijk,ik->ij', tensor1, tensor2)

with tf.Session() as sess:
    print(sess.run(dot).shape)

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

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