原理:
从深度学习模型中选出一个输出值进行优化。
缺点:
模型自己选择最像正包的标签可能会导致模型错误识别正类。
思考:
BN层非常影响训练速度,有时会导致模型的准确率降低。
程序:
from enum import auto from scipy.io import loadmat import numpy as np import torch import torch.utils.data as data_utils from torch import nn import torch.optim as optimm = loadmat(r"musk_2_original.mat") daaa=m.keys() from torch import nn network=nn.Sequential(nn.Linear(166,80),nn.Sigmoid(),nn.Linear(80,1),nn.Sigmoid()) #network=nn.Sequential(nn.BatchNorm1d(166),nn.Linear(166,2),nn.Softmax(dim=1)) w=nn.BatchNorm1d(166)
optimizer = optim.Adam(network.parameters(), lr=0.00004) m1 = loadmat(r"musk_1_original.mat") val_bag=[] val_bag_lable=[] train_bag=[] train_bag_lable=[] for i in m1['pos_bag']: for j in i: val_bag.append(j.T) val_bag_lable.append(1) for i in m1['neg_bag']: for j in i: val_bag.append(j.T) val_bag_lable.append(0) for i in m['pos_bag']: for j in i: train_bag.append(j.T) train_bag_lable.append(1) for i in m['neg_bag']: for j in i: train_bag.append(j.T) train_bag_lable.append(0) t=0.1 for j in range(5000): r=0 e=0 er=0 acc=0 sum=0 network.train() for i in range(len(train_bag)): if train_bag_lable[i]==0: sum=sum+1 y=network(torch.Tensor(train_bag[i])) index=torch.argmax(y[:]) l1=0.5*(y[index]-0)**2 l1.backward() Y = torch.ge(y, 0.5).float() er=er+l1.cpu().mean() if 1 not in torch.ge(y[:], 0.5).float(): acc=acc+1 if train_bag_lable[i]==1: sum=sum+1 y=network(torch.Tensor(train_bag[i])) #然后选出最大的 index=torch.argmax(y[:]) #l=-torch.log(y[index]) l=0.5*(y[index]-1)**2 l.backward() er=er+l.cpu() if 1 in torch.ge(y[:], 0.5).float(): acc=acc+1 optimizer.step() optimizer.zero_grad() gai=0 if acc/sum>1 : for i in range(len(train_bag)): if torch.Tensor(train_bag[i]).shape[0]!=1: y=network(torch.Tensor(train_bag[i])) if train_bag_lable[i]==0: index=torch.argmax(y[:]) index1=torch.argmin(y[:]) if y[index1]/y[index]<=t: train_bag_lable[i]=1 gai=gai+1 if train_bag_lable[i]==1: index=torch.argmax(y[:]) if y[index]/(1-y[index])<=t: train_bag_lable[i]=0 gai=gai+1 #print(len(train_bag_lable)) if gai<0.001*sum: t=t+0.1 t=min(t,0.9)
print("gai t",gai,t) network.eval() test_acc=0 for i in range(len(val_bag)): y=network(torch.Tensor(val_bag[i])) if val_bag_lable[i]==1: Y = torch.ge(y[:], 0.5).float() if 1 in Y: test_acc=test_acc+1 else: e=e+1 else: Y = torch.ge(y[:], 0.5).float() if 1 not in Y: test_acc=test_acc+1 else: r=r+1 print(j,er,acc/sum,test_acc/len(val_bag),e,r) 标签:acc,Multi,val,nn,bag,torch,Instance,train,Neural From: https://www.cnblogs.com/hahaah/p/16857501.html