06-pytorch(快速搭建神经网络)

import torch 
from torch.autograd import Variable 
import torch.nn.functional as F
import numpy as np
n_data = torch.ones(100,2) # 打印[100,2]矩阵的1
# 第一个数据集
x0 = torch.normal(2*n_data,1)
y0 = torch.zeros(100)
# 第二个数据集
x1 = torch.normal(-2*n_data,1)
y1 = torch.ones(100)
# 合并数据集  --> 合并 并改变格式
x = torch.cat((x0,x1),0).type(torch.FloatTensor)     # 32位浮点数
y = torch.cat((y0,y1)).type(torch.LongTensor)         # 64 位整型

快速定义一个神经网络

net2 = torch.nn.Sequential(
        torch.nn.Linear(2,10),
        torch.nn.ReLU(),
        torch.nn.Linear(10,2)
)
print(net2)
Sequential(
  (0): Linear(in_features=2, out_features=10, bias=True)
  (1): ReLU()
  (2): Linear(in_features=10, out_features=2, bias=True)
)
posted @ 2019-07-08 18:08  childhood_2  阅读(180)  评论(0编辑  收藏  举报