'How to Slice ONNXRuntime Tensor?
Assume that a Microsoft.ML.Onnxruntime.Tensors.Tensor variable has been created with dimensions [d1, d2, d3]. Is there a way to return a copy or view of a slice over certain dimensions? I wanted to do the equivalent of subset = inputs[start_idx:start_idx+batch_size, :, :], but ended up having to create a new tensor and copy in each value one-by-one.
var subset = new DenseTensor<float>(new[] {
batch_size, inputs.Dimensions[1], inputs.Dimensions[2]});
var start_idx = bidx * batch_size;
// create the input vector on a subset of the full batch
// inputs[start_idx:start_idx+batch_size, :, :] in python
for (int ridx = 0; ridx < batch_size; ++ridx)
{
for (int cidx = 0; cidx < inputs.Dimensions[1]; ++cidx)
{
subset[ridx, cidx, 0] = inputs[start_idx + ridx, cidx, 0];
}
}
The [] operator for a tensor can accept a sequence of integers as shown in this example, and also a ReadOnlySpan, but I couldn't figure out how to get these to do what I wanted.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
