TensorFlow Basics to Mastery Series Part-1

Source: Deep Learning on Medium

TensorFlow Basics to Mastery Series Part-1

This series will master your skills in building neural networks using google’s TensorFlow framework

All the amazing work that Deep Learning (DL) can do is been around quite some time creating all the airs it can, driving people crazy.

Lets stop talking about it with all the stupid jaw dropping scenes for every DL breakthrough news feed scroll we do. Its time to get our get our hands dirty with this piece of cake and experience its taste to be worth on the card

Lets first list some popular Deep Learning tools/framework

Researchers have made our life easy by building some high level wrappers on top of some DL frameworks to build a neural net even with minimal knowledge on it nuances

This series will be on TensorFlow right from basics all the way up to mastery level. Fingers crossed for some exciting learning ahead and with out any further do lets get started with this new paradigm.

Quick time travel back in to programming history (Traditional vs Machine Learning programming):

Traditional programming is where we try to solve a problem and achieve the unknown result by codifying the rules with respect to data (known: rules and data, unknown: output answer)

Machine learning is where we make the system to figure out the rules by feeding in data and answers (known : output answer and data, unknown: rules)

The above image shows the comparison between Traditional Programming and Machine Learning

Now lets see the pros and cons of traditional programming

Pros — Set up hand crafted rules based on observation for every unique scenario

Cons — Becomes very difficult and messy when you have to codify rules every possible scenario(tons usually in any solving any real world problem)

Rather with a Machine learning approach we teach the machine every possible scenarios and let it figure out all possible rules with data and its corresponding labels.

Quick stroll into Machine learning’s basics..

Machine Learning learns and understands patterns in the data and tries to come up with rules without any explicit coding. This is the most exciting part of this whole new paradigm.

Once the hypothesis function(rules) is been learnt from the data presented. Ex: If y = 5x + 10 is the hypothesis function learnt then we have to evaluate how good the hypothesis function which translates the goodness factor of the model. Based on which we can decide on whether to stick on the same hypothesis function or tweak it to reduce the error made by the one formulated before.

Loss functions help us in evaluating this goodness of fit for the hypothesis function formulated. This loss function is measured between the actual output Y available to us from the data and Yhat the one produced by our hypothesis function. Some of the loss functions are

— Mean Squared Error (MSE)

— Root Mean Squared Error (RMSE)

— Mean Absolute Error (MAE)

— Mean Absolute Percentage Error (MAPE)

Making use of one of the above methods(chosen appropriately based on type of problem we try to solve) to calculate the loss/error for the formulated hypothesis function. Once we have the loss/error calculated we need to somehow find a way to reduce the it. This is done with the help of a functionality called gradient descent. Again there are various ways of making use of gradient descent to reduce error which we will cover as we progress through this series.

Machine learning & Deep learning:

Deep learning is a subset of machine learning. In deep learning we try to replicate the way our human brain works with neurons. Hence the model we build are called as Neural Networks.

One primary thing which differentiates traditional machine learning models from deep learning models is that the feature selection. In classic machine learning approach we select the features to be trained upon and the hypothesis function formulates the importance for each feature we picked in. But in case of deep learning neural net approach there is no layer of feature selection and the entire data is been consumed. Thus this makes them have a pet name BLACK BOX.

This figure shows the components like layers, neurons and weights(arrows) inside the so called black box of neural network

Now lets quickly see the fundamental units of a neural network and start with basic Keras implementation

— Neuron : placeholder for input/computed value

— Layers : set of neurons in one particular level of hierarchy

— Weights : defines the importance of a neuron aiding to stay close to actual data in prediction

— loss : Difference between actual data output and model/network output

— optimizer : method or function that reduces the loss/error

“Keras” — TensorFlow’s high level API

As mentioned earlier Keras is a high level wrapper or API to build neural network with TensorFlow/Theano/CNTK as its granular level

Neural network is nothing but a set of functions put together in a logical arrangement which can learn patterns embedded in data presented.

Install TensorFlow followed by its API Keras

All the mathematical operation that happens behind the hood in a machine learning model is made available as functions(well optimized code written by researchers)in Keras.

Lets build a simple model using TensorFlow with Keras API

x and y are input and output labels respectively. If we do a simple math by observing the relationship between x and y gives us

y = 3x + 1

Now we will try to replicate the math that happened in our biological brain to artificial neural network. First lets lay the network structure

  1. TensorFlow and Numpy libraries are imported first.

2. Keras Sequential call is defined to create neural network layers one-after the other.

3. Next we define Dense layer which becomes our first layer with only one neuron since our input length for each data point is one.

4.We define the loss function which is MSE

5.”Stochastic Gradient Descent” optimizer is initialized to reduce the loss

Now our architecture(layers, loss function, optimizer etc) is all set to train on our data (x & y)

6. Model.fit method call will start training our model for 300 epochs

Now lets see how much our network has learned from our data

Trying to predict on a new data point say 5 should produce 16 (i.e 3*5 +1).

The model outputs 15.98 which is very close to 16. Thats it!! we have tried and replicated our biological behavior of neurons outside human system:)

Will see you soon in part-2 of this series, until then keep hustling…!!!!