<jupyter_start><jupyter_text>07. Vector-Jacobian ProductGenerally speaking, `torch.autograd` is an engine for computing vector-Jacobian product.<jupyter_code>import torch
x = torch.randn(3, requires_grad=True)
print(x)
y = x * 2
print(y)
while y.data.norm() < 1000:
    y = y * 2
print(y)<jupyter_output>tensor([-1092.8602,   487.1178,  -279.8656], grad_fn=<MulBackward0>)<jupyter_text>Now in this case `y` is no longer a scalar. `torch.autograd` cloud not compute the full Jacobian directly, but if we just want the vector-Jacobian product, simply pass the vector to `backward` as argument:<jupyter_code>v = torch.tensor([0.1, 1.0, 0.0001], dtype=torch.float)
y.backward(v)

print(x.grad)<jupyter_output>tensor([1.0240e+02, 1.0240e+03, 1.0240e-01])<jupyter_text>You can also stop autograd from tracking history on Tensors with `.requires_grad=True` either by wrapping the code block in `with torch.no_grad():`<jupyter_code>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>Or by using `.detach()` to get a new Tensor with the same content but that does not require gradients:<jupyter_code>print(x.requires_grad)
y = x.detach()
print(y.requires_grad)
print(x.eq(y).all())<jupyter_output>True
False
tensor(True)
{'fraction_non_alphanumeric': 0.13129770992366413, 'fraction_numerical': 0.04885496183206107, 'mean_word_length': 6.2032967032967035, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 14, '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': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '609841', 'license': 'MIT', 'n_tokens_mistral': 542, 'n_tokens_neox': 493, 'n_words': 122, 'path': '01-basics/07-vectorJacobian.ipynb', 'repo_name': 'shuhua-wang/pytorch-tutorials', 'size': '1310'}