x = torch.Tensor([[1,2],
[3,4]])
print(x)
print('x[0,0] :', x[0,0])
print('x[0,1] :', x[0,1])
print('x[1,0] :', x[1,0])
print('x[1,1] :',x[1,1])
print('x[:,0] :', x[:,0])
print('x[:,1] :',x[:,1])
print('x[0,:] :',x[0,:])
print('x[1,:] :',x[1,:])
tensor([[1., 2.],
[3., 4.]])
x[0,0] : tensor(1.)
x[0,1] : tensor(2.)
x[1,0] : tensor(3.)
x[1,1] : tensor(4.)
x[:,0] : tensor([1., 3.])
x[:,1] : tensor([2., 4.])
x[0,:] : tensor([1., 2.])
x[1,:] : tensor([3., 4.])
x = torch.randn(4,5)
print(x)
y = x.view(20) # flat하게 20으로 펼쳐줌
print(y)
z = x.view(5,-1) # 5개의 묶음으로, -1로써 알아서 추려줘!
print(z)
tensor([[ 0.9239, 0.3347, -0.1050, -0.2384, -1.2842],
[ 0.7395, 0.6810, 1.6807, 1.1335, -0.6835],
[ 0.7531, -0.0191, 1.1821, -2.7346, 0.4662],
[-0.0083, -0.2901, 0.7707, -0.1552, 1.3053]])
tensor([ 0.9239, 0.3347, -0.1050, -0.2384, -1.2842, 0.7395, 0.6810, 1.6807,
1.1335, -0.6835, 0.7531, -0.0191, 1.1821, -2.7346, 0.4662, -0.0083,
-0.2901, 0.7707, -0.1552, 1.3053])
tensor([[ 0.9239, 0.3347, -0.1050, -0.2384],
[-1.2842, 0.7395, 0.6810, 1.6807],
[ 1.1335, -0.6835, 0.7531, -0.0191],
[ 1.1821, -2.7346, 0.4662, -0.0083],
[-0.2901, 0.7707, -0.1552, 1.3053]])
tensor = torch.rand(1,3,3) print(tensor) print(tensor.shape)
결과값
tensor([[[0.6422, 0.0997, 0.9823], [0.9551, 0.9470, 0.0162], [0.7225, 0.4936, 0.2203]]]) torch.Size([1, 3, 3]) # torch.Size가 ([1,3,3])이라는 것을 알 수 있음.
tensor.squeeze() 사용
t = tensor.squeeze() print(t) print(t.shape)
결과값
tensor([[0.6422, 0.0997, 0.9823], [0.9551, 0.9470, 0.0162], [0.7225, 0.4936, 0.2203]]) torch.Size([3, 3]) # torch.size가 ([3,3])으로 변한 것을 알 수 있음.
t = torch.randn(3,3)
print(t)
print(t.shape)
tensor([[ 0.7461, 0.8344, 0.3284],
[-0.5651, 1.9802, -0.7310],
[-0.8895, 0.9181, -1.2999]])
torch.Size([3, 3])
torch.unsqueeze 사용
tensor = t.unsqueeze(dim=0) # 0차원에 차수를 증가시킴. 맨앞에 1이붙음. print(tensor) print(tensor.shape)
결과값
tensor([[[ 0.7461, 0.8344, 0.3284], [-0.5651, 1.9802, -0.7310], [-0.8895, 0.9181, -1.2999]]]) torch.Size([1, 3, 3])
tensor = t.unsqueeze(dim=2) # 0,1,2차원에 차수를 증가시킴. 맨 뒤에 1이붙음. print(tensor) print(tensor.shape)
결과값
tensor([[[ 0.7461], [ 0.8344], [ 0.3284]], [[-0.5651], [ 1.9802], [-0.7310]], [[-0.8895], [ 0.9181], [-1.2999]]]) torch.Size([3, 3, 1])
x = torch.FloatTensor([1,4])
print(x)
y = torch.FloatTensor([2,5])
print(y)
z = torch.FloatTensor([3,6])
print(z)
print(torch.stack([x,y,z]))
tensor([1., 4.])
tensor([2., 5.])
tensor([3., 6.])
tensor([[1., 4.],
[2., 5.],
[3., 6.]])
a = torch.randn(1,3,3) print(a) b = torch.randn(1,3,3) print(b) c = torch.cat((a,b), dim = 0) # 0차원을 기준으로 concat 해줘 - 그래서 맨 앞자리끼리 더해져서 1 +1 = 2 print(c) print(c.size())
결과값
tensor([[[ 0.4152, -1.5082, -0.0938], [-0.1822, 0.2784, 0.9010], [-0.1542, 0.6093, -0.7067]]]) tensor([[[ 0.2072, 0.6136, 0.0738], [ 1.5876, 0.1681, -0.4224], [ 0.6551, 1.8578, 0.5681]]]) tensor([[[ 0.4152, -1.5082, -0.0938], [-0.1822, 0.2784, 0.9010], [-0.1542, 0.6093, -0.7067]], [[ 0.2072, 0.6136, 0.0738], [ 1.5876, 0.1681, -0.4224], [ 0.6551, 1.8578, 0.5681]]]) torch.Size([2, 3, 3])
a = torch.randn(1,3,3) print(a) b = torch.randn(1,3,3) print(b) c = torch.cat((a,b), dim = 1) # 0차원을 기준으로 concat 해줘 - 그래서 맨 앞자리끼리 더해져서 1 +1 = 2 print(c) print(c.size())
결과값
tensor([[[-1.1485, -0.4205, 0.0642], [-0.0410, -1.2241, -0.8416], [-0.5100, -1.0091, 1.9121]]]) tensor([[[ 0.9528, 1.0127, -1.1162], [ 0.3664, 0.2822, 0.4986], [-1.3757, 0.4239, -0.1506]]]) tensor([[[-1.1485, -0.4205, 0.0642], [-0.0410, -1.2241, -0.8416], [-0.5100, -1.0091, 1.9121], [ 0.9528, 1.0127, -1.1162], [ 0.3664, 0.2822, 0.4986], [-1.3757, 0.4239, -0.1506]]]) torch.Size([1, 6, 3])
a = torch.randn(1,3,3) print(a) b = torch.randn(1,3,3) print(b) c = torch.cat((a,b), dim = 2) # 0차원을 기준으로 concat 해줘 - 그래서 맨 앞자리끼리 더해져서 1 +1 = 2 print(c) print(c.size())
결과값
tensor([[[ 1.4339, 0.6223, -0.0402], [ 0.1431, 1.2022, -0.0739], [-0.3410, 0.0795, -0.5344]]]) tensor([[[-1.6797, 0.1316, 0.0733], [-1.9522, 1.3740, -1.4289], [ 0.5454, 0.6269, 0.7874]]]) tensor([[[ 1.4339, 0.6223, -0.0402, -1.6797, 0.1316, 0.0733], [ 0.1431, 1.2022, -0.0739, -1.9522, 1.3740, -1.4289], [-0.3410, 0.0795, -0.5344, 0.5454, 0.6269, 0.7874]]]) torch.Size([1, 3, 6])
tensor = torch.rand(3,6)
print(tensor)
t1, t2, t3 = torch.chunk(tensor,3, dim=1)
print(t1)
print(t2)
print(t3)
tensor = torch.rand(3,6)
t1, t2 = torch.split(tensor, 3, dim=1)
print(tensor)
print(t1)
print(t2)
[참고][chunk]https://sanghyu.tistory.com/6