DeepGraphLearning / torchdrug

A powerful and flexible machine learning platform for drug discovery
https://torchdrug.ai/
Apache License 2.0
1.42k stars 199 forks source link

How to load an existing checkpoint and train it for more epochs #20

Open scintiller opened 3 years ago

scintiller commented 3 years ago

For example, after train a model for 3 epochs and save it as model_3epoch.pkl, how to load model_3epoch.pkl and train it for more epochs?

Besides that, if we train model_3epoch.pkl with the training dataset, can we load it and train it with the test dataset then?

The question was asked here at first.

KiddoZhu commented 3 years ago

Yes. You can load a checkpoint at any time by solver.load("model_3epoch.pkl"). And then do whatever you want, like further training or inference.

Notice that currently there are a little performance difference between a model trained from checkpoint (e.g. 3 epochs + 3 epochs) and a model trained from scratch (e.g. 6 epochs).

lonngxiang commented 1 year ago

Yes. You can load a checkpoint at any time by solver.load("model_3epoch.pkl"). And then do whatever you want, like further training or inference.

Notice that currently there are a little performance difference between a model trained from checkpoint (e.g. 3 epochs + 3 epochs) and a model trained from scratch (e.g. 6 epochs).

Can you provide the complete load code, and how to define model and task

GZ82 commented 9 months ago

Hi Followed by the question of lonngxiang, I also looking for example code to predict new data using trained model. Currently in order to load the training model I need to at least prepare train_set as following:

model = models.GIN(input_dim=67,
                   hidden_dims=[261, 251],
                   short_cut=True, batch_norm=True, concat_hidden=True) # input_dim=dataset.node_feature_dim
task = tasks.PropertyPrediction(
    model, task=['degradable'],
    criterion="bce", metric=("auprc", "auroc"),
) # task=dataset.tasks
optimizer = torch.optim.Adam(task.parameters(), lr=1e-3) 
solver = core.Engine(
    task, train_set=train_set, valid_set=None, test_set=None, optimizer=optimizer,
    gpus=[0], batch_size=1024, log_interval=1000
)

solver.load("trianed_model.pkl")

before I made predictions using:

preds = F.sigmoid(task.predict(samples))
targets = task.target(samples)

is it possible that not prepare the train_set to make predictions? btw why predictions does not need solver? I guess solver and task are linked, sorry this may be a noob question