Skip to content

Feature Request in Layers: Store best model with save_best_only. #296

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

Closed
cpury opened this issue May 15, 2018 · 7 comments
Closed

Feature Request in Layers: Store best model with save_best_only. #296

cpury opened this issue May 15, 2018 · 7 comments

Comments

@cpury
Copy link

cpury commented May 15, 2018

TensorFlow.js version

0.10.3

Browser version

Latest Chrome

Describe the problem or feature request

When training on a just a few samples as usually happens in the browser, training and validation loss tend to jump around a bit.

It would be great if we could automatically get the best model after training finishes.

This is possible in Keras with the ModelCheckpoint(save_best_only=True) callback and afterwards loading the weights from that saved file.

Alternatively, if I knew how to copy a model in its current state, it would be straightforward to write the logic for this in a onEpochEnd callback.

@cpury cpury changed the title Store best model Feature Request: Store best model May 15, 2018
@bileschi
Copy link
Contributor

Great suggestion! FYI, model copying is coming soon. Please follow tensorflow/tfjs-core#1038 for guidance on that front.

@cpury
Copy link
Author

cpury commented Jul 25, 2018

@bileschi it seems model copying is now implemented, which indirectly makes this feature possible.

Is there a best practice / guide to retain the best model in training? That would also be a useful addition to the docs, I believe.

@bileschi
Copy link
Contributor

There is not such a guide or demo at this point. Agreed that it would make an excellent addition to the tutorials.

@cpury
Copy link
Author

cpury commented Jul 25, 2018

@bileschi I went ahead and tried it out. Works great! Thanks for pointing me to the other issue.

Here's what I did:

let bestValLoss = Number.MAX_SAFE_INTEGER;
const bestModelPath = 'localstorage://best-model';

model.fit(..., {
    ...,
    callbacks: {
        onEpochEnd: async function (epoch, logs) {
            if (logs.val_loss < bestValLoss) {
                // Store best model:
                bestValLoss = logs.val_loss;
                await model.save(bestModelPath);
            }
        },
        onTrainEnd: async function () {
            // Load best model:
            model = await tf.loadModel(bestModelPath);
        },
    }
});

Note that if you want to keep training afterwards, you need to .compile() the model again.

@cpury
Copy link
Author

cpury commented Jul 25, 2018

Quick question: Does TF.JS await the callbacks? Otherwise there might be a race condition here.

@nsthorat nsthorat added P3 type:feature New feature or request labels Oct 24, 2018
@nsthorat nsthorat changed the title Feature Request: Store best model Feature Request in Layers: Store best model with save_best_only. Oct 24, 2018
@caisq caisq assigned davidsoergel and unassigned caisq Feb 12, 2019
@google-ml-butler
Copy link

This issue has been automatically marked as stale because it has not had recent activity. It will be closed in 7 days if no further activity occurs. Thank you.

@google-ml-butler
Copy link

Closing as stale. Please @mention us if this needs more attention.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants