목록기초 노트/PyTorch (7)
플로라도의 data workout
1.모델1-1. 가중치 로드 model.load(weight_path, map_location=device) # 코드 확인 1-2. 모델 객체 이동 확인 모델객체.to(device) # 코드 확인 1-3. 모델 평가모드 설정with torch.no_grad() 루프와 model.eval()모드 확인 2. tensor이동'data(tensor).to(device)' 코드 확인 3. nn.DataParallel 삭제멀티-GPU 환경에서 학습한 스크립트 일부를 이용할시, 추론단계에서는 'nn.DataParallel' 관련 코드 삭제 model.to('cuda')model = nn.DataParallel(model)model.to('cuda') 과 의 model의 state_dict의 key값은 상이하..
(거의) 모든 것 파이토치의 DataLoader 클래스는 데이터셋을 다루기 위한 강력한 도구로, 모델 학습을 위한 배치(batch)를 생성하고 데이터를 불러오는(load) 작업을 관리하는 기능을 한다.각각의 인자(arugment)들은 데이터로더의 설정을 돕게 된다. DataLoader- Dataset에 대하여 data를 로드하는 iterative한 객체- 아래와 같은 argument를 받음import torchfrom torch.utils.data import Dataset, DataLoader, SequentialSampler, RandomSampler, SubsetRandomSampler, BatchSamplerDataLoader(dataset, batch_size=1, shuffle=False, s..
파이토치에서 torch.nn.functional모듈은 통상 F로 임포트하여 사용되곤하는데이때 nn모듈의 클래스들과 비교가 된다. 예를 들면, F.avg_pool2d와 nn.AvgPool2d는 정확히 동일한 기능을 제공하지만, 두 함수의 사용 방법과 적용하는 컨텍스트에는 차이가 있다. 1.F.avg_pool2d- 모듈 : torch.nn.functional - 특징 : 1. 매 호출마다 풀링 파라미터를 지정해야 하므로, 동적인 풀링 사이즈나 조건에 따른 풀링을 적용해야 할때 사용한다. 2. 함수형 API를 사용하므로, 코드에서 '절차적' 접근을할때 적합하다. 3. 상태가 없는 절차적 함수이므로 nn.Sequential의 구성요소로 추가될 수 없다. - 사용 예import torchimport torch..
torchivsion의 pre-trained된 Resnet18, Resnet50과 같은 모델의 가장 마지막단에는 Global Average Pooling을 사용하여 Linear Classifier의 역할을 대체하고 있다. Global Average Pooling의 아이디어는 Resnet뿐만 아니라 많은 딥러닝 아키텍쳐에서 쓰이는데 파이토치에서 제공하는 pooling layer에는 별도의 global average pooling layer가 없다. 그러나 기존의 average pooling 2d와 adaptive_avg_pool2d로 gap를 간단히 구현할 수 있었다. import torchimport torch.nn.functional as F# 임의의 텐서를 생성;: 배치 크기 1, 10개의 채널, 2..
loss = nn.CrossEntropyLoss()(preds, label.to(device)) 에러 메세지 : TypeError: cross_entropy_loss(): argument 'input' (position 1) must be Tensor, not NoneType 학습 루프안의 loss를 계산하는 과정에서 첫번째 포지션 인자인 preds가 NoneType이라는 에러로, 모델의 forward 메서드에서 리턴값이 누락된 경우
파이토치(Pytorch)의 기본 데이터 구조인 텐서(Tensor)는 데이터와 gradient계산을 위한 메타데이터를 포함하고 있다. 기본적으로 텐서는 다음과 같은 주요 속성을 갖는다. 1. data 텐서의 주요 구성 요소로, 실제 수치 정보를 저장한다. 데이터는 다양한 차원을 가질 수 있으며, 신경망에서는 주로 벡터, 행렬 또는 더 높은 차원의 배열로 사용한다. 2. dtype 텐서에 저장된 데이터 타입을 정의한다. 예를 들어, 'torch.float32'; 'torch.int64'등이 있다. 3. device 텐서가 어떤 장치(CPU,GPU)에 할당되어 있는지를 나타낸다. 4. requires_grad 이 속성이 'True'로 설정되어 있으면, 텐서에 대한 모든 연산은 자동 미분 시스템에 의해서 추적되..
평소에는 주로 코랩을 사용 하였으나 점차 코랩 보다는 로컬 환경에 익숙해져야 할것같아서 윈도우 로컬에서 vscode및 주피터랩으로 python을 설치하고, 여러 필요한 라이브러리를 설치하고자 하였다. 파이토치도 마찬가지 대상이었다. 아나콘다를 사용하는 주피터 환경부터 세팅 해주려 했고, cuda 11.7버전의 그래픽카드를 사용하고 있어서 이에 맞는 cuDNN과 CUDA toolkit의 설치를 마친 뒤 호기롭게 pip install torch 를 통해 pytorch를 설치 구글 코랩과 같이 멀끔하게 동작하는 환경을 기대하였으나 웬걸, torch.cuda.is_available() >>> False cuda가 잡히지 않았다.(error 1) 뒤늦게 cuda버전에 맞는 pytorch로 설정해줘야 한다는것을 알..