将MLMultiArray转换为[Double]会导致NaN

时间:2020-07-14 02:11:55

标签: swift coreml

我正在将CoreML(4 b1)中prediction()的返回值从MLMultiArray转换为[Double],结果是创建了NaN,对我来说通常没有任何意义。可能会发生什么?

这是我正在使用的转换函数:

static func toDoubleArray(_ o: MLMultiArray) -> [Double] {
        let length = o.count
        let doublePtr =  o.dataPointer.bindMemory(to: Double.self, capacity: length)
        let doubleBuffer = UnsafeBufferPointer(start: doublePtr, count: length)
        let output = Array(doubleBuffer)
        return output
    }

这是示例输出,其中包含神秘的NaN:

original:
zn: Float32 1 x 30 matrix
[1.082742,-0.6218173,0.4097924,0.7741141,0.3768867,0.1923163,-0.6453421,-1.51211,0.9758957,-0.01491163,0.3188823,0.6816303,-0.3234658,-0.1413545,-0.3082153,-0.3349644,0.6796142,0.0495025,1.436066,-1.348089,0.2413646,-0.931259,-0.1012118,0.849587,-0.1338869,-0.5497701,-0.5438929,1.653574,-0.6829579,-0.539122]

converted: [-0.0001189622124778386, 0.0006766730199516765, 9.747163042077938e-09, -0.13711044152516036, -1.7618037121610124e-17, 0.00023267642183680645, -9.748075633049926e-10, -8.196165002097733e-07, 1.901572700078848e-13, -0.05577234175498982, -0.003711220331500651, 0.0015560479289331661, -5.481938547240449e-05, 0.3071476808226601, -4.9620131810906736e-05, 3.793063e-317, 2.220121165e-314, 1.5e-323, -nan(0x3ffff89abcdef), 5.312137469e-314, 0.0, 0.0, 0.0, 4.4490612551694285e-308, 7.99e-321, 2.2200499266e-314, 5e-324, 5.417665788e-314, 0.0, 0.0]

似乎我只是从某个地方获取了垃圾内存...但是话又说回来,那不应该包含NaN。非常困惑。

更新:好的,正如Matthijs指出的那样,底层内存是Float32。如果我只想要一个包含Doubles的MLMultiArray作为Swift数组,那么toDoubleArray()函数就可以了。但就我而言,我既要转换为Double 又要转换为 Swift Array。此版本提供了该功能,并且似乎提供了预期的输出:

static func castToDoubleArray(_ o: MLMultiArray) -> [Double] {
        var result: [Double] = Array(repeating: 0.0, count: o.count)
        for i in 0 ..< o.count {
            result[i] = o[i].doubleValue
        }
        return result
    }

1 个答案:

答案 0 :(得分:1)

原始数组是Float32值的数组,因此您应该转换为Float数组,而不是Double。