gpt4 book ai didi

python - 如何在 nn.LSTM pytorch 中取得 R2 分数

转载 作者:行者123 更新时间:2023-12-02 02:24:53 25 4
gpt4 key购买 nike

我试图在 nn.LSTM 中使用 R2 制作损失函数,但我找不到任何关于它的文档。我已经使用了 pytorch 的 RMSE 和 MAE 损失。

我的数据是一个时间序列,我在做时间序列预测

这是我在数据训练中使用 RMSE 损失函数的代码

model = LSTM_model(input_size=1, output_size=1, hidden_size=512, num_layers=2, dropout=0).to(device)
criterion = nn.MSELoss(reduction="sum")
optimizer = optim.Adam(model.parameters(), lr=0.001)
callback = Callback(model, early_stop_patience=10 ,outdir="model/lstm", plot_every=20,)


from tqdm.auto import tqdm

def loop_fn(mode, dataset, dataloader, model, criterion, optimizer,device):
if mode =="train":
model.train()
elif mode =="test":
model.eval()
cost = 0
for feature, target in tqdm(dataloader, desc=mode.title()):
feature, target = feature.to(device), target.to(device)
output , hidden = model(feature,None)
loss = torch.sqrt(criterion(output,target))

if mode =="train":
loss.backward()
optimizer.step()
optimizer.zero_grad()

cost += loss.item() * feature.shape[0]
cost = cost / len(dataset)
return cost

这是开始数据训练的代码

while True :
train_cost = loop_fn("train", train_set, trainloader, model, criterion, optimizer,device)
with torch.no_grad():
test_cost = loop_fn("test", test_set, testloader, model, criterion, optimizer,device)

callback.log(train_cost, test_cost)

callback.save_checkpoint()

callback.cost_runtime_plotting()


if callback.early_stopping(model, monitor="test_cost"):
callback.plot_cost()
break

任何人都可以帮助我使用 R2 损失函数吗?提前谢谢你

最佳答案

这是一个实现,

"""
From https://en.wikipedia.org/wiki/Coefficient_of_determination
"""
def r2_loss(output, target):
target_mean = torch.mean(target)
ss_tot = torch.sum((target - target_mean) ** 2)
ss_res = torch.sum((target - output) ** 2)
r2 = 1 - ss_res / ss_tot
return r2

你可以像下面这样使用它,

loss = r2_loss(output, target)
loss.backward()

关于python - 如何在 nn.LSTM pytorch 中取得 R2 分数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65840698/

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