Tensor

The variables in TensorFlow are represented as tensors that are arrays of any number of dimensions. There are three main types of tensors—tf.Variable, tf.constant, and tf.placeholder. Except for tf.Variable, all the other tensors are immutable.

To check the shape of a tensor, we will use the following code:

# constant
a = tf.constant(1)
print(a.shape)
>> ()

# array of five elements
b = tf.constant([1,2,3,4,5])
print(b.shape)
>> (5,)

The elements of a tensor are easily accessible, and the mechanisms are similar to those employed by Python:

a = tf.constant([1,2,3,4,5])
first_three_elem = a[:3]
fourth_elem = a[3]

sess = tf.Session()
print(sess.run(first_three_elem))

>> array([1,2,3])

print(sess.run(fourth_elem))

>> 4
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset