当前位置:   article > 正文

玩转ChatGPT:Transformer分类模型_transform分类

transform分类

一、写在前面

之前,我们介绍了Sklearn包以及Boost辈的各种分类模型,这些模型都很经典了,实际上也很强大,比如说Xgboost。

然而,近期随着ChatGPT的大火,其底层框架Transformer也逐渐火了,现在大家朝着通用人工智能的方向靠拢。因此,我在想,尝试使用Transformer框架做一做临床诊断/预后问题(本质上就是分类问题),看看效果如何。

首先使用GPT简单科普Transformer框架:

二、尝鲜过程

实际上,我对于Transformer框架一概不知。所以,无脑求助GPT。

(1)用TA提供一个基础代码:

咒语:我有一个<临床数据>,其中“X1”列表示病人的预后(0表示生存,1表示死亡),其余列“X3”至“X19”表示临床自变量。

我需要写一个Transformer框架做分类的代码,使用“X3”至“X19”这17个自变量去预测“X1”,请用python帮我写出代码。

数据格式如下:

<

X1 X3 X4 X5 X6 X7 X8 X9 X10 X11 X12 X13 X14 X15 X16 X17 X18 X19

0 0 0 0 0 0 0 2.17 22 6.5 295 1.16 0.08 3.52 224 0.33 0 82.6

0 1 0 0 0 1 0 1.17 23 24.3 261 0.79 0.04 3.75 59 0.27 0.02 53.3

0 0 0 0 0 0 0 1.82 63.99 3.9 460.67 1.2 0.05 4.8 21 0.07 0 38.3

1 0 0 0 1 1 0 2.13 63.99 15.7 401.3 1.24 0.12 5.1 147 0.9 0.01 80.84

>

回复如下:

首先得安装PyTorch,具体安装教程就不说了。算了,还是说一下:

(a)打开Anaconda Prompt;

