본문 바로가기

pyTorch12

nanoGPT, PyTorch DDP, LLM 시각화 GPT-2와 같은 거대한 규모의 Decoder 구조의 언어 모델을 pre-trian 부터 학습하는 경험을 하고 싶은 경우가 있다. 이때는 Andrej Karpathy가 만든 Nano GPT를 참조하면 좋다. Github에 소스 코드가 공개되어 있을 뿐만 아니라 4시간짜리 친절한 강의 영상도 있다. Let's reproduce GPT-2 강의 영상 (링크)nanoGPT 깃허브 (링크)build nano GPT 깃허브 (링크)한국 파이토치에서의 소개 영상 (링크) 개인적으로 유용하게 익혔던 내용은 다음과 같다. Pre-train을 위한 next token prediction과 loss 함수 구성gradient accumulation 구현mixed precision을 torch.autocast로 적용 Mu.. 2025. 4. 18.
Computer Vision Data Augmentation 컴퓨터 비전 분야에서의 데이터 증강은 표준화 되어 있고 코드로 잘 구현되어 있다. PyTorch의 torchvision.transforms와 Albumentations를 활용해서 구현하면 간편하게 구현할 수 있다. Crop, resize, rotate, color change, jitter 등등을 포함한다. Torchvision transforms torchvision의 transforms의 예시 (링크) Geometric Transforms 더보기PadResizeCenterCropFiveCropRandomPerspectiveRandomRotationRandomAffineElasticTransformRandomCropRandomResizedCropPhotometric Transforms 더보기Grays.. 2025. 4. 17.
PyTorch pre-trained Models PyTorch에서는 사전 학습된 모델들을 제공한다. torchvision을 통해서 computer vision 분야의 모델들을 가져와서 학습할 수 있다. 2025.04.17 기준이다. 자세한 내용은 링크를 확인하면 된다. 아래의 컴퓨터 비전 세부 분야에 따라서 모델들을 불러올 수 있다.ClassificationSemantic SegmentationObject Detection, Instance Segmentation and Person Keypoint DetectionVideo ClassificationOptical FlowClassification더보기AlexNet ConvNeXt DenseNet EfficientNet EfficientNetV2 GoogLeNet Inception V3 MaxVit M.. 2025. 4. 17.
PyTorch Tensor의 차원 변환 파이토치에서 사용하는 텐서는 여러가지 형태로 차원을 변환할 수 있다. 차원의 추가, 삭제도 포함이다. 여기서는 squeeze, unsqueeze, view, reshape, traspose, permute를 알아본다. 1. unsqueeze 지정된 위치에 크기가 1일 차원을 추가 import torchx = torch.tensor([[1, 2, 3], [4, 5, 6]])x.shape>> torch.Size([2, 3])y = x.unsqueeze(0)y, y.shape>> (tensor([[[1, 2, 3], [4, 5, 6]]]), torch.Size([1, 2, 3])) y = x.unsqueeze(1)y, y.shape>>(tensor([[[1, 2.. 2025. 4. 16.
ML와 DL에서의 seed 고정 난수 생성, Random Number Generation (RNG)에 쓰이는 seed 시드를 고정한다. 시드를 고정하면 랜덤 넘버가 생성되는 패턴이 고정되고 재현성이 생긴다. 다음의 패키지에 대해서 적용하면 되는듯 하다. randomnumpysklearntorchtorch.cudaLLM의 경우 multi-gpu 사용시 모든 GPU들에 대한 시드 고려 필요 # Fix the Seeddef set_seed(seed: int = 42): """ PyTorch, NumPy, Python Random, CUDA의 모든 시드를 고정하는 함수 Args: seed (int): 고정할 시드값 (기본값: 42) """ # Python Random 시드 고정 random.. 2025. 1. 2.
PyTorch repeat, repeat_interleave, expand 차이 PyTorch의 repeat와 repeat_interleave와 expand의 주요 차이점을 설명한다.1. torch.repeat:1차원 텐서 x = torch.tensor([1, 2, 3])result = x.repeat(3)>> tensor([1, 2, 3, 1, 2, 3, 1, 2, 3]) 2차원 텐서 x = torch.tensor([[1, 2, 3], [4, 5, 6]])x.repeat(2, 1)>> tensor([[1, 2, 3], [4, 5, 6], [1, 2, 3], [4, 5, 6]]) 이때 repeat 안에 들어가는 숫자는 복사하고자 하는 텐서의 차원이 같아야 한다. 2차원 텐서에 대해서 아래와 같이 입력하면 에러.. 2024. 11. 1.