import torch
from torch.autograd import Variable
import torch.nn.functional as F
import matplotlib.pyplot as plt # torch.manual_seed(1) # reproducible # make fake data
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 # The code below is deprecated in Pytorch 0.4. Now, autograd directly supports tensors
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) # hidden layer
self.out = torch.nn.Linear(n_hidden, n_output) # output layer def forward(self, x):
x = F.relu(self.hidden(x)) # activation function for hidden layer
x = self.out(x)
return x net = Net(n_feature=2, n_hidden=10, n_output=2) # define the network,输入两个特征
print(net) # net architecture optimizer = torch.optim.SGD(net.parameters(), lr=0.02)
loss_func = torch.nn.CrossEntropyLoss() # the target label is NOT an one-hotted
#分类输出的为概率 plt.ion() # something about plotting for t in range(100):
out = net(x) # input x and predict based on x,输出原值不是概率,需要用激活函数转化为概率
loss = loss_func(out, y) # must be (1. nn output, 2. target), the target label is NOT one-hotted optimizer.zero_grad() # clear gradients for next train
loss.backward() # backpropagation, compute gradients
optimizer.step() # apply gradients if t % 2 == 0:
# plot and show learning process
plt.cla()
prediction = torch.max(F.softmax(out), 1)[1]
pred_y = prediction.data.numpy()
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()

输出结果是将散点图分为两类。

最新文章

  1. html5 Websockets development guidance
  2. rake deploy ! [rejected] master -> master (non-fast-forward) error: failed to push some refs to解决方法
  3. Culcurse
  4. Windows metro app wcf 地址可配置
  5. javascript中方法调用与方括号[]
  6. AutoCompleteTextView不能使用的问题
  7. 想了解JAVA的,看看(转载)
  8. Redis 安装与配置
  9. ASP.NET MVC 教程汇总
  10. bzoj2631: tree lct
  11. ipa制作
  12. 简单的用js打印乘法口诀表
  13. ionic 3 常见报错及解决办法
  14. Python开发端口扫描器
  15. 让IIS7和IIS6识别PUT和DELETE请求
  16. x86,x64,Any CPU区别
  17. 全源最短路径 - floyd算法 - O(N ^ 3)
  18. 代码: js日期
  19. htoi的实现
  20. thinkphp中获取参数值的方法

热门文章

  1. Windows将自己的代码发布到Github上
  2. vue style标签中使用less
  3. 【重磅】FineUIPro基础版免费,是时候和ExtJS说再见了!
  4. Mariadb第一章:介绍及安装--小白博客
  5. Python Revisited Day 05(模块)
  6. DEVOPS 运维开发系列
  7. 使用FaceNet 图像相识度对比
  8. nodejs开发辅助工具nodemon
  9. 根据 Power BI Desktop(预览版)中的报表页创建工具提示
  10. Djangon