<jupyter_start><jupyter_text>02_Variable_Autograd<jupyter_code>import torch
from torch.autograd import Variable<jupyter_output><empty_output><jupyter_text>Tensor vs Variable<jupyter_code>tensor = torch.FloatTensor([[1,2],[3,4]])            # build a tensor
x = Variable(tensor, requires_grad=True)      # build a variable, usually for compute gradients

print(tensor)       # [torch.FloatTensor of size 2x2]
print(x)     # [torch.FloatTensor of size 2x2]
x.sum(), x.sqrt(), x**2<jupyter_output><empty_output><jupyter_text>Variables of a Variable<jupyter_code>x.data
x.requires_grad, x.grad, x.grad_fn, x.volatile<jupyter_output><empty_output><jupyter_text>Auto Graph & Gradient<jupyter_code># create graph

x = Variable(torch.FloatTensor([3]),requires_grad=True)
y = 2*x +3

x.requires_grad, y.requires_grad
y.grad_fn.next_functions[0][0]
y.backward()  # calculate dy / dx   == y.backward(torch.Tensor([1.0]))
x.grad # dy / dx is stored and accmulated
x.grad.data.zero_() # empty gradient storage
y.backward(torch.Tensor([3.0])) # calculate dy / dx
x.grad<jupyter_output><empty_output><jupyter_text>multivariate ouput and gradient<jupyter_code>x1 = Variable(torch.ones(2), requires_grad=True)
x2 = Variable(torch.ones(2))
y = x1*2 +  x2
y
y.backward(torch.Tensor([1,1]))
x1.grad, x2.grad
x1.grad.data.numpy()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1412556053811659, 'fraction_numerical': 0.02391629297458894, 'mean_word_length': 6.2771739130434785, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 21, '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': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '373866', 'license': 'MIT', 'n_tokens_mistral': 529, 'n_tokens_neox': 518, 'n_words': 90, 'path': 'Practice5/02_variable_autograd.ipynb', 'repo_name': 'kiseyno92/SNU_ML', 'size': '1338'}