NumPy is a separate library for numerical computing in Python. While PyTorch tensors can interoperate with NumPy arrays, NumPy is not a core component of the PyTorch ecosystem.
import torch import torch.nn as nn model = nn.Linear(2, 1) criterion = nn.MSELoss() optimizer = torch.optim.SGD(model.parameters(), lr=0.1) inputs = torch.tensor([[1.0, 2.0]]) targets = torch.tensor([[1.0]]) optimizer.zero_grad() outputs = model(inputs) loss = criterion(outputs, targets) loss.backward() optimizer.step() print(round(loss.item(), 4))
The initial weights are random, so the output will not match the target exactly. The MSE loss will be a positive value around 0.5 for this simple input and target.
TorchText provides tools for text processing, datasets, and tokenization for NLP tasks within the PyTorch ecosystem.
A very high learning rate can cause the model to overshoot minima, causing unstable training and possibly divergence.
outputs = torch.tensor([[2.0, 1.0, 0.1], [0.5, 2.5, 0.3], [1.2, 0.7, 2.1]]) labels = torch.tensor([0, 1, 2])
Using argmax over dimension 1 gives predicted class indices per sample. Comparing with labels and taking the mean of correct predictions gives accuracy.
