Creating Neural Network using Keras
This blog gives step by step procedure for creating your own neural network using Keras in Python.Some prerequisites for this procedure are:
- Data is available in csv(comma separated Values) file format.
- python 3.x is installed on system and includes packages keras, pandas and numpy.
- Load Data:
- import statement: import pandas as pd
- data = pd.read_csv(filename.csv)
- Processing Data: There are different possible steps to make data in the form we want to provide to neural network for training as well as for its functionality.But here most commonly used steps are mentioned.
- One Hot Encoding:
- Represents all categorical variables as binary vectors.
- Each integer value is represented as binary vector where index of integer is marked as 1 while other values are marked as zeroes
- syntax is: processedData = pd.get_dummies(DataToEncode, ColumnNameWithWhichToEncode)
- Normalise all data to make sure that all values are in between 0 and 1.
- Not necessary but its better for calculation purpose.
- Separate Feature Set and Label Set.
- Split Data into Training and Testing Set.
- Its good to have training and testing dataset to be separate since it will help avoiding overfitting and underfitting of model.
- Define Model Architecture
- Sequencialize model
- It is stack model.New layer for each NN can be added into model as stack layer.
- The first layer to be added in model should take input and last layer to be added should give output.
- import statement: import sequential from keras.model
- syntax is : model = Sequential()(REMEMBER its Sequential with S, not s)
- Addition of layer:
- import statement: from keras.layers.core import Dense Activation
- model.add(Dense(NumberOfOutputUnits,ActivationFunction,InputShape(*,*,..)))
- Dense will create perceptron with NumberOfOutputUnits as output units.
- InputShape is provided , mostly in first layer to accept predefined input.
- Preventing overfitting:
- import statement: from keras.layers.core import Dropout
- syntax : model.add(dropout(float value between 0 and 1))
- This will randomly drop units from neural netowrk while generating NN layer.
- This prevents overfitting of NN and generalizes NN.
- Compilation of model:
- syntax: model.compile(optimizerFunction,lossFunction,metrics)
- loss is nothing but Objective function to be used
- Fit he model:
- model.fit(FeatureData,LabelledData,epochs=PositiveIntegerValue, batch_size=PositiveIntegerValue,verbose = 0 or 1)
- Epochs:
- Number of times training is done to update weights
- Before each epoch run for all batches, weights are updated once
- batch_size:
- Set of N sample
- One batch updates weights only once.
- Verbose:
- It is used to enable or disable logging output.
Comments
Post a Comment