-
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
Completed constant #65
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
'use strict'; | ||
|
||
import {cast} from './cast.js'; | ||
import {Tensor, sizeOfShape} from '../src/lib/tensor.js'; | ||
/** | ||
* Create a constant array of specified data type and shape, | ||
* which contains data incrementing by step. | ||
* @param {Number} start | ||
* @param {Number} step | ||
* @param {Array} outputShape | ||
* @param {string} type | ||
* @return {Tensor} | ||
*/ | ||
export function constant(start, step, outputShape, type = 'float32') { | ||
const outputElementCount = sizeOfShape(outputShape); | ||
const data = []; | ||
for (let i = 0; i < outputElementCount; i++) { | ||
data.push(start + i * step); | ||
} | ||
const tensor = new Tensor(outputShape, data); | ||
return cast(tensor, type); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
'use strict'; | ||
|
||
import {constant} from '../src/constant.js'; | ||
import * as utils from './utils.js'; | ||
|
||
|
||
describe('test constant', function() { | ||
function testConstant(start, step, outputShape, type, expected) { | ||
const outputTensor = constant(start, step, outputShape, type); | ||
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. There may be some fallout from the discussion of webmachinelearning/webnn#492, but it would be a minor incremental change. e.g.
(so, I don't see a reason to block either way?) |
||
utils.checkShape(outputTensor, expected.shape); | ||
utils.checkValue(outputTensor, expected.data); | ||
} | ||
|
||
it('constant step > 0', function() { | ||
const expected ={ | ||
shape: [3, 3], | ||
data: [ | ||
0, 1, 2, | ||
3, 4, 5, | ||
6, 7, 8, | ||
], | ||
}; | ||
testConstant( | ||
0, 1, [3, 3], 'float32', expected); | ||
}); | ||
|
||
it('constant step < 0', function() { | ||
const expected ={ | ||
shape: [3, 3], | ||
data: [ | ||
9, 8, 7, | ||
6, 5, 4, | ||
3, 2, 1, | ||
], | ||
}; | ||
testConstant( | ||
9, -1, [3, 3], 'float32', expected); | ||
}); | ||
|
||
it('constant step = 0', function() { | ||
const expected ={ | ||
shape: [3, 3], | ||
data: [ | ||
1, 1, 1, | ||
1, 1, 1, | ||
1, 1, 1, | ||
], | ||
}; | ||
testConstant( | ||
1, 0, [3, 3], 'float64', expected); | ||
}); | ||
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. Would be good to include one scalar sanity-check case. e.g.
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. ok |
||
|
||
it('constant scalar', function() { | ||
const expected ={ | ||
shape: [], | ||
data: [42], | ||
}; | ||
testConstant(42, 0, [], 'float64', expected); | ||
}); | ||
}); |
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.
Please update this function's description and also add descriptions for
start
,step
andtype
parameters.