<jupyter_start><jupyter_text>A PyTorch *Hello, World!* [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/the-deep-learners/deep-learning-illustrated/blob/master/notebooks/pytorch.ipynb)<jupyter_code>import torch
x = torch.zeros(28, 28, 1, dtype=torch.uint8)
y = torch.randn(28, 28, 1, dtype=torch.float32)
print(x.shape)
print(x.dtype)
print(y.shape)
print(y.dtype)
x = torch.zeros(3, 3, dtype=torch.float32, requires_grad=True)

y = x - 4
z = y**3 * 6
out = z.mean()

out.backward()

print(x.grad)
# Define random tensors for the inputs and outputs
x = torch.randn(32, 784, requires_grad=True)
y = torch.randint(low=0, high=10, size=(32,))



# Define the model, using the Sequential class
model = torch.nn.Sequential(
    torch.nn.Linear(784, 100),
    torch.nn.Sigmoid(),
    torch.nn.Linear(100, 10),
    torch.nn.LogSoftmax(dim=1)
)

# Define the optimizer and loss function
optimizer = torch.optim.Adam(model.parameters())
loss_fn = torch.nn.NLLLoss()

for step in range(1000):
    # Make predictions by forward propagation
    y_hat = model(x)
    # Calculate the loss
    loss = loss_fn(y_hat, y)
    # Zero-out the gradient before performing a backward pass
    optimizer.zero_grad()
    # Compute the gradients w.r.t. the loss
    loss.backward()
    # Print the results
    print('Step: {:4d} - loss: {:0.4f}'.format(step+1, loss.item()))
    # Update the model parameters
    optimizer.step()<jupyter_output>Step:    1 - loss: 2.3263
Step:    2 - loss: 2.1633
Step:    3 - loss: 2.0075
Step:    4 - loss: 1.8599
Step:    5 - loss: 1.7213
Step:    6 - loss: 1.5918
Step:    7 - loss: 1.4717
Step:    8 - loss: 1.3607
Step:    9 - loss: 1.2586
Step:   10 - loss: 1.1650
Step:   11 - loss: 1.0794
Step:   12 - loss: 1.0011
Step:   13 - loss: 0.9297
Step:   14 - loss: 0.8646
Step:   15 - loss: 0.8052
Step:   16 - loss: 0.7509
Step:   17 - loss: 0.7013
Step:   18 - loss: 0.6558
Step:   19 - loss: 0.6141
Step:   20 - loss: 0.5758
Step:   21 - loss: 0.5406
Step:   22 - loss: 0.5081
Step:   23 - loss: 0.4780
Step:   24 - loss: 0.4502
Step:   25 - loss: 0.4245
Step:   26 - loss: 0.4005
Step:   27 - loss: 0.3783
Step:   28 - loss: 0.3576
Step:   29 - loss: 0.3383
Step:   30 - loss: 0.3204
Step:   31 - loss: 0.3036
Step:   32 - loss: 0.2880
Step:   33 - loss: 0.2733
Step:   34 - loss: 0.2597
Step:   35 - loss: 0.2469
Step:   36 - loss: 0.2350
Step:   37 - loss: 0.2238
Step:   38 - loss: 0.2133
Step:   39 -[...]
{'fraction_non_alphanumeric': 0.15162454873646208, 'fraction_numerical': 0.12394705174488568, 'mean_word_length': 2.842835130970724, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 4, 'https://': 2, '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': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '441950', 'license': 'MIT', 'n_tokens_mistral': 1197, 'n_tokens_neox': 1017, 'n_words': 277, 'path': 'notebooks/pytorch.ipynb', 'repo_name': 'tromgy/deep-learning-illustrated', 'size': '2493'}