大前端

前端学习之家-大前端

2021 全国研究生数学建模D题 神经网络预测生物活性并可视化

import numpy as np
import pandas as pd
import pylab as pl
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import MinMaxScaler
import torch
import torch.nn.functional as F
from visdom import Visdom
# from torch.autograd import Variable
# import torch.nn as nn
# import torchvision
# import torch.optim as optim
import matplotlib.pyplot as plt
# import time

dataframe = pd.read_excel(r'C:\Users\Shinelon\Desktop\after_feature_select_Molecular.xls', header=0, index_col=0,
                          na_values=np.NaN)

# 保存列名
col_name = dataframe.columns

# minmax标准化
dataframe = MinMaxScaler().fit_transform(dataframe)
dataframe = pd.DataFrame(dataframe)
# min-max 后列名丢失了 重新改名
for i in range(0, dataframe.shape[1]):
    dataframe.rename(columns={i: col_name[i]}, inplace= True)
feature = dataframe.iloc[:, :20]
target = dataframe.iloc[:, 20]

# 划分训练集 和 测试集
feature_train, feature_test, target_train, target_test=train_test_split(feature,  target, train_size=0.8, random_state=0)
# 转为 tensor
feature_train = torch.from_numpy(feature_train.values)  # dataframe 转torch.tensor
feature_test = torch.from_numpy(feature_test.values)
print(feature_train.shape)
print(feature_test.shape)
target_train = torch.from_numpy(target_train.values)  # series 转torch.tensor
target_train = torch.unsqueeze(target_train, dim=1) # 增加一个维度 行向量转为列向量 与 预测值列向量对应
# target_test = torch.from_numpy(target_test.values)
# target_test = torch.unsqueeze(target_test, dim=1)

# 定义一个构建神经网络的类
class Net(torch.nn.Module):  # 继承torch.nn.Module类
    def __init__(self, n_feature, n_hidden, n_output):
        super(Net, self).__init__()  # 获得Net类的超类(父类)的构造方法
        # 定义神经网络的每层结构形式
        # 各个层的信息都是Net类对象的属性
        self.hidden = torch.nn.Linear(n_feature, n_hidden)  # 隐藏层线性输出
        self.predict = torch.nn.Linear(n_hidden, n_output)  # 输出层线性输出

    # 将各层的神经元搭建成完整的神经网络的前向通路
    def forward(self, x):
        x = F.relu(self.hidden(x))  # 对隐藏层的输出进行relu激活
        x = self.predict(x)
        return x

# 定义神经网络
torch.set_default_tensor_type(torch.DoubleTensor)
net = Net(20, 10, 1) #输入层 隐藏层 输出层 结点数
print(net) # 打印输出net的结构
# 定义优化器和损失函数
optimizer = torch.optim.SGD(net.parameters(), lr=0.5) # 传入网络参数和学习率
loss_function = torch.nn.MSELoss() # 最小均方误差


# # 神经网络训练过程
# plt.ion()  # 动态学习过程展示
# plt.show()
training_loss = []
#print(training_loss)
# 训练神经网络 使用训练集训练网络 输入是 feature_train 1579*20  真实目标 feature_target 1579*1
for batch in range(0, 200):
  prediction = net(feature_train) # 把数据x喂给net,输出预测值
  loss = loss_function(prediction, target_train) # 计算两者的误差,要注意两个参数的顺序
  training_loss.append(loss.detach().numpy().copy())   # 保存训练 loss
  optimizer.zero_grad() # 清空上一步的更新参数值
  loss.backward() # 误差反相传播,计算新的更新参数值
  optimizer.step() # 将计算得到的更新值赋给net.parameters()

# 测试集合上测试数据  395*20
prediction =  net(feature_test)
# prediction = prediction.t()
prediction = torch.squeeze(prediction, dim=1)
prediction = prediction.detach().numpy().copy()
#print(prediction)

x1 = range(0, 200)
x2 = range(0, 395)
y1 = training_loss
y2 = prediction
y3 = target_test
plt.subplot(2, 1, 1)
plt.plot(x1, y1, 'r--')
plt.title('Training Loss vs. epoches')
plt.ylabel('Loss')
plt.subplot(2, 1, 2)
plt.plot(x2, y2, 'r--')
plt.plot(x2, y3, 'b-')
plt.legend(["test", "real"])
plt.xlabel('Test Acc per sample')
plt.ylabel('Acc')
plt.show()
# plt.savefig("accuracy_loss.jpg")

发表评论:

Copyright Your WebSite.Some Rights Reserved.