Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Path: blob/main/beginner_source/examples_nn/polynomial_nn.py
Views: 713
# -*- coding: utf-8 -*-1"""2PyTorch: nn3-----------45A third order polynomial, trained to predict :math:`y=\sin(x)` from :math:`-\pi`6to :math:`pi` by minimizing squared Euclidean distance.78This implementation uses the nn package from PyTorch to build the network.9PyTorch autograd makes it easy to define computational graphs and take gradients,10but raw autograd can be a bit too low-level for defining complex neural networks;11this is where the nn package can help. The nn package defines a set of Modules,12which you can think of as a neural network layer that produces output from13input and may have some trainable weights.14"""15import torch16import math171819# Create Tensors to hold input and outputs.20x = torch.linspace(-math.pi, math.pi, 2000)21y = torch.sin(x)2223# For this example, the output y is a linear function of (x, x^2, x^3), so24# we can consider it as a linear layer neural network. Let's prepare the25# tensor (x, x^2, x^3).26p = torch.tensor([1, 2, 3])27xx = x.unsqueeze(-1).pow(p)2829# In the above code, x.unsqueeze(-1) has shape (2000, 1), and p has shape30# (3,), for this case, broadcasting semantics will apply to obtain a tensor31# of shape (2000, 3)3233# Use the nn package to define our model as a sequence of layers. nn.Sequential34# is a Module which contains other Modules, and applies them in sequence to35# produce its output. The Linear Module computes output from input using a36# linear function, and holds internal Tensors for its weight and bias.37# The Flatten layer flatens the output of the linear layer to a 1D tensor,38# to match the shape of `y`.39model = torch.nn.Sequential(40torch.nn.Linear(3, 1),41torch.nn.Flatten(0, 1)42)4344# The nn package also contains definitions of popular loss functions; in this45# case we will use Mean Squared Error (MSE) as our loss function.46loss_fn = torch.nn.MSELoss(reduction='sum')4748learning_rate = 1e-649for t in range(2000):5051# Forward pass: compute predicted y by passing x to the model. Module objects52# override the __call__ operator so you can call them like functions. When53# doing so you pass a Tensor of input data to the Module and it produces54# a Tensor of output data.55y_pred = model(xx)5657# Compute and print loss. We pass Tensors containing the predicted and true58# values of y, and the loss function returns a Tensor containing the59# loss.60loss = loss_fn(y_pred, y)61if t % 100 == 99:62print(t, loss.item())6364# Zero the gradients before running the backward pass.65model.zero_grad()6667# Backward pass: compute gradient of the loss with respect to all the learnable68# parameters of the model. Internally, the parameters of each Module are stored69# in Tensors with requires_grad=True, so this call will compute gradients for70# all learnable parameters in the model.71loss.backward()7273# Update the weights using gradient descent. Each parameter is a Tensor, so74# we can access its gradients like we did before.75with torch.no_grad():76for param in model.parameters():77param -= learning_rate * param.grad7879# You can access the first layer of `model` like accessing the first item of a list80linear_layer = model[0]8182# For linear layer, its parameters are stored as `weight` and `bias`.83print(f'Result: y = {linear_layer.bias.item()} + {linear_layer.weight[:, 0].item()} x + {linear_layer.weight[:, 1].item()} x^2 + {linear_layer.weight[:, 2].item()} x^3')848586