首页 > 其他分享 >Pytorch实现分类器

Pytorch实现分类器

时间:2023-04-17 15:13:46浏览次数:32  
标签:实现 train torch 分类器 test Pytorch num np dot

本文实现两个分类器: softmax分类器和感知机分类器

Softmax分类器

Softmax分类是一种常用的多类别分类算法,它可以将输入数据映射到一个概率分布上。Softmax分类首先将输入数据通过线性变换得到一个向量,然后将向量中的每个元素进行指数函数运算,最后将指数运算结果归一化得到一个概率分布。这个概率分布可以被解释为每个类别的概率估计。

定义

定义一个softmax分类器类:

class SoftmaxClassifier(nn.Module):
    def __init__(self,input_size,output_size):
        # 调用父类的__init__()方法进行初始化
        super(SoftmaxClassifier,self).__init__()
        # 定义一个nn.Linear对象,用于将输入特征映射到输出类别
        self.linear = nn.Linear(input_size,output_size)

    def forward(self,x):
        x = self.linear(x) # 传递给线性层
        return nn.functional.softmax(x,dim=1) # 得到概率分布

    def compute_accuracy(self,output,labels):
        preds = torch.argmax(output,dim=1) # 获取每个样本的预测标签
        correct = torch.sum(preds == labels).item() # 计算正确预测的数量
        accuracy = correct / len(labels) # 除以总样本数得到准确率
        return accuracy

如上定义三个方法:

  1. __init__(self):构造函数,在类初始化时运行,调用父类的__init__()方法进行初始化
  2. forward(self):模型前向计算过程
  3. compute_accuracy(self):计算模型的预测准确率

训练

生成训练数据:

import numpy as np

# 生成随机样本(包含训练数据和测试数据)
def generate_rand_samples(dot_num=100):
    x_p = np.random.normal(3., 1, dot_num)
    y_p = np.random.normal(3., 1, dot_num)
    y = np.zeros(dot_num)
    C1 = np.array([x_p, y_p, y]).T
    x_n = np.random.normal(7., 1, dot_num)
    y_n = np.random.normal(7., 1, dot_num)
    y = np.ones(dot_num)
    C2 = np.array([x_n, y_n, y]).T
    x_n = np.random.normal(3., 1, dot_num)
    y_n = np.random.normal(7., 1, dot_num)
    y = np.ones(dot_num)*2
    C3 = np.array([x_n, y_n, y]).T
    x_n = np.random.normal(7, 1, dot_num)
    y_n = np.random.normal(3, 1, dot_num)
    y = np.ones(dot_num)*3
    C4 = np.array([x_n, y_n, y]).T
    data_set = np.concatenate((C1, C2, C3, C4), axis=0)
    np.random.shuffle(data_set)

    return data_set[:,:2].astype(np.float32),data_set[:,2].astype(np.int32)

X_train,y_train = generate_rand_samples()
y_train[y_train == -1] = 0

设置训练前的前置参数,并初始化分类器

num_inputs = 2  # 输入维度大小
num_outputs = 4  # 输出维度大小
learning_rate = 0.01  # 学习率
num_epochs = 2000 # 训练周期数

# 归一化数据 将数据特征减去均值再除以标准差
X_train = (X_train - X_train.mean(axis=0)) / X_train.std(axis=0)
y_train = y_train.astype(np.compat.long)

# 创建model并初始化
model = SoftmaxClassifier(num_inputs, num_outputs)
criterion = nn.CrossEntropyLoss() # 交叉熵损失
optimizer = optim.SGD(model.parameters(), lr=learning_rate)  # SGD优化器

训练:

# 遍历训练周期数
for epoch in range(num_epochs):
    outputs = model(torch.tensor(X_train))  # 前向传递计算
    loss = criterion(outputs,torch.tensor(y_train))  # 计算预测输出和真实标签之间的损失
    train_accuracy = model.compute_accuracy(outputs,torch.tensor(y_train))  # 计算模型当前训练周期中准确率

    optimizer.zero_grad()  # 清楚优化器中梯度
    loss.backward()  # 计算损失对模型参数的梯度
    optimizer.step()
	
    # 打印信息
    if (epoch + 1) % 10 == 0:
        print(f"Epoch [{epoch+1}/{num_epochs}], Loss: {loss.item():.4f}, Accuracy: {train_accuracy:.4f}")

运行:

Epoch [1820/2000], Loss: 0.9947, Accuracy: 0.9575
Epoch [1830/2000], Loss: 0.9940, Accuracy: 0.9600
Epoch [1840/2000], Loss: 0.9932, Accuracy: 0.9600
Epoch [1850/2000], Loss: 0.9925, Accuracy: 0.9600
Epoch [1860/2000], Loss: 0.9917, Accuracy: 0.9600
....

测试

生成测试并测试:

X_test, y_test = generate_rand_samples()  # 生成测试数据
X_test = (X_test- np.mean(X_test)) / np.std(X_test)  # 归一化
y_test = y_test.astype(np.compat.long)
predicts = model(torch.tensor(X_test))  # 获取模型输出
accuracy = model.compute_accuracy(predicts,torch.tensor(y_test))  # 计算准确度
print(f'Test Accuracy: {accuracy:.4f}')

