Keras
Keras is a high-level neural networks API, written in Python and capable of running on top of TensorFlow, CNTK,
or Theano. It was developed with a focus on enabling fast experimentation. Being able to go from idea to result
with the least possible delay is key to doing good research.
What is Keras ?
Deep neural network library in Python
High-level neural networks API
Modular – Building model is just stacking layers and connecting computational graphs
Runs on top of either TensorFlow or Theano or CNTK
Why use Keras ?
Useful for fast prototyping, ignoring the details of implementing backprop or writing optimization
procedure
Supports Convolution, Recurrent layer and combination of both.
Runs seamlessly on CPU and GPU
Almost any architecture can be designed using this framework
Open Source code – Large community support
Working principle - Backend
Computational Graphs
Expressing complex expressions as a combination of simple operations
Useful for calculating derivatives during backpropagation
Easier to implement distributed computation
Just specify the inputs, outputs and make sure the graph is connected
General pipeline for implementing an ANN
Design and define the neural network architecture
Select the optimizer that performs optimization
Select the loss function and train it
Select the appropriate evaluation metric for the given problem
Implementing a neural network in Keras
Five major steps
Preparing the input and specify the input dimension (size)
Define the model architecture and build the computational graph
Specify the optimizer and configure the learning process
Specify the Inputs, Outputs of the computational graph (model) and the Loss function
Train and test the model on the dataset
Note: Gradient calculations are taken care by Auto – Differentiation and parameter updates are done
automatically in the backend
Build A Linear Regression Framework
We have already built a similar model with TensorFlow. Now we will build the same model with Keras to show you
how easy and intuitive it is.
In [1]: import numpy as np
import pandas as pd
from keras.models import Sequential
from keras.layers import Dense
from keras.wrappers.scikit_learn import KerasRegressor
import warnings
warnings.filterwarnings("ignore")
Using TensorFlow backend.
In [2]: n=4
p=2
npX = np.array([1, 2, 3, 4, 4, 2, 4, 3])
npX = np.reshape(npX, newshape=(n,p), order='F')
npy = np.array([5, 4, 7, 7])
npy = np.reshape(npy, (n,1))
The following is the summary of the model
The model consists of two sequential layers, one for the input layer and another for the output layer.
The input dimension is 2 for this example. The activation function is linear since it is a linear regression
problem.
The loss function or cost function that is to minimize is the mean of squared errors. Here error is the difference
between actual values and predicted values.
Stochastic gradient descent optimization is used to minimize the loss function.
In [3]: def baseline_model():
# create model
model = Sequential()
model.add(Dense(p, input_dim=p, use_bias=True, init='uniform', act
ivation='linear'))
model.add(Dense(1, init='uniform', activation='linear'))
# Compile model
model.compile(loss='mean_squared_error', optimizer='sgd')
return model
In [6]: # evaluate model with standardized dataset
estimator = KerasRegressor(build_fn=baseline_model, epochs=1000, batch_siz
e=5, verbose=0)
estimator.fit(npX, npy)
y_hat = estimator.predict(npX)
Let us check how close the actuals and predictions are.
In [5]: pd.DataFrame({"Actual":np.reshape(npy, (n,)), "Predicted":y_hat})
Out[5]:
Actual Predicted
0 5 5.005006
1 4 4.065510
2 7 6.968617
3 7 6.989771