April 2024 - NLIT Cloudera Real-Time LLM Streaming 2024
KERAS Python Tutorial
1. TUTORIAL ON KERAS
STUDENT NAME : MAHMUT KAMALAK
LECTURE : INTRODUCTION TO PYTHON
PROFESSOR : PROF. DR. BAHADIR AKTUĞ
ID NUMBER :16290155
2. OBJECTIVE OF THE TUTORIAL
I decided to prepare a tutorial about Keras , because I am
interested in Deep Learning for months ,and I am working on a
project which is about ECG arythmia classification. In my
opinion; before start to learn Keras, there are some pre-requires
that are deep learning theory, basic python knowledge, basic
linear algebra ,basic statisctics and probabilty. I strongly advice;
after supply pre-requires , new learners should go deep projects
and articles.These days, Deep Learning is so popular by
researchers and companies in many field because of the
capability of artifial intelligence. Especially I would like to say
deep learning will take amazing responsibility of diognastic and
imaging on medical ındustry. From my view , Deep Learning has
a deep way but, there are huge magical things.
3. OVERVIEW OF THE TUTORIAL
A. Introduction
B. Installation
C. Backend Configuration
D. Overview of Deep Learning
E. Deep Learning with Keras
F. Modules
G. Layers
H. Model Compilation
I. Convolution Neural Network
J. LSTM – RNN
K. Applications
4. A.INTRODUCTION
What is Keras ?
• Keras is a deep learning libriary for Python.
• Keras is a so effective to build deep learning model and
training the models.
• This libriary makes less time-consuming to do practice for
researchers.
• It is capable of running on top of TensorFlow.
• It was developed as part of the research effort of project
ONEIROS (Open-ended Neuro-Electronic Intelligent Robot
Operating System), and its primary author and maintainer is
François Chollet, a Google engineer.
5. A.INTRODUCTION
WHY USE KERAS ?
• Keras allows to run the code without any change on both CPU
and GPU.
• High-level neural networks API.
• Useful for fast prototyping, ignoring the details of implenting
backprop or writing optimization procedure.
• Provides to work Convolution , Recurrent layer and combination
of both.
• Most of architecture is designed by using this framework.
• Keras and Python gives a amazing power to developer as
6. A.INTRODUCTION
• Keras is usually used for small datasets as it is comparitively
slower. On the other hand, TensorFlow and PyTorch are used
for high performance models and large datasets that require
fast execution.
• We can differentiate Keras like that Keras tops the list
followed by TensorFlow.Keras gained amazing popularity due
to its simplicity.
8. B.INSTALLATION
1) PREREQUISITES
You must satisfy the following requirements:
• Any kind of OS (Windows, Linux or Mac)
• Python version 3.5 or higher.
• Keras is python based neural network library so python must be
installed on your machine. If python is properly installed on
your machine, then open your terminal and type python, you
can check as specified below :
9. B.INSTALLATION
Step 1: Create virtual environment
• Virtualenv is used to manage Python packages for different
projects. This will be helpful to avoid breaking the packages
installed in the other environments. So, it is always recommended
to use a virtual environment while developing Python
applications.
• Linux or mac OS users, go to your project root directory and type
the below command to create virtual environment :
« python3 -m venv kerasenv «
• Windows user can use the below command :
« py -m venv keras »
10. B.INSTALLATION
Step 2: Activate the environment
This step will configure python and pip executables in your shell
path.
Linux/Mac OS
• Now we have created a virtual environment named “kerasvenv”.
Move to the folder and type the below command,
«$ cd kerasvenv
kerasvenv $ source bin/activate «
Windows users move inside the “kerasenv” folder and type the
below command:
«.envScriptsactivate «
11. B.INSTALLATION
Step 3: Python libraries
Keras depends on the following python libraries : Numpy , Pandas
, Scikit-learn , Matplotlib , Scipy , Seaborn.
When you installed the keras, u have already installed these
libriaries on your computer.But If there is a problem about any
libriary , we can install these one by one . As below structure
which is on command window :
« pip install ‘name of libriary’ «
12. B.INSTALLATION
• Example for Step-3 : If you have already installed , you will get
:
If you havent done before, you will get :
13. B.INSTALLATION
Step 4 : Keras Installation Using Python
As of now, we have completed basic requirements for the
installtion of Keras. Now, install the Keras using same procedure
on the command as specified below:
« pip install keras «
*** Quit virtual environment ***
After finishing all your changes in your project, then simply run
the below command to quit the environment:
« deactivate »
14. C.BACKEND CONFIGURATION
• This step explains Keras backend implementations TensorFlow
and Theano .
TensorFlow
TensorFlow is an open source machine learning library used for
numerical computational tasks developed by Google. Keras is a
high level API built on top of TensorFlow or Theano. We know
already how to install TensorFlow using pip.
« pip install theano «
Theano
Theano is an open source deep learning library that allows you to
evaluate multi-dimensional arrays effectively. We can easily install
15. D. OVERVIEW OF DEEP LEARNING
• We can define like that Deep Learning is consisting of layers
which are follows each other to get useful features.
• Deep learning is sub-branch of machine learning.
• ‘Deep’ means that it does not mean that get deeeeeep magicful
knowledge.This word means that multi-layers and amount of
layers point out the depth of a model.
• Modern deep learning algortithms have thousunds layers.
• This layered notation is called ‘ Neural Networks ‘ which is
inspired neurobiology but , we can say that this model
process does not exactly match with neuro-science even some
articles show opposite idea.
16. D. OVERVIEW OF
DEEP LEARNING
In deep learning, a computer
model learns to perform
classification tasks directly from
images, text, or sound. Deep
learning models can achieve
state-of-the-art accuracy,
sometimes exceeding human-
level performance. Models are
trained by using a large set of
labeled data and neural network
architectures that contain many
layers..On the right side , you
can see the single perception of
deep learning.
17. D. OVERVIEW OF
DEEP LEARNING
Multi-Layer perceptron is the
simplest form of ANN. It consists
of a single input layer, one or
more hidden layer and finally an
output layer. A layer consists of
a collection of perceptron. Input
layer is basically one or more
features of the input data. Every
hidden layer consists of one or
more neurons and process
certain aspect of the feature and
send the processed information
into the next hidden layer. The
output layer process receives the
data from last hidden layer and
18. D. OVERVIEW OF DEEP LEARNING
General pipeline for implementing an ANN (Artifical NEURAL
NETWORKS):
We can simply define the procedure like that ;
• DECIDE WHICH DEEP LEARNING ARCITECHTURE YOU NEED (CNN ?
,RNN ? ) !!!
• Firstly we need labeled data ( as much as big data).And do not worry
about how your code understand which part of data is necessary for
your train., Keras handle it.
• After defining how we need input and output data shape.( datas
should be seperated as x_train, y_train , x_test , y_train).
• Train and test datas should be different each other.
• Design a architecture model to catch maximum accurucy.(Do not
forget our goal is reaching max accurucy without overfitting)
• Choose the correct optimizers ,actication function ,loss function.
19. E. DEEP LEARNING WITH KERAS
Implementing a neural network in Keras :
• 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
20. E. DEEP LEARNING WITH KERAS
• In Keras, every ANN is represented by Keras Models. In turn, every
Keras Model is composition of Keras Layers and represents ANN layers
like input, hidden layer, output layers, convolution layer, pooling layer,
etc., Keras model and layer access Keras modules for activation
function, loss function, regularization function, etc., Using Keras
model, Keras Layer, and Keras modules, any ANN algorithm (CNN, RNN,
etc.,) can be represented in a simple and efficient manner. The
following diagram depicts the relationship between model, layer and
core modules:
21. F. MODULE
Keras modules cosist of classes, functions and variables.These are pre-defined,The modules make simpler
to build model.
List of Modules :
• Initializers
• Constrains
• Activations
• Utilities
• Backend:
• Sequence processing
• Image processing
• Text processing
• Optimizers
• Callback
• Metrics
• Losses
22. G.LAYERS
• As I pointed out , Keras layers are the essentials for the Keras
models. Each layer takes input what previous layer provide as
output, and this circle keeps on the untill last layer.
• A layer needs to know shape of the input to get structue of the
input data.
• Layers also needs to know number of neurons , initializers,
regularizers, constraints and activation function.
23. G.LAYERS
Let us create a simple Keras layer using Sequential model API to get the
idea of how Keras model ;
EXAMPLE - 1 :
NOTE : Google Colaboratory is
used to execute.Google gives
us free Cpu and Gpu.
25. G.LAYERS
Activations
• The activation function makes input data non linear , and model
learn better.
• The output of a perceptron (neuron) is simply the result of the
activation function, which accepts the summation of all input
multiplied with its corresponding weight plus overall bias. (result
= Activation(SUMOF(input * weight) + bias)
• Activation Function in the keras module = linear , elu , selu , relu
, softmax , softplus , softsign , tanh , sigmoid , hard_sigmoid ,
exponential .
26. G.LAYERS
Dense Layer
• Dense layer does the below operation on the input and return the
output.
• output = activation(dot(input, kernel) + bias)
EXAMPLE 2- Lets find a result with using input and kernel :
Let us consider sample input and weights as below and try to find
the result:
• input as 2 x 2 matrix [ [1, 2], [3, 4] ]
• kernel as 2 x 2 matrix [ [0.5, 0.75], [0.25, 0.5] ]
• bias value as 0
28. G.LAYERS
Dropout Layers
Dropout layer is used generally in most of models, to prevent
overfitting.Dropout layer deletes a noise which node has . We can
simply define that when we train the model, yes we want to teach
to model essential things to predict correctly. But ıf models
memorize the labeled input data ,the model works on only what
model see before. The model should work on not even see before
test data. Memorizing and overfitting are similar things.
29. G.LAYERS
• Dropout has three arguments and they are as follows:
« keras.layers.Dropout(rate, noise_shape=None, seed=None) «
rate represent the fraction of the input unit to be dropped. It will
be from 0 to 1.
• noise_shape represent the dimension of the shape in which the
dropout to be applied. For example, the input shape is
(batch_size, timesteps, features). Then, to apply dropout in the
timesteps, (batch_size, 1, features) need to be specified as
noise_shape
• seed - random seed.
30. G.LAYERS
Flatten Layers
• Flatten layers also are used to get results together on the one
row or one column .Most of time have been on before last layer.
• For example, if flatten is applied to layer having input shape as
(batch_size, 2,2), then the output shape of the layer will be
(batch_size, 4) .
• « keras.layers.Flatten(data_format=None) «
32. H.MODEL COMPILATION
• Now, how to compile the model will be explained .
• The compilation is the final part of building a model.
Loss = Loss function is used to find error or deviation in the
learning process. Keras requires loss function during model
compilation process. Keras have pre-defined some loss
functions. We chooce one of these depends on our desire.
33. H.MODEL COMPILATION
Optimizer = Optimization is a vital , when training. This is for
optimizing the input weights by comparing the prediction and
loss function results.
For example ; Stochastic gradient descent optimizer , Adam
optimizer , Adamax optimizer ..
Metrics = Always we try to get better and better result. To do
this , we need to observe the performance of model.
Metrics is used to evaluate the performance , it is similar to loss
function, but not used in training process.
• For example ; accuracy , categorical_accuracy ..
34. H.MODEL COMPILATION
• Keras model provides a method, compile() to compile the
model.
• The argument and default value of the compile() method is as
follows:
« compile(optimizer, loss=None, metrics=None,
loss_weights=None, sample_weight_mode=None,
weighted_metrics=None, target_tensors=None) »
35. H.MODEL COMPILATION
Model Training
Models are trained by NumPy arrays using fit(). The main
purpose of this fit function is used to evaluate your model on
training. This can be also used for graphing model performance .
« model.fit(X, y, epochs=, batch_size=) »
• X, y - It is a tuple to evaluate your data.
• epochs - no of times the model is needed to be evaluated
during training.
• batch_size - training instances
38. H.MODEL COMPILATION
Example 5 : Let us choose a simple multi-layer perceptron (MLP) as represented
below and try to create the model using Keras ;
The core features of the model are as follows:
• Input layer consists of 784 values (28 x 28 = 784).
• First hidden layer, Dense consists of 512 neurons and ‘relu’ activation function.
• Second hidden layer, Dropout has 0.2 as its value.
• Third hidden layer, again Dense consists of 512 neurons and ‘relu’ activation
function.
• Fourth hidden layer, Dropout has 0.2 as its value.
• Fifth and final layer consists of 10 neurons and ‘softmax’ activation function.
• Use categorical_crossentropy as loss function.
• Use RMSprop() as Optimizer
• Use accuracy as metrics.
• Use 128 as batch size.
42. I. CONVOLUTIONAL NEURAL NETWORK
(CNN)
• The difference between CNN and ANN is that ANN is trained by
global image , but CNN learns partially for example; if there is
a image which is 10x10 , with cnn model learn 2 x2 , 2x2 ,
2x2.. Its similar to signal filtering.
• As you predict that there will be more details , and it causes
better accuracy.
• CNN is used most of time for images.
43. I. CONVOLUTIONAL NEURAL NETWORK
(CNN)
We will observe CNN with a example which is like ‘ hello world ‘ of
deep learning .
We will use mnist dataset that consists of 70,000 images
handwritten digits from 0 to 9 . Lets do the example to idetify them
using a CNN.
Example 6 : Mini-project idetify handwritten number
49. J. LSTM – RNN
• RNNs can use their internal state (memory) to process sequences of
inputs. This makes them applicable to tasks such as unsegmented,
connected handwriting recognition or speech recognition. In other
neural networks, all the inputs are independent of each other. But in
RNN, all the inputs are related to each other.
Advantages of Recurrent Neural Network
• RNN can model sequence of data so that each sample can be
assumed to be dependent on previous ones
• Recurrent neural network are even used with convolutional layers to
extend the effective pixel neighbourhood.
Disadvantages of Recurrent Neural Network
• Gradient vanishing and exploding problems.
• Training an RNN is a very difficult task.
• It cannot process very long sequences if using tanh or relu as an
activation function.
50. J. LSTM – RNN
• Long Short-Term Memory (LSTM) networks are a modified
version of recurrent neural networks, which makes it easier to
remember past data in memory.
• LSTM is well-suited to classify, process and predict time series
given time lags of unknown duration. It trains the model by
using back-propagation.
• Lets see this part with an example which is that while
predicting the actual price of a stock is an uphill climb, we can
build a model that will predict whether the price will go up or
down. The data and notebook used for this tutorial can be
found (https://github.com/mwitiderrick/stockprice). It’s
important to note that there are always other factors that affect
57. K.APPLICATIONS
As ı said before; more data , more accuracy. But if you have small
dataset for images . What will you do ?! There is a solution which
is Data Augmentation. That means that we play game with
images like ;symetry , rotation , zoom …
We need to just use ImageDataGenerator module. Lets see with
one example ...
Example -8 : We have 1 image and we want to much more
data.Here is our images that is by Ara Guler :
69. CONCLUSION
To sum up , the tutorial shows us that there are so many
parameters to adjuct on your model, this skill will be gained by
project experience . We can think like that we are in labaratory ,
we are doing experiment as building our deep learning model. I
gained a experience also about learning and teaching when
preparing the tutorial.In addition , I saw my weakness on some
points which I will fix these as soon as possible. Finally, after
working on this tutorial with much more detail, I will upload on my
github account.
70. REFERENCES
• Deep Learning with Python-François Chollet-Manning
Publications Company, 2017
• Keras.io
• https://www.kdnuggets.com/
• https://www.mathworks.com/
• medium.com