0% found this document useful (0 votes)
41 views14 pages

Introduction To Keras

Uploaded by

Rosina Ahiave
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
41 views14 pages

Introduction To Keras

Uploaded by

Rosina Ahiave
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 14

Keras

1
About Keras

1. Keras is a high-level library that allows the use of TensorFlow as a backend


deep learning library

2. Now Keras is included in TensorFlow Core as module tf.keras

3. Offers a consistent and simple API Modularity to allow the representation of


various elements as pluggable modules

4. Ease of extensibility to add new modules as classes and functions

5. Python-native for both code and model configuration

6. Out-of-the-box common network architectures that support CNN, RNN, or a


combination of both

2
Keras – Neural Networks

1. Neural network models in Keras are defined as the graph of layers.

2. The models in Keras can be created using the sequential or the functional
APIs.

3. Sequential API is an easy to use as we can build simple to deep neural


networks easily using well defined layers

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

1. Neural network models in Keras are defined as the graph of layers.

2. The models in Keras can be created using the sequential or the functional
APIs.

3. Sequential API is an easy to use as we can build simple to deep neural


networks easily using well defined layers

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()

2. Next add the layers to this model

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

6. The compile method takes three arguments:


a. optimizer: specify your own function or one of the functions provided by Keras.
I. This function is used to update the parameters in the optimization iterations.

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.

II. Keras provides the following loss functions: mean_squared_error


mean_absolute_error mean_absolute_pecentage_error
mean_squared_logarithmic_error squared_hinge hinge categorical_hinge
sparse_categorical_crossentropy binary_crossentropy poisson cosine proximity

9
Keras – Neural Networks

6. The compile method takes three arguments (Contd…):


a. metrics: list of metrics that need to be collected while training the model. All the loss
functions also work as the metric function.

10
Deep neural network in TF

Class Work - Implement this in Keras

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

8. # define some hyper parameters


9. batch_size = 100
10. n_inputs = 784
11. n_classes = 10
12. n_epochs = 10

13. # get the data

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

15. x_train = x_train.reshape( 60000, n_inputs)


16. x_test = x_test.reshape( 10000, n_inputs)

17. # convert the input values to float32


18. x_train = x_train.astype( np.float32)
19. x_test = x_test.astype( np.float32)

20. # normalize the values of image vectors to fit under 1


21. x_train /= 255
22. x_test /= 255

23. # convert output data into one hot encoded format


24. y_train = utils.to_categorical( y_train, n_classes)

25. y_test = utils.to_categorical( y_test, n_classes )

12
Keras – DNN - Mnist dataset - Walkthru (Contd…)

26. # build a sequential model

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()

34. # compile the model


35. model.compile( loss ='categorical_crossentropy', optimizer = SGD(), metrics =['accuracy'])

36. # train the model


37. model.fit( x_train, y_train, batch_size = batch_size, epochs = n_epochs)

38. # evaluate the model and print the accuracy score

39. scores = model.evaluate( x_test, y_test)

40. print('\ n loss:', scores[ 0])

41. print('\ n accuracy:', scores[ 1])

13
Thank You

14

You might also like