This repository was archived by the owner on Oct 17, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 96
Add Model.save(); Let loadModel() support IOHandlers #161
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
/** | ||
* @license | ||
* Copyright 2018 Google LLC | ||
* | ||
* Use of this source code is governed by an MIT-style | ||
* license that can be found in the LICENSE file or at | ||
* https://opensource.org/licenses/MIT. | ||
* ============================================================================= | ||
*/ | ||
|
||
import {io} from '@tensorflow/tfjs-core'; | ||
|
||
import {Dense} from './layers/core'; | ||
import {Sequential} from './models'; | ||
import {describeMathCPUAndGPU} from './utils/test_utils'; | ||
|
||
describeMathCPUAndGPU('Model.save', () => { | ||
class IOHandlerForTest implements io.IOHandler { | ||
savedArtifacts: io.ModelArtifacts; | ||
|
||
async save(modelArtifacts: io.ModelArtifacts): Promise<io.SaveResult> { | ||
this.savedArtifacts = modelArtifacts; | ||
return {modelArtifactsInfo: null}; | ||
} | ||
} | ||
|
||
class EmptyIOHandler implements io.IOHandler {} | ||
|
||
it('Saving all weights succeeds', async done => { | ||
const model = new Sequential(); | ||
model.add(new Dense({units: 3, inputShape: [5]})); | ||
const handler = new IOHandlerForTest(); | ||
|
||
model.save(handler) | ||
.then(saveResult => { | ||
expect(handler.savedArtifacts.modelTopology) | ||
.toEqual(model.toJSON(null, false)); | ||
expect(handler.savedArtifacts.weightSpecs.length).toEqual(2); | ||
expect(handler.savedArtifacts.weightSpecs[0].name.indexOf('/kernel')) | ||
.toBeGreaterThan(0); | ||
expect(handler.savedArtifacts.weightSpecs[0].shape).toEqual([5, 3]); | ||
expect(handler.savedArtifacts.weightSpecs[0].dtype) | ||
.toEqual('float32'); | ||
expect(handler.savedArtifacts.weightSpecs[1].name.indexOf('/bias')) | ||
.toBeGreaterThan(0); | ||
expect(handler.savedArtifacts.weightSpecs[1].shape).toEqual([3]); | ||
expect(handler.savedArtifacts.weightSpecs[1].dtype) | ||
.toEqual('float32'); | ||
done(); | ||
}) | ||
.catch(err => { | ||
console.error(err.stack); | ||
}); | ||
}); | ||
|
||
it('Saving only trainable weights succeeds', async done => { | ||
const model = new Sequential(); | ||
model.add(new Dense({units: 3, inputShape: [5], trainable: false})); | ||
model.add(new Dense({units: 2})); | ||
const handler = new IOHandlerForTest(); | ||
|
||
model.save(handler, {trainableOnly: true}) | ||
.then(saveResult => { | ||
expect(handler.savedArtifacts.modelTopology) | ||
.toEqual(model.toJSON(null, false)); | ||
// Verify that only the trainable weights (i.e., weights from the | ||
// 2nd, trainable Dense layer) are saved. | ||
expect(handler.savedArtifacts.weightSpecs.length).toEqual(2); | ||
expect(handler.savedArtifacts.weightSpecs[0].name.indexOf('/kernel')) | ||
.toBeGreaterThan(0); | ||
expect(handler.savedArtifacts.weightSpecs[0].shape).toEqual([3, 2]); | ||
expect(handler.savedArtifacts.weightSpecs[0].dtype) | ||
.toEqual('float32'); | ||
expect(handler.savedArtifacts.weightSpecs[1].name.indexOf('/bias')) | ||
.toBeGreaterThan(0); | ||
expect(handler.savedArtifacts.weightSpecs[1].shape).toEqual([2]); | ||
expect(handler.savedArtifacts.weightSpecs[1].dtype) | ||
.toEqual('float32'); | ||
done(); | ||
}) | ||
.catch(err => { | ||
console.error(err.stack); | ||
}); | ||
}); | ||
|
||
it('Saving to a handler without save method fails', async done => { | ||
const model = new Sequential(); | ||
model.add(new Dense({units: 3, inputShape: [5]})); | ||
const handler = new EmptyIOHandler(); | ||
model.save(handler) | ||
.then(saveResult => { | ||
fail( | ||
'Saving with an IOHandler without `save` succeeded ' + | ||
'unexpectedly.'); | ||
}) | ||
.catch(err => { | ||
expect(err.message) | ||
.toEqual( | ||
'Model.save() cannot proceed because the IOHandler ' + | ||
'provided does not have the `save` attribute defined.'); | ||
done(); | ||
}); | ||
}); | ||
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the design settled on is not backward compatible, so we'll need to make sure we bump the middle version number, right? (While we still accept strings like before, we require a prefix that we didn't in the past)