28,14 €
Introduction to deep learning and PyTorch by building a convolutional neural network and recurrent neural network for real-world use cases such as image classification, transfer learning, and natural language processing.
Key Features
Book Description
PyTorch is extremely powerful and yet easy to learn. It provides advanced features, such as supporting multiprocessor, distributed, and parallel computation. This book is an excellent entry point for those wanting to explore deep learning with PyTorch to harness its power.
This book will introduce you to the PyTorch deep learning library and teach you how to train deep learning models without any hassle. We will set up the deep learning environment using PyTorch, and then train and deploy different types of deep learning models, such as CNN, RNN, and autoencoders.
You will learn how to optimize models by tuning hyperparameters and how to use PyTorch in multiprocessor and distributed environments. We will discuss long short-term memory network (LSTMs) and build a language model to predict text.
By the end of this book, you will be familiar with PyTorch's capabilities and be able to utilize the library to train your neural networks with relative ease.
What you will learn
Who this book is for
Developers and Data Scientist familiar with Machine Learning but new to deep learning, or existing practitioners of deep learning who would like to use PyTorch to train their deep learning models will find this book to be useful. Having knowledge of Python programming will be an added advantage, while previous exposure to PyTorch is not needed.
Das E-Book können Sie in Legimi-Apps oder einer beliebigen App lesen, die das folgende Format unterstützen:
Seitenzahl: 176
Veröffentlichungsjahr: 2018
Copyright © 2018 Packt Publishing
All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews.
Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the author(s), nor Packt Publishing or its dealers and distributors, will be held liable for any damages caused or alleged to have been caused directly or indirectly by this book.
Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information.
Commissioning Editor: Amey VarangaonkarAcquisition Editor: Noyonika DasContent Development Editor:Kirk DsouzaTechnical Editor: Sushmeeta JenaCopy Editor: Safis EditingProject Coordinator:Hardik BhindeProofreader: Safis EditingIndexer: Mariammal ChettiyarGraphics: Alishon MendonsaProduction Coordinator: Nilesh Mohite
First published: December 2018
Production reference: 1201218
Published by Packt Publishing Ltd. Livery Place 35 Livery Street Birmingham B3 2PB, UK.
ISBN 978-1-78953-409-2
www.packtpub.com
Mapt is an online digital library that gives you full access to over 5,000 books and videos, as well as industry leading tools to help you plan your personal development and advance your career. For more information, please visit our website.
Spend less time learning and more time coding with practical eBooks and Videos from over 4,000 industry professionals
Improve your learning with Skill Plans built especially for you
Get a free eBook or video every month
Mapt is fully searchable
Copy and paste, print, and bookmark content
Did you know that Packt offers eBook versions of every book published, with PDF and ePub files available? You can upgrade to the eBook version at www.packt.com and as a print book customer, you are entitled to a discount on the eBook copy. Get in touch with us at [email protected] for more details.
At www.packt.com, you can also read a collection of free technical articles, sign up for a range of free newsletters, and receive exclusive discounts and offers on Packt books and eBooks.
David Julian is a freelance technology consultant and educator. He has worked as a consultant for government, private, and community organizations on a variety of projects, including using machine learning to detect insect outbreaks in controlled agricultural environments (Urban Ecological Systems Ltd., Bluesmart Farms), designing and implementing event management data systems (Sustainable Industry Expo, Lismore City Council), and designing multimedia interactive installations (Adelaide University). He has also written Designing Machine Learning Systems With Python for Packt Publishing and was technical reviewer for Python Machine Learning and Hands-On Data Structures and Algorithms with Python - Second Edition, published by Packt.
AshishSingh Bhatia has more than 10 years' IT experience in different domains, including ERP, banking, education, and resource management. He is a learner, reader, and developer at heart. He is passionate about Python, Java, and R. He loves to explore new technologies. He has also published two books: Machine Learning with Java and R and Natural Language Processing with Java. Apart from this, he has also recorded a video tutorial on PyTorch.
If you're interested in becoming an author for Packt, please visit authors.packtpub.com and apply today. We have worked with thousands of developers and tech professionals, just like you, to help them share their insight with the global tech community. You can make a general application, apply for a specific hot topic that we are recruiting an author for, or submit your own idea.
Title Page
Copyright and Credits
Deep Learning with PyTorch Quick Start Guide
About Packt
Why subscribe?
Packt.com
Contributors
About the author
About the reviewer
Packt is searching for authors like you
Preface
Who this book is for
What this book covers
To get the most out of this book
Download the example code files
Download the color images
Conventions used
Get in touch
Reviews
Introduction to PyTorch
What is PyTorch?
Installing PyTorch
Digital Ocean
Tunneling in to IPython
Amazon Web Services (AWS)
Basic PyTorch operations
Default value initialization
Converting between tensors and NumPy arrays
Slicing and indexing and reshaping
In place operations
Loading data
PyTorch dataset loaders
Displaying an image
DataLoader
Creating a custom dataset
Transforms
ImageFolder
Concatenating datasets
Summary
Deep Learning Fundamentals
 Approaches to machine learning
