相比起逻辑回归的二分类,多元分类使用softmax来替代了sigmoid,假如需要分k类,那么应该有k个输入值1...k,然后输出k个概率,且概率之和为1。
顺便给出softmax的定义:i= e^i/ e^l
import matplotlib.pyplot as plt
import torch
from torch import nn,optim
import torch.nn.functional as Func
# 500*2的大小
cluster = torch.ones(500, 2)
# 4,-4为期望值,2为标准差生成一堆数据
data0 = torch.normal(4 * cluster, 2)
data1 = torch.normal(-4 * cluster, 1)
data2 = torch.normal(-8 * cluster, 1)
label0 = torch.zeros(500)
label1 = torch.ones(500)
label2 = label1*2
x = torch.cat((data0, data1, data2), ).type(torch.FloatTensor)
y = torch.cat((label0, label1, label2), ).type(torch.LongTensor)
plt.scatter(x.numpy()[:, 0], x.numpy()[:, 1], c=y.numpy(), s=10, lw=0, cmap='RdYlGn')
plt.show()
class Net(nn.Module):
def __init__(self, input_feature, num_hidden, outputs):
super(Net,self).__init__()
# 创建了输入特征到隐藏层的线性变换
self.hidden = nn.Linear(input_feature,num_hidden)
# 创建了隐藏层到输出层的线性变换
self.out = nn.Linear(num_hidden,outputs)
def forward(self, x):
# 对输入数据 x 进行隐藏层的线性变换,并经过 ReLU
x = Func.relu(self.hidden(x))
# 表示将数据 x 通过输出层的线性变换
x = self.out(x)
x = Func.softmax(x,dim=1)
return x
CUDA = torch.cuda.is_available()
if CUDA:
# x的维度为2,输出三类
net = Net(input_feature=2, num_hidden=20, outputs=3).cuda()
inputs = x.cuda()
target = y.cuda()
else:
net = Net(input_feature=2, num_hidden=20, outputs=3)
inputs = x
target = y
criterion = nn.CrossEntropyLoss()
optimizer = optim.SGD(net.parameters(),lr=2e-2)
def draw(output):
if CUDA:
output = output.cpu()
# 清空画布
plt.cla()
output = torch.max((output), 1)[1]
pred_y = output.data.numpy().squeeze()
target_y = y.numpy()
plt.scatter(x.numpy()[:, 0], x.numpy()[:, 1], c=pred_y, s=10, lw=0, cmap='RdYlGn')
accuracy = sum(pred_y == target_y) / 1500.0
plt.text(1.5, -4, 'Accuracy=%s' % (accuracy), fontdict={'size': 20, 'color': 'red'})
plt.pause(0.1)
def Train(model,criterion,optimizer,epochs):
for epochs in range(epochs):
output = model(inputs)
loss = criterion(output,target)
optimizer.zero_grad()
loss.backward()
optimizer.step() #权重更新
if epochs % 40 == 0:
draw(output)
plt.pause(0.1)
Train(net,criterion,optimizer,1000)