输出:

Test Accuracy: 0.9725

绘制图像:

# 绘制图像
x_min, x_max = X_test[:, 0].min() - 1, X_test[:, 0].max() + 1
y_min, y_max = X_test[:, 1].min() - 1, X_test[:, 1].max() + 1
xx, yy = np.meshgrid(np.arange(x_min, x_max, 0.1), np.arange(y_min, y_max, 0.1))
Z = model(torch.tensor(np.c_[xx.ravel(), yy.ravel()], dtype=torch.float32)).argmax(dim=1).numpy()
Z = Z.reshape(xx.shape)
plt.contourf(xx, yy, Z, alpha=0.4)
plt.scatter(X_test[:, 0], X_test[:, 1], c=y_test, s=20, edgecolor='k')
plt.show()

感知机分类器

实现与上述softmax分类器相似,此处实现sigmod感知机,采用sigmod作为分类函数,该函数可以将线性变换的结果映射为0到1之间的实数值,通常被用作神经网络中的激活函数

sigmoid感知机的学习算法与普通的感知机类似,也是采用随机梯度下降(SGD)的方式进行更新。不同之处在于,sigmoid感知机的输出是一个概率值,需要将其转化为类别标签。

通常使用阈值来决定输出值所属的类别,如将输出值大于0.5的样本归为正类,小于等于0.5的样本归为负类。

定义

# 感知机分类器
class PerceptronClassifier(nn.Module):
    def __init__(self, input_size,output_size):
        super(PerceptronClassifier, self).__init__()
        self.linear = nn.Linear(input_size,output_size)

    def forward(self, x):
        logits = self.linear(x)
        return torch.sigmoid(logits)

    def compute_accuracy(self, pred, target):
        pred = torch.where(pred >= 0.5, 1, -1)
        accuracy = (pred == target).sum().item() / target.size(0)
        return accuracy

给定一个输入向量(x1,x2,x3...xn),输出为y=σ(wx+b)=1/(e^−(wx+b))

训练

生成训练集:

def generate_rand_samples(dot_num=100):
    x_p = np.random.normal(3., 1, dot_num)
    y_p = np.random.normal(3., 1, dot_num)
    y = np.ones(dot_num)
    C1 = np.array([x_p, y_p, y]).T
    x_n = np.random.normal(6., 1, dot_num)
    y_n = np.random.normal(0., 1, dot_num)
    y = np.ones(dot_num)*-1
    C2 = np.array([x_n, y_n, y]).T
    data_set = np.concatenate((C1, C2), axis=0)
    np.random.shuffle(data_set)
    return data_set[:,:2].astype(np.float32),data_set[:,2].astype(np.int32)

X_train,y_train = generate_rand_samples()
X_test,y_test = generate_rand_samples()

该过程与上述softmax分类器相似:

num_inputs = 2
num_outputs = 1
learning_rate = 0.01
num_epochs = 200

# 归一化数据 将数据特征减去均值再除以标准差
X_train = (X_train - X_train.mean(axis=0)) / X_train.std(axis=0)

# 创建model并初始化
model = PerceptronClassifier(num_inputs, num_outputs)
optimizer = optim.SGD(model.parameters(), lr=learning_rate)  # SGD优化器

criterion = nn.functional.binary_cross_entropy

训练:

# 遍历训练周期数
for epoch in range(num_epochs):
    outputs = model(torch.tensor(X_train))
    labels = torch.tensor(y_train).unsqueeze(1)
    loss = criterion(outputs,labels.float())
    train_accuracy = model.compute_accuracy(outputs, labels)

    optimizer.zero_grad()
    loss.backward()
    optimizer.step()

    if (epoch + 1) % 10 == 0:
        print(f"Epoch [{epoch+1}/{num_epochs}], Loss: {loss.item():.4f}, Accuracy: {train_accuracy:.4f}")

输出:

Epoch [80/200], Loss: -0.5429, Accuracy: 0.9550
Epoch [90/200], Loss: -0.6235, Accuracy: 0.9550
Epoch [100/200], Loss: -0.7015, Accuracy: 0.9500
Epoch [110/200], Loss: -0.7773, Accuracy: 0.9400
....

测试

X_test, y_test = generate_rand_samples() # 生成测试集
X_test = (X_test - X_test.mean(axis=0)) / X_test.std(axis=0)

test_inputs = torch.tensor(X_test)
test_labels = torch.tensor(y_test).unsqueeze(1)
with torch.no_grad():
    outputs = model(test_inputs)
    accuracy = model.compute_accuracy(outputs, test_labels)
    print(f"Test Accuracy: {accuracy:.4f}")

绘图:

x_min, x_max = X_test[:, 0].min() - 1, X_test[:, 0].max() + 1
y_min, y_max = X_test[:, 1].min() - 1, X_test[:, 1].max() + 1
xx, yy = torch.meshgrid(torch.linspace(x_min, x_max, 100), torch.linspace(y_min, y_max, 100))

