<jupyter_start><jupyter_text>PyTorch AutoGrad<jupyter_code>import torch
x = torch.FloatTensor([[1, 2],
                       [3, 4]]).requires_grad_(True)
x1 = x + 2
x2 = x - 2
x3 = x1 * x2
y = x3.sum()

print(x1)
print(x2)
print(x3)
print(y)
y.backward()<jupyter_output><empty_output><jupyter_text>$$\begin{gathered}x=\begin{bmatrix}x_{(1,1)} & x_{(1,2)} \\x_{(2,1)} & x_{(2,2)}\end{bmatrix}\\\\x_1=x+2 \\x_2=x-2 \\\\\begin{aligned}x_3&=x_1\times{x_2} \\&=(x+2)(x-2) \\&=x^2-4\end{aligned} \\\\\begin{aligned}y&=\text{sum}(x_3) \\&=x_{3,(1,1)}+x_{3,(1,2)}+x_{3,(2,1)}+x_{3,(2,2)}\end{aligned} \\\\\text{x.grad}=\begin{bmatrix}\frac{\partial{y}}{\partial{x_{(1,1)}}} & \frac{\partial{y}}{\partial{x_{(1,2)}}} \\\frac{\partial{y}}{\partial{x_{(2,1)}}} & \frac{\partial{y}}{\partial{x_{(2,2)}}}\end{bmatrix} \\\\\frac{dy}{dx}=2x\end{gathered}$$<jupyter_code>print(x.grad)
print(x)
x3.numpy()
x3.detach_().numpy()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.310308182784272, 'fraction_numerical': 0.061636556854410204, 'mean_word_length': 5.364864864864865, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, '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': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '472820', 'license': 'MIT', 'n_tokens_mistral': 462, 'n_tokens_neox': 438, 'n_words': 41, 'path': '07-gradient_descent/07-05-auto_grad.ipynb', 'repo_name': 'sujungleeml/testvscode', 'size': '941'}