-
Notifications
You must be signed in to change notification settings - Fork 8
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
Implement where #56
Merged
Merged
Implement where #56
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
cec9e70
Implement where
mei1127 337b0f6
modified the format of where_test.js
mei1127 9aa625d
modified where.js and where_test.js
mei1127 afdb4c7
modified where_test.js
mei1127 cee6d20
modified where.js and where_test.js
mei1127 2d3f47f
modified where.js
mei1127 377098b
modified where.js
mei1127 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 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,32 @@ | ||
'use strict'; | ||
|
||
import {broadcast, getBroadcastShape} from './lib/broadcast.js'; | ||
import {Tensor, sizeOfShape, Scalar} from './lib/tensor.js'; | ||
|
||
/** | ||
* Select the values from the trueValues or the other tensor depending on | ||
* the corresponding Boolean values of the condition tensor. | ||
* @param {Tensor} condition | ||
* @param {Tensor} trueValues | ||
* @param {Tensor} falseValues | ||
* @return {Tensor} | ||
*/ | ||
export function where(condition, trueValues, falseValues) { | ||
const trueValuesReshape = trueValues.shape.length === 0 ? | ||
new Scalar(trueValues.data) :trueValues; | ||
const falseValuesReshape = falseValues.shape.length === 0 ? | ||
new Scalar(falseValues.data) : falseValues; | ||
const valueShape = getBroadcastShape(trueValuesReshape.shape, falseValuesReshape.shape); | ||
const outputShape = getBroadcastShape(condition.shape, valueShape); | ||
const trueValuesReshapeBroadcast = broadcast(trueValuesReshape, outputShape); | ||
const falseValuesReshapeBroadcast = broadcast(falseValuesReshape, outputShape); | ||
const conditionBroadcast = broadcast(condition, outputShape); | ||
const outputSize = sizeOfShape(outputShape); | ||
const output = new Tensor(outputShape); | ||
for (let i = 0; i < outputSize; ++i) { | ||
const value = conditionBroadcast.getValueByIndex(i) === 0 ? | ||
falseValuesReshapeBroadcast.getValueByIndex(i) : trueValuesReshapeBroadcast.getValueByIndex(i); | ||
output.setValueByIndex(i, value); | ||
} | ||
Mei0211 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return output; | ||
} |
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 |
---|---|---|
@@ -0,0 +1,231 @@ | ||
'use strict'; | ||
|
||
import {where} from '../src/where.js'; | ||
import {Tensor} from '../src/lib/tensor.js'; | ||
import * as utils from './utils.js'; | ||
|
||
describe('test where', function() { | ||
function testWhere(condition, trueValues, falseValues, expected) { | ||
const tensorCondition = new Tensor(condition.shape, condition.data); | ||
const tensorA = new Tensor(trueValues.shape, trueValues.data); | ||
const tensorB = new Tensor(falseValues.shape, falseValues.data); | ||
const outputTensor = where(tensorCondition, tensorA, tensorB); | ||
utils.checkShape(outputTensor, expected.shape); | ||
utils.checkValue(outputTensor, expected.data); | ||
} | ||
|
||
it('where', function() { | ||
const condition = { | ||
shape: [2, 3], | ||
data: [ | ||
1, 1, 0, | ||
0, 1, 0, | ||
Mei0211 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
], | ||
}; | ||
const trueValues = { | ||
shape: [2, 3], | ||
data: [ | ||
1, 2, 3, | ||
4, 5, 64, | ||
], | ||
}; | ||
const falseValues = { | ||
shape: [2, 3], | ||
data: [ | ||
6, 3, 5, | ||
7, 8, 0, | ||
], | ||
}; | ||
const expected = { | ||
shape: [2, 3], | ||
data: [ | ||
1, 2, 5, | ||
7, 5, 0, | ||
], | ||
}; | ||
testWhere(condition, trueValues, falseValues, expected); | ||
}); | ||
|
||
|
||
it('where broadcast condition1d×A2d×B2d', function() { | ||
const condition = { | ||
shape: [3], | ||
data: [1, 1, 0], | ||
}; | ||
const trueValues = { | ||
shape: [2, 3], | ||
data: [ | ||
1, 2, 3, | ||
4, 5, 64, | ||
], | ||
}; | ||
const falseValues = { | ||
shape: [2, 3], | ||
data: [ | ||
7, 8, 9, | ||
10, 11, 12, | ||
], | ||
}; | ||
const expected = { | ||
shape: [2, 3], | ||
data: [ | ||
1, 2, 9, | ||
4, 5, 12, | ||
], | ||
}; | ||
testWhere(condition, trueValues, falseValues, expected); | ||
}); | ||
|
||
it('where broadcast condition2d×A2d×B1d', function() { | ||
Mei0211 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
const condition = { | ||
shape: [2, 3], | ||
data: [ | ||
1, 1, 0, | ||
0, 0, 1, | ||
], | ||
}; | ||
const trueValues = { | ||
shape: [2, 3], | ||
data: [ | ||
1, 2, 3, | ||
4, 5, 64, | ||
], | ||
}; | ||
const falseValues = { | ||
shape: [3], | ||
data: [7, 8, 9], | ||
}; | ||
const expected = { | ||
shape: [2, 3], | ||
data: [ | ||
1, 2, 9, | ||
7, 8, 64, | ||
], | ||
}; | ||
testWhere(condition, trueValues, falseValues, expected); | ||
}); | ||
|
||
it('where broadcast condition1d×A2d×B3d', function() { | ||
const condition = { | ||
shape: [3], | ||
data: [ | ||
1, 1, 0, | ||
], | ||
}; | ||
const trueValues = { | ||
shape: [2, 3], | ||
data: [ | ||
1, 2, 3, | ||
4, 5, 64, | ||
], | ||
}; | ||
const falseValues = { | ||
shape: [2, 2, 3], | ||
data: [ | ||
7, 8, 9, 10, 11, 12, | ||
13, 14, 15, 16, 17, 18], | ||
}; | ||
const expected = { | ||
shape: [2, 2, 3], | ||
data: [ | ||
1, 2, 9, 4, 5, 12, | ||
1, 2, 15, 4, 5, 18, | ||
], | ||
}; | ||
testWhere(condition, trueValues, falseValues, expected); | ||
}); | ||
|
||
it('where broadcast condition3d×A2d×B1d', function() { | ||
const condition = { | ||
shape: [2, 2, 3], | ||
data: [ | ||
1, 1, 0, 1, 1, 0, | ||
1, 1, 0, 1, 1, 0, | ||
], | ||
}; | ||
const trueValues = { | ||
shape: [2, 3], | ||
data: [ | ||
1, 2, 3, | ||
4, 5, 64, | ||
], | ||
}; | ||
const falseValues = { | ||
shape: [3], | ||
data: [ | ||
7, 8, 9, | ||
], | ||
}; | ||
const expected = { | ||
shape: [2, 2, 3], | ||
data: [ | ||
1, 2, 9, 4, 5, 9, | ||
1, 2, 9, 4, 5, 9, | ||
], | ||
}; | ||
testWhere(condition, trueValues, falseValues, expected); | ||
}); | ||
|
||
it('where broadcast !0=true test', function() { | ||
const condition = { | ||
shape: [2, 3], | ||
data: [ | ||
2, 3, 0, | ||
0, 5, 0, | ||
], | ||
}; | ||
const trueValues = { | ||
shape: [2, 3], | ||
data: [ | ||
1, 2, 3, | ||
4, 5, 64, | ||
], | ||
}; | ||
const falseValues = { | ||
shape: [2, 3], | ||
data: [ | ||
6, 3, 5, | ||
7, 8, 0, | ||
], | ||
}; | ||
const expected = { | ||
shape: [2, 3], | ||
data: [ | ||
1, 2, 5, | ||
7, 5, 0, | ||
], | ||
}; | ||
testWhere(condition, trueValues, falseValues, expected); | ||
}); | ||
|
||
it('where broadcast condition2d×A(scalar)×B1d test', function() { | ||
const condition = { | ||
shape: [2, 3], | ||
data: [ | ||
1, 1, 0, | ||
0, 1, 0, | ||
], | ||
}; | ||
const trueValues = { | ||
shape: [], | ||
data: [ | ||
6, | ||
], | ||
}; | ||
const falseValues = { | ||
shape: [2, 3], | ||
data: [ | ||
6, 3, 5, | ||
7, 8, 0, | ||
], | ||
}; | ||
const expected = { | ||
shape: [2, 3], | ||
data: [ | ||
6, 6, 5, | ||
7, 6, 0, | ||
], | ||
}; | ||
testWhere(condition, trueValues, falseValues, expected); | ||
}); | ||
Mei0211 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
}); |
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.
Nit for when you touch this file in the future...