Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add save and load functions for BiVAECF model #608

Merged
merged 2 commits into from
Mar 27, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 56 additions & 0 deletions cornac/models/bivaecf/recom_bivaecf.py
Original file line number Diff line number Diff line change
Expand Up @@ -255,3 +255,59 @@ def get_item_vectors(self):
"""
item_vectors = self.bivae.mu_beta.detach().cpu().numpy()
return item_vectors

def save(self, save_dir=None, save_trainset=True):
"""Save model to the filesystem.

Parameters
----------
save_dir: str, default: None
Path to a directory for the model to be stored.

save_trainset: bool, default: True
Save train_set together with the model. This is useful
if we want to deploy model later because train_set is
required for certain evaluation steps.

Returns
-------
model_file : str
Path to the model file stored on the filesystem.
"""
import torch

if save_dir is None:
return

self.bivae.to(torch.device("cpu"))
model_file = Recommender.save(
self, save_dir=save_dir, save_trainset=save_trainset
)

return model_file

@staticmethod
def load(model_path, trainable=False):
"""Load model from the filesystem.

Parameters
----------
model_path: str, required
Path to a file or directory where the model is stored. If a directory is
provided, the latest model will be loaded.

trainable: boolean, optional, default: False
Set it to True if you would like to finetune the model. By default,
the model parameters are assumed to be fixed after being loaded.

Returns
-------
self : object
"""
import torch

model = Recommender.load(model_path, trainable)
if "cuda" in str(model.device) and torch.cuda.is_available():
model.bivae.to(model.device)

return model
Loading