BP神經網絡在阿爾茲海默癥診斷中的應用

Source:2022 Shuwei cup problem C
co-author:Chen Wang(BISTU)?;Wanlong Yu(YNU); Zhusheng Guo(XMU)
符號說明(symbol description):


Forward propagation:



交叉熵損失函數(cross entropy loss):

模型求解:Adam優(yōu)化器

訓練過程可視化:

對應代碼:
預裝庫:【torch;?sklearn;pandas】
import numpy as np
import pandas as pd
import torch
import matplotlib.pyplot as plt
from sklearn import preprocessing
import torch.nn.functional as func
from sklearn.model_selection import train_test_split
"""
Importing CSV files
"""
df=pd.read_excel(’../data/BP_input_raw_data.xlsx’, index_col=None)
dataset=np.array(df)
"""
Importing multidimensional feature data of patients
"""
data=dataset[:,0:9]
data=preprocessing.scale(data)
scaler = preprocessing.MinMaxScaler()
scaler.fit_transform(data.T)
labels=dataset[:,9]
X_train,X_test,Y_train,Y_test=train_test_split(data,labels,test_size=0.4)
TrainData=torch.FloatTensor(X_train)#trainning dataset
TrainLablel=torch.LongTensor(Y_train)#trainning labels
TestData=torch.FloatTensor(X_test)#testing dataset
TestLabel=torch.LongTensor(Y_test)#testing
"""
Model Preparation
"""
AlzheimersdDiagnosis=torch.nn.Sequential(torch.nn.Linear(9, 64),
torch.nn.ReLU(),
torch.nn.Linear(64, 32),
torch.nn.ReLU(),
torch.nn.Linear(32, 16),
torch.nn.ReLU(),
torch.nn.Linear(16,8),
torch.nn.ReLU(),
torch.nn.Linear(8,3),
)
optimizer=torch.optim.Adagrad(AlzheimersdDiagnosis.parameters(),lr=0.001)
lossfuc=torch.nn.CrossEntropyLoss()
def AccuracyCount(out,labels):
prediction = torch.max(out,1)[1]
pred_y = prediction.data.numpy()
target_y = labels.data.numpy()
accuracy = float((pred_y == target_y).astype(int).sum()) /
float(target_y.size)
return accuracy
"""
Trainning
"""
train_acc=[]
train_loss=[]
test_acc_per_epoch=[]
for t in range(4500):
out=AlzheimersdDiagnosis(TrainData)#forward Propagation
out_test_per_epoch=AlzheimersdDiagnosis(TestData)
loss=lossfuc(out,TrainLablel)
train_loss.append(loss)
accuracy=AccuracyCount(out,TrainLablel)
accuracy_test_per_epoch=AccuracyCount(out_test_per_epoch, TestLabel)
test_acc_per_epoch.append(accuracy_test_per_epoch)
train_acc.append(accuracy)
optimizer.zero_grad()
loss.backward()#reverse Propagation
optimizer.step()#optimization
"""
Testing
"""
out_test=AlzheimersdDiagnosis(TestData)
test_acc=AccuracyCount(out_test,TestLabel)
print("The accuracy is ",test_acc)
"""
Plotting training loss, training accuracy curve
"""
img_trainloss=[]
img_trainacc=[]
img_test_acc_per_epoch=[]
for i in range(4500):
img_trainloss.append((train_loss[i].data).item())
for j in range(4500):
img_trainacc.append((train_acc[j]))
for k in range(4500):
img_test_acc_per_epoch.append((test_acc_per_epoch[k]))
fig, ax = plt.subplots(figsize=(12,6))
ax.plot(img_trainloss,color="red", lw=2, ls=’-.’,label="train loss")
ax.plot(img_trainacc,color="blue", linewidth=1,label="train accuracy")
ax.plot(img_test_acc_per_epoch,color="yellow",lw=1.5,ls=’--’,label="test
accuracy of every epoch")
ax.grid(True)
ax.set_xlabel(’epoch’)
ax.set_ylabel(’acc/loss’)
ax.set_title(’The process of trainning’)
ax.legend(loc="upper right")

敏感性分析:

對應代碼:
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
import random
from sklearn import preprocessing
import torch
def AccuracyCount(out, labels):
prediction = torch.max(out, 1)[1]
pred_y = prediction.data.numpy()
target_y = labels.data.numpy()
accuracy = float((pred_y == target_y).astype(int).sum()) /
float(target_y.size)
return accuracy
df = pd.read_excel("../data/BP_input_raw_data.xlsx", index_col=None)
dataset = np.array(df)
data = dataset[:, 0:9]
labels = dataset[:, 9]
noise_test_accuracy = []
Noise_test_model = torch.load("../data/AlzheimersdDiagnosis_model.path")
Noise_test_model.eval()
Noise_range = [
0.05,
0.1,
0.15,
0.2,
0.25,
0.3,
0.35,
0.4,
0.45,
0.5,
0.55,
0.6,
0.65,
0.7,
0.75,
8,
0.85,
0.9,
0.95,
]
for noise_percent in Noise_range:
for i in range(data.shape[0]):
for j in range(data.shape[1]):
data[i, j] = data[i, j] + data[i, j] * random.uniform(
-1 * noise_percent, noise_percent
)
data = preprocessing.scale(data)
out_test_noise = Noise_test_model(torch.FloatTensor(data))
Noise_accuracy = AccuracyCount(out_test_noise, torch.LongTensor(labels))
noise_test_accuracy.append(Noise_accuracy)
"""
plot the accuracy
"""
x = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17,
18, 19])
fig, ax = plt.subplots(figsize=(12, 5))
ax.plot(
x,
noise_test_accuracy,
color="purple",
lw=1,
ls="--",
marker="s",
markersize=8,
markerfacecolor="yellow",
markeredgewidth=2,
markeredgecolor="blue",
)
ax.set_xlabel("noise degree")
ax.set_ylabel("accuracy of trained model")
# plt.ylim(0.65,0.8)
ax.set_title("Sensitivity analysis of BP neural network")
ax.set_xticklabels(
[
r"5%",
r"10%",
r"15%",
r"20%",
r"25%",
r"30%",
r"35%",
r"40%",
r"45%",
r"50%",
r"55%",
r"60%",
r"65%",
r"70%",
r"75%",
r"80%",
r"85%",
r"90%",
r"95%",
],
fontsize=10,
)
ax.set_xticks(x[::])
ax.grid(True)
# print("Under the ",error_percent,"percent noise, the accuracy of
test",Noise_accuracy)