본문 바로가기
AI/파이토치(Pytorch)

[파이토치] 텐서 기초

by Hyen4110 2021. 9. 8.
반응형

1. 텐서(tensor)의 생성

x = torch.arange(6).reshape(2,3)

x
# tensor([[0, 1, 2],
#         [3, 4, 5]])

2. 텐서(tensor)의 인덱싱

tensor[row_num , colum_num]

x
# tensor([[0, 1, 2],
#         [3, 4, 5]])

print(x[1,1])
# tensor(4)

 

3. 텐서(tensor)의 슬라이싱

tensor[ : , a : b]

' : ' -> 전체 row/column

x
# tensor([[0, 1, 2],
#         [3, 4, 5]])

print(x[ : , 1 ])
# tensor([1, 4])

② ' a : b ' -> row/column a에서 b-1까지

x
# tensor([[0, 1, 2],
#         [3, 4, 5]])

print(x[ : , 1:3])
# tensor([[1, 2],
#         [4, 5]])

http://scipy-lectures.org/_images/numpy_indexing.png

 

 

 

 

반응형

댓글