Tensorflow 2: Tensors

Expanding from my previous post:

I will create a new jupyter notebook called 2_Tensors.ipynb.  All my code can be found on github.

Tensors

A tensor is simply an array of data. The tensor rank is the number of dimensions the data has.  Tensors can be defined in the tensorflow API or using python native data types.  This means we can use libraries like numpy.

#Tensorflow constants
Scalar = tf.constant([2])
Vector = tf.constant([5,6,2])
Matrix = tf.constant([[1,2,3],[4,5,6],[7,8,9]])
Tensor = tf.constant([
    [[1,2,3],[2,3,4],[3,4,5]],
     [[1,2,3],[2,3,4],[3,4,5]],
     [[1,2,3],[2,3,4],[3,4,5]]
])
import numpy as np

#tensors using numpy
np_t_0 = np.array(10, dtype=np.int32)
np_t_1 = np.array([b"blue",b"red",b"green"])
np_t_2 = np.array([
[True,True,False],[False,False,True],[False,True,False]],dtype=np.bool)

Scalar_np = tf.constant(np_t_0)
Vector_np = tf.constant(np_t_1)
Matrix_np = tf.constant(np_t_2)

Sessions

Previously we had executed the graph by using 3 separate steps

session = tf.Session()
result = session.run(c)
print(result)
session.close()

But it is better to use the following form

with tf.Session() as session:
    result = session.run(Scalar)

This will automatically close the session.

Running

Then executing shows that the run command essentially asks Tensorflow to solve for that variable.

with tf.Session() as session:
    result = session.run(Scalar)
    print "Scalar (1 entry):\n %s \n" % result
    result = session.run(Vector)
    print "Vector (3 entries) :\n %s \n" % result
    result = session.run(Matrix)
    print "Matrix (3x3 entries):\n %s \n" % result
    result = session.run(Tensor)
    print "Tensor (3x3x3 entries) :\n %s \n" % result
    
    result = session.run(Scalar_np)
    print "Scalar (1 entry):\n %s \n" % result
    result = session.run(Vector_np)
    print "Vector (3 entries) :\n %s \n" % result
    result = session.run(Matrix_np)
    print "Matrix (3x3 entries):\n %s \n" % result

So the output is.

Scalar (1 entry):
[2]

Vector (3 entries) :
[5 6 2]

Matrix (3x3 entries):
[[1 2 3]
[4 5 6]
[7 8 9]]

Tensor (3x3x3 entries) :
[[[1 2 3]
[2 3 4]
[3 4 5]]

[[1 2 3]
[2 3 4]
[3 4 5]]

[[1 2 3]
[2 3 4]
[3 4 5]]]

Scalar (1 entry):
10

Vector (3 entries) :
['blue' 'red' 'green']

Matrix (3x3 entries):
[[ True True False]
[False False True]
[False True False]]

You may also see .eval() used instead of .run() sometimes.  .eval(X) is essentially shorthand for tf.get_default_session().run(X).  Eval will always use the default session where as .run(X) can be executed on different sessions.

Direct from Tensorflow documentation

# Using `Session.run()`.
sess = tf.Session()
c = tf.constant(5.0)
print(sess.run(c))

# Using `Tensor.eval()`.
c = tf.constant(5.0)
with tf.Session():
  print(c.eval())

Both give the same answer of 5.0

Continue… onto Tensorboard.