gpt4 book ai didi

python - 验证损失不随回归中的 MLP 移动

转载 作者:太空狗 更新时间:2023-10-30 01:17:58 27 4
gpt4 key购买 nike

给定输入特征,仅原始数字:

tensor([0.2153, 0.2190, 0.0685, 0.2127, 0.2145, 0.1260, 0.1480, 0.1483, 0.1489,
0.1400, 0.1906, 0.1876, 0.1900, 0.1925, 0.0149, 0.1857, 0.1871, 0.2715,
0.1887, 0.1804, 0.1656, 0.1665, 0.1137, 0.1668, 0.1168, 0.0278, 0.1170,
0.1189, 0.1163, 0.2337, 0.2319, 0.2315, 0.2325, 0.0519, 0.0594, 0.0603,
0.0586, 0.0067, 0.0624, 0.2691, 0.0617, 0.2790, 0.2805, 0.2848, 0.2454,
0.1268, 0.2483, 0.2454, 0.2475], device='cuda:0')

并且预期输出是单个实数输出,例如

tensor(-34.8500, device='cuda:0')

https://www.kaggle.com/alvations/pytorch-mlp-regression 上的完整代码

我尝试创建一个简单的 2 层网络:

class MLP(nn.Module):
def __init__(self, input_size, output_size, hidden_size):
super(MLP, self).__init__()
self.linear = nn.Linear(input_size, hidden_size)
self.classifier = nn.Linear(hidden_size, output_size)

def forward(self, inputs, hidden=None, dropout=0.5):
inputs = F.dropout(inputs, dropout) # Drop-in.
# First Layer.
output = F.relu(self.linear(inputs))

# Matrix manipulation magic.
batch_size, sequence_len, hidden_size = output.shape
# Technically, linear layer takes a 2-D matrix as input, so more manipulation...
output = output.contiguous().view(batch_size * sequence_len, hidden_size)
# Apply dropout.
output = F.dropout(output, dropout)

# Put it through the classifier
# And reshape it to [batch_size x sequence_len x vocab_size]
output = self.classifier(output).view(batch_size, sequence_len, -1)

return output

这样的训练:

# Training routine.
def train(num_epochs, dataloader, valid_dataset, model, criterion, optimizer):
losses = []
valid_losses = []
learning_rates = []
plt.ion()
x_valid, y_valid = valid_dataset
for _e in range(num_epochs):
for batch in tqdm(dataloader):
# Zero gradient.
optimizer.zero_grad()
#print(batch)
this_x = torch.tensor(batch['x'].view(len(batch['x']), 1, -1)).to(device)
this_y = torch.tensor(batch['y'].view(len(batch['y']), 1, 1)).to(device)

# Feed forward.
output = model(this_x)

prediction, _ = torch.max(output, dim=1)
loss = criterion(prediction, this_y.view(len(batch['y']), -1))
loss.backward()
optimizer.step()
losses.append(torch.sqrt(loss.float()).data)

with torch.no_grad():
# Zero gradient.
optimizer.zero_grad()
output = model(x_valid.view(len(x_valid), 1, -1))
prediction, _ = torch.max(output, dim=1)
loss = criterion(prediction, y_valid.view(len(y_valid), -1))
valid_losses.append(torch.sqrt(loss.float()).data)

clear_output(wait=True)
plt.plot(losses, label='Train')
plt.plot(valid_losses, label='Valid')
plt.legend()
plt.pause(0.05)

调整了几个超参数,看起来模型没有很好地训练,验证损失根本没有移动,例如

hyperparams = Hyperparams(input_size=train_dataset.x.shape[1], 
output_size=1,
hidden_size=150,
loss_func=nn.MSELoss,
learning_rate=1e-8,
optimizer=optim.Adam,
batch_size=500)

这是损失曲线:

enter image description here

知道网络出了什么问题吗?

我是否在使用错误的损失训练回归模型?或者我还没有找到合适的超参数?

还是我错误地验证了模型?

最佳答案

从您提供的代码中,很难说出为什么验证损失是恒定的,但我在您的代码中看到了几个问题。

  1. 为什么要对每个训练小批量进行验证?相反,您应该在完成一个完整时期的训练(遍历整个数据集一次)后验证您的模型。所以,骨架应该是这样的:
for _e in range(num_epochs):
for batch in tqdm(train_dataloader):
# training code

with torch.no_grad():
for batch in tqdm(valid_dataloader):
# validation code

# plot your loss values

此外,您可以在每个时期之后绘制,而不是在每次小批量训练之后绘制。

  1. 在训练过程中,您是否检查过 optimizer.step() 之后模型参数是否得到更新?你有多少验证示例?为什么不在验证期间使用小批量计算?

  2. 为什么在验证期间执行:optimizer.zero_grad()?这没有意义,因为在验证期间,您不会做任何与优化相关的事情。

  3. 您应该在验证期间使用 model.eval() 来关闭 dropouts。请参阅 PyTorch 文档以了解 .train().eval() 方法。

  4. 学习率设置为1e-8,是不是太小了?为什么不使用 Adam (1e-3) 的默认学习率?

以下需要一些推理。

  1. 为什么要使用如此大的批量大小?您的训练数据集大小是多少?

  2. 您可以直接绘制 MSELoss,而不是求平方根。

我的建议是:在 PyTorch 中使用一些现有的 MLP 资源。如果此时您没有足够的知识,请不要从头开始。这会让你很痛苦。

关于python - 验证损失不随回归中的 MLP 移动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56069685/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com