经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 程序设计 » Python3 » 查看文章
基于Pytorch实现分类器的示例详解
来源:jb51  时间:2023/4/19 9:01:10  对本文有异议

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

Softmax分类器

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

定义

定义一个softmax分类器类:

  1. class SoftmaxClassifier(nn.Module):
  2. def __init__(self,input_size,output_size):
  3. # 调用父类的__init__()方法进行初始化
  4. super(SoftmaxClassifier,self).__init__()
  5. # 定义一个nn.Linear对象,用于将输入特征映射到输出类别
  6. self.linear = nn.Linear(input_size,output_size)
  7. def forward(self,x):
  8. x = self.linear(x) # 传递给线性层
  9. return nn.functional.softmax(x,dim=1) # 得到概率分布
  10. def compute_accuracy(self,output,labels):
  11. preds = torch.argmax(output,dim=1) # 获取每个样本的预测标签
  12. correct = torch.sum(preds == labels).item() # 计算正确预测的数量
  13. accuracy = correct / len(labels) # 除以总样本数得到准确率
  14. return accuracy

如上定义三个方法:

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

训练

生成训练数据:

  1. import numpy as np
  2. # 生成随机样本(包含训练数据和测试数据)
  3. def generate_rand_samples(dot_num=100):
  4. x_p = np.random.normal(3., 1, dot_num)
  5. y_p = np.random.normal(3., 1, dot_num)
  6. y = np.zeros(dot_num)
  7. C1 = np.array([x_p, y_p, y]).T
  8. x_n = np.random.normal(7., 1, dot_num)
  9. y_n = np.random.normal(7., 1, dot_num)
  10. y = np.ones(dot_num)
  11. C2 = np.array([x_n, y_n, y]).T
  12. x_n = np.random.normal(3., 1, dot_num)
  13. y_n = np.random.normal(7., 1, dot_num)
  14. y = np.ones(dot_num)*2
  15. C3 = np.array([x_n, y_n, y]).T
  16. x_n = np.random.normal(7, 1, dot_num)
  17. y_n = np.random.normal(3, 1, dot_num)
  18. y = np.ones(dot_num)*3
  19. C4 = np.array([x_n, y_n, y]).T
  20. data_set = np.concatenate((C1, C2, C3, C4), axis=0)
  21. np.random.shuffle(data_set)
  22. return data_set[:,:2].astype(np.float32),data_set[:,2].astype(np.int32)
  23. X_train,y_train = generate_rand_samples()
  24. y_train[y_train == -1] = 0

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

  1. num_inputs = 2 # 输入维度大小
  2. num_outputs = 4 # 输出维度大小
  3. learning_rate = 0.01 # 学习率
  4. num_epochs = 2000 # 训练周期数
  5. # 归一化数据 将数据特征减去均值再除以标准差
  6. X_train = (X_train - X_train.mean(axis=0)) / X_train.std(axis=0)
  7. y_train = y_train.astype(np.compat.long)
  8. # 创建model并初始化
  9. model = SoftmaxClassifier(num_inputs, num_outputs)
  10. criterion = nn.CrossEntropyLoss() # 交叉熵损失
  11. optimizer = optim.SGD(model.parameters(), lr=learning_rate) # SGD优化器

训练:

  1. # 遍历训练周期数
  2. for epoch in range(num_epochs):
  3. outputs = model(torch.tensor(X_train)) # 前向传递计算
  4. loss = criterion(outputs,torch.tensor(y_train)) # 计算预测输出和真实标签之间的损失
  5. train_accuracy = model.compute_accuracy(outputs,torch.tensor(y_train)) # 计算模型当前训练周期中准确率
  6. optimizer.zero_grad() # 清楚优化器中梯度
  7. loss.backward() # 计算损失对模型参数的梯度
  8. optimizer.step()
  9. # 打印信息
  10. if (epoch + 1) % 10 == 0:
  11. 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
....

测试

生成测试并测试:

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

输出:

Test Accuracy: 0.9725

绘制图像:

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

感知机分类器

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

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

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

