Linear regression in PyTorch

Let's see how PyTorch implements a simple linear network. We could use autograd and backward to manually iterate through gradient descent. This unnecessarily low-level approach encumbers us with a lot of code that will be difficult to maintain, understand, and upgrade. Fortunately, PyTorch has a very straightforward object approach to building ANNs, using classes to represent models. The model classes we customize inherit all the foundation machinery required for building ANNs using the super class, torch.nn.Module. The following code demonstrates the standard way to implement modules (in this case, a linearModel) in PyTorch:

The nn.Module is the base class and is called through the super function on initialization. ...

Get Deep Learning with PyTorch Quick Start Guide now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.