'''
其实这是一个错误示例,因为将x输入神经网络时,把x=[1,2,3,...,100]作为一个整体输入到了模型中!!
错误原因在于:每个坐标都应该有一个向量表示,如果把x作为一个整体输入到模型中,相当于x是某个数的向量。
改错方案:将x变化为二维向量,即x = [[1],[2],[3],...,[100]],然后输入到网络中,经过线性变换和非线性变化,得到结果。
'''
import torch
import torch.nn as nn
import torch.optim as optim
import math
import random
import numpy as np
import torch.nn.functional as F
# 设置随机种子
def setup_seed(seed):
torch.manual_seed(seed) #为cpu分配随机种子
if torch.cuda.is_available():
torch.cuda.manual_seed(seed) #为gpu分配随机种子
torch.cuda.manual_seed_all(seed) #若使用多块gpu,使用该命令设置随机种子
random.seed(seed)
np.random.seed(seed)
torch.backends.cudnn.deterministic = True
torch.backends.cudnn.benchmard = False
setup_seed(666)
class model(nn.Module):
def __init__(self, n_feature):
super(model, self).__init__()
self.linear = nn.Linear(100, 100)
def forward(self, x, target):
y = self.linear(x)
loss = MSE(y, target)
# print(target)
return loss
# Training loop
x = torch.linspace(1, 101, 100)
target = 2*x + 0.2*torch.rand(x.size())
model = model(n_feature=1)
optimizer = optim.SGD(model.parameters(), lr=0.5, momentum=0.9)
MSE = nn.MSELoss()
for epoch in range(500):
loss = model(x, target)
print(loss)
optimizer.zero_grad()
loss.backward()
optimizer.step()
正确示例
import torch
import torch.nn as nn
import torch.optim as optim
import math
import random
import numpy as np
import torch.nn.functional as F
# 设置随机种子
def setup_seed(seed):
torch.manual_seed(seed) #为cpu分配随机种子
if torch.cuda.is_available():
torch.cuda.manual_seed(seed) #为gpu分配随机种子
torch.cuda.manual_seed_all(seed) #若使用多块gpu,使用该命令设置随机种子
random.seed(seed)
np.random.seed(seed)
torch.backends.cudnn.deterministic = True
torch.backends.cudnn.benchmard = False
setup_seed(666)
class model(nn.Module):
def __init__(self, n_feature, n_hidden):
super(model, self).__init__()
self.hidden = torch.nn.Linear(n_feature, n_hidden) # 隐藏层
self.predict = torch.nn.Linear(n_hidden, n_feature) # 输出层
# def reset_parameters(self):
# torch.nn.init.kaiming_uniform_(self.weight, a=math.sqrt(5))
# if self.bias is not None:
# fan_in, _ = torch.nn.init._calculate_fan_in_and_fan_out(self.weight)
# bound = 1 / math.sqrt(fan_in)
# torch.nn.init.uniform_(self.bias, -bound, bound)
def forward(self, x, target):
y = F.relu(self.hidden(x)) # 隐藏层用 relu
y = self.predict(y)
loss = MSE(y, target)
# print(target)
return loss
# Training loop
x_data = torch.linspace(-1, 1, 100) # x_data设置小一点,可以快速得到结果
x = torch.unsqueeze(x_data, dim=-1)
target = x.pow(2) + 0.2*torch.rand(x.size())
model = model(n_feature=1, n_hidden=10)
optimizer = optim.SGD(model.parameters(), lr=0.5, momentum=0.9) # 学习率设置大一点,也可以快速得到结果
MSE = nn.MSELoss()
for epoch in range(10):
loss = model(x, target)
print(loss)
optimizer.zero_grad()
loss.backward()
optimizer.step()