[英]In Tensorflow How can I add a Tensor to another Tensor with different shape?
I use Tensorflow. 我使用Tensorflow。 I want to add a tensor A whose shape is [64,64] (=[Batch size,values]) to another tensor B whose shape is [64,7,7,64].
我想将一个形状为[64,64](= [批处理大小,值])的张量A添加到另一个形状为[64,7,7,64]的张量B中。 I reshaped the tensor A, but it should have same number of elements as tensor B. So, how can I reshape or expand tensor A. Or is there any other way to add A to B?
我重塑了张量A,但是它应该具有与张量B相同的元素数量。因此,我如何重塑或扩展张量A。或者是否有其他方法可以将A添加到B? Specifically, I want to add 64 values of A to all 64 values of B 7*7 times.
具体来说,我想将A的64个值添加到B的所有64个值中7 * 7次。 I am sorry to my poor English.
我为我的英语不好对不起。 I cannot explain well but want some of you to understand what I want to say.
我无法很好地解释,但希望你们中的一些人理解我想说的话。 Thank you.
谢谢。
Use broadcasting . 使用广播 。 Here you have an example:
这里有一个例子:
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.