使用pytorch创建神经网络并解决线性拟合和分类问题

#线性拟合
import torch
import torch.nn.functional as F
import matplotlib.pyplot as plt

torch.manual_seed(1) # reproducible

制作数据

x = torch.unsqueeze(torch.linspace(-1, 1, 100), dim=1) # x data (tensor), shape=(100, 1)
y = x.pow(2) + 0.2*torch.rand(x.size()) # noisy y data (tensor), shape=(100, 1)

定义普通的神经网络

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.predict = torch.nn.Linear(n_hidden, n_output) # output layer
# (hidden): Linear(in_features=1, out_features=10, bias=True)
# (predict): Linear(in_features=10, out_features=1, bias=True)
# 前向传播过程
def forward(self, x):
x = F.rel

上一篇:pythorch创建简单的神经网络源码


下一篇:0607-参数初始化策略