定义

  1. # 感知机分类器
  2. class PerceptronClassifier(nn.Module):
  3. def __init__(self, input_size,output_size):
  4. super(PerceptronClassifier, self).__init__()
  5. self.linear = nn.Linear(input_size,output_size)
  6. def forward(self, x):
  7. logits = self.linear(x)
  8. return torch.sigmoid(logits)
  9. def compute_accuracy(self, pred, target):
  10. pred = torch.where(pred >= 0.5, 1, -1)
  11. accuracy = (pred == target).sum().item() / target.size(0)
  12. return accuracy

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

训练

生成训练集:

  1. def generate_rand_samples(dot_num=100):
  2. x_p = np.random.normal(3., 1, dot_num)
  3. y_p = np.random.normal(3., 1, dot_num)
  4. y = np.ones(dot_num)
  5. C1 = np.array([x_p, y_p, y]).T
  6. x_n = np.random.normal(6., 1, dot_num)
  7. y_n = np.random.normal(0., 1, dot_num)
  8. y = np.ones(dot_num)*-1
  9. C2 = np.array([x_n, y_n, y]).T
  10. data_set = np.concatenate((C1, C2), axis=0)
  11. np.random.shuffle(data_set)
  12. return data_set[:,:2].astype(np.float32),data_set[:,2].astype(np.int32)
  13. X_train,y_train = generate_rand_samples()
  14. X_test,y_test = generate_rand_samples()

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

  1. num_inputs = 2
  2. num_outputs = 1
  3. learning_rate = 0.01
  4. num_epochs = 200
  5. # 归一化数据 将数据特征减去均值再除以标准差
  6. X_train = (X_train - X_train.mean(axis=0)) / X_train.std(axis=0)
  7. # 创建model并初始化
  8. model = PerceptronClassifier(num_inputs, num_outputs)
  9. optimizer = optim.SGD(model.parameters(), lr=learning_rate) # SGD优化器
  10. criterion = nn.functional.binary_cross_entropy

训练:

  1. # 遍历训练周期数
  2. for epoch in range(num_epochs):
  3. outputs = model(torch.tensor(X_train))
  4. labels = torch.tensor(y_train).unsqueeze(1)
  5. loss = criterion(outputs,labels.float())
  6. train_accuracy = model.compute_accuracy(outputs, labels)
  7. optimizer.zero_grad()
  8. loss.backward()
  9. optimizer.step()
  10. if (epoch + 1) % 10 == 0:
  11. 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
....

测试

  1. X_test, y_test = generate_rand_samples() # 生成测试集
  2. X_test = (X_test - X_test.mean(axis=0)) / X_test.std(axis=0)
  3. test_inputs = torch.tensor(X_test)
  4. test_labels = torch.tensor(y_test).unsqueeze(1)
  5. with torch.no_grad():
  6. outputs = model(test_inputs)
  7. accuracy = model.compute_accuracy(outputs, test_labels)
  8. print(f"Test Accuracy: {accuracy:.4f}")

绘图:

  1. x_min, x_max = X_test[:, 0].min() - 1, X_test[:, 0].max() + 1
  2. y_min, y_max = X_test[:, 1].min() - 1, X_test[:, 1].max() + 1
  3. xx, yy = torch.meshgrid(torch.linspace(x_min, x_max, 100), torch.linspace(y_min, y_max, 100))
  4. # 预测每个点的类别
  5. Z = torch.argmax(model(torch.cat((xx.reshape(-1,1), yy.reshape(-1,1)), 1)), 1)
  6. Z = Z.reshape(xx.shape)
  7. # 绘制分类图
  8. plt.contourf(xx, yy, Z, cmap=plt.cm.Spectral,alpha=0.0)
  9. # 绘制分界线
  10. w = model.linear.weight.detach().numpy() # 权重
  11. b = model.linear.bias.detach().numpy() # 偏置
  12. x1 = np.linspace(x_min, x_max, 100)
  13. x2 = (-b - w[0][0]*x1) / w[0][1]
  14. plt.plot(x1, x2, 'k-')
  15. # 绘制样本点
  16. plt.scatter(X_train[:, 0], X_train[:, 1], c=y_train, cmap=plt.cm.Spectral)
  17. plt.show()

以上就是基于Pytorch实现分类器的示例详解的详细内容,更多关于Pytorch分类器的资料请关注w3xue其它相关文章!

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号