pytorch 코드 개인 소감

2023 단어 기계 학습
#1
nn.crossentropyloss () 클래스는 두 단계 함수를 포함합니다:logsoftmax와 nllloss, (log-likelihood loss), 후자는log 절차가 없습니다.
만약loss가 교차엔트로피만 원한다면softmax 절차를 제외하고 네트워크 마지막에nn을 추가할 수 있습니다.softmax층 및 torch.log () 함수를 출력하고 훈련된loss는 nn을 사용합니다.NLLLoss() 클래스.
#2
torch.log () 등 연산은 매개 변수를 제공할 필요가 없기 때문에 직접 호출하면 됩니다.
torch.nn.functional은 함수로 일반적인 입력 출력을 제외하고 계산에 사용되는 매개 변수를 입력해야 하기 때문에 직접 호출할 수 없습니다.정의는 다음과 같습니다.
def cross_entropy(input, target, weight=None, size_average=None, ignore_index=-100,
                  reduce=None, reduction='elementwise_mean'):
    if size_average is not None or reduce is not None:
        reduction = _Reduction.legacy_get_string(size_average, reduce)
    return nll_loss(log_softmax(input, 1), target, weight, None, ignore_index, None, reduction)

torch를 사용하기 위해서.nn.functional이 더 편리해서 클래스가 생겼어요.nn.Cross EntropyLoss, 여기에서 init는 필요한 매개 변수를 정의하고 forward에서 torch를 호출합니다.nn.functional, 이렇게 하면 파라미터의 저장 문제를 잘 해결하고 신경 네트워크 훈련 과정을 더욱 편리하게 할 수 있다.
class CrossEntropyLoss(_WeightedLoss):
    def __init__(self, weight=None, size_average=None, ignore_index=-100,
                 reduce=None, reduction='elementwise_mean'):
        super(CrossEntropyLoss, self).__init__(weight, size_average, reduce, reduction)
        self.ignore_index = ignore_index

    def forward(self, input, target):
        return F.cross_entropy(input, target, weight=self.weight,
                               ignore_index=self.ignore_index, reduction=self.reduction)

#3
버전 3.0.1에서Variable는 하나의 전체로 처리하는 것이 가장 좋고 분리하지 않는 것이 가장 좋다. 그렇지 않으면 사다리의 계산에 영향을 미치기 쉽다.
예를 들어 하나의 수조 변수 s의 개별 분량을 곱하기 등 처리하려면 s와 같은 사이즈의 변수 mask를 단독으로 설정하여 1로 초기화하고 mask를 s가 처리할 대응하는 위치에 곱하기 배율로 설정한 다음에 s*mask를 곱하기 시작한다.이렇게 하면 행렬 도수를 직접 얻을 수 있다.
 
#4
pytorch의 autograd 사다리 방식은 중간량 (예: x) 의 사다리를 저장하지 않고 클래스가 초기화한 변수 (예:self.conv () 인자만 저장합니다.
훅 함수는 CNN의 피처 맵과 같은 중간의 임의의 중간량을 기록할 수 있다.Grad_CAM에서 사용하는 이 함수입니다.
Pytorch에서 autograd 및 hook 함수 설명 참조

좋은 웹페이지 즐겨찾기