Jump to content

Connect SuperML | Leeroopedia MCP: Equip your AI agents with best practices, code verification, and debugging knowledge. Powered by Leeroo — building Organizational Superintelligence. Contact us at founders@leeroo.com.

Implementation:Microsoft Onnxruntime Tensor Data Extraction Nodejs

From Leeroopedia


Field Value
Implementation Name Tensor_Data_Extraction_Nodejs
Overview Extraction of typed array data from output tensors returned by ONNX Runtime inference in Node.js.
Type API Doc
Language JavaScript
Domains ML_Inference, JavaScript_Integration
Source Repository microsoft/onnxruntime
Last Updated 2026-02-10

Overview

Extraction of typed array data from output tensors returned by ONNX Runtime inference in Node.js. After calling session.run(), the results object contains ort.Tensor instances whose .data property provides the underlying TypedArray for downstream processing.

API

results.outputName.data  // property access on returned Tensor

Source Code Reference

The basic usage sample demonstrates accessing results.c.data to retrieve the Float32Array output of a MatMul operation.

I/O Contract

Direction Name Type Description
Input results Record<string, ort.Tensor> Results object returned from session.run()
Output data TypedArray (Float32Array, Int32Array, etc.) The underlying typed array containing raw output values

Usage Examples

Basic Data Extraction

const ort = require('onnxruntime-node');

async function main() {
    const session = await ort.InferenceSession.create('./model.onnx');

    const dataA = Float32Array.from([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]);
    const dataB = Float32Array.from([10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110, 120]);
    const tensorA = new ort.Tensor('float32', dataA, [3, 4]);
    const tensorB = new ort.Tensor('float32', dataB, [4, 3]);

    const feeds = { a: tensorA, b: tensorB };
    const results = await session.run(feeds);

    // Extract output data via .data property
    const outputData = results.c.data;  // Float32Array
    console.log(`Output data: ${outputData}`);
}
main();

Accessing Specific Values

const results = await session.run(feeds);
const outputData = results.c.data;

// Access individual elements by index
const firstValue = outputData[0];
const lastValue = outputData[outputData.length - 1];
console.log(`First value: ${firstValue}, Last value: ${lastValue}`);

Inspecting Output Shape and Type

const results = await session.run(feeds);

// Access metadata alongside data
const outputTensor = results.c;
console.log(`Type: ${outputTensor.type}`);     // e.g., 'float32'
console.log(`Dims: ${outputTensor.dims}`);     // e.g., [3, 3]
console.log(`Size: ${outputTensor.size}`);     // e.g., 9
console.log(`Data: ${outputTensor.data}`);     // Float32Array with 9 elements

Post-Processing: Argmax

const results = await session.run(feeds);
const outputData = results.logits.data;  // Float32Array of class scores

// Find the index of the maximum value (classification)
let maxIdx = 0;
for (let i = 1; i < outputData.length; i++) {
    if (outputData[i] > outputData[maxIdx]) {
        maxIdx = i;
    }
}
console.log(`Predicted class: ${maxIdx}`);

Iterating Over Multiple Outputs

const results = await session.run(feeds);

// Process all outputs
for (const [name, tensor] of Object.entries(results)) {
    console.log(`Output '${name}': type=${tensor.type}, shape=${tensor.dims}`);
    console.log(`  Data (first 5): ${Array.from(tensor.data).slice(0, 5)}`);
}

Key Details

  • The .data property returns the underlying TypedArray directly (e.g., Float32Array, Int32Array).
  • For multi-dimensional tensors, the data is in row-major (C-contiguous) order in the flat array.
  • The .dims property provides the shape needed to interpret the flat data as a multi-dimensional tensor.
  • The .type property indicates the element data type as a string.
  • The .size property returns the total number of elements.
  • No data copying occurs when accessing .data; it returns the same buffer held by the native layer.

Related Pages

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment