본문으로 건너뛰기
Minsing Log
뒤로 가기

텐서 조작 팁 - unsqueeze는 뭐냐?

Data & ML

텐서 조작 팁

https://sputnik-kr.tistory.com/229

unsqueeze는 뭐냐?

unsqueeze는 PyTorch에서 텐서에 새로운 차원(길이 1인 축)을 추가하는 함수입니다.
예를 들어,

이렇게 하면 기존에 없던 batch 차원이 생겨서,

왜 쓰나?

예시

import torch
x = torch.tensor([1, 2, 3, 4])       # shape: (4,)
x1 = x.unsqueeze(0)                  # shape: (1, 4)
x2 = x.unsqueeze(1)                  # shape: (4, 1)

정리:
unsqueeze는 텐서에 새로운 차원을 추가해 shape을 바꿔주는 함수입니다.
딥러닝에서 배치 차원 등을 맞출 때 필수적으로 사용합니다[2][3][4][5][7].

Citations: [1] https://pplx-res.cloudinary.com/image/private/user_uploads/35150927/dcfdae12-8bbe-4f0b-89ae-d9a629d4a871/image.jpg [2] https://pplx-res.cloudinary.com/image/private/user_uploads/35150927/dcfdae12-8bbe-4f0b-89ae-d9a629d4a871/image.jpg [3] https://pytorch.org/docs/stable/generated/torch.unsqueeze.html [4] https://stackoverflow.com/questions/57237352/what-does-unsqueeze-do-in-pytorch [5] https://www.codecademy.com/resources/docs/pytorch/tensors/unsqueeze [6] https://www.codecademy.com/resources/docs/pytorch/tensor-operations/unsqueeze [7] https://www.datasciencebyexample.com/2023/05/19/squeeze-and-unsqueeze-in-pytorch/ [8] https://dnmtechs.com/understanding-the-unsqueeze-function-in-pytorch/ [9] https://www.projectpro.io/recipes/unsqueeze-torch-tensor [10] https://blog.naver.com/je_un/222297587260 [11] https://codemax.app/snippet/what-does-unsqueeze-do-in-pytorch-in-python/ [12] https://stackoverflow.com/questions/57237352/what-does-unsqueeze-do-in-pytorch/57237378 [13] https://www.tutorialspoint.com/how-to-squeeze-and-unsqueeze-a-tensor-in-pytorch [14] https://iifx.dev/en/articles/332257550 [15] https://sanghyu.tistory.com/86 [16] https://www.slingacademy.com/article/pytorch-squeezing-and-unsqueezing-tensors/ [17] https://stackoverflow.com/questions/57237352/what-does-unsqueeze-do-in-pytorch/65831759 [18] https://www.youtube.com/watch?v=VTryujXrMOs [19] https://blog.csdn.net/chengyq116/article/details/106962842 [20] https://researchdatapod.com/understanding-unsqueeze-in-pytorch-a-beginner-friendly-guide/ [21] https://myscale.com/blog/mastering-tensor-manipulation-pytorch-guide-torch-unsqueeze/ [22] https://pytorch.org/docs/stable/generated/torch.Tensor.unsqueeze.html


Answer from Perplexity: pplx.ai/share

공개 여정 구독

글이 올라오면 이메일로 보내드릴게요.

개발, 창업, 배움, 실패와 회고를 짧고 솔직하게 기록합니다. 검색으로 스쳐가는 독자가 아니라 여정을 따라오는 팬을 만들기 위한 뉴스레터입니다.

뉴스레터는 준비 중입니다. 그동안 RSS 로 구독하거나 이메일 로 알림을 요청해 주세요.


이 글 공유하기:

댓글

GitHub 계정으로 로그인해 댓글을 남길 수 있습니다.


이전 글
임베딩과 tokenize개념
다음 글
nn.Embedding이란