文章目录
- 🍨 本文为🔗365天深度学习训练营 中的学习记录博客
- 🍖 原作者:K同学啊
一、前期准备工作
1. 设置硬件设备
import numpy as np
import pandas as pd
import torch
from torch import nn
import torch.nn.functional as F
import seaborn as sns
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
device
device(type=‘cpu’)
2. 导入数据
df = pd.read_csv("heart.csv")
df
age | sex | cp | trestbps | chol | fbs | restecg | thalach | exang | oldpeak | slope | ca | thal | target | |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
0 | 63 | 1 | 3 | 145 | 233 | 1 | 0 | 150 | 0 | 2.3 | 0 | 0 | 1 | 1 |
1 | 37 | 1 | 2 | 130 | 250 | 0 | 1 | 187 | 0 | 3.5 | 0 | 0 | 2 | 1 |
2 | 41 | 0 | 1 | 130 | 204 | 0 | 0 | 172 | 0 | 1.4 | 2 | 0 | 2 | 1 |
3 | 56 | 1 | 1 | 120 | 236 | 0 | 1 | 178 | 0 | 0.8 | 2 | 0 | 2 | 1 |
4 | 57 | 0 | 0 | 120 | 354 | 0 | 1 | 163 | 1 | 0.6 | 2 | 0 | 2 | 1 |
... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... |
298 | 57 | 0 | 0 | 140 | 241 | 0 | 1 | 123 | 1 | 0.2 | 1 | 0 | 3 | 0 |
299 | 45 | 1 | 3 | 110 | 264 | 0 | 1 | 132 | 0 | 1.2 | 1 | 0 | 3 | 0 |
300 | 68 | 1 | 0 | 144 | 193 | 1 | 1 | 141 | 0 | 3.4 | 1 | 2 | 3 | 0 |
301 | 57 | 1 | 0 | 130 | 131 | 0 | 1 | 115 | 1 | 1.2 | 1 | 1 | 3 | 0 |
302 | 57 | 0 | 1 | 130 | 236 | 0 | 0 | 174 | 0 | 0.0 | 1 | 1 | 2 | 0 |
303 rows × 14 columns
二、 构建数据集
1. 标准化
from sklearn.preprocessing import StandardScaler
from sklearn.model_selection import train_test_split
X = df.iloc[:,: -1]
y = df.iloc[:, -1]
# Standardize the data
sc = StandardScaler()
X = sc.fit_transform(X)
2. 划分数据集
X = torch.tensor(np.array(X), dtype=torch.float32)
y = torch.tensor(np.array(y), dtype=torch.int64)
X_train, X_test, y_train, y_test = train_test_split(X, y,
test_size=0.1,
random_state=1)
X_train.shape, y_train.shape
(torch.Size([272, 13]), torch.Size([272]))
3. 构建数据加载器
from torch.utils.data import TensorDataset, DataLoader
train_dl = DataLoader(TensorDataset(X_train, y_train),
batch_size = 64,
shuffle = False)
test_dl = DataLoader(TensorDataset(X_test, y_test),
batch_size = 64,
shuffle = False)
三、 模型训练
1. 构建模型
class model_rnn(nn.Module):
def __init__(self):
super(model_rnn, self).__init__()
self.rnn0 = nn.RNN(input_size=13, hidden_size=200,
num_layers=1, batch_first=True)
self.fc0 = nn.Linear(200, 50)
self.fc1 = nn.Linear(50, 2)
def forward(self, x):
out, hidden1 = self.rnn0(x)
out = self.fc0(out)
out = self.fc1(out)
return out
model = model_rnn().to(device)
model
model_rnn(
(rnn0): RNN(13, 200, batch_first=True)
(fc0): Linear(in_features=200, out_features=50, bias=True)
(fc1): Linear(in_features=50, out_features=2, bias=True)
)
model(torch.rand(30,13).to(device)).shape
torch.Size([30, 2])
2. 定义训练函数
# 训练循环
def train(dataloader, model, loss_fn, optimizer):
size = len(dataloader.dataset)
num_batches = len(dataloader)
train_loss, train_acc = 0, 0
for X, y in dataloader:
X, y = X.to(device), y.to(device)
pred = model(X)
loss = loss_fn(pred, y)
optimizer.zero_grad()
loss.backward()
optimizer.step()
train_acc += (pred.argmax(1) == y).type(torch.float).sum().item()
train_loss += loss.item()
train_acc /= size
train_loss /= num_batches
return train_acc, train_loss
3.定义测试函数
def test (dataloader, model, loss_fn):
size = len(dataloader.dataset)
num_batches = len(dataloader)
test_loss, test_acc = 0, 0
with torch.no_grad():
for imgs, target in dataloader:
imgs, target = imgs.to(device), target.to(device)
target_pred = model(imgs)
loss = loss_fn(target_pred, target)
test_loss += loss.item()
test_acc += (target_pred.argmax(1) == target).type(torch.float).sum().item()
test_acc /= size
test_loss /= num_batches
return test_acc, test_loss
4. 正式训练模型
loss_fn = nn.CrossEntropyLoss()
learn_rate = 1e-4
opt = torch.optim.Adam(model.parameters(), lr=learn_rate)
epochs = 50
train_loss = []
train_acc = []
test_loss = []
test_acc = []
for epoch in range(epochs):
model.train()
epoch_train_acc, epoch_train_loss = train(train_dl, model, loss_fn, opt)
model.eval()
epoch_test_acc, epoch_test_loss = test(test_dl, model, loss_fn)
train_acc.append(epoch_train_acc)
train_loss.append(epoch_train_loss)
test_acc.append(epoch_test_acc)
test_loss.append(epoch_test_loss)
lr = opt.state_dict()['param_groups'][0]['lr']
template = ('Epoch:{:2d}, Train_acc:{:.1f}%, Train_loss:{:.3f}, Test_acc:{:.1f}%, Test_loss:{:.3f}, Lr:{:.2E}')
print(template.format(epoch+1, epoch_train_acc*100, epoch_train_loss,
epoch_test_acc*100, epoch_test_loss, lr))
print("=" * 20, 'Done', "=" * 20)
Epoch: 1, Train_acc:41.9%, Train_loss:0.699, Test_acc:61.3%, Test_loss:0.679, Lr:1.00E-04
==================== Done ====================
Epoch: 2, Train_acc:48.9%, Train_loss:0.685, Test_acc:67.7%, Test_loss:0.664, Lr:1.00E-04
==================== Done ====================
Epoch: 3, Train_acc:59.6%, Train_loss:0.673, Test_acc:67.7%, Test_loss:0.649, Lr:1.00E-04
==================== Done ====================
Epoch: 4, Train_acc:69.5%, Train_loss:0.661, Test_acc:83.9%, Test_loss:0.635, Lr:1.00E-04
==================== Done ====================
Epoch: 5, Train_acc:74.6%, Train_loss:0.649, Test_acc:83.9%, Test_loss:0.622, Lr:1.00E-04
==================== Done ====================
Epoch: 6, Train_acc:77.6%, Train_loss:0.637, Test_acc:87.1%, Test_loss:0.608, Lr:1.00E-04
==================== Done ====================
Epoch: 7, Train_acc:80.1%, Train_loss:0.625, Test_acc:87.1%, Test_loss:0.595, Lr:1.00E-04
==================== Done ====================
Epoch: 8, Train_acc:81.6%, Train_loss:0.613, Test_acc:83.9%, Test_loss:0.581, Lr:1.00E-04
==================== Done ====================
Epoch: 9, Train_acc:81.6%, Train_loss:0.600, Test_acc:83.9%, Test_loss:0.568, Lr:1.00E-04
==================== Done ====================
Epoch:10, Train_acc:81.6%, Train_loss:0.588, Test_acc:83.9%, Test_loss:0.555, Lr:1.00E-04
==================== Done ====================
Epoch:11, Train_acc:82.4%, Train_loss:0.575, Test_acc:83.9%, Test_loss:0.542, Lr:1.00E-04
==================== Done ====================
Epoch:12, Train_acc:83.1%, Train_loss:0.561, Test_acc:83.9%, Test_loss:0.529, Lr:1.00E-04
==================== Done ====================
Epoch:13, Train_acc:83.5%, Train_loss:0.548, Test_acc:83.9%, Test_loss:0.517, Lr:1.00E-04
==================== Done ====================
Epoch:14, Train_acc:83.8%, Train_loss:0.534, Test_acc:83.9%, Test_loss:0.506, Lr:1.00E-04
==================== Done ====================
Epoch:15, Train_acc:83.5%, Train_loss:0.520, Test_acc:83.9%, Test_loss:0.495, Lr:1.00E-04
==================== Done ====================
Epoch:16, Train_acc:83.5%, Train_loss:0.505, Test_acc:83.9%, Test_loss:0.486, Lr:1.00E-04
==================== Done ====================
Epoch:17, Train_acc:83.5%, Train_loss:0.491, Test_acc:83.9%, Test_loss:0.478, Lr:1.00E-04
==================== Done ====================
Epoch:18, Train_acc:83.8%, Train_loss:0.477, Test_acc:83.9%, Test_loss:0.471, Lr:1.00E-04
==================== Done ====================
Epoch:19, Train_acc:83.5%, Train_loss:0.463, Test_acc:83.9%, Test_loss:0.465, Lr:1.00E-04
==================== Done ====================
Epoch:20, Train_acc:83.1%, Train_loss:0.450, Test_acc:80.6%, Test_loss:0.459, Lr:1.00E-04
==================== Done ====================
Epoch:21, Train_acc:83.5%, Train_loss:0.437, Test_acc:80.6%, Test_loss:0.454, Lr:1.00E-04
==================== Done ====================
Epoch:22, Train_acc:83.8%, Train_loss:0.424, Test_acc:80.6%, Test_loss:0.449, Lr:1.00E-04
==================== Done ====================
Epoch:23, Train_acc:84.2%, Train_loss:0.412, Test_acc:80.6%, Test_loss:0.442, Lr:1.00E-04
==================== Done ====================
Epoch:24, Train_acc:84.2%, Train_loss:0.399, Test_acc:83.9%, Test_loss:0.434, Lr:1.00E-04
==================== Done ====================
Epoch:25, Train_acc:84.2%, Train_loss:0.387, Test_acc:87.1%, Test_loss:0.426, Lr:1.00E-04
==================== Done ====================
Epoch:26, Train_acc:84.6%, Train_loss:0.376, Test_acc:87.1%, Test_loss:0.419, Lr:1.00E-04
==================== Done ====================
Epoch:27, Train_acc:85.3%, Train_loss:0.365, Test_acc:87.1%, Test_loss:0.412, Lr:1.00E-04
==================== Done ====================
Epoch:28, Train_acc:86.0%, Train_loss:0.354, Test_acc:87.1%, Test_loss:0.406, Lr:1.00E-04
==================== Done ====================
Epoch:29, Train_acc:85.3%, Train_loss:0.343, Test_acc:87.1%, Test_loss:0.401, Lr:1.00E-04
==================== Done ====================
Epoch:30, Train_acc:87.1%, Train_loss:0.333, Test_acc:87.1%, Test_loss:0.397, Lr:1.00E-04
==================== Done ====================
Epoch:31, Train_acc:87.5%, Train_loss:0.324, Test_acc:87.1%, Test_loss:0.394, Lr:1.00E-04
==================== Done ====================
Epoch:32, Train_acc:88.2%, Train_loss:0.314, Test_acc:87.1%, Test_loss:0.390, Lr:1.00E-04
==================== Done ====================
Epoch:33, Train_acc:88.2%, Train_loss:0.306, Test_acc:87.1%, Test_loss:0.388, Lr:1.00E-04
==================== Done ====================
Epoch:34, Train_acc:88.6%, Train_loss:0.297, Test_acc:87.1%, Test_loss:0.386, Lr:1.00E-04
==================== Done ====================
Epoch:35, Train_acc:89.0%, Train_loss:0.289, Test_acc:87.1%, Test_loss:0.384, Lr:1.00E-04
==================== Done ====================
Epoch:36, Train_acc:88.6%, Train_loss:0.282, Test_acc:87.1%, Test_loss:0.384, Lr:1.00E-04
==================== Done ====================
Epoch:37, Train_acc:89.0%, Train_loss:0.274, Test_acc:83.9%, Test_loss:0.384, Lr:1.00E-04
==================== Done ====================
Epoch:38, Train_acc:89.3%, Train_loss:0.267, Test_acc:80.6%, Test_loss:0.385, Lr:1.00E-04
==================== Done ====================
Epoch:39, Train_acc:89.3%, Train_loss:0.261, Test_acc:80.6%, Test_loss:0.386, Lr:1.00E-04
==================== Done ====================
Epoch:40, Train_acc:90.1%, Train_loss:0.254, Test_acc:80.6%, Test_loss:0.389, Lr:1.00E-04
==================== Done ====================
Epoch:41, Train_acc:90.1%, Train_loss:0.247, Test_acc:80.6%, Test_loss:0.392, Lr:1.00E-04
==================== Done ====================
Epoch:42, Train_acc:90.1%, Train_loss:0.240, Test_acc:80.6%, Test_loss:0.396, Lr:1.00E-04
==================== Done ====================
Epoch:43, Train_acc:90.1%, Train_loss:0.234, Test_acc:77.4%, Test_loss:0.400, Lr:1.00E-04
==================== Done ====================
Epoch:44, Train_acc:90.4%, Train_loss:0.227, Test_acc:77.4%, Test_loss:0.404, Lr:1.00E-04
==================== Done ====================
Epoch:45, Train_acc:91.9%, Train_loss:0.221, Test_acc:77.4%, Test_loss:0.409, Lr:1.00E-04
==================== Done ====================
Epoch:46, Train_acc:93.4%, Train_loss:0.214, Test_acc:77.4%, Test_loss:0.414, Lr:1.00E-04
==================== Done ====================
Epoch:47, Train_acc:93.4%, Train_loss:0.208, Test_acc:77.4%, Test_loss:0.418, Lr:1.00E-04
==================== Done ====================
Epoch:48, Train_acc:93.8%, Train_loss:0.202, Test_acc:77.4%, Test_loss:0.422, Lr:1.00E-04
==================== Done ====================
Epoch:49, Train_acc:93.8%, Train_loss:0.197, Test_acc:77.4%, Test_loss:0.426, Lr:1.00E-04
==================== Done ====================
Epoch:50, Train_acc:94.5%, Train_loss:0.191, Test_acc:77.4%, Test_loss:0.429, Lr:1.00E-04
==================== Done ====================
四、模型评估
1. Loss与Accuracy图
import matplotlib.pyplot as plt
from datetime import datetime
#隐藏警告
import warnings
warnings.filterwarnings("ignore") #忽略警告信息
current_time = datetime.now() # 获取当前时间
plt.rcParams['font.sans-serif'] = ['SimHei'] # 用来正常显示中文标签
plt.rcParams['axes.unicode_minus'] = False # 用来正常显示负号
plt.rcParams['figure.dpi'] = 200 #分辨率
epochs_range = range(epochs)
plt.figure(figsize=(12, 3))
plt.subplot(1, 2, 1)
plt.plot(epochs_range, train_acc, label='Training Accuracy')
plt.plot(epochs_range, test_acc, label='Test Accuracy')
plt.legend(loc='lower right')
plt.title('Training and Validation Accuracy')
plt.xlabel(current_time) # 打卡请带上时间戳,否则代码截图无效
plt.subplot(1, 2, 2)
plt.plot(epochs_range, train_loss, label='Training Loss')
plt.plot(epochs_range, test_loss, label='Test Loss')
plt.legend(loc='upper right')
plt.title('Training and Validation Loss')
plt.show()
2. 混淆矩阵
print("==============输入数据Shape为==============")
print("X_test.shape:",X_test.shape)
print("y_test.shape:",y_test.shape)
pred = model(X_test.to(device)).argmax(1).cpu().numpy()
print("\n==============输出数据Shape为==============")
print("pred.shape:",pred.shape)
输入数据Shape为
X_test.shape: torch.Size([31, 13])
y_test.shape: torch.Size([31])
输出数据Shape为
pred.shape: (31,)
import numpy as np
import matplotlib.pyplot as plt
from sklearn.metrics import confusion_matrix, ConfusionMatrixDisplay
# 计算混淆矩阵
cm = confusion_matrix(y_test, pred)
plt.figure(figsize=(6,5))
plt.suptitle('')
sns.heatmap(cm, annot=True, fmt="d", cmap="Blues")
# 修改字体大小
plt.xticks(fontsize=10)
plt.yticks(fontsize=10)
plt.title("Confusion Matrix", fontsize=12)
plt.xlabel("Predicted Label", fontsize=10)
plt.ylabel("True Label", fontsize=10)
# 显示图
plt.tight_layout() # 调整布局防止重叠
plt.show()
3. 调用模型进行预测
test_X = X_test[0].reshape(1, -1) # X_test[0]即我们的输入数据
pred = model(test_X.to(device)).argmax(1).item()
print("模型预测结果为:",pred)
print("=="*20)
print("0:不会患心脏病")
print("1:可能患心脏病")
模型预测结果为: 0
========================================
0:不会患心脏病
1:可能患心脏病
五、总结
本周主要学习了LSTM和RNN,通过实践项目更加深入地了解了RNN模型的结构。