MCPcopy Index your code
hub / github.com/tensorflow/tfjs / makeTensor

Function makeTensor

tfjs-core/src/ops/tensor_ops_util.ts:25–87  ·  view source on GitHub ↗
(
    values: TensorLike|WebGLData|WebGPUData, shape: number[],
    inferredShape: number[], dtype?: DataType)

Source from the content-addressed store, hash-verified

23
24/** This is shared code across all tensor creation methods. */
25export function makeTensor(
26 values: TensorLike|WebGLData|WebGPUData, shape: number[],
27 inferredShape: number[], dtype?: DataType): Tensor {
28 if (dtype == null) {
29 dtype = inferDtype(values);
30 } else if (dtype === 'complex64') {
31 throw new Error(
32 `Cannot construct a complex64 tensor directly. ` +
33 `Please use tf.complex(real, imag).`);
34 }
35
36 if (isWebGPUData(values) || isWebGLData(values)) {
37 if (dtype !== 'float32' && dtype !== 'int32') {
38 throw new Error(
39 `Creating tensor from GPU data only supports ` +
40 `'float32'|'int32' dtype, while the dtype is ${dtype}.`);
41 }
42 return ENGINE.backend.createTensorFromGPUData(
43 values, shape || inferredShape, dtype);
44 }
45
46 if (!isTypedArray(values) && !Array.isArray(values) &&
47 typeof values !== 'number' && typeof values !== 'boolean' &&
48 typeof values !== 'string') {
49 throw new Error(
50 'values passed to tensor(values) must be a number/boolean/string or ' +
51 'an array of numbers/booleans/strings, or a TypedArray');
52 }
53 // Verify that the shape matches the inferred shape.
54 if (shape != null) {
55 assertNonNegativeIntegerDimensions(shape);
56
57 const providedSize = sizeFromShape(shape);
58 const inferredSize = sizeFromShape(inferredShape);
59 assert(
60 providedSize === inferredSize,
61 () =>
62 `Based on the provided shape, [${shape}], the tensor should have ` +
63 `${providedSize} values but has ${inferredSize}`);
64
65 for (let i = 0; i < inferredShape.length; ++i) {
66 const inferred = inferredShape[i];
67 const flatDimsDontMatch = i === inferredShape.length - 1 ?
68 inferred !== sizeFromShape(shape.slice(i)) :
69 true;
70 assert(
71 inferredShape[i] === shape[i] || !flatDimsDontMatch,
72 () => `Error creating a new Tensor. Inferred shape ` +
73 `(${inferredShape}) does not match the provided ` +
74 `shape (${shape}). `);
75 }
76 }
77
78 if (!isTypedArray(values) && !Array.isArray(values)) {
79 values = [values] as number[];
80 }
81
82 shape = shape || inferredShape;

Callers 8

tensor2dFunction · 0.90
tensor5dFunction · 0.90
tensor4dFunction · 0.90
tensor3dFunction · 0.90
tensor1dFunction · 0.90
tensor6dFunction · 0.90
scalarFunction · 0.90
tensorFunction · 0.90

Calls 12

inferDtypeFunction · 0.90
isWebGPUDataFunction · 0.90
isWebGLDataFunction · 0.90
isTypedArrayFunction · 0.90
sizeFromShapeFunction · 0.90
assertFunction · 0.90
toTypedArrayFunction · 0.90
flattenFunction · 0.90
sliceMethod · 0.65
makeTensorMethod · 0.65

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…