-
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 reverse #116
Merged
+104
−3
Merged
implement reverse #116
Changes from 1 commit
Commits
Show all changes
3 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 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 |
---|---|---|
@@ -0,0 +1,34 @@ | ||
'use strict'; | ||
|
||
import {sizeOfShape, Tensor} from './lib/tensor.js'; | ||
import {validateReduceParams} from './lib/validate-input.js'; | ||
|
||
/** | ||
* Reverse input along the given axes. | ||
* @param {Tensor} input | ||
* @param {MLReverseOptions} [options] | ||
* @return {Tensor} | ||
*/ | ||
export function reverse(input, {axes}) { | ||
// reuse validateReduceParams to check parameters of reverse | ||
validateReduceParams(input, {axes}); | ||
|
||
const inputAxes = axes ?? new Array(input.rank).fill(0).map((_, i) => i); | ||
const inputShape = input.shape; | ||
const outputShape = inputShape.slice(); | ||
const outputSize = sizeOfShape(outputShape); | ||
const output = new Tensor(outputShape); | ||
|
||
for (let outputIndex = 0; outputIndex < outputSize; ++outputIndex) { | ||
const outputLocation = output.locationFromIndex(outputIndex); | ||
const selectedInputLocation = outputLocation.slice(); | ||
for (const axis of inputAxes) { | ||
const index = selectedInputLocation[axis]; | ||
selectedInputLocation[axis] = inputShape[axis] - index - 1; | ||
} | ||
const selectedInputValue = input.getValueByLocation(selectedInputLocation); | ||
output.setValueByLocation(outputLocation, selectedInputValue); | ||
} | ||
|
||
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,65 @@ | ||
'use strict'; | ||
|
||
import {reverse} from '../src/reverse.js'; | ||
import {Tensor} from '../src/lib/tensor.js'; | ||
import * as utils from './utils.js'; | ||
|
||
describe('test reverse', function() { | ||
function testReverse(inputShape, inputValue, expected, options = {}) { | ||
const inputTensor = new Tensor(inputShape, inputValue); | ||
const outputTensor = reverse(inputTensor, options); | ||
utils.checkShape(outputTensor, inputShape); | ||
utils.checkValue(outputTensor, expected); | ||
} | ||
|
||
it('reverse 0D scalar default options', function() { | ||
testReverse([], [2], [2]); | ||
}); | ||
|
||
it('reverse 4D default options', function() { | ||
testReverse( | ||
[2, 2, 2, 2], | ||
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16], | ||
[16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1]); | ||
}); | ||
|
||
it('reverse 2D with axes=[0]', function() { | ||
testReverse( | ||
[2, 2], | ||
[1, 2, 3, 4], | ||
[3, 4, 1, 2], | ||
{ | ||
axes: [0], | ||
}); | ||
}); | ||
|
||
it('reverse 4D with axes=[3]', function() { | ||
testReverse( | ||
[2, 2, 2, 2], | ||
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16], | ||
[2, 1, 4, 3, 6, 5, 8, 7, 10, 9, 12, 11, 14, 13, 16, 15], | ||
{ | ||
axes: [3], | ||
}); | ||
}); | ||
|
||
it('reverse 4D with axes=[1, 2]', function() { | ||
testReverse( | ||
[2, 2, 2, 2], | ||
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16], | ||
[7, 8, 5, 6, 3, 4, 1, 2, 15, 16, 13, 14, 11, 12, 9, 10], | ||
{ | ||
axes: [1, 2], | ||
}); | ||
}); | ||
|
||
it('reverse 4D with axes=[3, 1, 0, 2]', function() { | ||
testReverse( | ||
[2, 2, 2, 2], | ||
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16], | ||
[16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1], | ||
{ | ||
axes: [3, 1, 0, 2], | ||
}); | ||
}); | ||
}); |
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.
If
reverse
had been added beforereduce
, wouldreduce
callvalidateReverseParams
? RenamingvalidateReduceParams
to a more genericvalidateAxes
would make sense.