Pytorch

torch.nn과 torch.nn.functional

술임 2023. 9. 11. 16:46

https://github.com/yanghongji2007/cross_view_localization_L2LTR

 

GitHub - yanghongji2007/cross_view_localization_L2LTR

Contribute to yanghongji2007/cross_view_localization_L2LTR development by creating an account on GitHub.

github.com

Layer to layer transformer 코드 실습 중

 

https://github.com/jeonsworld/ViT-pytorch

 

GitHub - jeonsworld/ViT-pytorch: Pytorch reimplementation of the Vision Transformer (An Image is Worth 16x16 Words: Transformers

Pytorch reimplementation of the Vision Transformer (An Image is Worth 16x16 Words: Transformers for Image Recognition at Scale) - GitHub - jeonsworld/ViT-pytorch: Pytorch reimplementation of the Vi...

github.com

ViT-pytorch과 model 구축 과정에서 어떻게 달라졌는지를 확인해보다가

 

https://www.diffchecker.com/text-compare/

torch.nn과 torch.nn.functional의 차이에 대해서 찾아봤다.

 

족제비디아님(https://cvml.tistory.com/10)과 전자둥이님(https://cchhoo407.tistory.com/29)의 블로그를 참고했습니다.

 

torch.nn과 torch.nn.functional의 차이점

import torch
import torch.nn as nn

loss = nn.CrossEntropyLoss()
input = torch.randn(3,5,requires_grad=True)
target = torch.empty(3, dtype=torch.long).random_(5)
output = loss(input, target)
output.backward()
import torch
import torch.nn.functional as F

input = torch.randn(3,5,requires_grad=True)
target = torch.randint(5,(3,),dtype=torch.int64)
loss = F.cross_entropy(input,target)
loss.backward()

--> 클래스와 함수의 차이

--> 두 방식 모두 같은 결과를 제공해서 편한 것을 선택해서 개발

 

torch.nn.functional

- 바로 인스턴스화 시킬 필요 없이 입력값 받음

 

torch.nn

- 인스턴스 화가 필요함

- attribute를 활용해 state를 저장해서 활용 가능

'Pytorch' 카테고리의 다른 글

딥러닝 파이프라인  (0) 2025.04.07
torch.cat과 torch.stack  (1) 2023.11.27
ResNet Image Feature Extraction  (0) 2023.09.04
파이토치 모델 정의  (0) 2022.04.30
파이토치 기초 문법  (0) 2022.04.28