基于Python和PyTorch的实用
以下是一些基于Python和PyTorch的实用代码示例,涵盖深度学习常见任务。这些例子按类别组织,便于参考和实践。
基础张量操作
import torch
# 创建随机张量
x = torch.rand(3, 3)
# 矩阵乘法
y = torch.matmul(x, x.T)
# 索引操作
z = x[1:, :2]
pip install torch
运行结果
cpu = _conversion_method_template(device=torch.device("cpu"))
x = tensor([[0.2033, 0.6504, 0.1315],
[0.6639, 0.3658, 0.7341],
[0.7214, 0.6070, 0.5261]])
y = tensor([[0.4817, 0.4694, 0.6106],
[0.4694, 1.1135, 1.0871],
[0.6106, 1.0871, 1.1656]])
z = tensor([[0.6639, 0.3658],
[0.7214, 0.6070]])
运行
import torch
from torch.autograd import Variable
x = torch.randn(3)
x = Variable(x, requires_grad=True)
y = x * 3
print(y)
y.backward(torch.FloatTensor([1, 0.1, 0.01]))
print(x.grad)
运行结果
cpu = _conversion_method_template(device=torch.device("cpu"))
tensor([ 2.5690, 2.0595, -2.2016], grad_fn=<MulBackward0>)
tensor([3.0000, 0.3000, 0.0300])
一个简单实例
# %matplotlib inline
import torch
import matplotlib.pyplot as plt
import matplotlib.ticker as ticker
import math
a = torch.linspace(0., 2. * math.pi, steps=25, requires_grad=True)
print(a)
b = torch.sin(a)
plt.plot(a.detach(), b.detach())
plt.show()
运行
(.venv) F:\work\pyTorchPro>python -u src\test1.py
tensor([0.0000, 0.2618, 0.5236, 0.7854, 1.0472, 1.3090, 1.5708, 1.8326, 2.0944,
2.3562, 2.6180, 2.8798, 3.1416, 3.4034, 3.6652, 3.9270, 4.1888, 4.4506,
4.7124, 4.9742, 5.2360, 5.4978, 5.7596, 6.0214, 6.2832],
requires_grad=True)
Autograd分析
import torch
device = torch.device('cpu')
run_on_gpu = False
if torch.cuda.is_available():
device = torch.device('cuda')
run_on_gpu = True
x = torch.randn(2, 3, requires_grad=True)
y = torch.rand(2, 3, requires_grad=True)
z = torch.ones(2, 3, requires_grad=True)
with torch.autograd.profiler.profile(use_cuda=run_on_gpu) as prf:
for _ in range(1000):
z = (z / x) * y
print(prf.key_averages().table(sort_by='self_cpu_time_total'))
运行
(.venv) F:\work\pyTorchPro>python -u src\test2.py
运行结果
------------- ------------ ------------ ------------ ------------ ------------ ------------
Name Self CPU % Self CPU CPU total % CPU total CPU time avg # of Calls
------------- ------------ ------------ ------------ ------------ ------------ ------------
aten::div 61.60% 24.415ms 61.60% 24.415ms 24.415us 1000
aten::mul 38.40% 15.218ms 38.40% 15.218ms 15.218us 1000
------------- ------------ ------------ ------------ ------------ ------------ ------------
Self CPU time total: 39.632ms
简单神经网络
import torch.nn as nn
class Net(nn.Module):
def __init__(self):
super().__init__()
self.fc1 = nn.Linear(10, 5)
self.fc2 = nn.Linear(5, 1)
def forward(self, x):
x = torch.relu(self.fc1(x))
return torch.sigmoid(self.fc2(x))
图像分类模型
from torchvision import models
model = models.resnet18(pretrained=True)
# 修改最后一层
num_features = model.fc.in_features
model.fc = nn.Linear(num_features, 10) # 假设10分类
训练循环模板
optimizer = torch.optim.Adam(model.parameters(), lr=0.001)
criterion = nn.CrossEntropyLoss()
for epoch in range(10):
for data, target in dataloader:
optimizer.zero_grad()
output = model(data)
loss = criterion(output, target)
loss.backward()
optimizer.step()
自定义数据集
from torch.utils.data import Dataset
class CustomDataset(Dataset):
def __init__(self, data, labels):
self.data = data
self.labels = labels
def __len__(self):
return len(self.data)
def __getitem__(self, idx):
return self.data[idx], self.labels[idx]
GPU加速
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
model = model.to(device)
data = data.to(device)
模型保存与加载
# 保存
torch.save(model.state_dict(), 'model.pth')
# 加载
model.load_state_dict(torch.load('model.pth'))
数据增强
from torchvision import transforms
transform = transforms.Compose([
transforms.RandomHorizontalFlip(),
transforms.RandomRotation(10),
transforms.ToTensor()
])
每个示例都可以扩展为更完整的项目,建议从基础开始逐步尝试修改参数和结构。实践时注意结合具体任务调整网络架构和超参数。
基于Python和PyTorch的深度学习实例
以下是基于Python和PyTorch的深度学习实例分类与核心代码片段,涵盖计算机视觉、自然语言处理、生成模型等方向,以实用案例为主:
计算机视觉实例
图像分类(ResNet)
import torch
import torch.nn as nn
import torchvision.models as models
model = models.resnet18(pretrained=True)
num_ftrs = model.fc.in_features
model.fc = nn.Linear(num_ftrs, 10) # 替换输出层为10分类
criterion = nn.CrossEntropyLoss()
optimizer = torch.optim.SGD(model.parameters(), lr=0.001)
目标检测(Faster R-CNN)
from torchvision.models.detection import fasterrcnn_resnet50_fpn
model = fasterrcnn_resnet50_fpn(pretrained=True)
images = [torch.rand(3, 300, 400)] # 模拟输入
targets = [{"boxes": torch.tensor([[0, 0, 100, 100]]), "labels": torch.tensor([1])}]
loss_dict = model(images, targets) # 训练模式
自然语言处理实例
文本分类(LSTM)
class TextClassifier(nn.Module):
def __init__(self, vocab_size, embed_dim, hidden_dim):
super().__init__()
self.embedding = nn.Embedding(vocab_size, embed_dim)
self.lstm = nn.LSTM(embed_dim, hidden_dim, batch_first=True)
self.fc = nn.Linear(hidden_dim, 2)
def forward(self, x):
x = self.embedding(x)
_, (hidden, _) = self.lstm(x)
return self.fc(hidden.squeeze(0))
model = TextClassifier(vocab_size=10000, embed_dim=100, hidden_dim=256)
Transformer翻译模型
from torch.nn import Transformer
transformer = Transformer(
d_model=512, nhead=8, num_encoder_layers=6,
num_decoder_layers=6, dim_feedforward=2048
)
src = torch.rand(10, 32, 512) # (seq_len, batch, features)
tgt = torch.rand(20, 32, 512)
out = transformer(src, tgt) # 前向传播
生成对抗网络实例
DCGAN生成图像
class Generator(nn.Module):
def __init__(self, latent_dim):
super().__init__()
self.main = nn.Sequential(
nn.ConvTranspose2d(latent_dim, 512, 4, 1, 0),
nn.BatchNorm2d(512),
nn.ReLU(),
# 继续添加上采样层...
nn.Tanh()
)
def forward(self, z):
return self.main(z)
gen = Generator(latent_dim=100)
noise = torch.randn(64, 100, 1, 1)
fake_images = gen(noise)
强化学习实例
DQN游戏控制
class DQN(nn.Module):
def __init__(self, obs_dim,