《动手学深度学习》PyTorch版教程
项目介绍
本项目对中文版《动手学深度学习》(第一版)中的代码进行了PyTorch实现,并整理为PDF版本供下载。项目旨在帮助读者通过PyTorch框架学习和实践深度学习技术。除了原书内容外,本项目还增加了一些额外的附录和实现,如语义分割网络(U-Net)的实现,以增强学习体验。
项目快速启动
环境配置
首先,确保你已经安装了Python和PyTorch。你可以通过以下命令安装PyTorch:
pip install torch torchvision
下载项目
你可以通过以下命令克隆项目到本地:
git clone https://github.com/wzy6642/Dive-Into-Deep-Learning-PyTorch-PDF.git
运行示例代码
进入项目目录并运行示例代码:
cd Dive-Into-Deep-Learning-PyTorch-PDF
python examples/linear_regression.py
应用案例和最佳实践
线性回归
线性回归是深度学习中的基础模型之一。以下是一个简单的线性回归实现示例:
import torch
import torch.nn as nn
import numpy as np
# 生成数据
np.random.seed(0)
x = np.random.rand(100, 1)
y = 1 + 2 * x + 0.1 * np.random.randn(100, 1)
# 转换为Tensor
x_tensor = torch.from_numpy(x).float()
y_tensor = torch.from_numpy(y).float()
# 定义模型
class LinearRegression(nn.Module):
def __init__(self):
super(LinearRegression, self).__init__()
self.linear = nn.Linear(1, 1)
def forward(self, x):
return self.linear(x)
model = LinearRegression()
criterion = nn.MSELoss()
optimizer = torch.optim.SGD(model.parameters(), lr=0.01)
# 训练模型
for epoch in range(100):
model.train()
optimizer.zero_grad()
outputs = model(x_tensor)
loss = criterion(outputs, y_tensor)
loss.backward()
optimizer.step()
if (epoch+1) % 10 == 0:
print(f'Epoch [{epoch+1}/100], Loss: {loss.item():.4f}')
语义分割
语义分割是深度学习在计算机视觉中的一个重要应用。以下是一个简单的U-Net实现示例:
import torch
import torch.nn as nn
class DoubleConv(nn.Module):
def __init__(self, in_channels, out_channels):
super(DoubleConv, self).__init__()
self.conv = nn.Sequential(
nn.Conv2d(in_channels, out_channels, 3, 1, 1, bias=False),
nn.BatchNorm2d(out_channels),
nn.ReLU(inplace=True),
nn.Conv2d(out_channels, out_channels, 3, 1, 1, bias=False),
nn.BatchNorm2d(out_channels),
nn.ReLU(inplace=True),
)
def forward(self, x):
return self.conv(x)
class UNet(nn.Module):
def __init__(self, in_channels=1, out_channels=1, features=[64, 128, 256, 512]):
super(UNet, self).__init__()
self.ups = nn.ModuleList()
self.downs = nn.ModuleList()
self.pool = nn.MaxPool2d(kernel_size=2, stride=2)
# Down part
for feature in features:
self.downs.append(DoubleConv(in_channels, feature))
in_channels = feature
# Up part
for feature in reversed(features):
self.ups.append(