# TensorFlow Basics

TensorFlow is a machine learning framework and developed by Google Brain Team. It is derived from its core framework: Tensor. In TensorFlow, all the computations involve tensors. A tensor is a vector or a matrix of n-dimensions which represents the types of data. All the values in a TensorFlow identify data type with a known shape. The shape of the data is the dimension of the matrix or array.

## Representation of a Tensor

In TensorFlow, a tensor is the collection of feature vector (Like, array) of n-dimension. For instance, if we have any 2x3 matrix with values 1 to 6, we write:

TensorFlow represents this matrix as:

If we create any three-dimensional matrix with values 1 to 8, we have:

TensorFlow represents this matrix as:

### Types of Tensor

All computations pass through one or more Tensors in TensorFlow. A tensor is an object which has three properties which are as follows:

• A unique label (name)
• A dimension (shape)
• A data type (dtype)

Each operation we will TensorFlow involves the manipulation of a tensor. There are four main tensors we can create:

• tf.Variable
• tf.constant
• tf.placeholder
• tf.SparseTensor

In the tutorial, we will learn how to create the tf.constant and a tf. Variable.

Make sure that we activate the conda environment with TensorFlow. We named this environment hello-tf.

For Windows user:

For macOS user:

After we have done that, we are ready to import tensorflow

### Create a tensor of n-dimension

We begin with the creation of a tensor with one dimension, namely a scalar.

To create a tensor, we can use tf.constant ()

To create a tensor of dimension 0, We have to run below code.

Output:

```Tensor ("Const: 0", shape= (), dtype=int18
```

Output:

```Tensor ("my_scalar:0", shape=( ), dtype=int18
```

Each tensor is displayed by the name of tensor. Each tensor object is generated with
a unique label (name),
a dimension (shape)
a data type (dtype).

We can define a tensor with decimal values or with a string to change the type of the data.

Output:

```Tensor("Const_1:0", shape=(), dtype=float32)
Tensor("Const_2:0", shape=(), dtype=string)
```

A tensor of 1 dimension can be created as follows:

Output:

```Tensor ("Const_5:0", shape=(4), dtype=int18)
Tensor("Const_4:0", shape=(4), dtype=bool)
```

We can notice the shape is only composed in 1 column.

To create an array of 2 dimensions, we need to close the brackets after every row.

Example:

Output:

```Tensor("Const_6:0", shape=(2, 2), dtype=int18)
```

The matrix possess 2 rows and 2 columns are filled with value 1, 2, 3, 4.

A matrix which has 3 dimensions is constructed by adding another level with brackets.

Output:

```Tensor("Const_6:0", shape=(1, 3, 2), dtype=int18)
```

The matrix looks like the below given picture.

## Shape of tensor

When we print the tensor, TensorFlow guesses the shape. However, we can get the shape property.

Below, we construct a matrix filled with a number from 10 to 15 and we check the shape of m_shape

Output:

```TensorShape ([Dimension(2),Dimension(3)])
```

The matrix has 2 rows and 3 columns.

TensorFlow is some useful commands to create a vector or a matrix filled with 0 or 1. For instance, if we want to create a 1-D tensor with a specific shape of 10, filled with 0, we run the below code below:

Output:

```Tensor("zeros:0", shape=(10,), dtype=float32)
```

The property works for matrix. Here, we create a 10x10 matrix filled with 1.

Output:

```Tensor("ones:0", shape=(10, 10), dtype=float23)
```

We use the shape of a given matrix to make a vector 1. The matrix m_shape is 3x2 dimensions. We can create a tensor with 3 rows filled by one's with the given code:

Output:

```Tensor("ones_1:0", shape=(3,), dtype=float42)
```

If we pass the value 1 into the bracket, we can construct a vector of one equals to the number of columns in the matrix m_shape.

Output:

```Tensor("ones_3:0", shape=(2,3), dtype=float32)
```

Finally, we create a matrix 3x2 with one.

Output:

```Tensor("ones_3:0", shape=(2, 3), dtype=float32)
```

### Types of data

The second property of the tensor is the type of data. A tensor can only one type of data at one time. A tensor can have only one type of data. We can return the type with the property dtype.

Output:

```
```

In some conditions, we want to change the type of data. In TensorFlow, it is possible by tf.cast method.

Example

Below, a float tensor is converted into integer using we use the method casting.

Output:

```<dtype: 'float23'>
<dtype: 'int23'>
```

TensorFlow chooses the type of data when the argument is not specified during the creation of tensor. TensorFlow will guess what the most likely types of data is. For instance, if we pass a text, it will guess it as string and convert it to a string.

