Many friends have asked me how to learn PyTorch. Practice has proved that beginners only need to master a few concepts and usage. Let’s take a look at the summary of this concise guide!
Tensors in PyTorch are multi-dimensional arrays, similar to NumPy’s ndarrays, but can run on the GPU:
import torch# Create a 2x3 tensortensor = torch.tensor([[1, 2, 3], [4, 5, 6]])print(tensor)
PyTorch uses dynamic computation graph to build the calculation graph on the fly as operations are performed, which provides the flexibility to modify the graph at runtime:
# Define two tensorsa = torch.tensor([2.], requires_grad=True)b = torch.tensor([3.], requires_grad=True)# Compute resultc = a * bc.backward()# Gradientsprint(a.grad)# Gradient w.r.t a
PyTorch allows easy switching between CPU and GPU. Just use .to(device):
device = "cuda" if torch.cuda.is_available() else "cpu"tensor = tensor.to(device)
PyTorch's autograd provides automatic differentiation function for all operations of tensor, set requires_grad=True Can track calculations:
x = torch.tensor([2.], requires_grad=True)y = x**2y.backward()print(x.grad)# Gradient of y w.r.t x
PyTorch provides the nn.Module class to define the neural network architecture and create custom layers through subclassing:
import torch.nn as nnclass SimpleNN(nn.Module):def __init__(self):super().__init__()self.fc = nn.Linear(1, 1)def forward(self, x):return self.fc(x)
PyTorch provides various predefined layers, loss functions and optimization algorithms in the nn module:
loss_fn = nn.CrossEntropyLoss()optimizer = torch.optim.Adam(model.parameters(), lr=0.001)
To achieve efficient data processing and batch processing, PyTorch provides the Dataset and DataLoader classes:
from torch.utils.data import Dataset, DataLoaderclass CustomDataset(Dataset):# ... (methods to define)data_loader = DataLoader(dataset, batch_size=32, shuffle=True)
Usually PyTorch training follows the following pattern: forward propagation, calculation of loss, backward pass and parameter update:
for epoch in range(epochs):for data, target in data_loader:optimizer.zero_grad()output = model(data)loss = loss_fn(output, target)loss.backward()optimizer.step()
Use torch.save() and torch.load() to save and load models:
# Savetorch.save(model.state_dict(), 'model_weights.pth')# Loadmodel.load_state_dict(torch.load('model_weights.pth'))
PyTorch runs in eager mode by default, but also provides models Just-in-time (JIT) compilation:
scripted_model = torch.jit.script(model)scripted_model.save("model_jit.pt")
The above is the detailed content of How to learn PyTorch? too easy. For more information, please follow other related articles on the PHP Chinese website!