전체 글
-
파이토치 모델 파라메터 저장/불러오기PyTorch 2020. 5. 23. 16:52
파이토치로 신경망 모델 파라메터를 학습시키는데는 시간이 많이 걸린다. 그래서 한번 학습해서 얻어진 파라메터를 소중히 보관해야하는 방법을 알아봤다. 1. 저장하기 torch.save(model.state_dict(), 'PATH.pth') torch 모듈의 save() 함수와, torch.nn.Module의 state_dict()함수를 사용한다. state_dict()는 모델의 파라메터를 dictionary 타입으로 반환해 주고, save() 함수는 인수로 들어온 오브젝트를 파일로 저장해준다. 2. 불러오기 model.load_state_dict(torch.load('PATH.pth')) model.eval() torch 모듈의 load() 함수와, torch.nn.Module의 load_state_dic..
-
git rebase로 이전 커밋 수정하기Git 2020. 5. 23. 14:14
이미 커밋해버린 내용을 되돌리고 싶을때, git rebase를 사용할 수 있다. 참고: https://git-scm.com/docs/git-rebase Git - git-rebase Documentation In interactive mode, you can mark commits with the action "edit". However, this does not necessarily mean that git rebase expects the result of this edit to be exactly one commit. Indeed, you can undo the commit, or you can add other commits. This can be u git-scm.com git-rebase - ..
-
torchvision으로 MNIST 데이터 로드하기PyTorch 2020. 5. 23. 01:45
MNIST 데이터를 쉽게 로드하기 위해서는 torchvision 모듈을 사용해야한다. torchvision에 관한 문서를 한번 읽어봤다. *참고: https://pytorch.org/docs/stable/torchvision/index.html torchvision — PyTorch 1.5.0 documentation Shortcuts pytorch.org TORCHVISION The torchvision package consists of popular datasets, model architectures, and common image transformations for computer vision. 그런데 특별한 설명은 없고, 컴퓨터 비전을 위한 이미지 변환, 네트워크 아키텍쳐, 데이터셋 등을 모아..
-
torch.optimPyTorch 2020. 5. 22. 22:25
신경망 훈련에는 SGD, Adam등의 상황에 따라 다양한 optimizer가 사용된다. 파이토치에서는 torch.optim 모듈을 이용해서 optimizer를 지정하는데, 베이스 클래스인 torch.optim.Optimizer를 상속받아서 여러가지 optimizer 클래스가 미리 구현되어있다. 1. optimizer 클래스 초기화 제일 중요한 매개변수는 신경망의 파라메터이다. Variable 타입의 파라메터들을 iterable 오브젝트로 넣어줘야한다. 그 외에는 각 optimizer 타입에 따라 learning rate, weight decay 등을 넣어주면 된다. optimizer = optim.SGD(model.parameters(), lr=0.01, momentum=0.9) optimizer = o..
-
torch.nn.ModulePyTorch 2020. 5. 22. 21:54
파이토치에서 네트워크를 구현할 때 항상 torch.nn.Module을 상속받아 시작하는데, 정확히 어떻게 정의된 클래스인지 잘 모르는 상태로 코딩을 해왔다. 그래서 공식 문서에는 어떻게 적혀있나 살펴봤다. https://pytorch.org/docs/stable/nn.html#containers torch.nn — PyTorch 1.5.0 documentation Shortcuts pytorch.org (torch.nn 페이지 중에 'Containers' 라는 카테고리에 들어있는데, Containers 라는건 또 뭘까?...) 일단 Module 클래스 자체의 설명은 이렇게 되어있다. CLASStorch.nn.Module Base class for all neural network modules. Your..
-
super()Python 2020. 5. 22. 20:35
미리 정의된 클래스를 상속받아 새로운 클래스를 만들때, super()를 자주 보게된다. class Parent(object) { def __init(self)__: print('I am parent') } class Child(Parent) { def __init(self)__: super(Parent, self) print('I am child') } 보통 이런식으로 사용되는것 같은데, 왜 심플한 파이썬에서 이렇게 명시적으로 부모의 생성자를 불러줘야 하는걸까? 정확히 super()는 어떤 경우에 사용하는 걸까? 파이썬 공식 문서를 조금 살펴보기로 했다. *참조: https://docs.python.org/3/library/functions.html#super Built-in Functions — Pyt..