(
x: T|TensorLike, argName: string, functionName: string,
parseAsDtype: DataType|'numeric'|'string_or_numeric' = 'numeric')
| 96 | } |
| 97 | |
| 98 | export function convertToTensor<T extends Tensor>( |
| 99 | x: T|TensorLike, argName: string, functionName: string, |
| 100 | parseAsDtype: DataType|'numeric'|'string_or_numeric' = 'numeric'): T { |
| 101 | if (x instanceof getGlobalTensorClass()) { |
| 102 | assertDtype(parseAsDtype, x.dtype, argName, functionName); |
| 103 | return x; |
| 104 | } |
| 105 | let inferredDtype = inferDtype(x); |
| 106 | // If the user expects a bool/int/float, use that info to update the |
| 107 | // inferredDtype when it is not a string. |
| 108 | if (inferredDtype !== 'string' && |
| 109 | ['bool', 'int32', 'float32'].indexOf(parseAsDtype) >= 0) { |
| 110 | inferredDtype = parseAsDtype as DataType; |
| 111 | } |
| 112 | assertDtype(parseAsDtype, inferredDtype, argName, functionName); |
| 113 | |
| 114 | if ((x == null) || |
| 115 | (!isTypedArray(x) && !Array.isArray(x) && typeof x !== 'number' && |
| 116 | typeof x !== 'boolean' && typeof x !== 'string')) { |
| 117 | const type = x == null ? 'null' : (x as {}).constructor.name; |
| 118 | throw new Error( |
| 119 | `Argument '${argName}' passed to '${functionName}' must be a ` + |
| 120 | `Tensor or TensorLike, but got '${type}'`); |
| 121 | } |
| 122 | const inferredShape = inferShape(x, inferredDtype); |
| 123 | if (!isTypedArray(x) && !Array.isArray(x)) { |
| 124 | x = [x] as number[]; |
| 125 | } |
| 126 | const skipTypedArray = true; |
| 127 | const values = inferredDtype !== 'string' ? |
| 128 | toTypedArray(x, inferredDtype as DataType) : |
| 129 | flatten(x as string[], [], skipTypedArray) as string[]; |
| 130 | return ENGINE.makeTensor(values, inferredShape, inferredDtype) as T; |
| 131 | } |
| 132 | |
| 133 | export function convertToTensorArray<T extends Tensor>( |
| 134 | arg: Array<T|TensorLike>, argName: string, functionName: string, |
no test coverage detected
searching dependent graphs…