今日热搜:Pytorch实现分类器

2023-04-17 15:44:23 来源:博客园

本文实现两个分类器: 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

如上定义三个方法:

__init__(self):构造函数,在类初始化时运行,调用父类的__init__()方法进行初始化forward(self):模型前向计算过程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.9575Epoch [1830/2000], Loss: 0.9940, Accuracy: 0.9600Epoch [1840/2000], Loss: 0.9932, Accuracy: 0.9600Epoch [1850/2000], Loss: 0.9925, Accuracy: 0.9600Epoch [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() + 1y_min, y_max = X_test[:, 1].min() - 1, X_test[:, 1].max() + 1xx, 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 = 2num_outputs = 1learning_rate = 0.01num_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.9550Epoch [90/200], Loss: -0.6235, Accuracy: 0.9550Epoch [100/200], Loss: -0.7015, Accuracy: 0.9500Epoch [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() + 1y_min, y_max = X_test[:, 1].min() - 1, X_test[:, 1].max() + 1xx, 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()

标签:

“共享充电宝”涨至4元每小时?西安这些品牌收费规则竟不同

近日,共享充电宝涨至4元每小时共享充电宝客服回应涨价等话题冲上微博热搜,共享充电宝的价格与服务再遭...

2022-08-25 13:51:39

唐山加快验收时间 为建筑工地开复工保驾护航

唐山市住建局坚持两手抓、两手硬,统筹疫情防控和经济发展,想尽一切办法,简化一切手续,实现一路绿灯...

2022-03-19 15:15:42

消费者“身材焦虑” 减肥市场疯狂吸金

三月不减肥,四月徒伤悲。冬去春来之际,为了能穿上各种漂亮轻薄的服装,变得更瘦成为了许多人的追求,...

2022-03-19 15:14:49

2月份秦皇岛新建商品住宅销售价格环比下降0.2% 同比下降4%

3月16日,国家统计局公布2月份70个大中城市商品住宅销售价格变动情况,河北省入统的石家庄、唐山、秦皇...

2022-03-19 15:13:01

深圳技术进出口全年合同数量共1347项 同比增长2.51%

科技部火炬中心近日公布了2021年度全国技术合同交易数据。截至2021年12月31日,全国共登记技术合同67050...

2022-03-19 15:10:37

邢台柏乡:打造羊肚菌产业示范带 引领村级集体经济发展

我们通过打造食用菌产业高标准试验示范园,不仅盘活了闲置土地,还进一步增加了群众和村集体经济收入。...

2022-03-19 15:09:40

胡金秋32分17板 浙江广厦男篮“双杀”稠州金租

CBA浙江德比二番战昨晚开打,再度狭路相逢的浙江广厦男篮与浙江稠州金租男篮表现难分伯仲,比赛直到最后...

2022-03-19 15:07:37

新疆北部有降雪 湖北、湖南等地出现大雾

  中新网12月13日电 据中央气象台网站消息,昨日8时至今日6时,内蒙古东北部、黑龙江东北部等地部分...

2021-12-13 08:35:21

黑龙江新增本土核酸检测初筛阳性人员5例 均在讷河市

  中新网12月13日电 据黑龙江省卫生健康委员会网站消息,2021年12月12日0-24时,黑龙江省无新增确诊...

2021-12-13 08:35:21

“恋爱盲盒”抽的不是爱情,是急功近利的心

  越是急功近利,越是焦虑,对恋爱关系的处理就越可能出现问题。  “玩法”简单,最低只需花费0 99...

2021-12-13 08:35:20
x 广告
x 广告

Copyright ©  2015-2022 大众经营网版权所有  备案号:豫ICP备20014643号-14   联系邮箱: 905 14 41 07@qq.com