Introduction To Keras
Introduction To Keras
1
About Keras
2
Keras – Neural Networks
2. The models in Keras can be created using the sequential or the functional
APIs.
4. The functional API makes it easier to build the complex models that have
multiple inputs, multiple outputs and shared layers. Reserved for experts
3
Keras – Sequential API
2. The models in Keras can be created using the sequential or the functional
APIs.
4. The functional API makes it easier to build the complex models that have
multiple inputs, multiple outputs and shared layers. Reserved for experts
4
5
6
7
Keras – Neural Networks
1. create the empty model with the following code: model = Sequential()
3. In the sequential API, create layers by instantiating an object of one of the layer types
The created layers are then added to the model using the model.add() function
4. As an example, we will create a model and then add two layers to it:
a. model = Sequential()
b. model.add( Dense( 10, input_shape =( 256,))
c. model.add( Activation(' tanh’))
d. model.add( Dense( 10))
e. model.add( Activation(' softmax'))
8
Keras – Neural Networks
5. model built in the previous sections needs to be compiled with the model.compile() method
before it can be used for training and prediction
II. Keras offers the following built-in optimizer functions: SGD RMSprop Adagrad
Adadelta Adam Adamax Nadam
b. loss: specify your own loss function or use one of the provided loss functions
I. The optimizer function optimizes the parameters so that the output of this loss
function is minimized.
9
Keras – Neural Networks
10
Deep neural network in TF
11
Keras – DNN - Mnist dataset - Walkthru
1. import keras
2. from keras.datasets import mnist
3. from keras.models import Sequential
4. from keras.layers import Dense, Dropout
5. from keras.optimizers import SGD
6. from keras import utils
7. import numpy as np
14. (x_train, y_train), (x_test, y_test) = mnist.load_data() # reshape the two dimensional 28 x 28 pixels # sized images into a single vector of 784 pixels
12
Keras – DNN - Mnist dataset - Walkthru (Contd…)
27. model = Sequential() # the first layer has to specify the dimensions of the input vector
28. model.add( Dense( units = 128, activation ='sigmoid', input_shape =( n_inputs,))) # add dropout layer for preventing overfitting
29. model.add( Dropout( 0.1))
30. model.add( Dense( units = 128, activation ='sigmoid'))
31. model.add( Dropout( 0.1)) # output layer can only have the neurons equal to the number of outputs
32. model.add( Dense( units = n_classes, activation ='softmax')) # print the summary of our model
33. model.summary()
13
Thank You
14