Learning tasks
Unsupervised learning
Clustering
Principle component analysis
Reinforcement learning
Supervised learning
Classification
Evaluating classifiers
Features
Handling text and categories
Models
Linear algebra review
Linear models
Gradient descent
Multiple features
The normal equation
Logistic regression
Nonlinear models
Artificial neural networks
The perceptron
Summary
Computational Graphs and Linear Models
autograd
Computational graphs
Linear models
Linear regression in PyTorch
Saving models
Logistic regression
Activation functions in PyTorch
Multi-class classification example
Summary
Convolutional Networks
Hyper-parameters and multilayered networks
Benchmarking models
Convolutional networks
A single convolutional layer
Multiple kernels
Multiple convolutional layers
Pooling layers
Building a single-layer CNN
Building a multiple-layer CNN
Batch normalization
Summary
Other NN Architectures
Introduction to recurrent networks
Recurrent artificial neurons 
Implementing a recurrent network
Long short-term memory networks
Implementing an LSTM
Building a language model with a gated recurrent unit
Summary
Getting the Most out of PyTorch
Multiprocessor and distributed environments
Using a GPU
Distributed environments
torch.distributed
torch.multiprocessing
Optimization techniques
Optimizer algorithms
Learning rate scheduler
Parameter groups
Pretrained models
Implementing a pretrained model
Summary
Other Books You May Enjoy
Leave a review - let other readers know what you think
PyTorch is surprisingly easy to learn and provides advanced features such as a supporting multiprocessor, as well as distributed and parallel computation. PyTorch has a library of pre-trained models, providing out-of-the-box solutions for image classification. PyTorch offers one of the most accessible entry points into cutting-edge deep learning. It is tightly integrated with the Python programming language, so for Python programmers, coding it seems natural and intuitive. The unique, dynamic way of treating computational graphs means that PyTorch is both efficient and flexible.
This book is for anyone who wants a straightforward, practical introduction to deep learning using PyTorch. The aim is to give you an understanding of deep learning models by direct experimentation. This book is perfect for those who are familiar with Python, know some machine learning basics, and are looking for a way to productively develop their skills. The book will focus on the most important features and give practical examples. It assumes you have a working knowledge of Python and are familiar with the relevant mathematical ideas, including with linear algebra and differential calculus. The book provides enough theory to get you up and running without requiring rigorous mathematical understanding. By the end of the book, you will have a practical knowledge of deep learning systems and able to apply PyTorch models to solve the problems that you care about.
Chapter 1, Introduction to PyTorch, gets you up and running with PyTorch, demonstrates its installation on a variety of platforms, and explores key syntax elements and how to import and use data in PyTorch.
Chapter 2, Deep Learning Fundamentals, is a whirlwind tour of the basics of deep learning, covering the mathematics and theory of optimization, linear networks, and neural networks.
Chapter 3, Computational Graphs and Linear Models, demonstrates how to calculate the error gradient of a linear network and how to harness it to classify images.
Chapter 4, Convolutional Networks, examines the theory of convolutional networks and how to use them for image classification.
Chapter 5, Other NN Architectures, discusses the theory behind recurrent networks and shows how to use them to make predictions about sequence data. It also discusses long short-term memorynetworks (LSTMs) and has you build a language model to predict text.
Chapter 6, Getting the Most out of PyTorch, examines some advanced features, such as using PyTorch in multiprocessor and parallel environments. You will build a flexible solution for image classification using out-of-the-box pre-trained models.
This book does not assume any specialist knowledge, only solid general computer skills. Python is a relatively easy (and incredibly useful!) language to learn, so don't worry if you have limited or no programming background.
The book does contain some relatively simple mathematics, and some theory, that some readers may find difficult at first. Deep learning models are complex systems and understanding the behavior of even simple neural networks is a non-trivial exercise. Fortunately, PyTorch acts as a high-level framework around these complicated systems, so it is possible to achieve very good results without an expert understanding of the theoretical foundations.
Installing the software is easy, and essentially only two packages are required: the Anaconda distribution of Python, and PyTorch itself. The software runs on Windows 7and 10 , macOS 10.10 or above, and most versions of Linux. It can be run on a desktop machine or in a server environment. All the code in this book was tested using PyTorch version 1.0 and Python 3, running on Ubuntu 16.
You can download the example code files for this book from your account at www.packt.com. If you purchased this book elsewhere, you can visit www.packt.com/support and register to have the files emailed directly to you.
You can download the code files by following these steps:
Log in or register at
www.packt.com
.
Select the
SUPPORT
tab.
Click on
Code Downloads & Errata
.
Enter the name of the book in the
Search
box and follow the onscreen instructions.
Once the file is downloaded, please make sure that you unzip or extract the folder using the latest version of:
WinRAR/7-Zip for Windows
Zipeg/iZip/UnRarX for Mac
7-Zip/PeaZip for Linux
The code bundle for the book is also hosted on GitHub at https://github.com/PacktPublishing/Deep-Learning-with-PyTorch-Quick-Start-Guide. In case there's an update to the code, it will be updated on the existing GitHub repository.
We also have other code bundles from our rich catalog of books and videos available at https://github.com/PacktPublishing/. Check them out!
We also provide a PDF file that has color images of the screenshots/diagrams used in this book. You can download it here: https://www.packtpub.com/sites/default/files/downloads/9781789534092_ColorImages.pdf.
Feedback from our readers is always welcome.
General feedback: If you have questions about any aspect of this book, mention the book title in the subject of your message and email us at [email protected].
Errata: Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you have found a mistake in this book, we would be grateful if you would report this to us. Please visit www.packt.com/submit-errata, selecting your book, clicking on the Errata Submission Form link, and entering the details.
Piracy: If you come across any illegal copies of our works in any form on the Internet, we would be grateful if you would provide us with the location address or website name. Please contact us at [email protected] with a link to the material.
If you are interested in becoming an author: If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, please visit authors.packtpub.com.
Please leave a review. Once you have read and used this book, why not leave a review on the site that you purchased it from? Potential readers can then see and use your unbiased opinion to make purchase decisions, we at Packt can understand what you think about our products, and our authors can see your feedback on their book. Thank you!
For more information about Packt, please visit packt.com.
This is a step-by-step introduction to deep learning using the PyTorch framework. PyTorch is a great entry point into deep learning and if you have some knowledge of Python then you will find PyTorch an intuitive, productive, and enlightening experience. The ability to rapidly prototype experiments and test ideas is a core strength of PyTorch. Together with the possibility of being able to turn experiments into productive, deployable resources, the learning curve challenge is abundantly rewarded.
PyTorch is a relatively easy and fun way to understand deep learning concepts. You may be surprised at how few lines of code it takes to solve common problems of classification, such as hand-writing recognition and image classification. Having said that PyTorch is easy cannot override the fact that deep learning is, in many ways, hard. It involves some complicated math and some intractable logical conundrums. This should not, however, distract from the fun and useful part of this enterprise. There is no doubt machine learning can provide deep insights and solve important problems in the world around us but to get there can take some work.
This book is an attempt, not to gloss over important ideas, but to explain them in a way that is jargon free and succinct. If the idea of solving complicated differential equations makes you break out in a cold sweat, you are not alone. This might be related to some high school trauma of a bad-tempered math teacher furiously demanding you cite Euler's formula or the trigonometric identities. This is a problem because math itself should be fun, and insight arises not from the laborious memorizing of formulas but through understanding relationships and foundational concepts.
Another thing that can make deep learning appear difficult is that it has a diverse and dynamic frontier of research. This may be confusing for the novice because it does not present an obvious entry point. If you understand some principles and want to test your ideas, it can be a bewildering task to find a suitable set of tools. The combinations of development language, framework, deployment architecture, and so on, present a non-trivial decision process.
The science of machine learning has matured to the point that a set of general purpose algorithms for solving problems such has classification and regression have emerged. Subsequently, several frameworks have been created to harness the power of these algorithms and use them for general problem solving. This means that the entry point is at such a level that these technologies are now in the hands of the non-computer science professional. Experts in a diverse array of domains can now use these ideas to advance their endeavors. By the end of this book, and with a little dedication, you will be able to build and deploy useful deep learning models to help solve the problems you are interested in.
In this chapter, we will discuss the following topics:
What is PyTorch?
Installing PyTorch
Basic operations
Loading data
PyTorch is a dynamic tensor-based, deep learning framework for experimentation, research, and production. It can be used as a GPU-enabled replacement for NumPy or a flexible, efficient platform for building neural networks. The dynamic graph creation and tight Python integration makes PyTorch a standout in deep learning frameworks.
If you are at all familiar with the deep learning ecosystem, then frameworks such as Theano and TensorFlow, or higher-level derivatives such as Keras, are amongst the most popular. PyTorch is a relative newcomer to the deep learning framework set. Despite this, it is now being used extensively by Google, Twitter, and Facebook. It stands out from other frameworks in that both Theano and TensorFlow encode computational graphs in static structures that need to be run in self-contained sessions. In contrast, PyTorch can dynamically implement computational graphs. The consequence for a neural net is that the network can change behavior as it is being run, with little or no overhead. In TensorFlow and Theano, to change behavior, you effectively have to rebuild the network from scratch.
This dynamic implementation comes about through a process called tape-based auto-diif, allowing PyTorch expressions to be automatically differentiated. This has numerous advantages. Gradients can be calculated on the fly and since the computational graph is dynamic, it can be changed at each function call, allowing it to be used in interesting ways in loops and under conditional calls that can respond, for example, to input parameters or intermediate results. This dynamic behavior and great flexibility has made PyTorch a favored experimental platform for deep learning.
Another advantage of PyTorch is that it is closely integrated with the Python language. For Python coders, it is very intuitive and it interoperates seamlessly with other Python packages, such as NumPy and SciPy. PyTorch is very easy to experiment with. It makes an ideal tool for not only building and running useful models, but also as a way to understand deep learning principles by direct experimentation.
As you would expect, PyTorch can be run on multiple graphical processing units (GPUs). Deep learning algorithms can be computationally expensive. This is especially true for big datasets. PyTorch has strong GPU support, with intelligent memory sharing of tensors between processes. This basically means there is an efficient and user-friendly way to distribute the processing load across the CPU and GPUs. This can make a big difference to the time it takes to test and run large complex models.
Dynamic graph generation, tight Python language integration, and a relatively simple API makes PyTorch an excellent platform for research and experimentation. However, versions prior to PyTorch 1 had deficits that prevented it from excelling in production environments. This deficiency is being addressed in PyTorch 1.
Research is an important application for deep learning, but increasingly, deep learning is being embedded in applications that run live on the web, on a device, or in a robot. Such an application may service thousands of simultaneous queries and interact with massive, dynamic data. Although Python is one of the best languages for humans to work with, specific efficiencies and optimizations are available in other languages, most commonly C++ and Java. Even though the best way to build a particular deep learning model may be with PyTorch, this may not be the best way to deploy it. This is no longer a problem because now with PyTorch 1, we can export Python free representations of PyTorch models.
This has come about through a partnership between Facebook, the major stakeholder of PyTorch, and Microsoft, to create the Open Neural Network Exchange (ONNX) to assist developers in converting neural net models between frameworks. This has led to the merging of PyTorch with the more production-ready framework, CAFFE2. In CAFFE2, models are represented by a plain text schema, making them language agnostic. This means they are more easily deployed to Android, iOS, or Rasberry Pi devices.
With this in mind, PyTorch version 1 has expanded its API included production-ready capabilities, such as optimizing code for Android and iPhone, a just in time (JIT
