MCPcopy
hub / github.com/tensorflow/tfjs / convertToTensor

Function convertToTensor

tfjs-core/src/tensor_util_env.ts:98–131  ·  view source on GitHub ↗
(
    x: T|TensorLike, argName: string, functionName: string,
    parseAsDtype: DataType|'numeric'|'string_or_numeric' = 'numeric')

Source from the content-addressed store, hash-verified

96}
97
98export 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
133export function convertToTensorArray<T extends Tensor>(
134 arg: Array<T|TensorLike>, argName: string, functionName: string,

Callers 15

aFunction · 0.90
aFunction · 0.90
gradFunction · 0.90
gradsFunction · 0.90
moments_Function · 0.90
greater_Function · 0.90
sqrt_Function · 0.90
logicalNot_Function · 0.90
depthwiseConv2d_Function · 0.90
relu_Function · 0.90
atan2_Function · 0.90

Calls 8

getGlobalTensorClassFunction · 0.90
inferDtypeFunction · 0.90
isTypedArrayFunction · 0.90
toTypedArrayFunction · 0.90
flattenFunction · 0.90
assertDtypeFunction · 0.85
inferShapeFunction · 0.85
makeTensorMethod · 0.65

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…