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_optim.py
Views: 713
# -*- coding: utf-8 -*-1"""2PyTorch: optim3--------------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.910Rather than manually updating the weights of the model as we have been doing,11we use the optim package to define an Optimizer that will update the weights12for us. The optim package defines many optimization algorithms that are commonly13used for deep learning, including SGD+momentum, RMSProp, Adam, etc.14"""15import torch16import math171819# Create Tensors to hold input and outputs.20x = torch.linspace(-math.pi, math.pi, 2000)21y = torch.sin(x)2223# Prepare the input tensor (x, x^2, x^3).24p = torch.tensor([1, 2, 3])25xx = x.unsqueeze(-1).pow(p)2627# Use the nn package to define our model and loss function.28model = torch.nn.Sequential(29torch.nn.Linear(3, 1),30torch.nn.Flatten(0, 1)31)32loss_fn = torch.nn.MSELoss(reduction='sum')3334# Use the optim package to define an Optimizer that will update the weights of35# the model for us. Here we will use RMSprop; the optim package contains many other36# optimization algorithms. The first argument to the RMSprop constructor tells the37# optimizer which Tensors it should update.38learning_rate = 1e-339optimizer = torch.optim.RMSprop(model.parameters(), lr=learning_rate)40for t in range(2000):41# Forward pass: compute predicted y by passing x to the model.42y_pred = model(xx)4344# Compute and print loss.45loss = loss_fn(y_pred, y)46if t % 100 == 99:47print(t, loss.item())4849# Before the backward pass, use the optimizer object to zero all of the50# gradients for the variables it will update (which are the learnable51# weights of the model). This is because by default, gradients are52# accumulated in buffers( i.e, not overwritten) whenever .backward()53# is called. Checkout docs of torch.autograd.backward for more details.54optimizer.zero_grad()5556# Backward pass: compute gradient of the loss with respect to model57# parameters58loss.backward()5960# Calling the step function on an Optimizer makes an update to its61# parameters62optimizer.step()636465linear_layer = model[0]66print(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')676869