[英]How can I trim / remove part of a Tensor to match the shape of another Tensor with PyTorch?
[英]In Tensorflow How can I add a Tensor to another Tensor with different shape?
我使用Tensorflow。 我想将一个形状为[64,64](= [批处理大小,值])的张量A添加到另一个形状为[64,7,7,64]的张量B中。 我重塑了张量A,但是它应该具有与张量B相同的元素数量。因此,我如何重塑或扩展张量A。或者是否有其他方法可以将A添加到B? 具体来说,我想将A的64个值添加到B的所有64个值中7 * 7次。 我为我的英语不好对不起。 我无法很好地解释,但希望你们中的一些人理解我想说的话。 谢谢。
使用广播 。 这里有一个例子:
import tensorflow as tf
import numpy as np
A = tf.constant(np.arange(64*64), shape=(64, 64), dtype=tf.int32)
B = tf.ones(shape=(64, 7, 7, 64), dtype=tf.int32)
A_ = A[:, None, None, :] # Shape=(64, 1, 1, 64)
result = A_ + B
with tf.Session() as sess:
print(sess.run(result))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.