哈喽,大家好!我是风哥,一个资深Python工程师。今天给大家介绍深度学习界的超人——PyTorch!想要构建灵活的深度学习模型但被其他框架的静态图搞得头疼?PyTorch的动态计算图机制让你的创意可以自由翱翔!
让我们开启这段AI探索之旅吧!
一、安装
1pip install torch torchvision torchaudio
二、基础概念和操作
1. 张量(Tensor)操作:
1import torch
2
3def tensor_basics():
4 # 创建张量
5 tensor1 = torch.tensor([[1, 2], [3, 4]])
6 tensor2 = torch.tensor([[5, 6], [7, 8]])
7
8 # 基本运算
9 add = tensor1 + tensor2 # 加法
10 sub = tensor1 - tensor2 # 减法
11 mul = tensor1 * tensor2 # 元素级乘法
12 matmul = torch.matmul(tensor1, tensor2) # 矩阵乘法
13
14 # GPU加速(如果可用)
15 if torch.cuda.is_available():
16 tensor1 = tensor1.cuda()
17 tensor2 = tensor2.cuda()
18
19 return add, sub, mul, matmul
2. 构建神经网络:
1import torch.nn as nn
2import torch.nn.functional as F
3
4class SimpleNet(nn.Module):
5 def __init__(self):
6 super(SimpleNet, self).__init__()
7 self.fc1 = nn.Linear(784, 128)
8 self.fc2 = nn.Linear(128, 64)
9 self.fc3 = nn.Linear(64, 10)
10
11 def forward(self, x):
12 x = F.relu(self.fc1(x))
13 x = F.relu(self.fc2(x))
14 x = self.fc3(x)
15 return F.log_softmax(x, dim=1)
三、高级应用
1. 卷积神经网络(CNN):
1class ConvNet(nn.Module):
2 def __init__(self):
3 super(ConvNet, self).__init__()
4 self.conv1 = nn.Conv2d(1, 32, 3)
5 self.conv2 = nn.Conv2d(32, 64, 3)
6 self.pool = nn.MaxPool2d(2)
7 self.fc1 = nn.Linear(64 * 12 * 12, 128)
8 self.fc2 = nn.Linear(128, 10)
9
10 def forward(self, x):
11 x = self.pool(F.relu(self.conv1(x)))
12 x = self.pool(F.relu(self.conv2(x)))
13 x = x.view(-1, 64 * 12 * 12)
14 x = F.relu(self.fc1(x))
15 x = self.fc2(x)
16 return F.log_softmax(x, dim=1)
2. 循环神经网络(RNN):
1class RNNNet(nn.Module):
2 def __init__(self, input_size, hidden_size, num_layers):
3 super(RNNNet, self).__init__()
4 self.hidden_size = hidden_size
5 self.num_layers = num_layers
6
7 self.lstm = nn.LSTM(
8 input_size, hidden_size,
9 num_layers, batch_first=True
10 )
11 self.fc = nn.Linear(hidden_size, 1)
12
13 def forward(self, x):
14 h0 = torch.zeros(self.num_layers, x.size(0), self.hidden_size)
15 c0 = torch.zeros(self.num_layers, x.size(0), self.hidden_size)
16
17 out, _ = self.lstm(x, (h0, c0))
18 out = self.fc(out[:, -1, :])
19 return out
四、实战案例
1. 图像分类训练器:
1class ImageClassifier:
2 def __init__(self, model, device):
3 self.model = model.to(device)
4 self.device = device
5 self.criterion = nn.CrossEntropyLoss()
6 self.optimizer = torch.optim.Adam(model.parameters())
7
8 def train_epoch(self, train_loader):
9 self.model.train()
10 total_loss = 0
11
12 for batch_idx, (data, target) in enumerate(train_loader):
13 data, target = data.to(self.device), target.to(self.device)
14
15 self.optimizer.zero_grad()
16 output = self.model(data)
17 loss = self.criterion(output, target)
18
19 loss.backward()
20 self.optimizer.step()
21
22 total_loss += loss.item()
23
24 return total_loss / len(train_loader)
25
26 def evaluate(self, test_loader):
27 self.model.eval()
28 test_loss = 0
29 correct = 0
30
31 with torch.no_grad():
32 for data, target in test_loader:
33 data, target = data.to(self.device), target.to(self.device)
34 output = self.model(data)
35
36 test_loss += self.criterion(output, target).item()
37 pred = output.argmax(dim=1)
38 correct += pred.eq(target).sum().item()
39
40 return test_loss / len(test_loader), correct / len(test_loader.dataset)
2. 自定义数据集:
1from torch.utils.data import Dataset
2
3class CustomDataset(Dataset):
4 def __init__(self, data, labels, transform=None):
5 self.data = data
6 self.labels = labels
7 self.transform = transform
8
9 def __len__(self):
10 return len(self.data)
11
12 def __getitem__(self, idx):
13 sample = self.data[idx]
14 label = self.labels[idx]
15
16 if self.transform:
17 sample = self.transform(sample)
18
19 return sample, label
五、性能优化技巧
1. 数据加载优化:
1def create_data_loader(dataset, batch_size, num_workers=4):
2 return torch.utils.data.DataLoader(
3 dataset,
4 batch_size=batch_size,
5 shuffle=True,
6 num_workers=num_workers,
7 pin_memory=True
8 )
2. 模型并行化:
1def parallel_model(model):
2 if torch.cuda.device_count() > 1:
3 model = nn.DataParallel(model)
4 return model
六、模型部署
1. 保存和加载模型:
1def save_checkpoint(model, optimizer, epoch, path):
2 torch.save({
3 'epoch': epoch,
4 'model_state_dict': model.state_dict(),
5 'optimizer_state_dict': optimizer.state_dict(),
6 }, path)
7
8def load_checkpoint(model, optimizer, path):
9 checkpoint = torch.load(path)
10 model.load_state_dict(checkpoint['model_state_dict'])
11 optimizer.load_state_dict(checkpoint['optimizer_state_dict'])
12 epoch = checkpoint['epoch']
13 return model, optimizer, epoch
2. 模型导出:
1def export_model(model, input_shape, path):
2 # 导出为ONNX格式
3 dummy_input = torch.randn(input_shape)
4 torch.onnx.export(
5 model,
6 dummy_input,
7 path,
8 verbose=True,
9 input_names=['input'],
10 output_names=['output']
11 )
使用PyTorch时的一些建议:
善用自动求导机制
注意内存管理
合理使用GPU资源
使用适当的学习率调度器
进行充分的模型验证
今天的Python学习之旅就到这里啦!记得动手敲代码,有问题随时在评论区问风哥哦。祝大家学习愉快,收获满满!