-
Notifications
You must be signed in to change notification settings - Fork 8
Implement argMax and argMin #62
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
Merged
huningxin
merged 6 commits into
webmachinelearning:main
from
BruceDai:add_argmax_argmin
Dec 18, 2023
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
9856a81
Implement argMax and argMin
BruceDai cf4cae7
Fix output shape issues when keepDimensions=false for argMax/argMin a…
BruceDai ff919e5
Address comments of adding more tests and enhancing readability
BruceDai 857871f
Address comments of supporting axes=[] and code optimization
BruceDai 9321913
fix JS sort() by lambda
BruceDai 7fe0842
Avoid adding an element in negative index of an array
BruceDai 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
'use strict'; | ||
|
||
import {Tensor, sizeOfShape} from './lib/tensor.js'; | ||
import {reduceMax, reduceMin, selectValuesToReduce} from './reduce.js'; | ||
import {squeeze} from './squeeze.js'; | ||
|
||
/** | ||
* Get the index location of the minimum or maxmium values of all the input values along the axes. | ||
* @param {Tensor} input | ||
* @param {Function} reduceFunc | ||
* @param {MLArgMinMaxOptions} [options] | ||
* @return {Tensor} | ||
*/ | ||
export function argMaxMin( | ||
input, | ||
reduceFunc, | ||
{ | ||
axes = null, | ||
keepDimensions = false, | ||
selectLastIndex = false, | ||
} = {}) { | ||
// If axes aren't present (defaulting to null), all dimensions are reduced. | ||
// See https://webmachinelearning.github.io/webnn/#dom-mlargminmaxoptions-axes. | ||
const inputAxes = axes ?? new Array(input.rank).fill(0).map((_, i) => i); | ||
const outputShape = input.shape.slice(); | ||
|
||
for (let i = 0; i < inputAxes.length; ++i) { | ||
outputShape[inputAxes[i]] = 1; | ||
} | ||
|
||
let output = new Tensor(outputShape); | ||
const tensor = reduceFunc(input, {axes: inputAxes, keepDimensions: true}); | ||
|
||
for (let outputIndex = 0; outputIndex < sizeOfShape(outputShape); ++outputIndex) { | ||
const value = tensor.getValueByIndex(outputIndex); | ||
const inputLocation = output.locationFromIndex(outputIndex); | ||
const selectedArray = selectValuesToReduce(input, inputAxes, inputLocation); | ||
const index = | ||
selectLastIndex ? selectedArray.lastIndexOf(value) : selectedArray.indexOf(value); | ||
output.setValueByIndex(outputIndex, index); | ||
} | ||
|
||
if (!keepDimensions) { | ||
output = squeeze(output, {axes}); | ||
} | ||
|
||
return output; | ||
} | ||
|
||
/** | ||
* Get the index location of the maxmium values of all the input values along the axes. | ||
* @param {Tensor} input | ||
* @param {MLArgMinMaxOptions} [options] | ||
* @return {Tensor} | ||
*/ | ||
export function argMax(input, options = {}) { | ||
return argMaxMin(input, reduceMax, options); | ||
} | ||
|
||
/** | ||
* Get the index location of the minimum values of all the input values along the axes. | ||
* @param {Tensor} input | ||
* @param {MLArgMinMaxOptions} [options] | ||
* @return {Tensor} | ||
*/ | ||
export function argMin(input, options = {}) { | ||
return argMaxMin(input, reduceMin, options); | ||
} |
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
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.
Uh oh!
There was an error while loading. Please reload this page.