Skorch refits the data when I call load_params to load the saved parameters

username: Nidhal_Baccouri
cooked: <p>So I’m using skorch for a Project. I find it super cool, the API is straightforward to work with. I trained my NN and I’m using a Checkpoint callback to save the best params, optimizer and history as described here: [<a href="https://skorch.readthedocs.io/en/stable/callbacks.html" class="inline-onebox" rel="noopener nofollow ugc">skorch.callbacks — skorch 0.15.0 documentation</a>] , so now I want to load the saved model or rather parameters and I’m using the description in the docs:<br>
<a href="https://skorch.readthedocs.io/en/v0.7.0/user/save_load.html" rel="noopener nofollow ugc">load_params</a> . As I said, I saved the model using Checkpoint object so I thought why not using the same Checkpoint object to load the model but I think it is messy because I must declare the Checkpoint object again etc… I noticed when I load the model that somehow the fit function is called again and my model is starting a training process again! can someone tell me why it is happening or what is wrong with my code maybe? another thing is that I find it bothering to again write a Checkpoint object to load the model after saving it and I’m sure there is a better way that’s why I want to ask is there any way to do this like in pytorch by simply calling torch.load(model_path) ?</p>
<p>this is my code:</p>
<blockquote>
<p>from skorch.callbacks import Checkpoint, TrainEndCheckpoint, LoadInitState<br>
from skorch.dataset import CVSplit<br>
from skorch.regressor import NeuralNetRegressor<br>
from models import BearingCoarseNetwork, BearingDifferenceNetwork<br>
import torch<br>
from torch import optim<br>
from main import get_datasets</p>
<p>device = ‘cuda’ if torch.cuda.is_available() else ‘cpu’</p>
<p>coarse_checkpoint = Checkpoint(dirname=‘results/bearing/coarse’)<br>
coarse_train_end_checkpoint = TrainEndCheckpoint(dirname=‘results/bearing/coarse’)<br>
bearing_coarse_model = NeuralNetRegressor(<br>
module=BearingCoarseNetwork(n_hidden=32),<br>
)</p>
<p>diff_checkpoint = Checkpoint(dirname=‘results/bearing/difference’)<br>
diff_train_end_checkpoint = TrainEndCheckpoint(dirname=‘results/bearing/difference’)<br>
bearing_difference_model = NeuralNetRegressor(<br>
module=BearingDifferenceNetwork(n_hidden=32),<br>
)</p>
<p>bearing_coarse_model.initialize()<br>
bearing_difference_model.initialize()<br>
bearing_coarse_model.load_params(checkpoint=coarse_checkpoint)<br>
bearing_difference_model.load_params(checkpoint=diff_checkpoint)</p>
<p>trainset, testset = get_datasets()<br>
coarse_preds = bearing_coarse_model.predict(trainset)<br>
diff_preds = bearing_difference_model.predict(trainset)<br>
y_final = coarse_preds + diff_preds<br>
print(y_final.shape)</p>
</blockquote>

username: ottonemo
cooked: <blockquote>
<p>I noticed when I load the model that somehow the fit function is called again and my model is starting a training process again! can someone tell me why it is happening or what is wrong with my code maybe?</p>
</blockquote>
<p>Loading parameters from a checkpoint or from somewhere does not start the fit loop. Your code doesn’t seem complete, so it is hard for me to replicate this issue. Can you confirm this is not a bug on your side or post a more complete example?</p>
<blockquote>
<p>another thing is that I find it bothering to again write a <code>Checkpoint</code> object to load the model after saving it and I’m sure there is a better way that’s why I want to ask is there any way to do this like in pytorch by simply calling <code>torch.load(model_path)</code>?</p>
</blockquote>
<p>Once the checkpoint callback writes a model (i.e., parameters, history and optimizer), you can always specify the parameter file path in <code>net.load_params</code> alone via the <code>f_params</code> parameter. In your example you have</p>
<pre><code>coarse_checkpoint = Checkpoint(dirname=‘results/bearing/coarse’)
</code></pre>
<p>which will write the model parameter to <code>'results/bearing/coarse/params.pt'</code> (<a href="https://skorch.readthedocs.io/en/stable/callbacks.html#skorch.callbacks.Checkpoint" rel="noopener nofollow ugc">documentation</a>). You can then load the parameters individually using</p>
<pre><code>net.load_params(f_params='results/bearing/coarse/params.pt')
</code></pre>
<p>The basic loading/saving workflow is documented <a href="https://skorch.readthedocs.io/en/stable/user/save_load.html#general-approach" rel="noopener nofollow ugc">here</a>.</p>