
x = torch.arange(12)
print('view generation:', x.is_contiguous())
print('x=:', x)
print('shape of x:', x.shape)
view generation: True
x=: tensor([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11])
shape of x: torch.Size([12])
y = x.view(3, -1)
print('y=:', y)
print('shape of y:', y.shape)
y=: tensor([[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11]])
shape of y: torch.Size([3, 4])
z = x.view(2, 2, -1)
print('z=:', z)
print('shape of z:', z.shape)
z=: tensor([[[ 0, 1, 2],
[ 3, 4, 5]],
[[ 6, 7, 8],
[ 9, 10, 11]]])
shape of z: torch.Size([2, 2, 3])
x[0] = 10
print('x[0]:', x[0])
print('y[0,0]:', y[0,0])
print('z[0,0,0]:', z[0,0,0])
print(x.storage().data_ptr()==y.storage().data_ptr())
x[0]: tensor(10)
y[0,0]: tensor(10)
z[0,0,0]: tensor(10)
True
x = torch.arange(6)
y = x.view(1, 2, -1)
print('y=:', y)
print('shape of y:', y.shape)
y=: tensor([[[0, 1, 2],
[3, 4, 5]]])
shape of y: torch.Size([1, 2, 3])
y2 = y.flatten()
print('y2=:', y2)
print('shape of y2:', y2)
y2=: tensor([0, 1, 2, 3, 4, 5])
shape of y2: tensor([0, 1, 2, 3, 4, 5])
y3 = y.flatten(1)
print('y3=:', y3)
print('shape of y3:', y3.shape)
y3=: tensor([[0, 1, 2, 3, 4, 5]])
shape of y3: torch.Size([1, 6])
y4 = y.flatten(0, 1)
print('y4=:', y4)
print('shape of y4:', y4.shape)
y4=: tensor([[0, 1, 2],
[3, 4, 5]])
shape of y4: torch.Size([2, 3])
주의 : end_dim은 미포함
x[0] = 10
print('y=:', y)
print('y2=:', y2)
print('y3=:', y3)
print('y4=:', y4)
y=: tensor([[[10, 1, 2],
[ 3, 4, 5]]])
y2=: tensor([10, 1, 2, 3, 4, 5])
y3=: tensor([[10, 1, 2, 3, 4, 5]])
y4=: tensor([[10, 1, 2],
[ 3, 4, 5]])
x = torch.arange(6)
y = x.reshape(shape=(-1, 3))
print('x=:', x)
print('y=:', y)
print('contiguous:', y.is_contiguous())
x=: tensor([0, 1, 2, 3, 4, 5])
y=: tensor([[0, 1, 2],
[3, 4, 5]])
contiguous: True
z = y.transpose(dim0 = 0, dim1 = 1)
print('z=:', z)
print('contiguous:', z.is_contiguous())
z=: tensor([[0, 3],
[1, 4],
[2, 5]])
contiguous: False
z = z.contiguous()
print(z.is_contiguous())
print('z.view(-1, 3)=:', z.view(-1, 3))
True
z.view(-1, 3)=: tensor([[0, 3, 1],
[4, 2, 5]])
x = torch.arange(6).view(1, -1, 1)
print('x=:', x)
print('shape of x:', x.shape)
x=: tensor([[[0],
[1],
[2],
[3],
[4],
[5]]])
shape of x: torch.Size([1, 6, 1])
y = torch.squeeze(x)
print('y=:', y)
print('shape of y:', y.shape)
y=: tensor([0, 1, 2, 3, 4, 5])
shape of y: torch.Size([6])
z = torch.squeeze(x, dim=0)
print('z=:', z)
print('shape of z:', z.shape)
z=: tensor([[0],
[1],
[2],
[3],
[4],
[5]])
shape of z: torch.Size([6, 1])
w = torch.unsqueeze(z, dim=0)
print('w=:', w)
print('shape of w:', w.shape)
w=: tensor([[[0],
[1],
[2],
[3],
[4],
[5]]])
shape of w: torch.Size([1, 6, 1])
x = torch.arange(4).view(-1, 2)
y = torch.tensor([4, 5])
print('x=:', x)
print('y=:', y)
print('shape of y:', y.shape)
x=: tensor([[0, 1],
[2, 3]])
y=: tensor([4, 5])
shape of y: torch.Size([2])
z = torch.cat((x, y.reshape(1, 2)))
print('z=:', z)
print('shape of z:', z.shape)
z=: tensor([[0, 1],
[2, 3],
[4, 5]])
shape of z: torch.Size([3, 2])
z2 = torch.concat((x, y.reshape(1, 2)))
print('z2=:', z2)
print('shape of z2:', z2.shape)
z2=: tensor([[0, 1],
[2, 3],
[4, 5]])
shape of z2: torch.Size([3, 2])
w = torch.cat((x, y.reshape(2, 1)), dim = 1)
print('w=:', w)
print('w.shape=:', w.shape)
w=: tensor([[0, 1, 4],
[2, 3, 5]])
w.shape=: torch.Size([2, 3])
x = torch.tensor([[0, 1], [2, 3]])
print('shape of x:', x.shape)
shape of x: torch.Size([2, 2])
y = torch.stack((x, x, x))
print('y=:', y)
print('shape of y:', y.shape)
y=: tensor([[[0, 1],
[2, 3]],
[[0, 1],
[2, 3]],
[[0, 1],
[2, 3]]])
shape of y: torch.Size([3, 2, 2])
z = torch.stack((x, x, x), dim=1)
print('z=:', z)
print('shape of z:', z.shape)
z=: tensor([[[0, 1],
[0, 1],
[0, 1]],
[[2, 3],
[2, 3],
[2, 3]]])
shape of z: torch.Size([2, 3, 2])
x = torch.tensor([0, 1, 2])
print('shape of x:', x.shape)
shape of x: torch.Size([3])
y = x.expand(2, 3)
print('y=:', y)
print('shape of y:', y.shape)
y=: tensor([[0, 1, 2],
[0, 1, 2]])
shape of y: torch.Size([2, 3])
x = x.view(3, 1)
z = x.expand(3, 2)
print('x=:', x)
print('z=:', z)
print('shape of z:', z.shape)
x=: tensor([[0],
[1],
[2]])
z=: tensor([[0, 0],
[1, 1],
[2, 2]])
shape of z: torch.Size([3, 2])
x[0] = 10
print('y=:', y)
print('z=:', z)
y=: tensor([[10, 1, 2],
[10, 1, 2]])
z=: tensor([[10, 10],
[ 1, 1],
[ 2, 2]])
x = torch.tensor([0, 1, 2])
print('shape of x:', x.shape)
shape of x: torch.Size([3])
y = x.repeat(2, 3)
print('y=:', y)
print('shape of y:', y.shape)
y=: tensor([[0, 1, 2, 0, 1, 2, 0, 1, 2],
[0, 1, 2, 0, 1, 2, 0, 1, 2]])
shape of y: torch.Size([2, 9])
x = x.view(3, 1)
z = x.repeat(2, 3)
print('z=:', z)
print('shape of z:', z.shape)
z=: tensor([[0, 0, 0],
[1, 1, 1],
[2, 2, 2],
[0, 0, 0],
[1, 1, 1],
[2, 2, 2]])
shape of z: torch.Size([6, 3])