当前位置:   article > 正文

Pytorch实现二分类器_torch 二分类

torch 二分类

以下我们用 PyTorch 实现一个很简单的二分类器,所用的数据来自 Scikit learn。

首先来生成含200个样本的数据,并绘制出样本的散点图如下图所示:

  1. import matplotlib.pyplot as plt
  2. from sklearn.cluster import SpectralClustering
  3. import sklearn.datasets
  4. X,y = sklearn.datasets.make_moons(200,noise=0.2)
  5. plt.scatter(X[:,0],X[:,1],s=40,c=y,cmap=plt.cm.Spectral)
<matplotlib.collections.PathCollection at 0x149ce908>

可以看到生成了两类数据,分别用 0 和 1 来表示。我们接下来将要在这个样本数据上构造一个分类器,采用的是一个很简单的全连接网络,网络结构如下:

这个网络包含一个输入层,一个中间层,一个输出层。中间层包含 3 个神经元,使用的激活函数是 tanh。当然,中间层的神经元越多,分类效果一般越好,但这个 3 层的网络对于我们的样本数据已经足够用了。我们来算一下参数数量:上图中一共有 6+6 = 12 条线,就是 12 个权重,加上 3+ 2 = 5 个 bias,一共 17 个参数需要训练。

接下来将样本数据从 numpy 转成 tensor:

  1. X = torch.from_numpy(X).type(torch.FloatTensor)
  2. y = torch.from_numpy(y).type(torch.LongTensor)

开始构建神经网络,其中损失函数用交叉熵损失函数,梯度优化器用Adam。 

  1. import torch.nn as nn
  2. import torch.nn.functional as F
  3. class MyClassifier(nn.Module):
  4. def __init__(self):
  5. super(MyClassifier,self).__init__()
  6. self.fc1 = nn.Linear(2,3)
  7. self.fc2 = nn.Linear(3,2)
  8. def forward(self,x):
  9. x = self.fc1(x)
  10. x = F.tanh(x)
  11. x = self.fc2(x)
  12. return x
  13. def predict(self,x):
  14. pred = F.softmax(self.forward(x))
  15. ans = []
  16. for t in pred:
  17. if t[0]>t[1]:
  18. ans.append(0)
  19. else:
  20. ans.append(1)
  21. return torch.tensor(ans)
  1. model = Net()
  2. criterion = nn.CrossEntropyLoss() #交叉熵损失函数
  3. optimizer = torch.optim.Adam(model.parameters(), lr=0.01) #Adam梯度优化器

 训练:

  1. epochs = 10000
  2. losses = []
  3. for i in range(epochs):
  4. y_pred = model.forward(X)
  5. loss = criterion(y_pred,y)
  6. losses.append(loss.item())
  7. optimizer.zero_grad()
  8. loss.backward()
  9. optimizer.step()

查看训练误差: 

  1. from sklearn.metrics import accuracy_score
  2. print(accuracy_score(model.predict(X),y))
  3. # Output
  4. 0.995

 下面的函数帮助我们在两个分类之间画一条分界线,便于将结果可视化。

  1. def predict(x):
  2. x = torch.from_numpy(x).type(torch.FloatTensor)
  3. ans = model.predict(x)
  4. return ans.numpy()
  5. def plot_decision_boundary(pred_func,X,y):
  6. x_min, x_max = X[:, 0].min() - .5, X[:, 0].max()+ .5
  7. y_min, y_max = X[:, 1].min() - .5, X[:, 1].max()+ .5
  8. h = 0.01
  9. xx,yy=np.meshgrid(np.arange(x_min, x_max, h), np.arange(y_min, y_max, h))
  10. Z = pred_func(np.c_[xx.ravel(), yy.ravel()])
  11. Z = Z.reshape(xx.shape)
  12. plt.contourf(xx, yy, Z, cmap=plt.cm.Spectral)
  13. plt.scatter(X[:, 0], X[:, 1], c=y, cmap=plt.cm.binary)

分类结果: 

plot_decision_boundary(lambda x : predict(x) ,X.numpy(), y.numpy())

