# getting started with tensorflow

Tensorflow is an open source machine learning library which is developed and maintained by Google.

Tensorflow provides a large set of deep learning and machine learning library on different platforms like desktop, Web, cloud, and mobile. Tensorflow provides a library for Machine learning, Image classification and many highly optimize neural networks.

Before moving into Tensorflow let’s understand take some background on machine learning.

#### What is Machine learning

Machine learning is the ability of a computer to learn itself from a given set of data.

**There is mainly 3 broad category of machine learning.**

**Supervised**

In supervised learning, we train machine on what could be the output of given input. Its example could be Chatbots

**Unsupervised**

In unsupervised learning learn from itself and make a prediction.

Its example could be clustering of data.

**Reinforcement learning**

This is based on rewards and punishment. When the computer is successful then we give rewards and when it fails we punish it. In this way, the computer learns from its experience.

Its example is self-driving cars.

**There is 2 way to learn Tensorflow**

1. You install Tensorflow into your local machine and start programming.

1. To install Tensorflow into your local machine use below command

2. For CPU pip install tensorflow

3. For GPU pip install tensorflow-gpu

b. You can learn it online from the official website of Tensorflow.

1. Link: https://www.tensorflow.org/tutorials

2. They provide an online compiler to test your code if your machine is not too powerful.

Let’s start with some example.

There are many machine learning algorithms which are implemented in tensorflow like

- Linear regression
- Polynomial regression.
- Decision Tree
- Random Forest
- K-means
- Artificial neural networks
- CNN
- RNN with LSTM

And many more.

#### In this article, we will learn how to use tensorflow for basic classification.

Example of tensorflow

Import library import tensorflow as tf mnist = tf.keras.datasets.mnist #Divide data for training and testing (x_train, y_train),(x_test, y_test) = mnist.load_data() x_train, x_test = x_train / 255.0, x_test / 255.0 # Designing the model model = tf.keras.models.Sequential([ tf.keras.layers.Flatten(input_shape=(28, 28)), tf.keras.layers.Dense(512, activation=tf.nn.relu), tf.keras.layers.Dropout(0.2), tf.keras.layers.Dense(10, activation=tf.nn.softmax) ]) # Compiling the model model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy']) # Train model on data model.fit(x_train, y_train, epochs=5) # Identify accuracy of trained model. model.evaluate(x_test, y_test)

Above is the sample code referenced from tensorflow official website. Let’s understand the code line by line.

**First Line**

Import tensorflow as tf.

Here we are including tensorflow library in our project

mnist = tf.keras.datasets.mnist

Keras is a deep learning library which is run on tensorflow in the above line we are downloading the fashion data which contains dataset of 60,000 images in grayscale.

You can read more details about keras from https://keras.io/datasets/#fashion-mnist-database-of-fashion-articles

**Next line**

(x_train, y_train),(x_test, y_test) = mnist.load_data()

We are dividing the data into training and testing data.

We never train our machine learning model on whole data to check how it will perform on untrained data.

So train data is the data on which model will do training and test data is the data on which model we will test our model.

**Design of Tensorflow model.**

# Designing the model

model = tf.keras.models.Sequential([

tf.keras.layers.Flatten(input_shape=(28, 28)),

tf.keras.layers.Dense(512, activation=tf.nn.relu),

tf.keras.layers.Dropout(0.2),

tf.keras.layers.Dense(10, activation=tf.nn.softmax)

])

Tensorflow provides a different type of model. Sequential model is one on them.

In the neural network, there is multiple hidden layers on each layer data is getting filtered.

Tf.keras.sequential here we are defining the type of model

Below are the hidden layers in our network each layer has its own work.

tf.keras.layers.Flatten(input_shape=(28, 28)),

tf.keras.layers.Dense(512, activation=tf.nn.relu),

tf.keras.layers.Dropout(0.2),

tf.keras.layers.Dense(10, activation=tf.nn.softmax)

I can not explain each and every content here, but I will give you some idea about what they are.

layers.Flatten ##this layer compressed the input size of image data and change image orientation.

layers.Dense(512,activation=tf.nn.relu) #### In the above line, 512 is no of nodes we want in a layer. And activation function we can say that way of prediction.

There are different types of activation functions. It depends on the problem statement which one we can use.

We can create n numbers of hidden layers in our network.

tf.keras.layers.Dense(10, activation=tf.nn.softmax)

This is the output layer of a network. With 10 nodes and softmax as an activation function.

If you are interested in learning how activation function work then you can read the same from the below link.

Relu :

https://en.wikipedia.org/wiki/Rectifier_(neural_networks)

Softmax:

https://en.wikipedia.org/wiki/Softmax_function

**Compile our model**

Till now we have successfully designed our model. Now compile it.

# Compiling the model

model.compile(optimizer=’adam’,

loss=’sparse_categorical_crossentropy’,

metrics=[‘accuracy’])

Parameters here can confuse you. I explain to you one by one.

**Optimizer: Way to minimize the error rate means how best our model can predict.**

There are lots of Optimizer Algo each one has its own benefits

You can read detailed Algo from https://en.wikipedia.org/wiki/Category:Optimization_algorithms_and_methods

Loss: loss function help network to predict the actual output.

Sparse_categorical_crossentropy is one of the best loss function.

**Model Training**

We have successfully compiled our network. Now it’s time to train out the network.

model.fit(x_train, y_train, epochs=5)

X_train if input data

Y_train is output on that data

We have added the input and output of that data.

Epochs are no of times our model will be trained on given data.

After training on data our model is ready to predict.

**Check model efficiency**

model.evaluate(x_test, y_test)

X_test is the data which is never seen by our model

Y_test is expected output.

Evaluate function check how much our predicted output is matching with actual output.