(b)打开Pytorch官网(https://pytorch.org/)寻找安装命令代码,根据自己的需求选择安装条件,比如我的LOW电脑没有GPU,只能用CPU;

(c)在Anaconda Prompt输入代码安装(记得切换好安装环境):

conda install pytorch torchvision torchaudio cpuonly -c pytorch

回车安装即可。

再附上GPT给的代码:

  1. import torch
  2. import torch.nn as nn
  3. import torch.optim as optim
  4. from sklearn.model_selection import train_test_split
  5. from sklearn.preprocessing import StandardScaler
  6. import pandas as pd
  7. import numpy as np
  8. # 加载数据
  9. data = pd.read_csv('your_data.csv')  # 请用您数据的实际路径替换 'your_data.csv'
  10. X = data.loc[:, 'X3':'X19'].values
  11. y = data['X1'].values
  12. # 数据预处理
  13. scaler = StandardScaler()
  14. X = scaler.fit_transform(X)
  15. # 划分数据集
  16. X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
  17. # 创建PyTorch数据加载器
  18. train_data = torch.utils.data.TensorDataset(torch.Tensor(X_train), torch.Tensor(y_train))
  19. test_data = torch.utils.data.TensorDataset(torch.Tensor(X_test), torch.Tensor(y_test))
  20. train_loader = torch.utils.data.DataLoader(train_data, batch_size=32, shuffle=True)
  21. test_loader = torch.utils.data.DataLoader(test_data, batch_size=32, shuffle=True)
  22. # 定义设备
  23. device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
  24. # 创建Transformer模型
  25. class TransformerModel(nn.Module):
  26.     def __init__(self, input_dim, output_dim, nhead=1):
  27.         super(TransformerModel, self).__init__()
  28.         self.encoder = nn.TransformerEncoder(
  29.             nn.TransformerEncoderLayer(d_model=input_dim, nhead=nhead),
  30.             num_layers=3
  31.         )
  32.         self.classifier = nn.Linear(input_dim, output_dim)
  33.     def forward(self, x):
  34.         x = self.encoder(x)
  35.         x = self.classifier(x)
  36.         return x
  37. # 初始化模型和优化器
  38. model = TransformerModel(input_dim=X.shape[1], output_dim=2).to(device)
  39. optimizer = optim.Adam(model.parameters(), lr=0.001)
  40. criterion = nn.CrossEntropyLoss()
  41. # 训练模型
  42. for epoch in range(100):
  43.     for i, (inputs, labels) in enumerate(train_loader):
  44.         inputs, labels = inputs.to(device), labels.to(device)
  45.         
  46.         optimizer.zero_grad()
  47.         
  48.         outputs = model(inputs)
  49.         loss = criterion(outputs, labels.long())
  50.         
  51.         loss.backward()
  52.         optimizer.step()
  53.     print(f'Epoch {epoch+1} / 100: Average Loss: {loss.item()}')

(2)模型需要计算性能参数

写咒语:我还需要做得出模型在训练集和测试集上的性能指标,包括灵敏度、特异度和AUC值,帮我更新代码。

TA又更新了代码(只显示更新的部分):

  1. # 训练模型
  2. for epoch in range(20):
  3.     for i, (inputs, labels) in enumerate(train_loader):
  4.         inputs, labels = inputs.to(device), labels.to(device)
  5.         optimizer.zero_grad()
  6.         outputs = model(inputs)
  7.         loss = criterion(outputs, labels.long())
  8.         loss.backward()
  9.         optimizer.step()
  10.     # 验证模型
  11.     model.eval()
  12.     with torch.no_grad():
  13.         # 训练集
  14.         predictions = []
  15.         truths = []
  16.         for inputs, labels in train_loader:
  17.             inputs, labels = inputs.to(device), labels.to(device)
  18.             output = model(inputs)
  19.             _, predicted = torch.max(output.data, 1)
  20.             predictions += predicted.tolist()
  21.             truths += labels.tolist()
  22.         tn, fp, fn, tp = confusion_matrix(truths, predictions).ravel()
  23.         sensitivity = tp / (tp + fn)
  24.         specificity = tn / (tn + fp)
  25.         auc = roc_auc_score(truths, predictions)
  26.         #print(f'Epoch {epoch+1} / 100, Training: Sensitivity: {sensitivity}, Specificity: {specificity}, AUC: {auc}')
  27.  # 测试集
  28.         predictions = []
  29.         truths = []
  30.         for inputs, labels in test_loader:
  31.             inputs, labels = inputs.to(device), labels.to(device)
  32.             output = model(inputs)
  33.             _, predicted = torch.max(output.data, 1)
  34.             predictions += predicted.tolist()
  35.             truths += labels.tolist()
  36.         tn, fp, fn, tp = confusion_matrix(truths, predictions).ravel()
  37.         sensitivity = tp / (tp + fn)
  38.         specificity = tn / (tn + fp)
  39.         auc = roc_auc_score(truths, predictions)
  40.         print(f'Epoch {epoch+1} / 100, Testing: Sensitivity: {sensitivity}, Specificity: {specificity}, AUC: {auc}')

把数据带进去运行试试:

可以看到,应该还是有一些过拟合的现象。

(3)继续施咒,如何改善过拟合。

施咒:上述的代码运行后出现过拟合,如何改善?

总体的挺到位的。我们来看看:

数据量就不考虑了,我也想要更多数据。更简单的模型,说的有道理,不过现在是测试这个面模型的性能。早停法也不适合,因为从20次迭代来看,都是过拟合的,你怎么样TA停,同样,交叉验证也是不可行。所以,试试其他的办法。

(4)增加正则化。

咒语:如何在代码中增加正则化项,请帮我改写相关代码段。

然后我加进去了,没啥效果。这是L2正则化,然后又问了L1正则化:

同样效果也不明显。

(5)减少模型复杂度。

咒语:如何做到“减少编码器层的数量、减少自注意力机制的头数或降低前馈神经网络的隐藏单元数量”,请分别修改代码段展示。

GPT注意给了回复,这里我一起展示:

  1. # 创建Transformer模型
  2. class TransformerModel(nn.Module):
  3.     def __init__(self, input_dim, output_dim, nhead=1):
  4.         super(TransformerModel, self).__init__()
  5.         self.encoder = nn.TransformerEncoder(
  6.             nn.TransformerEncoderLayer(d_model=input_dim, nhead=nhead),
  7.             num_layers=int(0.5 * input_dim) # 减少层数
  8.         )
  9.         self.norm = nn.BatchNorm1d(input_dim)  # 添加批归一化层        
  10.         self.classifier = nn.Linear(input_dim, output_dim)
  11.         self.dropout = nn.Dropout(1)  # 添加一个dropout层  

然后,效果也是不明显。

看来过拟合不好解决,同时我们可以看到,Sensitivity都是比Specificity要小的,这也可以理解,因为这是一个不平衡数据(正比负是1:3),下一步考虑从这一点入手。

(6)处理不平衡数据。

咒语:Sensitivity远远比Specificity要小,请问如何改进代码?

我们来分析下:重采样就算了,尽量不要动原始数据。第三个看不懂。所以,试试改变阈值。

(7)改变阈值。

咒语:改代码中如何改变阈值?请修改相应代码段。

代码如下:

  1. # 验证模型
  2. model.eval()
  3. with torch.no_grad():
  4.     # 测试集
  5.     predictions = []
  6.     truths = []
  7.     for inputs, labels in test_loader:
  8.         inputs, labels = inputs.to(device), labels.to(device)
  9.         output = model(inputs)
  10.         # 将输出结果用 softmax 函数转换为概率
  11.         probabilities = torch.nn.functional.softmax(output, dim=1)
  12.         # 调整阈值,例如设定阈值为 0.3
  13.         threshold = 0.3
  14.         predicted = (probabilities[:, 1] > threshold).long()
  15.         predictions += predicted.tolist()
  16.         truths += labels.tolist()
  17.     tn, fp, fn, tp = confusion_matrix(truths, predictions).ravel()
  18.     sensitivity = tp / (tp + fn)
  19.     specificity = tn / (tn + fp)
  20.     auc = roc_auc_score(truths, predictions)
  21.     print(f'Epoch {epoch+1} / 100, Testing: Sensitivity: {sensitivity}, Specificity: {specificity}, AUC: {auc}')

当然值变动了测试集,我们来试试效果,阈值改成0.3:

可以看到,特异度的份额被分到了灵敏度了,总体的AUC值变化不大。因此,性能基本上也就这样了。

最终的代码:

  1. import torch
  2. import torch.nn as nn
  3. import torch.optim as optim
  4. from sklearn.model_selection import train_test_split
  5. from sklearn.preprocessing import StandardScaler
  6. from sklearn.metrics import confusion_matrix, roc_auc_score
  7. import pandas as pd
  8. import numpy as np
  9. # 加载数据
  10. data = pd.read_csv('Entry model3.csv')  # 请用您数据的实际路径替换 'your_data.csv'
  11. X = data.loc[:, 'X3':'X19'].values
  12. y = data['X1'].values
  13. # 数据预处理
  14. scaler = StandardScaler()
  15. X = scaler.fit_transform(X)
  16. # 划分数据集
  17. X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=2338)
  18. # 创建PyTorch数据加载器
  19. train_data = torch.utils.data.TensorDataset(torch.Tensor(X_train), torch.Tensor(y_train))
  20. test_data = torch.utils.data.TensorDataset(torch.Tensor(X_test), torch.Tensor(y_test))
  21. train_loader = torch.utils.data.DataLoader(train_data, batch_size=32, shuffle=True)
  22. test_loader = torch.utils.data.DataLoader(test_data, batch_size=32, shuffle=True)
  23. # 定义设备
  24. device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
  25. # 创建Transformer模型
  26. class TransformerModel(nn.Module):
  27.     def __init__(self, input_dim, output_dim, nhead=1):
  28.         super(TransformerModel, self).__init__()
  29.         self.encoder = nn.TransformerEncoder(
  30.             nn.TransformerEncoderLayer(d_model=input_dim, nhead=nhead),
  31.             num_layers=int(0.5 * input_dim) # 减少层数
  32.         )
  33.         self.norm = nn.BatchNorm1d(input_dim)  # 添加批归一化层        
  34.         self.classifier = nn.Linear(input_dim, output_dim)
  35.         self.dropout = nn.Dropout(1)  # 添加一个dropout层        
  36.     def forward(self, x):
  37.         x = self.encoder(x)
  38.         x = self.classifier(x)
  39.         return x
  40. # 初始化模型和优化器
  41. model = TransformerModel(input_dim=X.shape[1], output_dim=2).to(device)
  42. optimizer = optim.Adam(model.parameters(), lr=0.0001, weight_decay=1e-5)
  43. criterion = nn.CrossEntropyLoss()
  44. # 训练模型
  45. for epoch in range(20):
  46.     for i, (inputs, labels) in enumerate(train_loader):
  47.         inputs, labels = inputs.to(device), labels.to(device)
  48.         
  49.         optimizer.zero_grad()
  50.         
  51.         outputs = model(inputs)
  52.         loss = criterion(outputs, labels.long())
  53.         # 添加L1正则化
  54.         #l1_lambda = 0.001
  55.         #l1_norm = sum(p.abs().sum() for p in model.parameters())
  56.         #loss = loss + l1_lambda * l1_norm
  57.         
  58.         loss.backward()
  59.         optimizer.step()
  60.     
  61.     # 验证模型
  62.     model.eval()
  63.     with torch.no_grad():
  64.         # 训练集
  65.         predictions = []
  66.         truths = []
  67.         for inputs, labels in train_loader:
  68.             inputs, labels = inputs.to(device), labels.to(device)
  69.             output = model(inputs)
  70.             _, predicted = torch.max(output.data, 1)
  71.             predictions += predicted.tolist()
  72.             truths += labels.tolist()
  73.         
  74.         tn, fp, fn, tp = confusion_matrix(truths, predictions).ravel()
  75.         sensitivity = tp / (tp + fn)
  76.         specificity = tn / (tn + fp)
  77.         auc = roc_auc_score(truths, predictions)
  78.         #print(f'Epoch {epoch+1} / 100, Training: Sensitivity: {sensitivity}, Specificity: {specificity}, AUC: {auc}')
  79.         
  80.         # 测试集
  81.         predictions = []
  82.         truths = []
  83.         for inputs, labels in test_loader:
  84.             inputs, labels = inputs.to(device), labels.to(device)
  85.             output = model(inputs)
  86.             # 将输出结果用 softmax 函数转换为概率
  87.             probabilities = torch.nn.functional.softmax(output, dim=1)
  88.             # 调整阈值,例如设定阈值为 0.3
  89.             threshold = 0.3
  90.             predicted = (probabilities[:, 1] > threshold).long()
  91.             predictions += predicted.tolist()
  92.             truths += labels.tolist()
  93.         tn, fp, fn, tp = confusion_matrix(truths, predictions).ravel()
  94.         sensitivity = tp / (tp + fn)
  95.         specificity = tn / (tn + fp)
  96.         auc = roc_auc_score(truths, predictions)
  97.         print(f'Epoch {epoch+1} / 100, Testing: Sensitivity: {sensitivity}, Specificity: {specificity}, AUC: {auc}')

三、总结

以上,Transformer框架能解决分类问题。不过在这个例子中,性能不太好,可能是因为数据量太小了吧(400多例而已)。反而,同样的数据,Xgboost略胜一筹(AUC:0.75),所以有时候,合适的模型才是最好的。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家自动化/article/detail/596593
推荐阅读
相关标签
  

闽ICP备14008679号