Book Image

PyTorch Deep Learning Hands-On

By : Sherin Thomas, Sudhanshu Passi
Book Image

PyTorch Deep Learning Hands-On

By: Sherin Thomas, Sudhanshu Passi

Overview of this book

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.
Table of Contents (11 chapters)
10
Index

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...