-
Notifications
You must be signed in to change notification settings - Fork 902
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
Word2Vec tests #173
Merged
Merged
Word2Vec tests #173
Changes from 1 commit
Commits
Show all changes
4 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
Add tests for Word2Vec
- validates that there are no leaked tensors - validates basic functionality of nearest - validates that add, subtract and average return things - Fixes memory leaks in add, subtract, average, and addOrSubtract functions - Adds a general dispose to the Word2Vec class
- Loading branch information
commit f8b83e27dd6979d15b279ccdc5800636336b32fc
There are no files selected for viewing
This file contains 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 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 |
---|---|---|
@@ -1,19 +1,96 @@ | ||
// import Word2Vec from './index'; | ||
|
||
// const URL = 'https://raw.githubusercontent.com/ml5js/ml5-examples/master/p5js/07_Word2Vec/data/wordvecs1000.json'; | ||
|
||
// describe('initialize word2vec', () => { | ||
// let word2vec; | ||
// beforeAll((done) => { | ||
// // word2vec = new Word2Vec(URL); | ||
// done(); | ||
// }); | ||
|
||
// // it('creates a new instance', (done) => { | ||
// // expect(word2vec).toEqual(jasmine.objectContaining({ | ||
// // ready: true, | ||
// // modelSize: 1, | ||
// // })); | ||
// // done(); | ||
// // }); | ||
// }); | ||
const { tf, word2vec } = ml5; | ||
|
||
const URL = 'https://raw.githubusercontent.com/ml5js/ml5-examples/master/p5js/Word2Vec/data/wordvecs1000.json'; | ||
|
||
describe('word2vec', () => { | ||
let word2vecInstance; | ||
let numTensorsBeforeAll; | ||
let numTensorsBeforeEach; | ||
beforeAll((done) => { | ||
numTensorsBeforeAll = tf.memory().numTensors; | ||
word2vecInstance = word2vec(URL, done); | ||
}); | ||
|
||
afterAll(() => { | ||
word2vecInstance.dispose(); | ||
let numTensorsAfterAll = tf.memory().numTensors; | ||
if(numTensorsBeforeAll !== numTensorsAfterAll) { | ||
throw new Error(`Leaking Tensors (${numTensorsAfterAll} vs ${numTensorsBeforeAll})`); | ||
} | ||
}); | ||
|
||
beforeEach(() => { | ||
numTensorsBeforeEach = tf.memory().numTensors; | ||
}); | ||
|
||
afterEach(() => { | ||
let numTensorsAfterEach = tf.memory().numTensors; | ||
if(numTensorsBeforeEach !== numTensorsAfterEach) { | ||
throw new Error(`Leaking Tensors (${numTensorsAfterEach} vs ${numTensorsBeforeEach})`); | ||
} | ||
}); | ||
|
||
it('creates a new instance', () => { | ||
expect(word2vecInstance).toEqual(jasmine.objectContaining({ | ||
ready: true, | ||
modelSize: 1, | ||
})); | ||
}); | ||
|
||
describe('getRandomWord', () => { | ||
it('returns a word', () => { | ||
let word = word2vecInstance.getRandomWord(); | ||
expect(typeof word).toEqual('string'); | ||
}); | ||
}); | ||
|
||
describe('nearest', () => { | ||
it('returns a sorted array of nearest words', () => { | ||
for(let i = 0; i < 100; i++) { | ||
let word = word2vecInstance.getRandomWord(); | ||
let nearest = word2vecInstance.nearest(word); | ||
let currentDistance = 0; | ||
for(let { word, distance: nextDistance } of nearest) { | ||
expect(typeof word).toEqual('string'); | ||
expect(nextDistance).toBeGreaterThan(currentDistance); | ||
currentDistance = nextDistance; | ||
} | ||
} | ||
}); | ||
|
||
it('returns a list of the right length', () => { | ||
for(let i = 0; i < 100; i++) { | ||
let word = word2vecInstance.getRandomWord(); | ||
let nearest = word2vecInstance.nearest(word, i); | ||
expect(nearest.length).toEqual(i); | ||
} | ||
}); | ||
}); | ||
|
||
describe('add', () => { | ||
it('returns a value', () => { | ||
let word1 = word2vecInstance.getRandomWord(); | ||
let word2 = word2vecInstance.getRandomWord(); | ||
let sum = word2vecInstance.subtract([word1, word2]); | ||
expect(sum[0].distance).toBeGreaterThan(0); | ||
}) | ||
}); | ||
|
||
describe('subtract', () => { | ||
it('returns a value', () => { | ||
let word1 = word2vecInstance.getRandomWord(); | ||
let word2 = word2vecInstance.getRandomWord(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is it technically possible the same word could be picked twice? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It is, but the probability is 0.0001% |
||
let sum = word2vecInstance.subtract([word1, word2]); | ||
expect(sum[0].distance).toBeGreaterThan(0); | ||
}) | ||
}); | ||
|
||
describe('average', () => { | ||
it('returns a value', () => { | ||
let word1 = word2vecInstance.getRandomWord(); | ||
let word2 = word2vecInstance.getRandomWord(); | ||
let average = word2vecInstance.average([word1, word2]); | ||
expect(average[0].distance).toBeGreaterThan(0); | ||
}); | ||
}); | ||
}); |
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 you forgot the remove this log
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.
Good catch :)