xrd
xrd

Reputation: 4069

What's the difference between [[1,2,3,4]] and tf.constant( [[1,2,3,4]] ) in tensorflow?

If I use this inside my IPython notebook:

import tensorflow as tf

with tf.Session():
    input1 = [[1, 2, 3, 5]] 
    input2 = [[5], [6], [7], [8]]
    output = tf.matmul( input1, input2 )
    print( output.eval() )

I get [[78]]

If I use the following code (with tf.constant), I see the same result:

import tensorflow as tf

with tf.Session():
    input1 = tf.constant( [[1, 2, 3, 5]] )
    input2 = tf.constant( [[5], [6], [7], [8]] )
    output = tf.matmul( input1, input2 )
    print( output.eval() )

Is one preferred over the other? Does this change the computational graph behind the scenes, and if so, in which way that I should care about?

Upvotes: 0

Views: 214

Answers (1)

Brandon McKinzie
Brandon McKinzie

Reputation: 46

Your two cases are indeed doing the same thing. TensorFlow will convert any list/numpy array to a tensor as soon as it gets the chance. For example, input1 is converted to a constant tensor within tf.matmul for your first case, and directly within tf.constant for your second case. TensorFlow wants to make it so that you don't have to worry about converting from lists to tensors (if possible). For example, anything object that can be passed to tf.convert_to_tensor can also be passed to any TensorFlow function/method that accepts tensors as its arguments (which can be quite handy at times).

In simple cases like these, where your input values are known ahead of time, feel free to do whichever of the two you prefer.

Upvotes: 1

Related Questions