Principle:Microsoft Onnxruntime Nodejs Output Processing
| Field | Value |
|---|---|
| Principle Name | Nodejs_Output_Processing |
| Overview | Extraction of typed array data from output tensors returned by ONNX Runtime inference in Node.js. |
| Category | API Doc |
| 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 running inference, the output tensors contain the model's predictions in their underlying TypedArrays, which can be accessed for downstream processing such as classification, post-processing, or display.
Description
After inference, output tensors are accessed by name from the results object returned by session.run(). The results object is a Record<string, ort.Tensor> where each key is an output name defined in the ONNX model graph.
Each output tensor exposes several properties:
- .data: Returns the underlying TypedArray (Float32Array, Int32Array, etc.) containing the raw output values.
- .dims: Returns the shape of the output tensor as a number[] array.
- .type: Returns the element type as a string (e.g., Template:'float32', Template:'int64').
- .size: Returns the total number of elements in the tensor.
The .data property is the primary access point for extracting inference results. The returned TypedArray can be directly iterated, indexed, or passed to downstream processing functions. For multi-dimensional outputs, the data is stored in row-major (C-contiguous) order in the flat TypedArray, matching standard ONNX tensor layout conventions.
Common post-processing operations include:
- Argmax: Finding the index of the maximum value for classification tasks.
- Softmax: Converting logits to probabilities.
- Thresholding: Converting continuous outputs to binary decisions.
- Reshaping: Interpreting the flat buffer according to the tensor's dimensions.
Theoretical Basis
The output processing step bridges the gap between the mathematical tensor representation used internally by the ONNX Runtime engine and the JavaScript data structures needed by application code. The TypedArray interface provides efficient, typed access to the raw numerical output without data copying or boxing overhead.
Output tensor data follows ONNX's standard row-major memory layout, where the last dimension varies fastest. This is consistent with NumPy's default C-order and allows direct interpretation of multi-dimensional results from the flat buffer using standard index arithmetic: for a tensor with shape [d0, d1, d2], the element at position [i, j, k] is at flat index i * d1 * d2 + j * d2 + k.
Usage
Output processing is the final step in the Node.js inference pipeline:
- Execute session.run(feeds) to obtain the results object.
- Access the desired output tensor by name (e.g., results.outputName).
- Read the .data property to get the underlying TypedArray.
- Optionally inspect .dims and .type for shape and type information.
- Perform application-specific post-processing on the raw data.