Hello everyone:
Right now I am transforming a tensorflow code to pytorch, I want a function like
tf.add_n , it;s used for (Adds all input tensors element-wise.)
I survey the pytorch doc ,but I didn’t find any function names as torch.add_n
tf.add_n
https://www.tensorflow.org/api_docs/python/tf/math/add_n
zhl515
December 26, 2018, 8:23am
2
Just use sum() function in raw Python, eg
a = torch.randn(3, 4)
b = [10 * a]
c = sum(b) # input b is a list of tensor with same shape, return c is tensor with matched shpe
2 Likes
Thanks Hao Zheng ,but I want to do this:
Here is the tf.add_n exmaple code
import tensorflow as tf;
import numpy as np;
input1 = tf.constant([1.0, 2.0, 3.0])
input2 = tf.Variable(tf.random_uniform([3]))
output = tf.add_n([input1, input2])
init_op = tf.global_variables_initializer()
with tf.Session() as sess:
sess.run(init_op)
print(sess.run(input1))
print(sess.run(input2))
print(sess.run(output))
Output:
[1. 2. 3.]
[0.6231705 0.50447917 0.28593874]
[1.6231705 2.5044792 3.2859387]
But I want to do is like this , I could plus this two tensor!!
ptrblck
December 26, 2018, 2:42pm
4
You can just use the plus sign for both tensors or torch.add
:
a = torch.tensor([1., 2., 3.])
b = torch.randn(3)
c = a+b