Skip to content

Commit

Permalink
Improve the code snippet for texture to tensor (#7694)
Browse files Browse the repository at this point in the history
DOC

* Improve example

* add
  • Loading branch information
Linchenn authored May 17, 2023
1 parent 4cabd2c commit 03c6053
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions tfjs-core/src/ops/tensor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,13 @@ import {makeTensor} from './tensor_ops_util';
* // downloading the values.
*
* // Example for WebGL2:
* const customCanvas = document.createElement('canvas');
* const customBackend = new tf.MathBackendWebGL(customCanvas);
* tf.registerBackend('custom-webgl', () => customBackend);
* if (tf.findBackend('custom-webgl') == null) {
* const customCanvas = document.createElement('canvas');
* const customBackend = new tf.MathBackendWebGL(customCanvas);
* tf.registerBackend('custom-webgl', () => customBackend);
* }
* await tf.setBackend('custom-webgl');
* const gl = customBackend.gpgpu.gl;
* const gl = tf.backend().gpgpu.gl;
* const texture = gl.createTexture();
* const tex2d = gl.TEXTURE_2D;
* const width = 2;
Expand All @@ -81,6 +83,7 @@ import {makeTensor} from './tensor_ops_util';
*
* const logicalShape = [height * width * 2];
* const a = tf.tensor({texture, height, width, channels: 'BR'}, logicalShape);
* a.print();
* // Tensor value will be [2, 0, 6, 4, 10, 8, 14, 12], since [2, 0] is the
* // values of 'B' and 'R' channels of Pixel0, [6, 4] is the values of 'B' and
* 'R'
Expand Down Expand Up @@ -158,6 +161,7 @@ import {makeTensor} from './tensor_ops_util';
* const a = tf.tensor({buffer: aBuffer}, shape, dtype);
* const b = tf.tensor(bData, shape, dtype);
* const result = tf.add(a, b);
* result.print();
* a.dispose();
* b.dispose();
* result.dispose();
Expand Down

0 comments on commit 03c6053

Please sign in to comment.