Search icon CANCEL
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Python Deep Learning Cookbook

You're reading from   Python Deep Learning Cookbook Over 75 practical recipes on neural network modeling, reinforcement learning, and transfer learning using Python

Arrow left icon
Product type Paperback
Published in Oct 2017
Publisher Packt
ISBN-13 9781787125193
Length 330 pages
Edition 1st Edition
Languages
Tools
Arrow right icon
Author (1):
Arrow left icon
Indra den Bakker Indra den Bakker
Author Profile Icon Indra den Bakker
Indra den Bakker
Arrow right icon
View More author details
Toc

Table of Contents (15) Chapters Close

Preface 1. Programming Environments, GPU Computing, Cloud Solutions, and Deep Learning Frameworks 2. Feed-Forward Neural Networks FREE CHAPTER 3. Convolutional Neural Networks 4. Recurrent Neural Networks 5. Reinforcement Learning 6. Generative Adversarial Networks 7. Computer Vision 8. Natural Language Processing 9. Speech Recognition and Video Analysis 10. Time Series and Structured Data 11. Game Playing Agents and Robotics 12. Hyperparameter Selection, Tuning, and Neural Network Learning 13. Network Internals 14. Pretrained Models

Intuitively building networks with Keras 

Keras is a deep learning framework that is widely known and adopted by deep learning engineers. It provides a wrapper around the TensorFlow, CNTK, and the Theano frameworks. This wrapper you gives the ability to easily create deep learning models by stacking different types of layers. The power of Keras lies in its simplicity and readability of the code. If you want to use multiple GPUs during training, you need to set the devices in the same way as with TensorFlow.

How to do it...

  1. We start by installing Keras on our local Anaconda environment as follows:
conda install -c conda-forge keras 

Make sure your deep learning environment is activated before executing this command.

  1. Next, we import keras library into our Python environment:
from keras.models import Sequential
from keras.layers import Dense

This command outputs the backend used by Keras. By default, the TensorFlow framework is used:

Figure 1.3: Keras prints the backend used
  1. To provide a dummy dataset, we will use numpy and the following code:
import numpy as np
x_input = np.array([[1,2,3,4,5]])
y_input = np.array([[10]])
  1. When using sequential mode, it's straightforward to stack multiple layers in Keras. In this example, we use one hidden layer with 32 units and an output layer with one unit:
model = Sequential()
model.add(Dense(units=32, input_dim=x_input.shape[1]))
model.add(Dense(units=1))
  1. Next, we need to compile our model. While compiling, we can set different settings such as loss function, optimizer, and metrics:
model.compile(loss='mse',
optimizer='sgd',
metrics=['accuracy'])
  1. In Keras, you can easily print a summary of your model. It will also show the number of parameters within the defined model:
model.summary()

In the following figure, you can see the model summary of our build model:

Figure 1.4: Example of a Keras model summary
  1. Training the model is straightforward with one command, while simultaneously saving the results to a variable called history:
history = model.fit(x_input, y_input, epochs=10, batch_size=32)
  1. For testing, the prediction function can be used after training:
pred = model.predict(x_input, batch_size=128)
In this short introduction to Keras, we have demonstrated how easy it is to implement a neural network in just a couple of lines of code. However, don't confuse simplicity with power. The Keras framework provides much more than we've just demonstrated here and one can adjust their model up to a granular level if needed.
lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at €18.99/month. Cancel anytime