Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletter Hub
Free Learning
Arrow right icon
timer SALE ENDS IN
0 Days
:
00 Hours
:
00 Minutes
:
00 Seconds
PyTorch Deep Learning Hands-On
PyTorch Deep Learning Hands-On

PyTorch Deep Learning Hands-On: Build CNNs, RNNs, GANs, reinforcement learning, and more, quickly and easily

Arrow left icon
Profile Icon Sherin Thomas Profile Icon Sudhanshu Passi
Arrow right icon
€18.99 per month
Full star icon Full star icon Half star icon Empty star icon Empty star icon 2.9 (10 Ratings)
Paperback Apr 2019 250 pages 1st Edition
eBook
€17.99 €26.99
Paperback
€32.99
Subscription
Free Trial
Renews at €18.99p/m
Arrow left icon
Profile Icon Sherin Thomas Profile Icon Sudhanshu Passi
Arrow right icon
€18.99 per month
Full star icon Full star icon Half star icon Empty star icon Empty star icon 2.9 (10 Ratings)
Paperback Apr 2019 250 pages 1st Edition
eBook
€17.99 €26.99
Paperback
€32.99
Subscription
Free Trial
Renews at €18.99p/m
eBook
€17.99 €26.99
Paperback
€32.99
Subscription
Free Trial
Renews at €18.99p/m

What do you get with a Packt Subscription?

Free for first 7 days. $19.99 p/m after that. Cancel any time!
Product feature icon Unlimited ad-free access to the largest independent learning library in tech. Access this title and thousands more!
Product feature icon 50+ new titles added per month, including many first-to-market concepts and exclusive early access to books as they are being written.
Product feature icon Innovative learning tools, including AI book assistants, code context explainers, and text-to-speech.
Product feature icon Thousands of reference materials covering every tech concept you need to stay up to date.
Subscribe now
View plans & pricing
Table of content icon View table of contents Preview book icon Preview Book

PyTorch Deep Learning Hands-On

Chapter 2. A Simple Neural Network

Learning the PyTorch way of building a neural network is really important. It is the most efficient and clean way of writing PyTorch code, and it also helps you to find tutorials and sample snippets easy to follow, since they have the same structure. More importantly, you'll end up with the efficient form of your code, which is also highly readable.

Don't worry, PyTorch is not trying to add another spike into your learning curve by implementing a brand-new methodology. If you know how to code in Python, you'll feel at home right away. However, we won't learn those building blocks as we did in the first chapter; in this chapter, we will build a simple network. Instead of choosing a typical entry-level neural network use case, we'll be teaching our network to do mathematics in the NumPy way. Then we'll convert that to a PyTorch network. By the end of this chapter, you will have the skills to become...

Introduction to the neural network

In this section, we'll go through the problem statement at hand and the dataset we are using. Then, we'll go and build a basic neural network, before building it up to a proper PyTorch network.

The problem

Have you ever played the game Fizz buzz? Don't worry if you haven't. The following is a simple explanation of what the game is about.

Note

As per Wikipedia, Fizz buzz [1] is a group word game for children that teaches them about division. Players take turns to count incrementally. Any number divisible [2] by three is replaced by the word fizz and any number divisible by five is replaced by the word buzz. Numbers divisible by both become fizz buzz.

Fizz buzz has been used in a fun example by Joel Grus, one of the research engineers at the Allen Institute of Artificial Intelligence (AI2), while writing a blog post [3] on TensorFlow. Although this particular example doesn't solve any practical problems, the blog post got quite a lot of traction and it is fun to see how a neural network learns to find a mathematical pattern from a number stream.

Dataset

Building a data pipeline is as important as the architecture of your network, especially when you train your network in real time. The data that you get from the wild is never going to be clean, and you'll have to process it before throwing it at your network. For example, if we were to collect data for predicting whether a person buys a product or not, we would end up having outliers. Outliers could be of any kind and unpredictable. Somebody could have made an order accidently, for example, or they could have given access to their friends who then made the order, and so on.

