Rate this Page

Note

Go to the endto download the full example code.

PyTorch: nn#

Created On: Dec 03, 2020 | Last Updated: Sep 29, 2025 | Last Verified: Nov 05, 2024

A third order polynomial, trained to predict\(y=\sin(x)\) from\(-\pi\)to\(\pi\) by minimizing squared Euclidean distance.

This implementation uses the nn package from PyTorch to build the network.PyTorch autograd makes it easy to define computational graphs and take gradients,but raw autograd can be a bit too low-level for defining complex neural networks;this is where the nn package can help. The nn package defines a set of Modules,which you can think of as a neural network layer that produces output frominput and may have some trainable weights.

99 689.6170654296875199 459.6072692871094299 307.3345947265625399 206.517822265625499 139.763427734375599 95.55880737304688699 66.28359985351562799 46.893592834472656899 34.04959487915039999 25.5404701232910161099 19.9026527404785161199 16.1667118072509771299 13.690647125244141399 12.0493593215942381499 10.9612674713134771599 10.239793777465821699 9.7612895965576171799 9.4438648223876951899 9.233279228210451999 9.093530654907227Result: y = -0.0045463936403393745 + 0.8411293625831604 x + 0.0007843282073736191 x^2 + -0.09110979735851288 x^3

importtorchimportmath# Create Tensors to hold input and outputs.x=torch.linspace(-math.pi,math.pi,2000)y=torch.sin(x)# For this example, the output y is a linear function of (x, x^2, x^3), so# we can consider it as a linear layer neural network. Let's prepare the# tensor (x, x^2, x^3).p=torch.tensor([1,2,3])xx=x.unsqueeze(-1).pow(p)# In the above code, x.unsqueeze(-1) has shape (2000, 1), and p has shape# (3,), for this case, broadcasting semantics will apply to obtain a tensor# of shape (2000, 3)# Use the nn package to define our model as a sequence of layers. nn.Sequential# is a Module which contains other Modules, and applies them in sequence to# produce its output. The Linear Module computes output from input using a# linear function, and holds internal Tensors for its weight and bias.# The Flatten layer flatens the output of the linear layer to a 1D tensor,# to match the shape of `y`.model=torch.nn.Sequential(torch.nn.Linear(3,1),torch.nn.Flatten(0,1))# The nn package also contains definitions of popular loss functions; in this# case we will use Mean Squared Error (MSE) as our loss function.loss_fn=torch.nn.MSELoss(reduction='sum')learning_rate=1e-6fortinrange(2000):# Forward pass: compute predicted y by passing x to the model. Module objects# override the __call__ operator so you can call them like functions. When# doing so you pass a Tensor of input data to the Module and it produces# a Tensor of output data.y_pred=model(xx)# Compute and print loss. We pass Tensors containing the predicted and true# values of y, and the loss function returns a Tensor containing the# loss.loss=loss_fn(y_pred,y)ift%100==99:print(t,loss.item())# Zero the gradients before running the backward pass.model.zero_grad()# Backward pass: compute gradient of the loss with respect to all the learnable# parameters of the model. Internally, the parameters of each Module are stored# in Tensors with requires_grad=True, so this call will compute gradients for# all learnable parameters in the model.loss.backward()# Update the weights using gradient descent. Each parameter is a Tensor, so# we can access its gradients like we did before.withtorch.no_grad():forparaminmodel.parameters():param-=learning_rate*param.grad# You can access the first layer of `model` like accessing the first item of a listlinear_layer=model[0]# For linear layer, its parameters are stored as `weight` and `bias`.print(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')

Total running time of the script: (0 minutes 0.553 seconds)