Reputation: 33
I use 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]. 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? Specifically, I want to add 64 values of A to all 64 values of B 7*7 times. 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.
Upvotes: 3
Views: 2228
Reputation: 11895
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))
Upvotes: 3