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. ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access