# Tensorflow Basics

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

You can import the library:

``````

In :

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 :

hello = tf.constant('Hello World')

``````
``````

In :

type(hello)

``````
``````

Out:

tensorflow.python.framework.ops.Tensor

``````
``````

In :

x = tf.constant(100)

``````
``````

In :

type(x)

``````
``````

Out:

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 :

sess = tf.Session()

``````
``````

In :

sess.run(hello)

``````
``````

Out:

b'Hello World'

``````
``````

In :

type(sess.run(hello))

``````
``````

Out:

bytes

``````
``````

In :

sess.run(x)

``````
``````

Out:

100

``````
``````

In :

type(sess.run(x))

``````
``````

Out:

numpy.int32

``````

## Operations

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

``````

In :

x = tf.constant(2)
y = tf.constant(3)

``````
``````

In :

with tf.Session() as sess:
print('Operations with Constants')
print('Subtraction',sess.run(x-y))
print('Multiplication',sess.run(x*y))
print('Division',sess.run(x/y))

``````
``````

Operations with Constants
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 :

x = tf.placeholder(tf.int32)
y = tf.placeholder(tf.int32)

``````
``````

In :

x

``````
``````

Out:

<tf.Tensor 'Placeholder_2:0' shape=<unknown> dtype=int16>

``````
``````

In :

type(x)

``````
``````

Out:

tensorflow.python.framework.ops.Tensor

``````

#### Defining Operations

``````

In :

sub = tf.sub(x,y)
mul = tf.mul(x,y)

``````

Running operations with variable input:

``````

In :

d = {x:20,y:30}

``````
``````

In :

with tf.Session() as sess:
print('Operations with Constants')
print('Subtraction',sess.run(sub,feed_dict=d))
print('Multiplication',sess.run(mul,feed_dict=d))

``````
``````

Operations with Constants
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 :

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 :

a

``````
``````

Out:

array([[ 5.,  5.]])

``````
``````

In :

a.shape

``````
``````

Out:

(1, 2)

``````
``````

In :

b

``````
``````

Out:

array([[ 2.],
[ 2.]])

``````
``````

In :

b.shape

``````
``````

Out:

(2, 1)

``````
``````

In :

mat1 = tf.constant(a)

``````
``````

In :

mat2 = tf.constant(b)

``````

The matrix multiplication operation:

``````

In :

matrix_multi = tf.matmul(mat1,mat2)

``````

Now run the session to perform the Operation:

``````

In :

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!