基于深度神经网络的HIV预测模型训练与测试
import torch import torch.nn as nn import pandas as pd import numpy as np import matplotlib.pyplot as plt from sklearn.model_selection import train_test_split from sklearn.metrics import roc_curve, auc
读取数据
data = pd.read_excel('C:\Users\lenovo\Desktop\HIV\DNN神经网络测试\output_data.xlsx') x = data.iloc[:, 1:].values y = data.iloc[:, 0].values
数据归一化
x = (x - x.mean()) / x.std()
划分训练集和测试集
x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.2, random_state=42)
将numpy数组转换为张量
x_train = torch.Tensor(x_train) y_train = torch.Tensor(y_train) x_test = torch.Tensor(x_test) y_test = torch.Tensor(y_test)
定义模型
class DNN(nn.Module): def init(self, input_size, hidden_size, num_classes): super(DNN, self).init() self.fc1 = nn.Linear(input_size, hidden_size) self.fc2 = nn.Linear(hidden_size, hidden_size) self.fc3 = nn.Linear(hidden_size, hidden_size) self.out = nn.Linear(hidden_size, num_classes) self.dropout = nn.Dropout(p=0.5) self.attention = nn.Sequential( nn.Linear(hidden_size, 1), nn.Tanh(), nn.Softmax(dim=1) )
def forward(self, x):
out = torch.relu(self.fc1(x))
out = self.dropout(out)
out = torch.relu(self.fc2(out))
out = self.dropout(out)
out = torch.relu(self.fc3(out))
out = self.dropout(out)
attention_weights = self.attention(out)
out = attention_weights * out
out = self.out(out)
return out
定义超参数
input_size = 16 hidden_size = 128 num_classes = 2 learning_rate = 0.001 num_epochs = 200
初始化模型
model = DNN(input_size, hidden_size, num_classes)
定义损失函数和优化器
criterion = nn.CrossEntropyLoss() optimizer = torch.optim.Adam(model.parameters(), lr=learning_rate)
训练模型
train_loss = [] train_accuracy = [] for epoch in range(num_epochs): # 前向传播和反向传播 outputs = model(x_train) loss = criterion(outputs, y_train.long()) optimizer.zero_grad() loss.backward() optimizer.step()
# 计算准确率
_, predicted = torch.max(outputs.data, 1)
total = y_train.size(0)
correct = (predicted == y_train.long()).sum().item()
accuracy = correct / total
train_accuracy.append(accuracy)
train_loss.append(loss.item())
# 输出训练信息
print('Epoch [{}/{}], Loss: {:.4f}, Accuracy: {:.2f}%'
.format(epoch + 1, num_epochs, loss.item(), accuracy * 100))
输出每个样本的概率
train_prob = torch.softmax(outputs, dim=1)[:, 1] print('训练集每个样本的概率:', train_prob.detach().numpy().reshape(-1, 1))
绘制准确率变化的图
plt.plot(train_accuracy) plt.title('Accuracy') plt.xlabel('Epoch') plt.ylabel('Accuracy') plt.show()
绘制损失变化的图
plt.plot(train_loss) plt.title('Loss') plt.xlabel('Epoch') plt.ylabel('Loss') plt.show()
绘制ROC图
fpr, tpr, threshold = roc_curve(y_train, train_prob.detach().numpy()) roc_auc = auc(fpr, tpr) plt.title('ROC Curve') plt.plot(fpr, tpr, 'b', label='AUC = %0.2f' % roc_auc) plt.legend(loc='lower right') plt.plot([0, 1], [0, 1], 'r--') plt.xlim([0, 1]) plt.ylim([0, 1]) plt.ylabel('True Positive Rate') plt.xlabel('False Positive Rate') plt.show()
测试模型
model.eval() test_outputs = model(x_test) test_loss = criterion(test_outputs, y_test.long())
计算准确率
_, test_predicted = torch.max(test_outputs.data, 1) test_total = y_test.size(0) test_correct = (test_predicted == y_test.long()).sum().item() test_accuracy = test_correct / test_total print('Test Loss: {:.4f}, Accuracy: {:.2f}%'.format(test_loss.item(), test_accuracy * 100))
输出测试集每个样本的概率
test_prob = torch.softmax(test_outputs, dim=1)[:, 1] print('测试集每个样本的概率:', test_prob.detach().numpy().reshape(-1, 1))
绘制测试集ROC图
test_fpr, test_tpr, test_threshold = roc_curve(y_test, test_prob.detach().numpy()) test_roc_auc = auc(test_fpr, test_tpr) plt.title('Test ROC Curve') plt.plot(test_fpr, test_tpr, 'b', label='AUC = %0.2f' % test_roc_auc) plt.legend(loc='lower right') plt.plot([0, 1], [0, 1], 'r--') plt.xlim([0, 1]) plt.ylim([0, 1]) plt.ylabel('True Positive Rate') plt.xlabel('False Positive Rate') plt.show()
原文地址: https://www.cveoy.top/t/topic/nel7 著作权归作者所有。请勿转载和采集!