Search notes:

torch.Tensor: view()

view() reshapes a tensor without copying memory (similar to numpy's reshape())
Unlike numpy's reshape(), however, the tensor returned by view() shares the underlying data with the source tensor (so it is a view to the original data).
import torch

t = torch.tensor([ x for x in range(12) ])

print(t)
#
# tensor([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11])

v = t.view(2, 6)

t[4] = -1 # Change of element in t is reflected in v

print(v)
#
# tensor([[ 0,  1,  2,  3, -1,  5],
#         [ 6,  7,  8,  9, 10, 11]])

w = v.view(2, 3, 2)

t[9] = -2 # change of eleemnt in t is also reflected in w

print(w) 
#
# tensor([[[ 0,  1],
#          [ 2,  3],
#          [-1,  5]],
# 
#         [[ 6,  7],
#          [ 8, -2],
#          [10, 11]]])

See also

expand()

Index