Tensorflow Basics




Tensorflow Basics

Remember to reference the video for full explanations, this is just a notebook for code reference.

You can import the library:


In [1]:
import tensorflow as tf

Simple Constants

Let's show how to create a simple constant with Tensorflow, which TF stores as a tensor object:


In [2]:
hello = tf.constant('Hello World')

In [3]:
type(hello)


Out[3]:
tensorflow.python.framework.ops.Tensor

In [4]:
x = tf.constant(100)

In [5]:
type(x)


Out[5]:
tensorflow.python.framework.ops.Tensor

Running Sessions

Now you can create a TensorFlow Session, which is a class for running TensorFlow operations.

A Session object encapsulates the environment in which Operation objects are executed, and Tensor objects are evaluated. For example:


In [10]:
sess = tf.Session()

In [15]:
sess.run(hello)


Out[15]:
b'Hello World'

In [16]:
type(sess.run(hello))


Out[16]:
bytes

In [17]:
sess.run(x)


Out[17]:
100

In [14]:
type(sess.run(x))


Out[14]:
numpy.int32

Operations

You can line up multiple Tensorflow operations in to be run during a session:


In [33]:
x = tf.constant(2)
y = tf.constant(3)

In [34]:
with tf.Session() as sess:
    print('Operations with Constants')
    print('Addition',sess.run(x+y))
    print('Subtraction',sess.run(x-y))
    print('Multiplication',sess.run(x*y))
    print('Division',sess.run(x/y))


Operations with Constants
Addition 5
Subtraction -1
Multiplication 6
Division 0.666666666667

Placeholder

You may not always have the constants right away, and you may be waiting for a constant to appear after a cycle of operations. tf.placeholder is a tool for this. It inserts a placeholder for a tensor that will be always fed.

Important: This tensor will produce an error if evaluated. Its value must be fed using the feed_dict optional argument to Session.run(), Tensor.eval(), or Operation.run(). For example, for a placeholder of a matrix of floating point numbers:

x = tf.placeholder(tf.float32, shape=(1024, 1024))

Here is an example for integer placeholders:


In [46]:
x = tf.placeholder(tf.int32)
y = tf.placeholder(tf.int32)

In [47]:
x


Out[47]:
<tf.Tensor 'Placeholder_2:0' shape=<unknown> dtype=int16>

In [48]:
type(x)


Out[48]:
tensorflow.python.framework.ops.Tensor

Defining Operations


In [30]:
add = tf.add(x,y)
sub = tf.sub(x,y)
mul = tf.mul(x,y)

Running operations with variable input:


In [31]:
d = {x:20,y:30}

In [32]:
with tf.Session() as sess:
    print('Operations with Constants')
    print('Addition',sess.run(add,feed_dict=d))
    print('Subtraction',sess.run(sub,feed_dict=d))
    print('Multiplication',sess.run(mul,feed_dict=d))


Operations with Constants
Addition 50
Subtraction -10
Multiplication 600

Now let's see an example of a more complex operation, using Matrix Multiplication. First we need to create the matrices:


In [69]:
import numpy as np
# Make sure to use floats here, int64 will cause an error.
a = np.array([[5.0,5.0]])
b = np.array([[2.0],[2.0]])

In [75]:
a


Out[75]:
array([[ 5.,  5.]])

In [76]:
a.shape


Out[76]:
(1, 2)

In [77]:
b


Out[77]:
array([[ 2.],
       [ 2.]])

In [78]:
b.shape


Out[78]:
(2, 1)

In [79]:
mat1 = tf.constant(a)

In [80]:
mat2 = tf.constant(b)

The matrix multiplication operation:


In [81]:
matrix_multi = tf.matmul(mat1,mat2)

Now run the session to perform the Operation:


In [82]:
with tf.Session() as sess:
    result = sess.run(matrix_multi)
    print(result)


[[ 20.]]

That is all for now! Next we will expand these basic concepts to construct out own Multi-Layer Perceptron model!

Great Job!