- 🍨 本文为🔗365天深度学习训练营中的学习记录博客
🍖 原作者:K同学啊 | 接辅导、项目定制
- 🚀 文章来源:K同学的学习圈子
目录
我的环境
- 语言环境:python3.8.18
- 编译器:jupyter notebook
- 深度学习环境:torch==2.0.1+cu118,torchvision==0.15.2+cu118
一、代码实现
1.掉包
import torch
import torch.nn as nn
import torch.optim as optim
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
import matplotlib.pyplot as plt
2.读取数据
data = pd.read_csv('heart.csv')
# 对目标为0的样本进行过采样
data_0 = data[data['target'] == 0]
data = pd.concat([data, data_0.sample(27)])
# 特征和标签分离
dt_x = data.drop(columns=['target']) # 特征
dt_y = data['target'] # 标签
data_0
# 数据标准化
scaler = StandardScaler()
dt_x = scaler.fit_transform(dt_x)
# 划分训练集和测试集
train_x, test_x, train_y, test_y = train_test_split(dt_x, dt_y, test_size=0.2)
# 转换为PyTorch张量
train_x = torch.tensor(train_x, dtype=torch.float32)
train_y = torch.tensor(train_y.values, dtype=torch.int64)
test_x = torch.tensor(test_x, dtype=torch.float32)
test_y = torch.tensor(test_y.values, dtype=torch.int64)
3.定义模型
# 网络定义
class MyModel(nn.Module):
def __init__(self):
super(MyModel, self).__init__()
self.fc1 = nn.Linear(13, 64) # 输入特征数为13,输出特征数为64
self.fc2 = nn.Linear(64, 2) # 输出特征数为2,对应2个类别
def forward(self, x):
x = torch.relu(self.fc1(x))
x = self.fc2(x)
return x
model = MyModel()
4.定义超参数并训练
# 损失函数和优化器
criterion = nn.CrossEntropyLoss()
optimizer = optim.Adam(model.parameters(), lr=0.001)
# 记录训练过程中的损失和准确率
train_losses = []
train_accuracies = []
test_losses = []
test_accuracies = []
# 训练模型
epochs = 100
for epoch in range(epochs):
optimizer.zero_grad()
outputs = model(train_x)
loss = criterion(outputs, train_y)
loss.backward()
optimizer.step()
# 记录训练损失
train_losses.append(loss.item())
# 计算训练集准确率
_, predicted = torch.max(outputs, 1)
accuracy = (predicted == train_y).sum().item() / train_y.size(0)
train_accuracies.append(accuracy)
if (epoch + 1) % 10 == 0:
print(f'Epoch [{epoch + 1}/{epochs}], Loss: {loss.item():.4f}, Train Accuracy: {accuracy:.4f}')
# 测试模型
with torch.no_grad():
outputs = model(test_x)
loss = criterion(outputs, test_y)
test_losses.append(loss.item())
# 计算测试集准确率
_, predicted = torch.max(outputs, 1)
accuracy = (predicted == test_y).sum().item() / test_y.size(0)
test_accuracies.append(accuracy)
5.结果可视化
# 绘制损失曲线
plt.figure(figsize=(10, 5))
plt.plot(train_losses, label='Train Loss', color='blue')
plt.plot(test_losses, label='Test Loss', color='orange')
plt.xlabel('Epochs')
plt.ylabel('Loss')
plt.title('Training and Testing Loss')
plt.legend()
plt.show()
# 绘制准确率曲线
plt.figure(figsize=(10, 5))
plt.plot(train_accuracies, label='Train Accuracy', color='blue')
plt.plot(test_accuracies, label='Test Accuracy', color='orange')
plt.xlabel('Epochs')
plt.ylabel('Accuracy')
plt.title('Training and Testing Accuracy')
plt.legend()
plt.show()
二、个人总结
这周不做CNN系列了,因为不研究CV方向。于是开始了rnn方向的训练,rnn本人自学然就和很多,代码之前也编过很多,rnn模型作为最基础的循环神经网络,会出现梯度消失和梯度爆炸的·问题,因此还需要后续的LSTM和GRU等等。