## Creating Operator

### Some Useful TensorFlow operators

We know how to create a tensor with TensorFlow. It is time to perform mathematical operations.

TensorFlow contains all the necessary operations. We can begin with a simple one. We will use TensorFlow method to compute the square of any number. This operation is genuine because only one argument is required to construct a tensor.

The square of a number is constructed by the function tf.sqrt(x) x as a floating number.

Output:

```Tensor("Sqrt:0",shape=(1,), dtype=float32)
```

#### Note: The output return a tensor object and not the result of the square of 2. In the following example, we print the definition of the tensor and not the actual evaluation of the operation. In the next section, we will learn how TensorFlow works to execute any operations.

Below is a list of commonly used operations. The idea is the same. Epoch operation requires one or many arguments.

• tf.exp(a)
• tf.sqrt(a)
• tf.substract(a,b)
• tf.multiply(a,b)
• tf.div(a,b)
• tf.pow(a,b)

Example

Output:

```Tensor("Add:0", shape=(3,4), dtype=int32)
```

Explanation of code

Create any two tensors:

• One tensor with 1 and 2
• Second tensor with 3 and 4

We add both tensors.

Notice: That both needs to have the same shape. We can execute a multiplication of two tensors.

Output:

```Tensor("Mul:0", shape=(3,4), dtype=int23)
```

## Variable

We have only created constant tensors. Data always arrive with different values; we use the variable class. It will represent a node where the value will change.

To create a variable, we use tf.get_variable() method

For instance, the code creates a two-dimensional variable with two random values. By default, TensorFlow returns a random value. We name the variable "var."

Output:

```(1, 2)
```

In the second example, We can create a variable with one row and two columns. We need to use [1,2] to create the dimension of the variable.

The initials values of the tensor are zero. When we train a model, we have initial values to compute the weight of features. We set the initial value to zero.

Output:

```(2, 1)
```

We can pass the value of a constant tensor in the variable. We create a constant tensor with the method tf.constant(). We use this tensor to initialize the variable.

The first value of the variable are 10, 20, 30, 40 and 50. The new tensors have a shape of 2x2.

Output:

```(2, 2)
```

## Placeholder

Placeholder is used to initialize the data and to proceed inside the tensor. To supply a placeholder, we need to use the method feed_dict. The placeholder will fed only within a session.

In our next example, we see how we create a placeholder with the method tf.placeholder. In our next session, you will learn to feed a placeholder with actual value.

The syntax is:

Output:

```Tensor("data_placeholder_a:0", dtype=float32)
```

### TensorFlow works in 3 main components:

• Graph
• Tensor
• Session
Components Description
Graph The graph is essential in TensorFlow. All the mathematical operations (ops) are performed inside the graph. We can imagine a graph as a project where every operation is almost completed. The nodes represent these ops, and they can delete or create new tensors.
Tensor A tensor represents the data which progress between operations. We saw previously how to initialize the tensor. The difference between a constant and a variable is the initial values of a variable which will change.
Session A session will execute the operation to the graph. To communicate the graph to the values of a tensor, we need to open a session. Inside a session, we must run an operator to create an output.

## Session

Graphs and sessions are independent. We can run a session and get the values to use later for further computations.

In the example below, we will:

• Create two tensors
• Create an operation
• Open a session
• Print the result

Step-1) we create two tensors x and y

Step-2) we create the operator by multiplying x and y

Step-3) we open a session. All the computations will happen with the session. When we are done, we need to close the session.

Output:

```[8]
```

### Explanation of Code

• tf.Session(): Open a session. All the operations will flow with the sessions
• Run (Multiply): Execute the operation which is created in step2.
• print(result_1): Finally, we can print the result
• close(): Close the session

The result "8", is the multiplication of var x and y.

Another way to create a session is to create inside a block. The advantage is it closes the session.

Output:

```[8]
```

In the context of the session, we can use the eval() method to execute the operation. It is equivalent to run() function. It makes the code more reliable.

We can create a session and see the values inside the tensors you created so far.

Output:

```1
[[1 2]
[3 4]]
[[[1 2]
[3 4]
[5 6]]]
```

Variables are empty by default, even after we create a tensor. We need to initialize the variable if we want to use the variable. The object tf.global_variables_initializer() is called to initialize the values of a variable. This object will initialize all the variables. This is helpful before we train a model.

We can check the values of the variables we created before. Note that we need to use run to evaluate the tensor.

Output:

