<jupyter_start><jupyter_text><jupyter_code>import numpy as np
import torch
x = torch.tensor([[2., -1.], [1., 1.]], requires_grad=True)
print(x)
out = x.pow(2).sum()
out.backward()
x.grad<jupyter_output><empty_output><jupyter_text>Computing gradients for the same case that was present in `Chain_rule.ipynb` notebook in previous chapter<jupyter_code>x = np.array([[1,1]])
y = np.array([[0]])
x, y = [torch.tensor(i).float() for i in [x,y]]
W = [
    np.array([[-0.0053, 0.3793],
              [-0.5820, -0.5204],
              [-0.2723, 0.1896]], dtype=np.float32).T, 
    np.array([-0.0140, 0.5607, -0.0628], dtype=np.float32), 
    np.array([[ 0.1528, -0.1745, -0.1135]], dtype=np.float32).T, 
    np.array([-0.5516], dtype=np.float32)
]

W = [torch.tensor(i, requires_grad=True) for i in W]
def feed_forward(inputs, outputs, weights):
    pre_hidden = torch.matmul(inputs,weights[0])+ weights[1]
    hidden = 1/(1+torch.exp(-pre_hidden))
    out = torch.matmul(hidden, weights[2]) + weights[3]
    mean_squared_error = torch.mean(torch.square(out - outputs))
    return mean_squared_error
loss = feed_forward(x, y, W)
loss
loss.backward()
print([w.grad for w in W])
updated_W = [w-w.grad for w in W]
updated_W<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1861402095084609, 'fraction_numerical': 0.07010475423045931, 'mean_word_length': 4.008064516129032, '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': '307209', 'license': 'MIT', 'n_tokens_mistral': 545, 'n_tokens_neox': 506, 'n_words': 103, 'path': 'Chapter02/8_Auto_gradient_of_tensors.ipynb', 'repo_name': 'aihill/Modern-Computer-Vision-with-PyTorch', 'size': '1241'}