0% found this document useful (0 votes)
28 views22 pages

CI Keras

Uploaded by

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

CI Keras

Uploaded by

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

DEEP LEARNING

Introduction to Keras

Adrian Horzyk AGH University of


Science and Technology
[email protected] Krakow, Poland
What is Keras?

Keras developed by François Chollet:


• Is an official high-level and high-performing API of TensorFlow used to
specify and train different programs.
• Runs on top of TensorFlow, Theano, MXNet, or CNTK.
• Builds models by stacking layers and connecting graphs.
• Is actively developed by thousands of contributors across the world,
e.g. Microsoft, Google, Nvidia, AWS.
• Is used by hundred thousands of developers, e.g. NetFlix, Uber, Google,
Huawei, NVidia.
• Has good amount of documentation and easy to grasp all concepts.
• Supports GPU both of Nvidia and AMD and runs seamlessly on CPU and
GPU.
• Is multi-platform (Python, R) and multi-backend.
• Allows for fast prototyping and leaves freedom to design and
architecture
Keras Positive User Experience

Keras:
• Follows best practices for reducing cognitive load
• Offers consistent and simple APIs.
• Minimizes the number of user actions required for common use
cases.
• Provides a clear feedback upon user errors.
• More productive than many other frameworks.
• Integrates with lower-level Deep Learning languages like
TensorFlow or Theano.
• Implements everything which was built in base language, e.g.
TensorFlow.
• Produces models using GPU acceleration for various system like
Windows, Linux, Android, iOS, Raspberry Pi.
Working Principle

Keras is based on Computational Graphs like:

Where “a” and “b” are inputs used to compute “e” as


an output using intermediate variables “c” and “d”.
Computational Graphs allow to express complex
expressions as a combination of simple operations.
Keras Sequential Models
We can create various sequential models which linearly stack layers and can be used
for classification networks or autoencoders (consisting of encoders and decoders) like:
Keras Functional Models
Keras models can:
• Use multi-input, multi-output and arbitrary static graph topologies,
• Branch into two or more submodels,
• Share layers and/or weights.
Two Types of Execution of Keras Models

We can execute Keras model in two ways:


1. Deferred (symbolic)
• Using Python to build a computational graph, next
compiling and executing it.
• Symbolic tensors don’t have a value in the Python
code.
2. Eager (imperative)
• Here the Python runtime is the execution runtime,
which is similar to the execution with Numpy.
• Eager tensors have a value in the Python code.
• With the eager execution, value-dependent dynamic
topologies (tree-RNNs) can be constructed and used.
5 steps to implement a Neural Network

1. Prepare Input (e.g. text, audio, images, video) and specify


the input dimension (size).
2. Define the Model: its architecture, build the computational
graph, define sequential or functional style of the model and
the kind of the network (MLP, CNN, RNN etc.).
3. Specify the Optimizers (Stochastic Gradient Descent (SGD),
Root Mean Square (RMSprop), Adam etc.) to configure the
learning process.
4. Define the Loss Function (e.g. Mean Square Error (MSE),
Cross Entropy, Hinge) for checking the accuracy of the
achieved prediction to adapt and improve the model.
5. Train using training data, Test using testing/validation data,
and Evaluate the Model.
Installing TensorFlow and Keras
To start working with TensorFlow and Keras in Jupyter Notebook, you have to
install them using the following commands in the Anaconda Prompt window:
conda install pip # install pip in the virtual environment
pip install --upgrade tensorflow # for python 2.7
pip3 install --upgrade tensorflow # for python 3.*
It is recommended to install tensorflow with parameter –gpu to use GPU unit
and make computations faster:
pip install tensorflow-gpu
$ pip install Keras
If successfully installed check in Jupyter Notebook the version of the
TensorFlow using:
Implementing a CNN using Keras
We will try to create and train a simple Convolutional Neural Network (CNN) to
tackle with handwritten digit classification problem using MNIST dataset:

Each image in the MNIST dataset is 28x28 pixels and contains a centred,
grayscale digit form 0 to 9. Our goal is to classify these images to one of the ten
classes using ten output neurons of the CNN network.
Simple CNN for MNIST classification
Simple CNN for MNIST classification
Results of CNN MNIST classification
Let’s start with powerful computations!
✓ Questions?
✓ Remarks?
✓ Suggestions?
✓ Wishes?
Bibliography and Literature
1. https://www.youtube.com/watch?v=XNKeayZW4dY
2. https://victorzhou.com/blog/keras-cnn-tutorial/
3. https://github.com/keras-team/keras/tree/master/examples
4. https://medium.com/@margaretmz/anaconda-jupyter-notebook- Adrian Horzyk
tensorflow-and-keras-b91f381405f8 [email protected]
5. https://blog.tensorflow.org/2019/09/tensorflow-20-is-now- Google: Horzyk
available.html
6. http://coursera.org/specializations/tensorflow-in-practice
7. https://udacity.com/course/intro-to-tensorflow-for-deep-learning

University of Science
and Technology
in Krakow, Poland
XXX

xxx
Summary

xxx
Use-Case with Keras

Let’s try to predict the price of a bottle of wine just from its
description and variety using wide and deep networks using Keras.
We will use Wine dataset from kaggle.com:

• Starting with sequential model is easier, simply stacking the layers.


• Defining functional model allows for more flexibility and is best
suited for models with multiple inputs or combined models.
• Wide models have sparse feature vectors with mostly zero values.
• Multi-layer deep networks are necessary for tasks working on
images, speech recognition or other more complex training data.
Wine Dataset Description

This dataset has 12 attributes (offering a great opportunity for


sentimental analysis and various text-related predictive models):
• Country (of origin)
• Description (a few sentences describing the wine test and smell)
• Designation (the year and the grapes it has been made from)
• Points (from 1 to 10)
• Price (for the bottle of wine)
• Region_1 (country where the grapes were grown up)
• Region_2 (more specific region that can be black)
• Taster Name (who tasted and graded the wine)
• Taster Twitter Handle
• Title (of the wine)
• Variety
• Winery
Sample Wine Data

Input sample:

Output sample: We’ll use:


Google Colaboratory
Google Colaboratory is a free Jupyter notebook environment that requires no
setup and runs entirely in the cloud.
With Colaboratory you can write and execute code, save and share your analyses,
and access powerful computing resources, all for free from your browser.
Prerequisites

You can try to follow the use-case for the Wine dataset
prediction.

You might also like