<jupyter_start><jupyter_text>Create tensors for a linear equation\begin{equation*}y = w x + b\end{equation*}<jupyter_code>import torch
x = torch.tensor(1., requires_grad=True)
w = torch.tensor(2., requires_grad=True)
b = torch.tensor(3., requires_grad=True)
y = w * x + b<jupyter_output><empty_output><jupyter_text>These tensors are all scalars. `requires_grad` is used to remember the derivative value.<jupyter_code>print(x, x.shape)
print(w, w.shape)
print(b, b.shape)
print(y, y.shape)<jupyter_output>tensor(1., requires_grad=True) torch.Size([])
tensor(2., requires_grad=True) torch.Size([])
tensor(3., requires_grad=True) torch.Size([])
tensor(5., grad_fn=<AddBackward0>) torch.Size([])<jupyter_text>Now we can compute the derivative backward through the tensor graph, and verify the following derivatives.\begin{equation*}\frac{\partial y}{\partial x} = w,\quad\frac{\partial y}{\partial w} = x,\quad\frac{\partial y}{\partial b} = 1\end{equation*}<jupyter_code>y.backward()
print(x.grad)
print(w.grad)
print(b.grad)<jupyter_output>tensor(2.)
tensor(1.)
tensor(1.)
{'fraction_non_alphanumeric': 0.16542056074766356, 'fraction_numerical': 0.011214953271028037, 'mean_word_length': 6.875, 'pattern_counts': {'":': 0, '<': 12, '<?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': '122531', 'license': 'MIT', 'n_tokens_mistral': 390, 'n_tokens_neox': 382, 'n_words': 82, 'path': 'tutorials/01-basics/pytorch_basics/1-basic-autograd-example-1.ipynb', 'repo_name': 'pkuwwt/pytorch-tutorial', 'size': '1070'}