0

I want to load and access a pretrained model from the directory outside where all files are saved. Here is how directory structure is:

-MyProject
----Model_checkpoint_and_scripts
------access_model.py
--run_model.py
--other files

When run model calls access_model.py it looks for model.py in current working directory and does not find it. As suggested here, I could use

the_model = TheModelClass(*args, **kwargs)
the_model.load_state_dict(torch.load(PATH))

But in this case what is a good way to save all the arguments to initialize the model? I am thinking to pickle the command line args but there are some arguments like vocab size which are calculated.

Thanks

Parag
  • 662
  • 9
  • 15
  • I am wondering whether your question is related to loading pre-trained models from a different working directory or you want to save arguments which initialized the model? To save arguments to initialize the model, you can simply put arguments in a dictionary and save it any file, it shouldn't matter. – Wasi Ahmad Nov 26 '17 at 11:18
  • I want to save the arguments to initialize the model. Yes, I could put them in a dict and save, which I am able to do currently. I was looking for a better way. – Parag Nov 26 '17 at 11:34
  • better in what aspect? how are you thinking to improve? – Wasi Ahmad Nov 26 '17 at 12:05
  • If there is a way I could provide model class file location also... so that the_model = torch.load(PATH) works, which currently is not working because it looks explicitly for model.py in the current directory. It will be easy to distribute the trained model. – Parag Nov 26 '17 at 13:50
  • Essentially, now I will have to save 2 files. Which is not looking good(to me only maybe). – Parag Nov 26 '17 at 13:52

1 Answers1

1

I was able to load it by adding the following lines:

here = os.path.dirname(os.path.abspath(__file__))
sys.path.append(here)
Parag
  • 662
  • 9
  • 15