transposed(permutation:)
Returns a new transposed shaped array using a custom permutation.
Declaration
func transposed(permutation axes: [Int]) -> MLShapedArraySlice<Scalar>Discussion
Use this method to convert, for example, the image data layout from [C, H, W] to [C, W, H], where C is channel, W is width, and H is height.
// The source tensor has 3 channels, 128 x 64 image in [C, H, W] layout.
let imageCHW = MLShapedArray<Int32>(scalars: pixelValues,
shape: [3, 64, 128])
// Slice for the first two channels.
let imageSliceCHW = imageCHW[0..<2]
// Transpose.
let imageSliceCWH = imageSliceCHW.transposed(permutation: [0, 2, 1])
imageSliceCHW.shape // [2, 64, 128]
imageSliceCWH.shape // [2, 128, 64]The shape (shape_out) is transposed from the input shape (shape_in) as follows.
shape_out[i]
== permutation.map { shape_in[$0] }The scalar value of the output shaped array (array_out) is related to the input shaped array (array_in) as follows.
array_out(scalarAt: permutation.map { indices[$0] })
== array_in[scalarAt: indices]]