Search code examples
arraysswiftmachine-learningcoreml

Is MLMultiArray a flatten array of the model output matrix?


New to CoreML, trying to understand some basic concepts.

I'm working on a model with an output of:

float32 [1,896,16]

When using the model, and getting the output as MLMultiArray I get the following:

let output = prediction.regressors // MLMultiArray
print(output.debugDescription) // Float32 1 x 896 x 16 array
print(output.count) // 14336, which is 896x16

And I can access each of the elements simply using output[0]..output[1]..

Is it true for any data type that'll be stored in the MLMultiArray? Is it a "convenient" that Swift supplies us?

If flatten array is the case, will it be ordered at the same order of the matrix?


Solution

  • Is MLMultiArray a flatten array of the model output matrix? No it's not it's a multidimensional array of the given dimensions

    You can convert it to array like below and it should be the same order of the matrix.

    let length = output.count
        let doublePtr =  output.dataPointer.bindMemory(to: Double.self, capacity: length)
        let doubleBuffer = UnsafeBufferPointer(start: doublePtr, count: length)
        let outputArray = Array(doubleBuffer)
    

    For Data types apple documentations shows 3 types only: Here

    case int32

    Represents the integer type for multidimensional arrays and is commonly used for text encoding.

    case float32

    Represents the float type in multidimensional arrays.

    case double

    Represents the double type for multidimensional arrays.