Appearance
PyTorch
Open-source machine learning framework.
Overview
PyTorch is an open source machine learning framework based on the Torch library, used for applications such as computer vision and natural language processing.
Features
- Dynamic computation graphs
- Tensor computations
- Neural network modules
- Automatic differentiation
- GPU acceleration
- TorchScript for deployment
Getting Started
bash
pip install torch torchvisionBasic Example
python
import torch
import torch.nn as nn
import torch.optim as optim
from torch.utils.data import DataLoader, TensorDataset
# Define a neural network
class NeuralNetwork(nn.Module):
def __init__(self):
super(NeuralNetwork, self).__init__()
self.flatten = nn.Flatten()
self.linear_relu_stack = nn.Sequential(
nn.Linear(784, 512),
nn.ReLU(),
nn.Dropout(0.2),
nn.Linear(512, 256),
nn.ReLU(),
nn.Dropout(0.2),
nn.Linear(256, 10)
)
def forward(self, x):
x = self.flatten(x)
logits = self.linear_relu_stack(x)
return logits
# Create model instance
model = NeuralNetwork()
print(model)
# Generate dummy data
X = torch.randn(1000, 784)
y = torch.randint(0, 10, (1000,))
dataset = TensorDataset(X, y)
dataloader = DataLoader(dataset, batch_size=32, shuffle=True)
# Training setup
criterion = nn.CrossEntropyLoss()
optimizer = optim.Adam(model.parameters(), lr=0.001)
# Training loop
for epoch in range(5):
for batch_idx, (data, target) in enumerate(dataloader):
optimizer.zero_grad()
output = model(data)
loss = criterion(output, target)
loss.backward()
optimizer.step()
print(f'Epoch {epoch+1}, Loss: {loss.item():.4f}')
# Make predictions
model.eval()
with torch.no_grad():
sample = torch.randn(1, 784)
prediction = model(sample)
predicted_class = torch.argmax(prediction, dim=1)
print(f'Predicted class: {predicted_class.item()}')PAPER-CODE Integration
PAPER-CODE provides:
- PyTorch project templates
- Model architectures
- Training pipelines
- Deployment configurations