f.lust
f.lust

Reputation: 23

add two tensor with shape one dimension in tensorflow

hallo i am new in tensorflow, i am trying to add two tensor with shape one dimension and actually when i printed out there is nothing added so for example:

num_classes = 11

v1 = tf.Variable(tf.constant(1.0, shape=[num_classes]))
v2 = tf.Variable(tf.constant(1.0, shape=[1]))

result = tf.add(v1 ,  v2)

and that's the output when i printed out

result Tensor("Add:0", shape=(11,), dtype=float32)

So the result is still 11 instead of 12. Is my way wrong or there is another way to add them.

Upvotes: 1

Views: 3205

Answers (2)

SuperTetelman
SuperTetelman

Reputation: 617

From the documentation here: https://www.tensorflow.org/api_docs/python/tf/add

tf.add(x, y, name=None)

Returns x + y element-wise.
NOTE: Add supports broadcasting.

This means that the add function you are calling will iterate each row in v1 and broadcast v2 on it. It does not append lists as you expect, instead what is likely happening is that the value of v2 will be added to each row of v1.

So Tensorflow is doing this:

sess = tf.Session()
c1 = tf.constant(1.0, shape=[1])
c2 = tf.constant(1.0, shape=[11])
result = (tf.add(c1, c2))
output = sess.run(result)
print(output) # [ 2.  2.  2.  2.  2.  2.  2.  2.  2.  2.  2.]

If you want an output of shape 12 you should use concat. https://www.tensorflow.org/api_docs/python/tf/concat

sess = tf.Session()
c1 = tf.constant(1.0, shape=[1])
c2 = tf.constant(1.0, shape=[11])
result = (tf.concat([c1, c2], axis=0))
output = sess.run(result)
print(output) # [ 1.  1.  1.  1.  1.  1.  1.  1.  1.  1.  1.  1.]

Upvotes: 2

mrry
mrry

Reputation: 126154

It sounds like you might be wanting to concatenate the two tensors, so the tf.concat() op is probably what you want to use:

num_classes = 11
v1 = tf.Variable(tf.constant(1.0, shape=[num_classes]))
v2 = tf.Variable(tf.constant(1.0, shape=[1]))

result = tf.concat([v1, v2], axis=0)

The result tensor will have a shape of [12] (i.e. a vector with 12 elements).

Upvotes: 0

Related Questions