Theoretically, deep neural networks are ideal for finding patterns and solutions from your dataset because they are supposed to mimic the human brain. However, in practice, this is often not quite the case. Your network will be able to solve problems easily by finding the pattern if your data is clean and properly formatted. PyTorch gives data preprocessing wrappers out of the box...

Novice model

Now we are going to build a novice, NumPy-like model, not using any PyTorch-specific approach. Then, in the next session, we'll convert the same model to PyTorch's method. If you come from a NumPy background, you'll feel at home, but if you are an advanced deep learning practitioner who has used other frameworks, please take the liberty of skipping this session.

Autograd

So, now that we know which type our tensors should be, we can create PyTorch tensors from the NumPy array we got from get_numpy_data().

x = torch.from_numpy(trX).to(device=device, dtype=dtype)
y = torch.from_numpy(trY).to(device=device, dtype=dtype)
w1 = torch.randn(input_size, hidden_size, requires_grad=True, device=device, dtype=dtype)
w2 = torch.randn(hidden_size, output_size, requires_grad=True, device=device, dtype=dtype)
b1 = torch.zeros(1, hidden_size, requires_grad=True, device=device, dtype=dtype)
b2 = torch.zeros(1, output_size, requires_grad=True, device=device, dtype=dtype...

The PyTorch way

So far, we have developed a simple two-layer neural network in a hybrid NumPy-PyTorch style. We have coded each operation line by line, like how we do it in NumPy, and we have adopted automatic differentiation from PyTorch so that we don't have to code the backward pass.

On the way, we have learned how to wrap matrices (or tensors) in PyTorch, and that helps us with backpropagation. The PyTorch way of doing the same thing is a bit more convenient and that is what we are going to discuss in this section. PyTorch gives access to almost all the functionality required for a deep learning project inbuilt. Since PyTorch supports all the mathematical functions available in Python, it's not a tough task to build one function if it's not available in the core. You can not only build any functionality you need, but PyTorch defines the derivative function of the functionality you build implicitly.

PyTorch is helpful for people who need to know the low-level...

Summary

In this chapter, we have learned how to build a simple neural network in the most basic way and convert that to a PyTorch's way. The basic building block of deep learning starts here. Once we know how and why the methodology we follow exists, we'll be able to take the big steps. Any deep learning model, regardless of the size, usage, or algorithm, can be built with the concepts we have learned in this chapter. Because of that, understanding this chapter thoroughly is critical for going through future chapters. In the next chapter, we will dive into the deep learning workflow.

References

  1. Fizz buzz Wikipedia page, https://en.wikipedia.org/wiki/Fizz_buzz
  2. Division (mathematics) Wikipedia page, https://en.wikipedia.org/wiki/Division_(mathematics)
  3. Joel Grus, Fizz Buzz in Tensorflow, http://joelgrus.com/2016/05/23/fizz-buzz-in-tensorflow/
  4. Ian Goodfellow, Yoshua Bengio and Aaron Courville, Deep Learning Book, http://www.deeplearningbook.org/
Left arrow icon Right arrow icon

Key benefits

  • Internals and principles of PyTorch
  • Implement key deep learning methods in PyTorch: CNNs, GANs, RNNs, reinforcement learning, and more
  • Build deep learning workflows and take deep learning models from prototyping to production

Description

PyTorch Deep Learning Hands-On is a book for engineers who want a fast-paced guide to doing deep learning work with PyTorch. It is not an academic textbook and does not try to teach deep learning principles. The book will help you most if you want to get your hands dirty and put PyTorch to work quickly. PyTorch Deep Learning Hands-On shows how to implement the major deep learning architectures in PyTorch. It covers neural networks, computer vision, CNNs, natural language processing (RNN), GANs, and reinforcement learning. You will also build deep learning workflows with the PyTorch framework, migrate models built in Python to highly efficient TorchScript, and deploy to production using the most sophisticated available tools. Each chapter focuses on a different area of deep learning. Chapters start with a refresher on how the model works, before sharing the code you need to implement it in PyTorch. This book is ideal if you want to rapidly add PyTorch to your deep learning toolset.

Who is this book for?

Machine learning engineers who want to put PyTorch to work.

What you will learn

  • Use PyTorch to build:
  • Simple Neural Networks – build neural networks the PyTorch way, with high-level functions, optimizers, and more
  • Convolutional Neural Networks – create advanced computer vision systems
  • Recurrent Neural Networks – work with sequential data such as natural language and audio
  • Generative Adversarial Networks – create new content with models including SimpleGAN and CycleGAN
  • Reinforcement Learning – develop systems that can solve complex problems such as driving or game playing
  • Deep Learning workflows – move effectively from ideation to production with proper deep learning workflow using PyTorch and its utility packages
  • Production-ready models – package your models for high-performance production environments

Product Details

Country selected
Publication date, Length, Edition, Language, ISBN-13
Publication date : Apr 30, 2019
Length: 250 pages
Edition : 1st
Language : English
ISBN-13 : 9781788834131
Category :
Languages :
Concepts :
Tools :

What do you get with a Packt Subscription?

Free for first 7 days. $19.99 p/m after that. Cancel any time!
Product feature icon Unlimited ad-free access to the largest independent learning library in tech. Access this title and thousands more!
Product feature icon 50+ new titles added per month, including many first-to-market concepts and exclusive early access to books as they are being written.
Product feature icon Innovative learning tools, including AI book assistants, code context explainers, and text-to-speech.
Product feature icon Thousands of reference materials covering every tech concept you need to stay up to date.
Subscribe now
View plans & pricing

Product Details

Publication date : Apr 30, 2019
Length: 250 pages
Edition : 1st
Language : English
ISBN-13 : 9781788834131
Category :
Languages :
Concepts :
Tools :

Packt Subscriptions

See our plans and pricing
Modal Close icon
€18.99 billed monthly
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Simple pricing, no contract
€189.99 billed annually
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just €5 each
Feature tick icon Exclusive print discounts
€264.99 billed in 18 months
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just €5 each
Feature tick icon Exclusive print discounts

Frequently bought together


Stars icon
Total 102.97
PyTorch Deep Learning Hands-On
€32.99
Deep Learning with PyTorch
€32.99
Deep Reinforcement Learning Hands-On
€36.99
Total 102.97 Stars icon

Table of Contents

10 Chapters
1. Deep Learning Walkthrough and PyTorch Introduction Chevron down icon Chevron up icon
2. A Simple Neural Network Chevron down icon Chevron up icon
3. Deep Learning Workflow Chevron down icon Chevron up icon
4. Computer Vision Chevron down icon Chevron up icon
5. Sequential Data Processing Chevron down icon Chevron up icon
6. Generative Networks Chevron down icon Chevron up icon
7. Reinforcement Learning Chevron down icon Chevron up icon
Chapter 8. PyTorch to Production Chevron down icon Chevron up icon
Another Book You May Enjoy Chevron down icon Chevron up icon
Index Chevron down icon Chevron up icon

Customer reviews

Top Reviews
Rating distribution
Full star icon Full star icon Half star icon Empty star icon Empty star icon 2.9
(10 Ratings)
5 star 40%
4 star 0%
3 star 10%
2 star 10%
1 star 40%
Filter icon Filter
Top Reviews

Filter reviews by




JB.Malone Jun 18, 2019
Full star icon Full star icon Full star icon Full star icon Full star icon 5
This is a good book if you want a quick dive into PyTorch and build some basic ML projects. It covers a lot of topics fast so don't expect a deep book or lots of theory, what you get is some really useful PyTorch coding templates for CNNs, RNNs, GANs, so you can create these with PyTorch. I'm now getting up and running in PyTorch, and PyTorch rocks. I recommend PyTorch if you know ML but haven't tried it yet.
Amazon Verified review Amazon
Stephan Miller May 08, 2019
Full star icon Full star icon Full star icon Full star icon Full star icon 5
This is one of the books I wish I had when I got started in machine learning. Of course, I wish the current version of PyTorch was around then too. It will definitely get you started correctly if you're a beginner, will be a great refresher if you are an expert and will widen your knowledge of machine learning techniques if your knowledge only includes a few of the modern methods of extracting answers from data.This book will walk you through setting up your development environment right before you jump right into building a simple neural network. But neural networks is not all this book covers. You will learn how to create a convolutional neural network, the secret behind computer vision. You will also dive into recurrent neural networks and use long short-term memory and gated recurrent units. You will also study generative networks and learn about autoregressive models. Then you will use OpenAI's Gym library to explore Markov decision processes, the Bellman equation and deep Q-learning. Each of these technologies are taught with hands-on step-by-step tutorials.But that is not all. Along the way you will learn how to set up a pipeline to make developing and deploying your machine learning system much simpler and hassle free. And you will learn how to deploy your deep learning system to production using Flask and RedisAI.I would recommend this book to anyone interested in learning AI and machine learning. It will get you started quick and provides a broad overview of features of PyTorch and how you can use it for your own projects.
Amazon Verified review Amazon
Akshit Shah May 06, 2019
Full star icon Full star icon Full star icon Full star icon Full star icon 5
I am a Deep Learning Practioner, I have been primarily working with TensorFlow, I was thinking to migrate to Pytorch and explore the possibilities of the framework, This book helped me a lot in easy migration. It has all the building blocks required to learn Pytorch. Its advancing difficulty makes readers stick to the book. I especially loved the explanation of GANs. It is probably by far most up to date information provided.Thank You, Authors and Packt publishing for the amazing book.Cheers!
Amazon Verified review Amazon
Jessica Apr 29, 2019
Full star icon Full star icon Full star icon Full star icon Full star icon 5
This book is very enjoyable to read. The content covers the several main streams of the current deep learning research: NLP, GAN, DRL, etc, and presents step-by-step tutorials on implementing the popular approaches within each field, e.g. CycleGAN, DQN, WaveNet, etc. As a deep learning researcher working primarily with pytorch, I find that this book can work as a good refresher for the fields that I already have experience with, and as a gentle introduction to areas that I am less familiar with while presenting the right amount of implementation details. A well-structured and well-presented guide to deep learning research with pytorch!
Amazon Verified review Amazon
Colin Hagemeyer Jun 04, 2019
Full star icon Full star icon Full star icon Empty star icon Empty star icon 3
On the positive, the book walks you step-by-step through the various functions of PyTorch. However, there are a number of issues (in order of importance):1) The authors often use deprecated commands, and so you have to regularly check online if the way they are doing it is how it is currently done. Since this book was published 2 months ago, there's really no excuse.2) Sometimes they'll mention a command in a small paragraph, but fail to provide syntax, an example, or any clear explanation of what it does. In these cases you once again need to go online and figure out what the command does3) The writing is full of over-the-top cliched language like "turned the whole AI community upside down" or "be ready to be blown away". I find myself rolling my eyes almost constantly while reading this book. In addition, there are some awkward sentences, so the book probably needed another pass by the proof-readerMy conclusion is that the book needs more work. It's helpful to get some guidance rather than trying to start directly from online tutorials, but it's probably not worth the quite high price tag. It's also quite interesting how all the 5 star reviews are from unverified purchases, and one of them appears to have been posted a day before the book was published. I wonder why that would be.
Amazon Verified review Amazon
Get free access to Packt library with over 7500+ books and video courses for 7 days!
Start Free Trial

