nobody
nobody

Reputation: 455

How are tensors added in TensorFlow?

I am trying to understand some basics about tensorflow by reading this tutorial here: https://www.guru99.com/tensor-tensorflow.html What I cannot understand is why when running these commands:

# Add
tensor_a = tf.constant([[1,2]], dtype = tf.int32)
tensor_b = tf.constant([[3, 4]], dtype = tf.int32)
tensor_add = tf.add(tensor_a, tensor_b)
print(tensor_add)

I get this result:

Tensor("Add:0", shape=(1, 2), dtype=int32)

I did the calculation on paper and when adding these 2 vectors, I get something complete different(4,6), why is that? What is a "tensor" anyway?

Upvotes: 1

Views: 125

Answers (1)

OverLordGoldDragon
OverLordGoldDragon

Reputation: 19776

A "tensor" in TensorFlow is a computational object. What you get with tf.add is a NODE that adds its inputs, tensor_a and tensor_b - which is what you're seeing with Tensor("Add:0") (the :0 is its form of 'id'). This node, however, does nothing until executed - it's just "there" (see below). To execute, run

with tf.Session() as sess:       # 'with' ensures computing resources are 
    print(sess.run(tensor_add))  # properly handled, but isn't required

I suggest you check out some starter tutorials, as TF isn't exactly intuitive - e.g. here. Good luck

Upvotes: 1

Related Questions