在通道上重复一个数组

Repeat an array over channels

对于形状数组(示例数、行、高度、通道)。我怎样才能简单地用示例数量替换频道?我已经搜索了 np.repeat() 但我没有应用它。

import numpy as np
array = np.array([
                  [
                     [[0],[1]],
                     [[2],[3]],
                     [[4],[5]]
                  ],

                  [
                     [[0],[1]],
                     [[2],[3]],
                     [[4],[5]]
                  ],

                  [
                     [[0],[1]],
                     [[2],[3]],
                     [[4],[5]]
                  ],

                  [
                     [[0],[1]],
                     [[2],[3]],
                     [[4],[5]]
                  ]
                 ])
array.shape # (4, 3, 2, 1)

我想要一个形状为 (4, 3, 2, 4) 的数组。通道应替换为训练示例数。

您可以使用 np.tile:

np.tile(array, (1, 1, 1, array.shape[0]))

np.repeat:

np.repeat(array[:, :, :,], array.shape[0], axis=3)