(
values: TensorLike|WebGLData|WebGPUData, shape: number[],
inferredShape: number[], dtype?: DataType)
| 23 | |
| 24 | /** This is shared code across all tensor creation methods. */ |
| 25 | export 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; |
no test coverage detected
searching dependent graphs…