<jupyter_start><jupyter_text>AutogradNow that we know how to calculate a loss, how do we use it to perform backpropagation? Torch provides a module, `autograd`, for automatically calculating the gradients of tensors. We can use it to calculate the gradients of all our parameters with respect to the loss. Autograd works by keeping track of operations performed on tensors, then going backwards through those operations, calculating gradients along the way. To make sure PyTorch keeps track of operations on a tensor and calculates the gradients, you need to set `requires_grad = True` on a tensor. You can do this at creation with the `requires_grad` keyword, or at any time with `x.requires_grad_(True)`.You can turn off gradients for a block of code with the `torch.no_grad()` content:```pythonx = torch.zeros(1, requires_grad=True)>>> with torch.no_grad():...     y = x * 2>>> y.requires_gradFalse```Also, you can turn on or off gradients altogether with `torch.set_grad_enabled(True|False)`.The gradients are computed with respect to some variable `z` with `z.backward()`. This does a backward pass through the operations that created `z`.<jupyter_code>import torch
from torch import nn
import torch.nn.functional as F
x = torch.randn(2,2, requires_grad=True)
print(x)
y = x**2
print(y)
## grad_fn shows the function that generated this variable
print(y.grad_fn)
z = y.mean()
print(z)
print(x.grad)<jupyter_output>None<jupyter_text>To calculate the gradients, you need to run the `.backward` method on a Variable, `z` for example. This will calculate the gradient for `z` with respect to `x`$$\frac{\partial z}{\partial x} = \frac{\partial}{\partial x}\left[\frac{1}{n}\sum_i^n x_i^2\right] = \frac{x}{2}$$<jupyter_code>z.backward()
print(x.grad)
print(x/2)
z.item()
net = nn.Sequential(nn.Linear(10, 4, bias=False), nn.ReLU(), nn.Linear(4, 1, bias=False), nn.Sigmoid())
net2 = nn.Sequential(nn.Linear(10, 4, bias=False), nn.ReLU(), nn.Linear(4, 1, bias=False), nn.Sigmoid())
net2.load_state_dict(net.state_dict())y.grad
y_grads= torch.autograd.grad(z,y)
y_grads<jupyter_output><empty_output><jupyter_text>Copying module parameters:<jupyter_code>import numpy as np
x= np.array([0.05, 0.06, 0.01, 0.04, 0.2, 0.057, 0.084, 0.145, 0.014, 0.022], dtype=np.float32)
y= np.array([1], dtype=np.float32)
x=torch.as_tensor(x)
y=torch.as_tensor(y)
print(x)
print(y)<jupyter_output>tensor([0.0500, 0.0600, 0.0100, 0.0400, 0.2000, 0.0570, 0.0840, 0.1450, 0.0140,
        0.0220])
tensor([1.])<jupyter_text>Example for clonning a module parameters using load_state_dict()<jupyter_code>net = nn.Sequential(nn.Linear(10, 4, bias=False), nn.ReLU(), nn.Linear(4, 1, bias=False), nn.Sigmoid())
net2 = nn.Sequential(nn.Linear(10, 4, bias=False), nn.ReLU(), nn.Linear(4, 1, bias=False), nn.Sigmoid())
net2.load_state_dict(net.state_dict())<jupyter_output><empty_output><jupyter_text>Example for clonning a module parameters using torch.clone()<jupyter_code>net = nn.Sequential(nn.Linear(10, 4, bias=False), nn.ReLU(), nn.Linear(4, 1, bias=False), nn.Sigmoid())
net2 = nn.Sequential(nn.Linear(10, 4, bias=False), nn.ReLU(), nn.Linear(4, 1, bias=False), nn.Sigmoid())
net2[0].weight= torch.nn.Parameter(net[0].weight.clone())
net2[2].weight= torch.nn.Parameter(net[2].weight.clone())<jupyter_output><empty_output><jupyter_text>Example for clonning a module parameters using deepcopy()<jupyter_code>import copy
net = nn.Sequential(nn.Linear(10, 4, bias=False), nn.ReLU(), nn.Linear(4, 1, bias=False), nn.Sigmoid())
net2 = copy.deepcopy(net)<jupyter_output><empty_output><jupyter_text>Example for clonning a module parameters using detach().clone()<jupyter_code>net = nn.Sequential(nn.Linear(10, 4, bias=False), nn.ReLU(), nn.Linear(4, 1, bias=False), nn.Sigmoid())
net2[0].weight= torch.nn.Parameter(net[0].weight.detach().clone())
net2[2].weight= torch.nn.Parameter(net[2].weight.detach().clone())<jupyter_output><empty_output><jupyter_text>Now test each method:<jupyter_code>print(net)
print(net2)
print('Net parameters \n', list(net.parameters()))
print('Net2 parameters \n', list(net2.parameters()))
print('Net first linear layer weights \n', net._parameters)
print('Net first linear layer weights \n', net[0].weight)
print('Net2 first linear layer weights \n',net2[0].weight)
print('\n')
print('Net second linear layer weights \n', net[2].weight)
print('Net2 second linear layer weights \n', net2[2].weight)
print('Net first linear layer weights gradients \n', net[0].weight.grad)
print('Net2 first linear layer weights gradients \n',net2[0].weight.grad)
print('\n')
print('Net second linear layer weights gradients \n', net[2].weight.grad)
print('Net2 second linear layer weights gradients \n', net2[2].weight.grad)
# One gradient update step is called on Net2:
loss= nn.BCEWithLogitsLoss()
opt= torch.optim.SGD(net.parameters(), lr=0.01, momentum=0.9)
error = loss(net(x), y)
error.backward()
opt.step()
error
print('Net first linear layer weights \n', net[0].weight)
print('Net2 first linear layer weights \n',net2[0].weight)
print('\n')
print('Net second linear layer weights \n', net[2].weight)
print('Net2 second linear layer weights \n', net2[2].weight)
print('Net first linear layer weights gradients \n', net[0].weight.grad)
print('Net2 first linear layer weights gradients \n',net2[0].weight.grad)
print('\n')
print('Net second linear layer weights gradients \n', net[2].weight.grad)
print('Net2 second linear layer weights gradients \n', net2[2].weight.grad)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.15384615384615385, 'fraction_numerical': 0.03609187021509296, 'mean_word_length': 6.200787401574803, 'pattern_counts': {'":': 0, '<': 31, '<?xml version=': 0, '>': 37, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '8', 'dir': 'jupyter-structured-clean-dedup', 'id': '411313', 'license': 'MIT', 'n_tokens_mistral': 2013, 'n_tokens_neox': 1888, 'n_words': 531, 'path': 'exercise.ipynb', 'repo_name': 'itssamedozkan/TripletMAML', 'size': '5486'}