将一个大的[MLMultiArray]分割成较小的块[[MLMultiArray]]?

问题描述 投票:0回答:1

我有一个大的MLMultiArray,长度为15360个值。

示例。

Float32 1 x 15360
[14.78125,-0.6308594,5.609375,13.57812,-1.871094,-19.65625,9.5625,8.640625,-2.728516,3.654297,-3.189453,-1.740234...]

有什么方法可以将这个大数组转换成120个小的MLMultiArrays,每个数组有128个元素,而不改变数组的序列,并以最有效的方式?

整个数组的15360个元素都可以在这个 鏈接

swift multidimensional-array coreml
1个回答
1
投票
let chunkSize = 2
let shape = yourLargeArray.shape
let chunkedArrays = try? stride(from: 0, to: shape.count, by: chunkSize)
    .map { offset -> MLMultiArray in
        let startIndex = shape.index(shape.startIndex, offsetBy: offset)
        let endIndex = shape.index(startIndex, offsetBy: chunkSize, limitedBy: shape.endIndex) ?? shape.endIndex

        return try MLMultiArray(
            shape: Array(shape[startIndex ..< endIndex]), 
            dataType: yourLargeArray.dataType
        )
    }
© www.soinside.com 2019 - 2024. All rights reserved.