Note
Go to the endto download the full example code.
PyTorch: Tensors#
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 PyTorch tensors to manually compute the forward pass,loss, and backward pass.
A PyTorch Tensor is basically the same as a numpy array: it does not knowanything about deep learning or computational graphs or gradients, and is justa generic n-dimensional array to be used for arbitrary numeric computation.
The biggest difference between a numpy array and a PyTorch Tensor is thata PyTorch Tensor can run on either CPU or GPU. To run operations on the GPU,just cast the Tensor to a cuda datatype.
99 2799.269287109375199 1921.2630615234375299 1321.021484375399 910.195556640625499 628.6900024414062599 435.5760192871094699 302.9483337402344799 211.75830078125899 148.98963928222656999 105.73630523681641099 75.898429870605471199 55.2929458618164061299 41.048233032226561399 31.190748214721681499 24.3623828887939451599 19.6276817321777341699 16.3415565490722661799 14.0586977005004881899 12.4713706970214841999 11.366728782653809Result: y = 0.047708503901958466 + 0.8346153497695923 x + -0.008230511099100113 x^2 + -0.09018323570489883 x^3
importtorchimportmathdtype=torch.floatdevice=torch.device("cpu")# device = torch.device("cuda:0") # Uncomment this to run on GPU# Create random input and output datax=torch.linspace(-math.pi,math.pi,2000,device=device,dtype=dtype)y=torch.sin(x)# Randomly initialize weightsa=torch.randn((),device=device,dtype=dtype)b=torch.randn((),device=device,dtype=dtype)c=torch.randn((),device=device,dtype=dtype)d=torch.randn((),device=device,dtype=dtype)learning_rate=1e-6fortinrange(2000):# Forward pass: compute predicted yy_pred=a+b*x+c*x**2+d*x**3# Compute and print lossloss=(y_pred-y).pow(2).sum().item()ift%100==99:print(t,loss)# Backprop to compute gradients of a, b, c, d with respect to lossgrad_y_pred=2.0*(y_pred-y)grad_a=grad_y_pred.sum()grad_b=(grad_y_pred*x).sum()grad_c=(grad_y_pred*x**2).sum()grad_d=(grad_y_pred*x**3).sum()# Update weights using gradient descenta-=learning_rate*grad_ab-=learning_rate*grad_bc-=learning_rate*grad_cd-=learning_rate*grad_dprint(f'Result: y ={a.item()} +{b.item()} x +{c.item()} x^2 +{d.item()} x^3')
Total running time of the script: (0 minutes 0.211 seconds)