Skip to content

Commit bdd31dd

Browse files
committed
fix: update bias constant to zero and enhance documentation for tensor methods in Program class
1 parent 82c47ed commit bdd31dd

File tree

2 files changed

+30
-23
lines changed

2 files changed

+30
-23
lines changed

app/web-nn/web-nn.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ export default class WebNNView extends HTMLElement {
2727
// 2. build graph
2828
const features = this.#program.addToGraph("input", "features", {dataType: 'float32', shape: [1, 2]});
2929
const weights = this.#program.addConstant({dataType: 'float32', shape: [2,1]}, [0.7, 0.3]);
30-
const bias = this.#program.addConstant({dataType: 'float32', shape: [1]}, [-0.5]);
30+
const bias = this.#program.addConstant({dataType: 'float32', shape: [1]}, [0]);
3131

3232
const weightedSum = this.#program.addToGraph("matmul", features, weights);
3333
const output = this.#program.addToGraph("add", weightedSum, bias);

src/webnn/program.js

Lines changed: 29 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -40,22 +40,22 @@ export class Program {
4040
}
4141

4242
/**
43-
* Create tensor on context
44-
* @param {*} operand
45-
* @param {*} writable
46-
* @param {*} readable
47-
* @returns
43+
* Create tensor on context.
44+
* @param {Object} operand - The operand descriptor.
45+
* @param {boolean} writable - Whether the tensor is writable.
46+
* @param {boolean} readable - Whether the tensor is readable.
47+
* @returns {Object} The created tensor.
4848
*/
4949
#createTensor(operand, writable, readable) {
5050
return this.#context.createTensor({
5151
dataType: operand.dataType, shape: operand.shape, writable, readable
52-
})
52+
});
5353
}
5454

5555
/**
5656
* Add an input tensor to the program.
5757
* @param {string} name - The name of the tensor.
58-
* @param {Object} tensor - The tensor object.
58+
* @param {Object} operand - The operand object.
5959
* @returns {Promise<void>}
6060
*/
6161
async addInputTensor(name, operand) {
@@ -65,14 +65,20 @@ export class Program {
6565
/**
6666
* Add an output tensor to the program.
6767
* @param {string} name - The name of the tensor.
68-
* @param {Object} tensor - The tensor object.
68+
* @param {Object} operand - The operand object.
6969
* @returns {Promise<void>}
7070
*/
7171
async addOutputTensor(name, operand) {
7272
this.#outputTensors[name] = await this.#createTensor(operand, false, true);
7373
}
7474

75-
addConstant(descriptor, values) {
75+
/**
76+
* Add a constant to the graph.
77+
* @param {Object} descriptor - The descriptor of the constant.
78+
* @param {Array<number>} values - The values of the constant.
79+
* @returns {Object} The created constant.
80+
*/
81+
addConstant(descriptor, values) {
7682
return this.#graphBuilder.constant(descriptor, new Float32Array(values));
7783
}
7884

@@ -92,8 +98,8 @@ export class Program {
9298
* @param {Array<number>} values - The value to set.
9399
* @returns {Promise<void>}
94100
*/
95-
set(name, values) {
96-
this.#context.writeTensor(this.#inputTensors[name], new Float32Array(values));
101+
async set(name, values) {
102+
await this.#context.writeTensor(this.#inputTensors[name], new Float32Array(values));
97103
}
98104

99105
/**
@@ -124,19 +130,20 @@ export class Program {
124130
* const program = new Program();
125131
* await program.init();
126132
*
127-
* const descriptor = {dataType: 'float32', shape: [1]};
128-
* const A = program.addToGraph("input", "A", descriptor);
129-
* const B = program.addToGraph("input", "B", descriptor);
130-
* const C = program.addToGraph("add", A, B);
131-
* program.build({C});
133+
* const descriptor = {dataType: 'float32', shape: [1, 2]};
134+
* const features = program.addToGraph("input", "features", descriptor);
135+
* const weights = program.addConstant({dataType: 'float32', shape: [2, 1]}, [0.7, 0.3]);
136+
* const bias = program.addConstant({dataType: 'float32', shape: [1]}, [-0.5]);
137+
*
138+
* const weightedSum = program.addToGraph("matmul", features, weights);
139+
* const output = program.addToGraph("add", weightedSum, bias);
140+
* await program.build({output});
132141
*
133-
* await program.addInputTensor("A", A);
134-
* await program.addInputTensor("B", B);
135-
* await program.addOutputTensor("C", C);
142+
* await program.addInputTensor("features", features);
143+
* await program.addOutputTensor("output", output);
136144
*
137-
* await program.set("A", [1]);
138-
* await program.set("B", [2]);
145+
* await program.set("features", [0.5, 0.8]);
139146
*
140147
* const result = await program.run();
141-
* console.log(result); // Output: 3
148+
* console.log(result); // Output: some computed value
142149
*/

0 commit comments

Comments
 (0)