欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

用 pytorch 进行分类(二分类,多分类)

程序员文章站 2022-07-13 10:38:52
...
import numpy as np
import sklearn
import torch
import torch.nn as nn
from torch.autograd import Variable
import torch.nn.functional as F
import numpy as np
import matplotlib.pyplot as plt
import torch.optim
from sklearn import datasets

n_data = torch.ones(100,2)
x0 = torch.normal(2*n_data,1)
y0 = torch.zeros(100)
x1 = torch.normal(-2*n_data,1)
y1 = torch.ones(100)
x = torch.cat((x0,x1),0).type(torch.FloatTensor)
y = torch.cat((y0,y1),0).type(torch.LongTensor)


# plt.scatter(x.data.numpy(), y.data.numpy())
# plt.show()

class SoftMax(nn.Module):
     def __init__(self,n_feature,n_hidden,n_out):
          super(SoftMax,self).__init__()
          self.hidden = nn.Linear(n_feature,n_hidden)
          self.out = nn.Linear(n_hidden,n_out)

     def forward(self, x):
          x = torch.sigmoid(self.hidden(x))
          x = self.out(x)
          return F.softmax(x,dim=1)  #返回的是每个类的概率

net = SoftMax(n_feature=2,n_hidden=10,n_out=2)

opitmizer = torch.optim.SGD(net.parameters(),lr=0.03)
loss_fun = nn.MSELoss()   #选择 均方差为误差函数

epoches = 1000

for i in range(epoches):
     Length = len(x)
     loss_data = 0
     for k in range(Length):
          input_s = Variable(torch.FloatTensor(x[k])).unsqueeze(0)
          predict = net(input_s)
          # print(predict)
          target = None
          if (y[k] == 0):
               target = [1,0]
          else:
               target = [0,1]
          target = Variable(torch.LongTensor(target)).unsqueeze(0) #变成 1*2的 tensor
          loss = loss_fun(predict,target)
          loss_data += loss.item() # 获取tensor的值
          opitmizer.zero_grad()
          loss.backward()
          opitmizer.step()
     # print(loss_data/Length)

#torch.save(net,"test_classify.pkl")
# 
# 
# net = torch.load("test_classify.pkl")
# classify_res = net(x).data.numpy()
# print(classify_res)
# res = [0 if x[0] > 0.5 else 1 for x in classify_res] # 表示 第一类的概率大于0.5的时候 取 第一类 即类别 0
# print((np.array(res) == np.array(y)).sum()/len(y))

如果 误差函数 用了 CrossEntropyLoss  (内置了Softmax) 不要再用softmax

 

2、对iris数据进行分类

import sklearn
import torch
import torch.nn as nn
from torch.autograd import Variable
import torch.nn.functional as F
import numpy as np
import matplotlib.pyplot as plt
import torch.optim
from sklearn import datasets
#
iris = datasets.load_iris()

x = iris['data']
y = iris['target']
x = torch.FloatTensor(x)
y = torch.LongTensor(y)
x = Variable(x)
y = Variable(y)

class Net(nn.Module):
     def __init__(self,n_feature,n_hidden,n_out):
          super(Net,self).__init__()
          self.hidden = nn.Linear(n_feature,n_hidden)
          self.out = nn.Linear(n_hidden,n_out)

     def forward(self, x):
          x = F.sigmoid(self.hidden(x))
          x = self.out(x)
          out = F.log_softmax(x,dim=1)
          return out

net = Net(n_feature=4,n_hidden=5,n_out=4)

optimizer = torch.optim.SGD(net.parameters(),lr=0.03)

epochs = 10000

px = []; py = []
for i in range(epochs):
     predict = net(x)
     loss = F.nll_loss(predict,y)  # 输出层 用了log_softmax 则需要用这个误差函数
     optimizer.zero_grad()
     loss.backward()
     optimizer.step()
     print(i,"loss:",loss.data[0])
     px.append(i)
     py.append(loss.data[0])
     # if(i%10 == 0):
     #      plt.cla
     #      plt.title(u"训练过程的loss曲线")
     #      plt.xlabel(u"迭代次数")
     #      plt.ylabel(u"迭代损失")
     #      plt.plot(px,py,"r-",lw=1)
     #      plt.text(0,0,"Loss = %.4f" % loss.data[0],fontdict={"size":20,'color':'red'})
          #plt.pause(0.1)
torch.save(net,"my_model.pkl")




# iris_model = torch.load("iris_model.pkl")
# print(iris_model)
# net = torch.load("my_model.pkl")

# x1 = torch.FloatTensor([5.1000, 3.5000, 1.4000, 0.2000])
# x1 = Variable(x1)
# x2 = Variable(torch.FloatTensor([4.9000, 3.0000, 1.4000, 0.2000]))
# print(x1)
# print(x1.unsqueeze(0))
# print(net(x1.unsqueeze(0)))  # 单独一个样板 需要 unsqueeze(0)
# print(net(x2.unsqueeze(0)))
# x = iris['data']
# x = Variable(torch.FloatTensor(x))
# print(iris['data'])
# all_predict = net(x).data.numpy()
# 
# '''
#     argmax(data,axis = 1)  axis = 1表示 按照行求最大值的索引
# '''
# print((np.argmax(all_predict,axis=1) == iris['target']).sum()/len(y))

损失函数总结 : https://blog.csdn.net/zhangxb35/article/details/72464152?utm_source=itdadao&utm_medium=referral