当前位置:   article > 正文

PyTorch学习(6)—快速搭建法_import torchfrom torch.autograd import variableimp

import torchfrom torch.autograd import variableimport torch.nn.functional as

本篇博客主要介绍如何在PyTorch中更加快速便捷地搭建神经网络。

示例代码:

  1. import torch
  2. from torch.autograd import Variable
  3. import torch.nn.functional as F
  4. import matplotlib.pyplot as plt
  5. # 生成假数据
  6. n_data = torch.ones(100, 2)
  7. x0 = torch.normal(2*n_data, 1) # class0 x data (tensor), shape=(100, 2)
  8. y0 = torch.zeros(100) # class0 y data (tensor), shape=(100, 1)
  9. x1 = torch.normal(-2*n_data, 1) # class1 x data (tensor), shape=(100, 2)
  10. y1 = torch.ones(100) # class1 y data (tensor), shape=(100, 1)
  11. x = torch.cat((x0, x1), 0).type(torch.FloatTensor) # shape (200, 2) FloatTensor = 32-bit floating
  12. y = torch.cat((y0, y1), ).type(torch.LongTensor) # shape (200,) LongTensor = 64-bit integer
  13. # 将Tensor转换为torch
  14. x, y = Variable(x), Variable(y)
  15. # 打印数据散点图
  16. # plt.scatter(x.data.numpy()[:, 0], x.data.numpy()[:, 1], c=y.data.numpy(), s=100, l
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/IT小白/article/detail/358168?site
推荐阅读
相关标签
  

闽ICP备14008679号