Fork me on GitHub

Tutorial: Transposing, Reshaping, Permuting, Concatenating

Shapeshifting

Transposing

The transpose function will reverse the dimensions of a tensor.

Reshaping

The reshape function will change the shape of a tensor. The number of elements in the new and old shape must be the same.

For example:

let a = toSeq(1..24).toTensor().reshape(2,3,4)

# Tensor[system.int] of shape "[2, 3, 4]" on backend "Cpu"
#           0                      1
# |1      2     3     4| |13    14    15    16|
# |5      6     7     8| |17    18    19    20|
# |9     10    11    12| |21    22    23    24|

The 0 and 1 correspond to the index along the first dimension of the reshaped tensor.

Permuting - Reordering dimension

The permute proc can be used to reorder dimensions. Input is a tensor and the new dimension order

let a = toSeq(1..24).toTensor.reshape(2,3,4)
echo a

# Tensor[system.int] of shape "[2, 3, 4]" on backend "Cpu"
#           0                      1
# |1      2     3     4| |13    14    15    16|
# |5      6     7     8| |17    18    19    20|
# |9     10    11    12| |21    22    23    24|

echo a.permute(0,2,1) # dim 0 stays at 0, dim 1 becomes dim 2 and
dim 2 becomes dim 1

# Tensor[system.int] of shape "[2, 4, 3]" on backend "Cpu"
#        0                1
# |1      5     9| |13    17    21|
# |2      6    10| |14    18    22|
# |3      7    11| |15    19    23|
# |4      8    12| |16    20    24|

Concatenation

Tensors can be concatenated along an axis with the concat proc.

import ../arraymancer, sequtils


let a = toSeq(1..4).toTensor.reshape(2,2)

let b = toSeq(5..8).toTensor.reshape(2,2)

let c = toSeq(11..16).toTensor
let c0 = c.reshape(3,2)
let c1 = c.reshape(2,3)

echo concat(a,b,c0, axis = 0)
# Tensor[system.int] of shape "[7, 2]" on backend "Cpu"
# |1       2|
# |3       4|
# |5       6|
# |7       8|
# |11     12|
# |13     14|
# |15     16|

echo concat(a,b,c1, axis = 1)
# Tensor[system.int] of shape "[2, 7]" on backend "Cpu"
# |1       2     5     6    11    12    13|
# |3       4     7     8    14    15    16|
Arraymancer Technical reference Tutorial Spellbook (How-To's) Under the hood