# 预测每个点的类别
Z = torch.argmax(model(torch.cat((xx.reshape(-1,1), yy.reshape(-1,1)), 1)), 1)
Z = Z.reshape(xx.shape)

# 绘制分类图
plt.contourf(xx, yy, Z, cmap=plt.cm.Spectral,alpha=0.0)

# 绘制分界线
w = model.linear.weight.detach().numpy()  # 权重
b = model.linear.bias.detach().numpy()  # 偏置
x1 = np.linspace(x_min, x_max, 100)
x2 = (-b - w[0][0]*x1) / w[0][1]
plt.plot(x1, x2, 'k-')

# 绘制样本点
plt.scatter(X_train[:, 0], X_train[:, 1], c=y_train, cmap=plt.cm.Spectral)
plt.show()

标签:实现,train,torch,分类器,test,Pytorch,num,np,dot
From: https://www.cnblogs.com/N3ptune/p/17325901.html

相关文章

  • 小波变换原理简要实现和分析
    傅里叶变换是频谱分析的重要工具,适用于周期性平稳信号,但是对于非平稳信号而言,效果较差。其实质是卷积求正弦波相关性,由于正弦波不是能量有限信号,对无差别做一个全部卷积,算出来的结果不考虑时间。小波变换也是频谱分析的重要工具,基函数为能量有限信号,也可以进行时域分析。针对不......
  • Webpack中手动实现Loader与Plugin
    Loaderloader是一个转换器,用于对源代码进行转换。工作流程webpack.config.js里配置了一个模块的Loader;2.遇到相应模块文件时,触发了该模块的loader;3.loader接受了一个表示该模块文件内容的source;4.loader使用webapck提供的一系列api对source进行转换,得到一......
  • RocketMQ 多级存储设计与实现
    作者:张森泽随着RocketMQ5.1.0的正式发布,多级存储作为RocketMQ一个新的独立模块到达了TechnicalPreview里程碑:允许用户将消息从本地磁盘卸载到其他更便宜的存储介质,可以用较低的成本延长消息保留时间。本文详细介绍RocketMQ多级存储设计与实现。设计总览RocketMQ多级......
  • 如何通过人工神经网络实现图像识别?
    ​如何通过人工神经网络实现图像识别?图像识别是指让计算机能够自动地识别图像中的物体、场景或者特征的技术。人工神经网络是一种模仿生物神经系统的结构和功能的数学模型,它由大量的简单处理单元(神经元)相互连接而成,能够通过学习从数据中提取复杂的特征和规律。人工神经网络实现......
  • 【迭代器设计模式详解】C/Java/JS/Go/Python/TS不同语言实现
    简介迭代器模式(IteratorPattern),是一种结构型设计模式。给数据对象构建一套按顺序访问集合对象元素的方式,而不需要知道数据对象的底层表示。迭代器模式是与集合共存的,我们只要实现一个集合,就需要同时提供这个集合的迭代器,就像Java中的Collection,List、Set、Map等,这些集合都有自......
  • 碳交易机制下考虑需求响应的综合能源系统优化运行综合能源系统是实现“双碳&rdqu
    碳交易机制下考虑需求响应的综合能源系统优化运行综合能源系统是实现“双碳”目标的有效途径,为进一步挖掘其需求侧可调节潜力对碳减排的作用,提出了一种碳交易机制下考虑需求响应的综合能源系统优化运行模型。首先,根据负荷响应特性将需求响应分为价格型和替代型2类,分别建立了基......
  • 如何利用HTTP代理实现自动化爬虫任务管理
    在进行爬虫任务管理时,我们需要对爬取目标、爬取频率、数据存储等多个方面进行规划和管理。使用HTTP代理可以帮助我们提高爬虫效率,同时也可以实现自动化的爬虫任务管理。本文将介绍如何利用HTTP代理实现自动化爬虫任务管理。第一步:选择HTTP代理在选择HTTP代理时,需要考虑......
  • 225 队列实现stack
         解决方法简单粗暴,把队列前面的都取出来再加入队尾,让之前的队尾元素排到队头,这样就可以取出了: importqueueclassMyStack:def__init__(self):self.q=queue.Queue()self.top_element=0defpush(self,x:int)->None:......
  • 如何用 ModelScope 实现 “AI 换脸” 视频
    前言当下,视频内容火爆,带有争议性或反差大的换脸视频总能吸引人视线。虽然AI换脸在市面上已经流行了许久,相关制作工具或移动应用也是数不胜数。但是多数制作工具多数情况下不是会员就是收费,而且替换模板有限。以下在实战的角度,用阿里ModelScope的图像人脸融合实现一下AI视频......
  • [全程建模]《软件工程之全程建模实现》一书第二版签约公告
    自从发布了《[全程建模]《软件工程之全程建模实现》一书第二版公开寻找出版商》一文,小子得到了多家出版社的联系(其中包括电子工业出版社的另一个出版部门),经过近十个月的对比分析,最后选择了机械工业出版社。在今年英雄会上签了合同,预计时间是2010年3月底交付初稿,正式出版应该是在201......