FAQs

What is included in a Packt subscription? Chevron down icon Chevron up icon

A subscription provides you with full access to view all Packt and licnesed content online, this includes exclusive access to Early Access titles. Depending on the tier chosen you can also earn credits and discounts to use for owning content

How can I cancel my subscription? Chevron down icon Chevron up icon

To cancel your subscription with us simply go to the account page - found in the top right of the page or at https://subscription.packtpub.com/my-account/subscription - From here you will see the ‘cancel subscription’ button in the grey box with your subscription information in.

What are credits? Chevron down icon Chevron up icon

Credits can be earned from reading 40 section of any title within the payment cycle - a month starting from the day of subscription payment. You also earn a Credit every month if you subscribe to our annual or 18 month plans. Credits can be used to buy books DRM free, the same way that you would pay for a book. Your credits can be found in the subscription homepage - subscription.packtpub.com - clicking on ‘the my’ library dropdown and selecting ‘credits’.

What happens if an Early Access Course is cancelled? Chevron down icon Chevron up icon

Projects are rarely cancelled, but sometimes it's unavoidable. If an Early Access course is cancelled or excessively delayed, you can exchange your purchase for another course. For further details, please contact us here.

Where can I send feedback about an Early Access title? Chevron down icon Chevron up icon

If you have any feedback about the product you're reading, or Early Access in general, then please fill out a contact form here and we'll make sure the feedback gets to the right team. 

