MPSCNN重量订购

Eri*_*ner 6 neural-network ios swift metal conv-neural-network

Metal Performance Shader框架为构建您自己的卷积神经网络提供支持.例如,当创建时,MSPCNNConvolution它需要4D权重张量作为init参数,表示为1D浮点指针.

init(device: MTLDevice,
  convolutionDescriptor: MPSCNNConvolutionDescriptor,
  kernelWeights: UnsafePointer<Float>,
  biasTerms: UnsafePointer<Float>?,
  flags: MPSCNNConvolutionFlags)
Run Code Online (Sandbox Code Playgroud)

关于4D张量的文档有这个说法

过滤器权重的布局被安排为可以重新解释为4D张量(数组)权重[outputChannels] [kernelHeight] [kernelWidth] [inputChannels/groups]

不幸的是,这些信息并没有真正告诉我如何将4D数组排列成一维Float指针.

我尝试按照BNNS对手的要求订购重量,但没有运气.

如何将4D张量(数组)正确表示为1D Float指针(数组)?

PS:我尝试将它像C数组一样排列并获得指向扁平数组的指针,但它不起作用.

UPDATE

@RhythmicFistman:这就是我将它存储在一个普通数组中的方式,我可以将其转换为UsafePointer<Float>(但不起作用):

var output = Array<Float>(repeating: 0, count: weights.count)

for o in 0..<outputChannels {
    for ky in 0..<kernelHeight {
        for kx in 0..<kernelWidth {
            for i in 0..<inputChannels {
                let offset = ((o * kernelHeight + ky) * kernelWidth + kx) * inputChannels + i
                output[offset] = ...
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

小智 1

好吧,我想通了。这是我用来改造卷积和全连接矩阵的 2 个 python 函数

# shape required for MPSCNN [oC kH kW iC]
# tensorflow order is [kH kW iC oC]
def convshape(a):
    a = np.swapaxes(a, 2, 3)
    a = np.swapaxes(a, 1, 2)
    a = np.swapaxes(a, 0, 1)
    return a

# fully connected only requires a x/y swap
def fullshape(a):
    a = np.swapaxes(a, 0, 1)
    return a
Run Code Online (Sandbox Code Playgroud)