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

Method predict

tfjs-node/src/saved_model.ts:292–343  ·  view source on GitHub ↗

* Execute the inference for the input tensors. * * @param input The input tensors, when there is single input for the model, * inputs param should be a Tensor. For models with multiple inputs, inputs * params should be in either Tensor[] if the input order is fixed, or * otherwise Nam

(inputs: Tensor|Tensor[]|NamedTensorMap, config?: ModelPredictConfig)

Source from the content-addressed store, hash-verified

290 * @doc {heading: 'Models', subheading: 'SavedModel'}
291 */
292 predict(inputs: Tensor|Tensor[]|NamedTensorMap, config?: ModelPredictConfig):
293 Tensor|Tensor[]|NamedTensorMap {
294 if (this.disposed) {
295 throw new Error('The TFSavedModel has already been deleted!');
296 } else {
297 let inputTensors: Tensor[] = [];
298 if (inputs instanceof Tensor) {
299 inputTensors.push(inputs);
300 const result = this.backend.runSavedModel(
301 this.sessionId, inputTensors, Object.values(this.signature.inputs),
302 Object.values(this.outputNodeNames));
303 return result.length > 1 ? result : result[0];
304 } else if (Array.isArray(inputs)) {
305 inputTensors = inputs;
306 return this.backend.runSavedModel(
307 this.sessionId, inputTensors, Object.values(this.signature.inputs),
308 Object.values(this.outputNodeNames));
309 } else {
310 const inputTensorNames = Object.keys(this.signature.inputs);
311 const providedInputNames = Object.keys(inputs);
312 if (!stringArraysHaveSameElements(
313 inputTensorNames, providedInputNames)) {
314 throw new Error(`The model signatureDef input names are ${
315 inputTensorNames.join()}, however the provided input names are ${
316 providedInputNames.join()}.`);
317 }
318 const inputNodeNamesArray: ModelTensorInfo[] = [];
319 for (let i = 0; i < inputTensorNames.length; i++) {
320 inputTensors.push(inputs[inputTensorNames[i]]);
321 inputNodeNamesArray.push(this.signature.inputs[inputTensorNames[i]]);
322 }
323 const outputTensorNames = Object.keys(this.outputNodeNames);
324 const outputNodeNamesArray = [];
325 for (let i = 0; i < outputTensorNames.length; i++) {
326 outputNodeNamesArray.push(this.outputNodeNames[outputTensorNames[i]]);
327 }
328 const outputTensors = this.backend.runSavedModel(
329 this.sessionId, inputTensors, inputNodeNamesArray,
330 outputNodeNamesArray);
331 util.assert(
332 outputTensors.length === outputNodeNamesArray.length,
333 () => 'Output tensors do not match output node names, ' +
334 `receive ${outputTensors.length}) output tensors but ` +
335 `there are ${this.outputNodeNames.length} output nodes.`);
336 const outputMap: NamedTensorMap = {};
337 for (let i = 0; i < outputTensorNames.length; i++) {
338 outputMap[outputTensorNames[i]] = outputTensors[i];
339 }
340 return outputMap;
341 }
342 }
343 }
344
345 /**
346 * Execute the inference for the input tensors and return activation

Callers

nothing calls this directly

Calls 4

joinMethod · 0.80
runSavedModelMethod · 0.65
pushMethod · 0.45

Tested by

no test coverage detected