因为我每次初始化不同的模型表现不同,我猜测可能是过拟合,于是我决定根据第一层神经网络的权重来选择特征。代码:
import matplotlib.pyplot as plt import numpy as np import pandas as pd import torch import torch.fft as fft df = pd.read_csv('train.csv') df=df.drop(['ID'],axis=1) nmp=df.to_numpy() feature=nmp[:-20,:-1] label=nmp[:-20,-1]#(210,240) feature=torch.fft.fft(torch.Tensor(feature)) feature=torch.abs(feature)/240*2 feature=feature[:,[0, 1, 60, 180, 239, 58, 59, 61, 179, 181, 182, 120, 62, 178, 119, 121, 117, 123, 2, 238, 55, 65, 175, 185, 63, 116, 124, 177, 118, 122, 56, 64, 176, 184, 57, 183]] test_feature=nmp[-20:,:-1] test_label=nmp[-20:,-1]#(210,240)test_feature=torch.fft.fft(torch.Tensor(test_feature)) test_feature=torch.abs(test_feature)/240*2 test_feature=test_feature[:,[0, 1, 60, 180, 239, 58, 59, 61, 179, 181, 182, 120, 62, 178, 119, 121, 117, 123, 2, 238, 55, 65, 175, 185, 63, 116, 124, 177, 118, 122, 56, 64, 176, 184, 57, 183]] from torch import nn import torch loss=nn.MSELoss() feature=torch.Tensor(feature) label=torch.Tensor(label) label=label.reshape(-1,1)
test_feature=torch.Tensor(test_feature) test_label=torch.Tensor(test_label) test_label=test_label.reshape(-1,1) network=nn.Sequential(nn.Linear(36,2),nn.Sigmoid(),nn.Linear(2,2),nn.Sigmoid(),nn.Linear(2,1),nn.Sigmoid()) #network=nn.Sequential(nn.Linear(36,2),nn.Sigmoid(),nn.Linear(2,2),nn.Sigmoid(),nn.Linear(2,2),nn.Sigmoid(),nn.Linear(2,1),nn.Sigmoid()) #network=nn.Sequential(nn.Linear(36,2),nn.Sigmoid(),nn.Linear(2,1),nn.Sigmoid()) #network=nn.Sequential(nn.Linear(36,4),nn.Sigmoid(),nn.Linear(4,1),nn.Sigmoid()) import torch.optim as optim optimizer = optim.Adam(network.parameters(), lr=0.004) for epoch in range(100000): optimizer.zero_grad() out=network(feature) l=loss(out,label) l.backward() optimizer.step() Y = torch.ge(out, 0.5).float() acc=Y.eq(label).float().sum()/len(label) out=network(test_feature) Y = torch.ge(out, 0.5).float() test_acc=Y.eq(test_label).float().sum()/len(test_label) print(epoch,l,acc,test_acc) #if test_acc==0.50 and acc>0.93: if acc>=0.82 and test_acc>=0.90: break for k,v in network.named_parameters(): print(k) print(v) 标签:训练,nn,特征,torch,feature,label,神经网络,test,Linear From: https://www.cnblogs.com/hahaah/p/16947029.html