训练神经网络挑主要特征
因为我每次初始化不同的模型表现不同,我猜测可能是过拟合,于是我决定根据第一层神经网络的权重来选择特征。效果不是很好。
代码:
w1=[ 1.9525, 6.5730, 2.8011, 2.9227, 6.5043, 5.2393, -4.5891,
-7.5931, -7.5758, -4.5269, 5.1999, 8.7490, -5.9541, -5.9677,
13.0731, 13.2785, -0.5240, -0.3436, -5.3938, -5.4939, -8.0415,
9.2427, 9.3492, -7.9147, -10.4178, 11.7772, 11.9082, -10.3413,
6.7089, 6.6122, 8.1158, -9.9511, -9.9453, 7.8804, -8.3686,
-8.0557]
w2= [ 1.3374, 3.5777, 5.2819, 5.2723, 3.9039, 6.2792, -2.1695,
-3.5467, -3.4617, -2.0943, 6.0334, 8.0246, -7.2271, -7.2421,
-7.8121, -7.7906, 2.7177, 2.5098, 5.8960, 5.8361, -11.4560,
11.9718, 11.9299, -11.3580, -3.3174, -12.4707, -12.6972, -3.2552,
-5.9120, -5.8312, 9.0397, 4.0901, 4.2625, 9.0502, -4.6197,
-4.8009]
import numpy as np
w1=np.abs(np.array(w1)).astype(np.int)
w2=np.abs(np.array(w2))
bi=7
#2是35跟原模型一样。2.7是34。准确率是0.85。
#3是33.准确率是0.85。5是32。准确率是0.85。
#6是27效果不好.7是21效果不好不好。
w1=(w1>=bi).astype(np.int)
w2=(w2>=bi).astype(np.int)
index1=np.nonzero(w1)
index2=np.nonzero(w2)
#print(len([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]))
o=w1|w2
index=np.nonzero(o)
fe=[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]
fe=np.array(fe)
fe=list(fe[index])
print(len(fe))
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[:,fe]
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[:,fe]
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(len(fe),2),nn.Sigmoid(),nn.Linear(2,2),nn.Sigmoid(),nn.Linear(2,1),nn.Sigmoid())
network=nn.Sequential(nn.Linear(len(fe),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.8 and test_acc>=0.90:
#break
df = pd.read_csv('test.csv')
df=df.drop(['ID'],axis=1)
nmp=df.to_numpy()
feature=nmp[:,:]
feature=torch.fft.fft(torch.Tensor(feature))
feature=torch.abs(feature)/240*2
feature=torch.Tensor(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]])
out=network(feature)
out=out.detach().numpy()
out=out>0.5
out=out.astype(np.int)
out=pd.DataFrame(out)
out.columns = ['CLASS']
w=[]
for k in range(out.shape[0]):
w.append(k+210)
out['ID']=np.reshape(w,(-1,1))
out[['ID','CLASS']].to_csv('out.csv',index=False)
ax1 = plt.subplot(2,1,1)
ax2 = plt.subplot(2,1,2)
out=network(feature)
out=out.detach().numpy()
plt.sca(ax1)
list=[]
for i in range(out.shape[0]):
if label[i]==1:
plt.scatter(out[i],0,color='red')
if out[i]<0.5:
list.append(i)
if label[i]==0:
plt.scatter(out[i],0,color='blue')
if out[i]>0.5:
list.append(i)
print(list)
out=network(test_feature)
out=out.detach().numpy()
plt.sca(ax2)
for i in range(out.shape[0]):
if test_label[i]==1:
plt.scatter(out[i],0,color='red')
#if out[i]<0.6:
#print(i)
if test_label[i]==0:
plt.scatter(out[i],0,color='blue')
#if out[i]>0.35:
#print(i)
plt.show()
print('执行成功')
for k,v in network.named_parameters():
print(k)
print(v)
w1=[ 1.9525, 6.5730, 2.8011, 2.9227, 6.5043, 5.2393, -4.5891,
-7.5931, -7.5758, -4.5269, 5.1999, 8.7490, -5.9541, -5.9677,
13.0731, 13.2785, -0.5240, -0.3436, -5.3938, -5.4939, -8.0415,
9.2427, 9.3492, -7.9147, -10.4178, 11.7772, 11.9082, -10.3413,
6.7089, 6.6122, 8.1158, -9.9511, -9.9453, 7.8804, -8.3686,
-8.0557]
w2= [ 1.3374, 3.5777, 5.2819, 5.2723, 3.9039, 6.2792, -2.1695,
-3.5467, -3.4617, -2.0943, 6.0334, 8.0246, -7.2271, -7.2421,
-7.8121, -7.7906, 2.7177, 2.5098, 5.8960, 5.8361, -11.4560,
11.9718, 11.9299, -11.3580, -3.3174, -12.4707, -12.6972, -3.2552,
-5.9120, -5.8312, 9.0397, 4.0901, 4.2625, 9.0502, -4.6197,
-4.8009]
import numpy as np
w1=np.abs(np.array(w1)).astype(np.int)
w2=np.abs(np.array(w2))
bi=7
#2是35跟原模型一样。2.7是34。准确率是0.85。
#3是33.准确率是0.85。5是32。准确率是0.85。
#6是27效果不好.7是21效果不好不好。
w1=(w1>=bi).astype(np.int)
w2=(w2>=bi).astype(np.int)
index1=np.nonzero(w1)
index2=np.nonzero(w2)
#print(len([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]))
o=w1|w2
index=np.nonzero(o)
fe=[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]
fe=np.array(fe)
fe=list(fe[index])
print(len(fe))
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[:,fe]
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[:,fe]
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(len(fe),2),nn.Sigmoid(),nn.Linear(2,2),nn.Sigmoid(),nn.Linear(2,1),nn.Sigmoid())
network=nn.Sequential(nn.Linear(len(fe),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.8 and test_acc>=0.90:
#break
df = pd.read_csv('test.csv')
df=df.drop(['ID'],axis=1)
nmp=df.to_numpy()
feature=nmp[:,:]
feature=torch.fft.fft(torch.Tensor(feature))
feature=torch.abs(feature)/240*2
feature=torch.Tensor(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]])
out=network(feature)
out=out.detach().numpy()
out=out>0.5
out=out.astype(np.int)
out=pd.DataFrame(out)
out.columns = ['CLASS']
w=[]
for k in range(out.shape[0]):
w.append(k+210)
out['ID']=np.reshape(w,(-1,1))
out[['ID','CLASS']].to_csv('out.csv',index=False)
ax1 = plt.subplot(2,1,1)
ax2 = plt.subplot(2,1,2)
out=network(feature)
out=out.detach().numpy()
plt.sca(ax1)
list=[]
for i in range(out.shape[0]):
if label[i]==1:
plt.scatter(out[i],0,color='red')
if out[i]<0.5:
list.append(i)
if label[i]==0:
plt.scatter(out[i],0,color='blue')
if out[i]>0.5:
list.append(i)
print(list)
out=network(test_feature)
out=out.detach().numpy()
plt.sca(ax2)
for i in range(out.shape[0]):
if test_label[i]==1:
plt.scatter(out[i],0,color='red')
#if out[i]<0.6:
#print(i)
if test_label[i]==0:
plt.scatter(out[i],0,color='blue')
#if out[i]>0.35:
#print(i)
plt.show()
print('执行成功')

浙公网安备 33010602011771号