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

PyTorch学习(5)—分类

程序员文章站 2022-07-14 15:19:53
...

本篇博客主要介绍采用PyTorch对数据进行分类。

首先是分类数据(生成的假数据):

PyTorch学习(5)—分类

示例代码:

import torch
from torch.autograd import Variable
import torch.nn.functional as F
import matplotlib.pyplot as plt

# 生成假数据
n_data = torch.ones(100, 2)
x0 = torch.normal(2*n_data, 1)      # class0 x data (tensor), shape=(100, 2)
y0 = torch.zeros(100)               # class0 y data (tensor), shape=(100, 1)
x1 = torch.normal(-2*n_data, 1)     # class1 x data (tensor), shape=(100, 2)
y1 = torch.ones(100)                # class1 y data (tensor), shape=(100, 1)
x = torch.cat((x0, x1), 0).type(torch.FloatTensor)  # shape (200, 2) FloatTensor = 32-bit floating
y = torch.cat((y0, y1), ).type(torch.LongTensor)    # shape (200,) LongTensor = 64-bit integer

# 将Tensor转换为torch
x, y = Variable(x), Variable(y)

# 打印数据散点图
# plt.scatter(x.data.numpy()[:, 0], x.data.numpy()[:, 1], c=y.data.numpy(), s=100, lw=0, cmap='RdYlGn')
# plt.show()


class Net(torch.nn.Module):
    # 初始化
    def __init__(self, n_feature, n_hidden, n_output):
        super(Net, self).__init__()
        self.hidden = torch.nn.Linear(n_feature, n_hidden)
        self.predict = torch.nn.Linear(n_hidden, n_output)

    # 前向传递
    def forward(self, x):
        x = F.relu(self.hidden(x))
        x = self.predict(x)
        return x


net = Net(2, 10, 2)
# 输出定义的网络的结构
print(net)
plt.ion()
plt.show()

# 优化(给出神经网络的参数和学习速率)
optimizer = torch.optim.SGD(net.parameters(), lr=0.02)
# loss function,分类问题:交叉熵(CrossEntropyLoss) [0.1, 0.2, 0.7]=1表示分为每个类的概率
loss_func = torch.nn.CrossEntropyLoss()

for t in range(100):
    out = net(x)
    # 求误差
    loss = loss_func(out, y)

    # 优化
    # 每一步首先将梯度降为0
    optimizer.zero_grad()
    # 进行反向传递更新参数
    loss.backward()
    # 优化梯度
    optimizer.step()

    if t % 2 == 0:
        # plot and show learning process
        plt.cla()
        prediction = torch.max(out, 1)[1]
        pred_y = prediction.data.numpy().squeeze()
        target_y = y.data.numpy()
        plt.scatter(x.data.numpy()[:, 0], x.data.numpy()[:, 1], c=pred_y, s=100, lw=0, cmap='RdYlGn')
        accuracy = float((pred_y == target_y).astype(int).sum()) / float(target_y.size)
        plt.text(1.5, -4, 'Accuracy=%.2f' % accuracy, fontdict={'size': 20, 'color': 'red'})
        plt.pause(0.1)

plt.ioff()
plt.show()

分类过程:

PyTorch学习(5)—分类

PyTorch学习(5)—分类PyTorch学习(5)—分类

 

相关标签: PyTorch 分类