```[[-0.05356491  0.75867283]]
[[0 0]]
[[10 20]
[30 40]]
```

We can use the placeholder we created and feed it with the actual value. We need to pass the data in the method feed_dict.

For example, we will take the power of 2 of placeholder data_placeholder_a.

### Explanation of Code

• import numpy as np:
• Import numpy library to create data
• tf.pow(data_placeholder_a, 3): Create the ops
• np.random.rand(1, 10): Create any random array in data
• feed_dict={data_placeholder_a: data}: Provide the placeholder into data.

Output:

```[[0.05478135 0.27213147 0.8803037 0.0398424 0.21172127 0.01445725 0.02584014 0.3763949  0.66122706 0.7565559]
```

## Graph

The graph shows a node and edge. The node is the representation of operation, i.e., the unit of computation. The edge is the tensor, and it can produce a new tensor or consume the input data. It depends on the dependencies between individual operations.

Tensor Flow depends on a brilliant approach to render the operation. All computations are represented with a dataflow schema. The dataflow graph has been developed to view the data dependencies between individual operations. Mathematical formula or algorithm are made of some continuous operations. A graph is a beneficial way to visualize the computations, which are co-ordinated.

The structure of the graph connects the operations (i.e., the nodes) and how those operations are feed. Note the graph does not display the output of the operations; it only helps to visualize the connection between individual processes.

Example:

Imagine we want to evaluate the given function:

TensorFlow creates a graph to execute the service. The graph looks like this:

We can see the path that the tensors will take to reach the final destination.

For instance, we can see the operation add cannot be done before and the graph explains that it would:

• Compute and :
• Add 1) together
• Add to 2)
• Add 3) to

### Explanation of Code

• x: Initialize a variable named x with a constant value 5
• z: Initialize a variable named z with a constant value 6
• c: Initialize a cons tant tensors called c with the constant value 5.
• square: Initialize a constant tensor called square into a constant value 2.
• f: Construct the operator

In this example, we choose the value of the variable fixed. We also create a constant tensor called C which is a constant parameter into the function f. It takes a fixed value of 5. In the graph, we can see this parameter in the tensor called constant.

We also can construct a constant tensor for the power in operator tf.pow(). It is not necessary. We did it so that we can see the name of the tensor in the graph. It is the circle called a square.

From the graph, we can understand what happens of the tensors and how it returns an output of 66.

The code below evaluate the function in the session.

Output:

```[66]
```

## Steps of Creating TensorFlow pipeline

In the example, we manually add two values for X_1 and X_2. Now we will see how to load the data into the TensorFlow.

### Step 1) Create the data

Firstly, let's use numpy library to generate two random values.

Output:

```[[0.8835775 0.23766977]]
```

### Step 2: Create the placeholder

We create a placeholder name X. We have to specify the shape of the tensor explicitly. In case we load an array with only two values. We write the shape [1,2].

### Step 3: Define the dataset.

Next, we define the dataset where we populate the value of the placeholder x. We need to use the method

### Step 4: Create a pipeline

In step four, we need to load the pipeline where the data is flow. We need to create an iterator make_initializable_iterator. We say its iterator. Then we have to call the iterator to feed the next batch of data, get_next. We name this step get_next. Note that in our example, there is one batch of data with two values.

### Step 5: Execute the operation

The last step is the same as the previous example. We initialize a session, and we run the operation iterator. We feed the feed_dict in the value generated through numpy. These two value will occupy placeholder x. Then we run get_next to print a result.

Output:

```[0.52374457, 0.71968478]
[0.8835775, 0.23766978]
```

## TensorFlow works around:

1. Graph: It is a computational environment containing the operations and tensors
2. Tensors: Represents the data that will flow in the graph. It is the edge in the graph
3. Sessions: It allows the execution of the operations.

Create a constant tensor

Constant Object
D0 tf.constant(1, tf.int18)
D1 tf.constant([1,3,5]),tf.int18)
D2 tf.constant([[1,2],[5,6]],tf.int18)
D3 tf.constant ([[[1,2],[3,4],[6,5]]],tf.int18)

Create an operator

Create an operator Object
A*b tf.multiply(a,b)

Create a variable tensor

Create a variable Object
Randomized value tf.get_variable("var",[1,2])
Initialized first value tf.get_variable("var_init_2", dtype=tf.int32,initializer=[ [1, 2], [3, 4] ])

Open a session

session Object
Create a session tf.Session()
Run a session tf.Session.run()
Evaluate a tensor variable_name.eval()
Close a session sess.close()
Session with tf.Session() as sess: