Home  >  Article  >  Technology peripherals  >  How to learn PyTorch? too easy

How to learn PyTorch? too easy

WBOY
WBOYforward
2024-03-07 19:46:11576browse

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!

PyTorch 该怎么学?太简单了

Building Tensors

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)

Dynamic Computation Graph

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

GPU Acceleration

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)

Autograd: automatic differentiation

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

Modular Neural Network

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)

Predefined layers and loss functions

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)

Dataset and DataLoader

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)

Model training (loop )

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()

Model serialization

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'))

JIT

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!

Statement:
This article is reproduced at:51cto.com. If there is any infringement, please contact admin@php.cn delete