본문 바로가기
728x90
반응형

Torch13

Detection Task에서 collate_fn의 입·출력 구조와 default_collate의 한계 Detection task를 위한 collate_fnDetection 테스크의 경우, DataLoader에서 collate_fn을 일반적으로 다음과 같이 변경해줘야 함.def collate_fn(batch): # batch: [(img, target), (img, target), ...] imgs, targets = zip(*batch) return list(imgs), list(targets)loader = DataLoader( train_ds, batch_size=2, shuffle=True, collate_fn=collate_fn,)detection task의 경우, 각 image 별로 가지고 있는 object의 수가 다를 수 있음.그 결과 targets.. 2025. 12. 15.
torchvision.datasets.ImageFolder 사용하기. torchvision.datasets.ImageFolder는 PyTorch에서 Image Classification Task를 위한 Dataset을 쉽게 구성할 수 있게 해주는 클래스임. 정확한 Full qualified name은 torchvision.datasets.folder.ImageFolder 임: torchvision.datasets 에 reexport되어 있음.torch.utils.data.Dataset ↑torchvision.datasets.vision.VisionDataset ↑torchvision.datasets.folder.ImageFolder original API documentation:https://docs.pytorch.org/vision/stabl.. 2025. 6. 17.
[DL] Classification 을 위한 Activation Func. 와 Loss Func: PyTorch PyTorch: Classification에서의 Output Func(~Activation Func.)와 Loss Func. 요약PyTorch는 다양한 종류의 loss function(손실 함수)와 activation function(활성화 함수)를 제공하는데,이 중에서 classification task를 수행하는 모델에서 사용되는 것들을 위주로 정리함.최종 출력 함수로 사용되는 activation functions 와 이들과 같이 사용되는 loss functions는 다음과 같음.최종 출력으로로사용되는 activation functions: Sigmoid, Softmax, LogSoftmaxloss functions: BCELoss, BCEWithLogitsLoss, CrossEntropyLoss, .. 2024. 5. 23.
[PyTorch] Custom Model 과 torch.nn.Module의 메서드들. Custom Model 만들기0. nn.Module torch.nn.Module은 PyTorch에서 모든 신경망 모델과 계층의 기반이 되는 클래스임.Custom Model (사용자 정의 모델)부터 Built-in Layer(nn.Linear, nn.Conv2d, etc.)까지 전부 nn.Module을 상속받아 만들어짐.Model (or Submodule)을 구조화하고 학습 가능한 parameters를 관리하는 데 핵심적인 역할을 함. PyTorch에선 Model과 Layer 모두 nn.Module 의 sub-class가 됨: torchvision의 transformer들도 마찬가지임. 때문에 모든 Custom Model은 nn.Module (또는 그 sub-class)을 상속하여 만들어진다. nn.Mo.. 2024. 4. 12.
[PyTorch] CustomANN Example: From Celsius to Fahrenheit Optional Librariestorchviz: 0.0.2torchsummary: 1.5.1pip install --quiet torchvizpip install torchsummaryRequired Librariestorch : 2.2.1+cu121np : 1.25.2import numpy as npimport torchimport matplotlib.pyplot as plt# import torchviz, torchsummaryfrom torch.nn import Module, init, Linear, Parameter, ReLUfrom torch import optimSimple Data Generationdef gen_xy(cnt, std=4.): x = np.linspace(-10,10,.. 2024. 4. 12.
[PyTorch] torch.nn.init torch.nn.init 모듈 ANN을 구현할 때, 각 layer의 weight 와 bias를 초기화하는 방법을 제공함. 초기화는 ANN의 수렴 속도 및 학습 안정화에 매우 큰 영향을 줌. torch.nn.init는 일반적으로 사용되는 다양한 초기화 방법들이 구현되어 있음. .uniform_(tensor, a=0., b=1.) 지정된 parameters를 uniform distance로 초기화 a와 b는 값의 범위를 지정하는데 사용됨: [a,b) .normal_(tensor, mean=0., std=1.) normal distribution으로 초기화. .constant_(tensor, val=0.) val 에 지정된 상수값으로 초기화. .ones_() and .zeros_() 0과 1로 초기화. .xa.. 2024. 4. 11.
728x90
반응형