KalelPark's LAB

[ Pytorch ] Chunk 활용하기 본문

Python/Pytorch

[ Pytorch ] Chunk 활용하기

kalelpark 2023. 1. 3. 16:13

Chunk

     - Tensor를 지정된 Chunk의 개수로 분할하려고 합니다. 각 Chunk는 입력 텐서의 View이다.

     -  torch.tensor_split()은 항상 명확하게, Chunk하지만, torch.Chunk는 작거나, 같게 합니다. (유연성)

import torch

chunk_example = torch.arange(12)
print(chunk_example)
  ->  tensor([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11])
print(chunk_example.size())
  ->  torch.Size([12])
print(chunk_example.chunk(6, dim = -1))
  -> (tensor([0, 1]), tensor([2, 3]), tensor([4, 5]), tensor([6, 7]), tensor([8, 9]), tensor([10, 11]))

* 하단 링크의 예시

import torch

batch_size, nmic, nsample = 3, 6, 64000

x = torch.randn(batch_size, nmic, nsample)  # [3, 6, 64000]
mic1, mic2, mic3, mic4, mic5, mic6 = torch.chunk(x, nmic, dim = 1)
print(mic1.size(), mic2.size(), mic3.size(), mic4.size(), mic5.size(), mic6.size())
	-> torch.Size([3, 1, 64000]) torch.Size([3, 1, 64000]) torch.Size([3, 1, 64000]) torch.Size([3, 1, 64000]) torch.Size([3, 1, 64000]) torch.Size([3, 1, 64000])

 

참조

https://sanghyu.tistory.com/6

 

[PyTorch] Tensor 자르기/분리하기: chunk함수

Chunk함수란? chunk함수는 tensor를 쪼개는 함수이다. tensor를 몇개로 어떤 dimension으로 쪼갤지 설정해주고 사용하면된다. output = torch.chunk(input, n = (몇개로 쪼갤지 설정), dim = (어떤 차원에 적용할지)) i

sanghyu.tistory.com

https://pytorch.org/docs/stable/generated/torch.chunk.html#torch-chunk

 

torch.chunk — PyTorch 1.13 documentation

Shortcuts

pytorch.org

https://github.com/pytorch/pytorch/blob/master/torch/csrc/api/include/torch/data/datasets/chunk.h

 

GitHub - pytorch/pytorch: Tensors and Dynamic neural networks in Python with strong GPU acceleration

Tensors and Dynamic neural networks in Python with strong GPU acceleration - GitHub - pytorch/pytorch: Tensors and Dynamic neural networks in Python with strong GPU acceleration

github.com

 

Comments