简体   繁体   中英

In Tensorflow How can I add a Tensor to another Tensor with different shape?

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.

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))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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