Neural Network
Deep learning, a subfield of machine learning that is a set of algorithms that is inspired by the structure and function of the brain. These algorithms are usually called Artificial Neural Networks (ANN). Deep learning is one of the hottest fields in data science with many case studies that have astonishing results in robotics, image recognition and Artificial Intelligence (AI).
Neural networks are a set of algorithms, modeled loosely after the human brain, that are designed to recognize patterns. They interpret sensory data through a kind of machine perception, labeling or clustering raw input.
The patterns they recognize are numerical, contained in vectors, into which all real-world data, be it images, sound, text or time series, must be translated.
In the process of learning, a neural network finds the right function, or the correct manner of transforming x into y.
As seen from above diagram we conisider three layers in neural network :INPUT , HIDDEN & OUTPUT
Features
Cluster and classify.
Group unlabeled data according to similarities among the example inputs, and they classify data when they have a labeled dataset to train on.
Feature Extraction
Develope Algorithms for reinforcement learning, classification and regression (Deep Neural Networks)
General way to solve problems with Neural Networks
Neural networks is a special type of machine learning (ML) algorithm. So, like every ML algorithm, it follows the usual ML workflow of data preprocessing, model building and model evaluation. For the sake of conciseness, I have listed out a To-D0 list of how to approach a Neural Network problem.
Check if it is a problem where Neural Network gives you uplift over traditional algorithm
Do a survey of which Neural Network architecture is most suitable for the required problem
Define Neural Network architecture through whichever language / library you choose.
Convert data to right format and divide it in batches
Pre-process the data according to your needs
Augment Data to increase size and make better trained models
Feed batches to Neural Network
Train and monitor changes in training and validation data sets
Test your model, and save it for future use
About Libaries
Keras is a powerful easy-to-use Python library for developing and evaluating deep learning models. It wraps the efficient numerical computation libraries Theano and TensorFlow and allows you to define and train neural network models in a few short lines of code.
Implementation
Lets First Initialize with a simple random generated data.
Problem
We have a data that describes patient medical record data for Pima Indians and whether they had an onset of diabetes within five years.
As such, it is a binary classification problem (onset of diabetes as 1 or not as 0). All of the input variables that describe each patient are numerical. This makes it easy to use directly with neural networks that expect numerical input and output values, and ideal for our first neural network in Keras.
WHY RANDOM SEED?
This means they make use of randomness, such as initializing to random weights, and in turn the same network trained on the same data can produce different results.
The random initialization allows the network to learn a good approximation for the function being learned.
Randomness is used Neural Network performs better with it than without.
Define Model
Models in Keras are defined as a sequence of layers.
We create a Sequential model and add layers one at a time until we are happy with our network topology.
The first thing to get right is to ensure the input layer has the right number of inputs. This can be specified when creating the first layer with the input_dim argument and setting it to 8 for the 8 input variables.
We can piece it all together by adding each layer. The first layer has 12 neurons and expects 8 input variables. The second hidden layer has 8 neurons and finally, the output layer has 1 neuron to predict the class (onset of diabetes or not).
Compile Model
Now that the model is defined, we can compile it.
Compiling the model uses the efficient numerical libraries under the covers (the so-called backend) such as Theano or TensorFlow. The backend automatically chooses the best way to represent the network for training and making predictions to run on your hardware, such as CPU or GPU or even distributed.
When compiling, we must specify some additional properties required when training the network. Remember training a network means finding the best set of weights to make predictions for this problem.
We must specify the loss function to use to evaluate a set of weights, the optimizer used to search through different weights for the network and any optional metrics we would like to collect and report during training.
In this case, we will use logarithmic loss, which for a binary classification problem is defined in Keras as “binary_crossentropy“. We will also use the efficient gradient descent algorithm “adam” for no other reason that it is an efficient default.
Fit Model
We have defined our model and compiled it ready for efficient computation.Now it is time to execute the model on some data.We can train or fit our model on our loaded data by calling the fit() function on the model.
The training process will run for a fixed number of iterations through the dataset called epochs, that we must specify using the nepochs argument.
We can also set the number of instances that are evaluated before a weight update in the network is performed, called the batch size and set using the batch_size argument.
For this problem, we will run for a small number of iterations (150) and use a relatively small batch size of 10. Again, these can be chosen experimentally by trial and error.
Evaluate Model
We have trained our neural network on the entire dataset and we can evaluate the performance of the network on the same dataset.
This will only give us an idea of how well we have modeled the dataset (e.g. train accuracy), but no idea of how well the algorithm might perform on new data. We have done this for simplicity, but ideally, you could separate your data into train and test datasets for training and evaluation of your model.
Evaluate your model on your training dataset using the evaluate() function on your model and pass it the same input and output used to train the model.
Finally Make Predictions
epochs : give the number of times the model is trained over the entire dataset
EXAMPLE 2
We are using a network with 1 input, 10 neurons in the hidden layer, and 1 output. The network will use a mean squared error loss function and will be trained using the efficient ADAM algorithm.
The network needs about 1,000 epochs to solve this problem effectively, but we will only train it for 100 epochs. This is to ensure we get a model that makes errors when making predictions.
After the network is trained, we will make predictions on the dataset and print the mean squared error
Seed the Random Number Generator
Use a fixed seed for the random number generator.
Random numbers are generated using a pseudo-random number generator. A random number generator is a mathematical function that will generate a long sequence of numbers that are random enough for general purpose use, such as in machine learning algorithms.
The specific way to set the random number generator differs depending on the backend
Seed Random Numbers with the Theano Backend
Generally, Keras gets its source of randomness from the NumPy random number generator.
For the most part, so does the Theano backend.
We can seed the NumPy random number generator by calling the seed() function from the random module
Note: The importing and calling of the seed function is best done at the top of your code file.
This is a best practice because it is possible that some randomness is used when various Keras or Theano (or other) libraries are imported as part of their initialization, even before they are directly used.