Can I download the code files for Early Access titles? Chevron down icon Chevron up icon

We try to ensure that all books in Early Access have code available to use, download, and fork on GitHub. This helps us be more agile in the development of the book, and helps keep the often changing code base of new versions and new technologies as up to date as possible. Unfortunately, however, there will be rare cases when it is not possible for us to have downloadable code samples available until publication.

When we publish the book, the code files will also be available to download from the Packt website.

How accurate is the publication date? Chevron down icon Chevron up icon

The publication date is as accurate as we can be at any point in the project. Unfortunately, delays can happen. Often those delays are out of our control, such as changes to the technology code base or delays in the tech release. We do our best to give you an accurate estimate of the publication date at any given time, and as more chapters are delivered, the more accurate the delivery date will become.

How will I know when new chapters are ready? Chevron down icon Chevron up icon

We'll let you know every time there has been an update to a course that you've bought in Early Access. You'll get an email to let you know there has been a new chapter, or a change to a previous chapter. The new chapters are automatically added to your account, so you can also check back there any time you're ready and download or read them online.

I am a Packt subscriber, do I get Early Access? Chevron down icon Chevron up icon

Yes, all Early Access content is fully available through your subscription. You will need to have a paid for or active trial subscription in order to access all titles.

How is Early Access delivered? Chevron down icon Chevron up icon

Early Access is currently only available as a PDF or through our online reader. As we make changes or add new chapters, the files in your Packt account will be updated so you can download them again or view them online immediately.

How do I buy Early Access content? Chevron down icon Chevron up icon

Early Access is a way of us getting our content to you quicker, but the method of buying the Early Access course is still the same. Just find the course you want to buy, go through the check-out steps, and you’ll get a confirmation email from us with information and a link to the relevant Early Access courses.

What is Early Access? Chevron down icon Chevron up icon

Keeping up to date with the latest technology is difficult; new versions, new frameworks, new techniques. This feature gives you a head-start to our content, as it's being created. With Early Access you'll receive each chapter as it's written, and get regular updates throughout the product's development, as well as the final course as soon as it's ready.We created Early Access as a means of giving you the information you need, as soon as it's available. As we go through the process of developing a course, 99% of it can be ready but we can't publish until that last 1% falls in to place. Early Access helps to unlock the potential of our content early, to help you start your learning when you need it most. You not only get access to every chapter as it's delivered, edited, and updated, but you'll also get the finalized, DRM-free product to download in any format you want when it's published. As a member of Packt, you'll also be eligible for our exclusive offers, including a free course every day, and discounts on new and popular titles.