Pytorch-3 구조

HSUNEH·2022년 9월 26일
0

pytorch 구조 이해해보자

  • 초기 단계 : 대화식 개발 과정
    -> 학습과정과 디버깅 등 지속적인 확인
    .
  • 배포 및 공유 단계 :
    notebook: 공유의 어려움, 실행순서 꼬임
    DL 코드도 하나의 프로그램 : 개발 용이성 확보와 유지보수 향상 필요
    .
    코드 : OOP + 모듈 -> 프로젝트

*파이토치 프로젝트 추천 템플릿
https://github.com/victoresque/pytorch-template

코랩 꿀팁) SSH

외부에서(로컬로) 접속할 수 있는 remote -ssh 사용해보자

vs code로 코랩 환경 터미널 접속 가능

GPU 넣어서 해야지 사용편리

모듈 구성

![[4F44ED3D-EFFB-4089-937C-51FCA7D75A82.png]]

config .json: 설정 정보 모아둔 파일

config parser : config parser가 다시의 get_item 은 index 값을 넣어주면 정해준 값을 보여주는 함수

.

def init_obj(self, name, module, *args, **kwargs):
        """
        Finds a function handle with the name given as 'type' in config, and returns the
        instance initialized with corresponding arguments given.
        `object = config.init_obj('name', module, a, b=1)`
        is equivalent to
        `object = module.name(a, b=1)`
        """
				# self는 object 자기자신인데 []를 통해 config 파일을 불러옴
        module_name = self[name]['type']
        module_args = dict(self[name]['args'])
        assert all([k not in module_args for k in kwargs]), 'Overwriting kwargs given in config file is not allowed'
        module_args.update(kwargs)

				# 모듈안에 있는 attribute를 가져오는 것
        return getattr(module, module_name)(*args, **module_args)

init_obj : obj를 불러오는 것
getattr : 모듈안에 잇는 attribute를 가져오는 것

# model.model.py
# 다른 모델을 사용하고 싶으면 다른 모델을 정의하고 config파일에서 name 변경
class MnistModel(BaseModel):
    def __init__(self, num_classes=10):
        super().__init__()
        self.conv1 = nn.Conv2d(1, 10, kernel_size=5)
        self.conv2 = nn.Conv2d(10, 20, kernel_size=5)
        self.conv2_drop = nn.Dropout2d()
        self.fc1 = nn.Linear(320, 50)
        self.fc2 = nn.Linear(50, num_classes)

    def forward(self, x):
        x = F.relu(F.max_pool2d(self.conv1(x), 2))
        x = F.relu(F.max_pool2d(self.conv2_drop(self.conv2(x)), 2))
        x = x.view(-1, 320)
        x = F.relu(self.fc1(x))
        x = F.dropout(x, training=self.training)
        x = self.fc2(x)
        return F.log_softmax(x, dim=1)
  • 다른 model을 만들고 싶으면 model.model.py에서 새로운 모델을 만들어 주고, config.josn 파일을 수정하면 사용가능하다.
# trainer.py
trainer = Trainer(model, criterion, metrics, optimizer,
                      config=config,
                      device=device,
                      data_loader=data_loader,
                      valid_data_loader=valid_data_loader,
                      lr_scheduler=lr_scheduler)

# 학습시작
trainer.train()

.

팁)
주피터 노트북에서 벗어나거라 중생.
프로젝트 구조를 익혀라 아가야.

profile
watch_it_all_and_die

0개의 댓글