Search icon CANCEL
Subscription
0
Cart icon
Cart
Close icon
You have no products in your basket yet
Save more on your purchases!
Savings automatically calculated. No voucher code required
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Python Deep Learning Cookbook

You're reading from  Python Deep Learning Cookbook

Product type Book
Published in Oct 2017
Publisher Packt
ISBN-13 9781787125193
Pages 330 pages
Edition 1st Edition
Languages
Author (1):
Indra den Bakker Indra den Bakker
Profile icon Indra den Bakker
Toc

Table of Contents (21) Chapters close

Title Page
Credits
About the Author
About the Reviewer
www.PacktPub.com
Customer Feedback
Preface
1. Programming Environments, GPU Computing, Cloud Solutions, and Deep Learning Frameworks 2. Feed-Forward Neural Networks 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

Defining networks using simple and efficient code with Gluon


The newest addition to the range of deep learning frameworks is Gluon. Gluon is recently launched by AWS and Microsoft to provide an API simple, easy-to-understand code without the loss of performance. Gluon is already included in the latest release of MXNet and will be available in future releases of CNTK (and other frameworks). Just like Keras, Gluon is a wrapper around other deep learning frameworks. The main difference between Keras and Gluon, is that Gluon will (at first) focus on imperative frameworks. 

How to do it...

  1. At the moment, gluon is included in the latest release of MXNet (follow the steps in Building efficient models with MXNet to install MXNet). 
  2. After installing, we can directly import gluon as follows:
from mxnet import gluon
  1. Next, we create some dummy data. For this we need the data to be in MXNet's NDArray or Symbol:
import mxnet as mx
import numpy as np
x_input = mx.nd.empty((1, 5), mx.gpu())
x_input[:] = np.array([[1,2,3,4,5]], np.float32)

y_input = mx.nd.empty((1, 5), mx.gpu())
y_input[:] = np.array([[10, 15, 20, 22.5, 25]], np.float32)
  1. With Gluon, it's really straightforward to build a neural network by stacking layers:
net = gluon.nn.Sequential()
with net.name_scope():
    net.add(gluon.nn.Dense(16, activation="relu"))
    net.add(gluon.nn.Dense(len(y_input)))
  1. Next, we initialize the parameters and we store these on our GPU as follows:
net.collect_params().initialize(mx.init.Normal(), ctx=mx.gpu())
  1. With the following code we set the loss function and the optimizer:
softmax_cross_entropy = gluon.loss.SoftmaxCrossEntropyLoss()
trainer = gluon.Trainer(net.collect_params(), 'adam', {'learning_rate': .1})
  1. We're ready to start training or model:
n_epochs = 10

for e in range(n_epochs):
    for i in range(len(x_input)):
        input = x_input[i]
        target = y_input[i]
        with mx.autograd.record():
            output = net(input)
            loss = softmax_cross_entropy(output, target)
            loss.backward()
        trainer.step(input.shape[0])

Note

We've shortly demonstrated how to implement a neural network architecture with Gluon. Gluon is a powerful extension that can be used to implement deep learning architectures with clean code. At the same time, there is almost no performance loss when using Gluon.

 

You have been reading a chapter from
Python Deep Learning Cookbook
Published in: Oct 2017 Publisher: Packt ISBN-13: 9781787125193
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 $15.99/month. Cancel anytime