<jupyter_start><jupyter_text>Autograd<jupyter_code>import torch

x = torch.ones(2, 2, requires_grad=True)
x
y = x + 2
y
y.grad_fn
z = y*y*3
out = z.mean()

print(z)
print(out)
a = torch.rand(3,3)
a = (2*a)+3
print(a)
print(a.requires_grad)

# change requires_grad in place
a.requires_grad_(True)
print(a)
b = (a*a).sum()
print(b)
print(b.grad_fn)<jupyter_output>tensor([[3.1001, 4.3184, 4.5754],
        [3.5336, 3.3831, 4.7765],
        [3.9711, 3.7767, 3.1712]])
False
tensor([[3.1001, 4.3184, 4.5754],
        [3.5336, 3.3831, 4.7765],
        [3.9711, 3.7767, 3.1712]], requires_grad=True)
tensor(136.0296, grad_fn=<SumBackward0>)
<SumBackward0 object at 0x7f78ae01e070><jupyter_text>Compute Gradients<jupyter_code>print(out)
out.backward()

# d(out)/dx
x.grad
x = torch.randn(3, requires_grad=True)
print(x)

y = x*2
while y.data.norm() < 1000:
    y = y * 2

print(y)

# vector-Jacobian product
v = torch.tensor([0.1, 1.0, 0.0001], dtype=torch.float)
y.backward(v)

print(x.grad)

print(x.requires_grad)
print((x ** 2).requires_grad)

with torch.no_grad():
    print((x ** 2).requires_grad)<jupyter_output>True
True
False<jupyter_text>More examples<jupyter_code>import torch

t1 = torch.randn((3,5), requires_grad=True)
t1
a = torch.randn((3,3), requires_grad = True)

w1 = torch.randn((3,3), requires_grad = True)
w2 = torch.randn((3,3), requires_grad = True)
w3 = torch.randn((3,3), requires_grad = True)
w4 = torch.randn((3,3), requires_grad = True)

b = w1*a 
c = w2*a

d = w3*b + w4*c 

L = (10 - d).sum()

print("The grad fn for a is", a.grad_fn)
print("The grad fn for d is", d.grad_fn)


L.backward()
print(b.grad)<jupyter_output>None
{'fraction_non_alphanumeric': 0.18143203883495146, 'fraction_numerical': 0.09587378640776699, 'mean_word_length': 4.51505016722408, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 12, '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': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '40991', 'license': 'MIT', 'n_tokens_mistral': 842, 'n_tokens_neox': 759, 'n_words': 146, 'path': 'neural_networks/2-autograd.ipynb', 'repo_name': 'nbrockett/ml_fundamentals', 'size': '1648'}