任何人都可以告诉我如何检查Pytorch模型是否存在,如果存在,删除它并用新的替换它?

问题描述 投票:-1回答:1

因此,我保存了许多用于训练的火炬模型以及不同的批量大小和时代,并且模型使用时间序列和批量大小进行保存。基本上我有时会更改一些图层超参数和一些增强来检查预测结果,但如果火炬模型在那里,我想删除它并用新的替换它。

python pytorch
1个回答
0
投票

最简单的解决方案是简单地保存具有相同名称的模型,基本上覆盖现有模型。这相当于检查它是否存在,删除然后保存。

如果你想明确检查它是否存在,你可以使用os.轻松完成

import os
if os.path.exists('path/to/model.pth'):  # checking if there is a file with this name
    os.remove('path/to/model.pth')  # deleting the file
    torch.save(model, 'path/to/model.pth')  # saving a new model with the same name
© www.soinside.com 2019 - 2024. All rights reserved.