完整代码参见参考资料2,简单二分类器结果如下图所示 。

  1. import sklearn.datasets
  2. import torch
  3. import numpy as np
  4. np.random.seed(0)
  5. X, y = sklearn.datasets.make_moons(200,noise=0.2)
  6. import matplotlib.pyplot as plt
  7. plt.scatter(X[:,0],X[:,1],s=40,c=y,cmap=plt.cm.binary)
  8. X = torch.from_numpy(X).type(torch.FloatTensor)
  9. y = torch.from_numpy(y).type(torch.LongTensor)
  10. import torch.nn as nn
  11. import torch.nn.functional as F
  12. #our class must extend nn.Module
  13. class Net(nn.Module):
  14. def __init__(self):
  15. super(Net,self).__init__()
  16. #Our network consists of 3 layers. 1 input, 1 hidden and 1 output layer
  17. #This applies Linear transformation to input data.
  18. self.fc1 = nn.Linear(2,3)
  19. #This applies linear transformation to produce output data
  20. self.fc2 = nn.Linear(3,2)
  21. #This must be implemented
  22. def forward(self,x):
  23. #Output of the first layer
  24. x = self.fc1(x)
  25. #Activation function is Relu. Feel free to experiment with this
  26. x = F.tanh(x)
  27. #This produces output
  28. x = self.fc2(x)
  29. return x
  30. #This function takes an input and predicts the class, (0 or 1)
  31. def predict(self,x):
  32. #Apply softmax to output
  33. pred = F.softmax(self.forward(x))
  34. ans = []
  35. for t in pred:
  36. if t[0]>t[1]:
  37. ans.append(0)
  38. else:
  39. ans.append(1)
  40. return torch.tensor(ans)
  41. #Initialize the model
  42. model = Net()
  43. #Define loss criterion
  44. criterion = nn.CrossEntropyLoss()
  45. #Define the optimizer
  46. optimizer = torch.optim.Adam(model.parameters(), lr=0.01)
  47. #Number of epochs
  48. epochs = 50000
  49. #List to store losses
  50. losses = []
  51. for i in range(epochs):
  52. #Precit the output for Given input
  53. y_pred = model.forward(X)
  54. #Compute Cross entropy loss
  55. loss = criterion(y_pred,y)
  56. #Add loss to the list
  57. losses.append(loss.item())
  58. #Clear the previous gradients
  59. optimizer.zero_grad()
  60. #Compute gradients
  61. loss.backward()
  62. #Adjust weights
  63. optimizer.step()
  64. from sklearn.metrics import accuracy_score
  65. print(accuracy_score(model.predict(X),y))
  66. def predict(x):
  67. x = torch.from_numpy(x).type(torch.FloatTensor)
  68. ans = model.predict(x)
  69. return ans.numpy()
  70. # Helper function to plot a decision boundary.
  71. # If you don't fully understand this function don't worry, it just generates the contour plot below.
  72. def plot_decision_boundary(pred_func,X,y):
  73. # Set min and max values and give it some padding
  74. x_min, x_max = X[:, 0].min() - .5, X[:, 0].max() + .5
  75. y_min, y_max = X[:, 1].min() - .5, X[:, 1].max() + .5
  76. h = 0.01
  77. # Generate a grid of points with distance h between them
  78. xx,yy=np.meshgrid(np.arange(x_min, x_max, h), np.arange(y_min, y_max, h))
  79. # Predict the function value for the whole gid
  80. Z = pred_func(np.c_[xx.ravel(), yy.ravel()])
  81. Z = Z.reshape(xx.shape)
  82. # Plot the contour and training examples
  83. plt.contourf(xx, yy, Z, cmap=plt.cm.Spectral)
  84. plt.scatter(X[:, 0], X[:, 1], c=y, cmap=plt.cm.binary)
  85. plot_decision_boundary(lambda x : predict(x) ,X.numpy(), y.numpy())
# Output result:0.97

 

 

 

 

 

 

 

 

 

参考资料:

1. https://www.pytorchtutorial.com/pytorch-simple-classifier/

2. https://github.com/prudvinit/MyML/blob/master/lib/neural%20networks/pytorch%20moons.py

3. https://scikit-learn.org/stable/modules/classes.html#module-sklearn.cluster

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/酷酷是懒虫/article/detail/851238
推荐阅读
相关标签
  

闽ICP备14008679号