2

This also would seem something trivial but I've been searching the web and experimenting for a frustrating amount of time.

In onnx runtime web I create a tensor, i.e:

const a = new ort.Tensor('float32', array, [2, 10])

With array being some 20-element float32 array.

I can't seem to find a way to access the data using the standard format a[0] to retrieve an array of size 10. This is quite shocking to me.

The tensor has a dims property with the correct shape and a data property with the 1-dimensional 20 element array.

Within the examples supplied by onnx, I can see them using Array.prototype.slice to access the data, which seems inconvenient and in my mind, defeats the purpose of having a Tensor object to begin with.

So my question is, what's the recommended way of accessing the data using these Tensor instances?

Here is a sample on how I'm accessing the array data to illustrate the problem.

  for (j = 1 ; j < results.logits.dims[1] - 1; j++) {
    const s = j* results.logits.dims[2];
    const e = s + results.logits.dims[2]

    const arr = results.logits.data.slice(s, e)
    const probs = softmax(arr)
    const label = argmax(probs)

I dislike having to calculate manually the access function for the element at the position [i, j].

Thanks a ton for your help!

2 Answers2

0

Well, Tensors support vectorized actions so accessing a single element is not really a use case.

But more importantly, let's say you have accessed and retrieved a single element, what type is it going to be? The only option in Onnx is a Tensor, so slicing actually makes sense.

Maybe you can specify what you are trying to do?

user3689574
  • 1,596
  • 1
  • 11
  • 20
0

I am not sure if this will answer exactly your question, but I hope you will find it useful.

  1. According to the official doc, to get softmax which you mentioned in comments you can use:

    var outputSoftmax = softmax(Array.prototype.slice.call(output.data));

  2. Properties of tensors and accessing underlying data could be find here

flamingo
  • 148
  • 1
  • 11
  • Hi! Thanks for your answer, I downloaded the code for what they have in the official doc and it's just a softmax function implemented by hand. On the other hand, if you see the sample code I posted I'm accessing the data like that, but they don't provide a multi-dim array syntax, hence my question. – Juan Alberto López Cavallotti Jul 24 '22 at 15:38