December 2018
Beginner to intermediate
684 pages
21h 9m
English
PyTorch defines a neural network architecture using the Net() class. The central element is the forward function. autograd automatically defines the corresponding backward function that computes the gradients.
Any legal tensor operation is fair game for the forward function, providing a log of design flexibility. In our simple case, we just link the tensor through functional input-output relations after initializing their attributes, as follows:
import torch.nn as nnclass Net(nn.Module): def __init__(self, input_size, hidden_size, num_classes): super(Net, self).__init__() # Inherited from the parent class nn.Module self.fc1 = nn.Linear(input_size, hidden_size) self.logistic = nn.LogSigmoid() ...