for epoch in range(epochs):
……
# Clear gradient buffers because we don't want any gradient from previous epoch
optimizer.zero_grad()
# get output from the model, given the inputs
outputs = model(inputs)
# get loss for the predicted output
loss = criterion(outputs, labels)
# get gradients w.r.t to parameters
loss.backward()
# update parameters
optimizer.step()
………
- optimizer.zero_grad() - gradient 초기화 -> 다음 epoch 영향 안 미치기 위해
- model(input) -> 모델 돌림
- loss = criterion(outpus, labels) -> 로스 계산
- loss.bacward() -> gradients parameter 얻음
- optimizer.step() -> 파라미터 업데이트