Can anyone tell me how to checking if Pytorch model exists, and if it does, delete it and replace it with a new one?
So I save a lot of torch models for training and with different batchsize and epochs, and the models are saves with strings of epoch and batchsize. Basically I sometimes change some layers hyperparamters and some augmentation to check the prediction results, but if the torch model is there, I want to delete it and replace it with the new one.

The simplest solution is simply saving a model with the same name, essentially overwriting the existing one. This is equivalent to checking if it exists, deleting and then saving.

If you want to explicitly check if it exists, you can do that easily with 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

