<jupyter_start><jupyter_text>imports<jupyter_code># for python 2.* users
from __future__ import print_function
import torch<jupyter_output><empty_output><jupyter_text>tensors<jupyter_code>x = torch.Tensor(4, 3)
print(x)
x = torch.rand(4, 3)
print(x)
print(x.size())<jupyter_output><empty_output><jupyter_text>tensor operation<jupyter_code>y = torch.rand(4, 3)
print(x + y)
print(torch.add(x, y))
result = torch.Tensor(5, 3)
torch.add(x, y, out=result)
print(result)
y.add_(x)
print(y)<jupyter_output><empty_output><jupyter_text>indexing tensors<jupyter_code>print(x[:, 1])<jupyter_output><empty_output><jupyter_text>resizing tensors<jupyter_code>x = torch.randn(4, 4)
y = x.view(16)
# the size -1 is inferred from other dimensions
z = x.view(-1, 8)  
print(x.size(), y.size(), z.size())<jupyter_output><empty_output><jupyter_text>check CUDA<jupyter_code>if torch.cuda.is_available():
    x = x.cuda()
    y = y.cuda()
    print(x+y)<jupyter_output><empty_output><jupyter_text>Autograd<jupyter_code>import torch
from torch.autograd import Variable

x = Variable(torch.ones(2, 2), requires_grad=True)
print(x)
y = x + 2
print(y)
print(x.grad_fn)
print(y.grad_fn)
z = y * y * 3
out = z.mean()

print(z, out)
out.backward()
print(x.grad)<jupyter_output><empty_output><jupyter_text>Neural Network<jupyter_code>import torch
from torch.autograd import Variable
import torch.nn as nn
import torch.nn.functional as F

# inherit from nn.Module
class Net(nn.Module):

    def __init__(self):
        super(Net, self).__init__()
        # 1 input image channel, 6 output channels, 5x5 square convolution
        # kernel
        self.conv1 = nn.Conv2d(1, 6, 5)
        self.conv2 = nn.Conv2d(6, 16, 5)
        # an affine operation: y = Wx + b
        self.fc1 = nn.Linear(16 * 5 * 5, 120)
        self.fc2 = nn.Linear(120, 84)
        self.fc3 = nn.Linear(84, 10)

    def forward(self, x):
        # Max pooling over a (2, 2) window
        x = F.max_pool2d(F.relu(self.conv1(x)), (2, 2))
        # If the size is a square you can only specify a single number
        x = F.max_pool2d(F.relu(self.conv2(x)), 2)
        x = x.view(-1, self.num_flat_features(x))
        x = F.relu(self.fc1(x))
        x = F.relu(self.fc2(x))
        x = self.fc3(x)
        return x

    def num_flat_features(self, x):
        size = x.size()[1:]  # all dimensions except the batch dimension
        num_features = 1
        for s in size:
            num_features *= s
        return num_features


net = Net()
print(net)
params = list(net.parameters())
print(len(params))
print(params[0].size()) 
input = Variable(torch.randn(1, 1, 32, 32))
out = net(input)
print(out)
y.unsqueeze(0).size()
output = net(input)
target = Variable(torch.arange(1, 11))  # a dummy target, for example
criterion = nn.MSELoss()

loss = criterion(output, target)
print(loss)
loss.grad_fn.next_functions[0][0].next_functions[0][0].next_functions[0][0]
net.zero_grad()

print('conv1.bias.grad before backward')
print(net.conv1.bias.grad)

loss.backward()

print('conv1.bias.grad after backward')
print(net.conv1.bias.grad)
learning_rate = 0.01
for f in net.parameters():
    f.data.sub_(f.grad.data * learning_rate)<jupyter_output><empty_output><jupyter_text>optimizer<jupyter_code>import torch.optim as optim

optimizer = optim.SGD(net.parameters(), lr=0.01)

optimizer.zero_grad()
output = net(input)
loss = criterion(output, target)
loss.backward()
optimizer.step()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1461293128443027, 'fraction_numerical': 0.028124093940272543, 'mean_word_length': 4.203619909502263, 'pattern_counts': {'":': 0, '<': 37, '<?xml version=': 0, '>': 37, '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': '9', 'dir': 'jupyter-structured-clean-dedup', 'id': '160627', 'license': 'MIT', 'n_tokens_mistral': 1397, 'n_tokens_neox': 1329, 'n_words': 275, 'path': 'PyTorch/src.ipynb', 'repo_name': 'shams-sam/logic-lab', 'size': '3449'}