import 必要的库
import torch
import torch.nn as nn
from torch.autograd import Variable
定义超参数并赋初值
input_layer = 100 #输入层
hidden_layer = 1000 #隐藏层
output_layer = 1 #输出层
batch_size = 100 #训练样本数
EPOCH = 100 #迭代次数
LR = 0.1 #学习率
随机生成训练数据
x = Variable(torch.randn(batch_size,input_layer))
y = Variable(torch.randn(batch_size,output_layer))
使用序贯模型定义网络结构
model = nn.Sequential(nn.Linear(input_layer,hidden_layer),
nn.ReLU(),
nn.Linear(hidden_layer,output_layer)
)
定义损失函数和优化器
loss_fun = nn.MSELoss() #使用均方差误差作为损失函数
optimizer = torch.optim.Adam(model.parameters(),lr = LR) #使用Adam优化器
开始训练
for epoch