1 minute read


๋”ฅ๋Ÿฌ๋‹ ๋ชจ๋ธ์˜ ์ฝ”๋“œ๋ฅผ ์‚ดํŽด๋ณด๋‹ค ๋ณด๋ฉด Evaluation ๋ถ€๋ถ„์—์„œ ๊ผญ ์ด๋Ÿฐ ์ฝ”๋“œ๊ฐ€ ๋“ฑ์žฅํ•œ๋‹ค.

def evaluation(model, criterion, ...):
    model.eval()
    criterion.eval()
    ...

๋ฌด์Šจ ์˜๋ฏธ์ธ์ง€ ๊ถ๊ธˆํ•ด์„œ ์ฐพ์•„๋ณด๋‹ˆ, nn.Module์—์„œ train time๊ณผ eval time์—์„œ ์ˆ˜ํ–‰ํ•˜๋Š” ๋‹ค๋ฅธ ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์žˆ๋„๋ก switching ํ•˜๋Š” ํ•จ์ˆ˜๋ผ๊ณ  ํ•œ๋‹ค. stackoverflow

train time๊ณผ eval time์—์„œ ๋‹ค๋ฅด๊ฒŒ ๋™์ž‘ํ•ด์•ผ ํ•˜๋Š” ๋Œ€ํ‘œ์ ์ธ ์˜ˆ๋“ค์€

  • Dropout Layer
  • BatchNorm Layer

๋“ฑ๋“ฑ์ด ์žˆ๋‹ค๊ณ  ํ•œ๋‹ค.

.eval() ํ•จ์ˆ˜๋Š” evaluation ๊ณผ์ •์—์„œ ์‚ฌ์šฉํ•˜์ง€ ์•Š์•„์•ผ ํ•˜๋Š” layer๋“ค์„ ์•Œ์•„์„œ off ์‹œํ‚ค๋„๋ก ํ•˜๋Š” ํ•จ์ˆ˜์ธ ์…ˆ์ด๋‹ค.

evaluation/validation ๊ณผ์ •์—์„  ๋ณดํ†ต model.eval()๊ณผ torch.no_grad()๋ฅผ ํ•จ๊ป˜ ์‚ฌ์šฉํ•œ๋‹ค๊ณ  ํ•œ๋‹ค.

# evaluate model:
model.eval()

with torch.no_grad():
    ...
    out_data = model(data)
    ...

eval/val ์ž‘์—…์ด ๋๋‚œ ํ›„์—๋Š” ์žŠ์ง€๋ง๊ณ  train mode๋กœ ๋ชจ๋ธ์„ ๋ณ€๊ฒฝํ•ด์ค˜์•ผ ํ•œ๋‹ค. ์ด๊ฒƒ์€ .train() ํ•จ์ˆ˜๋ฅผ ์‹คํ–‰์‹œํ‚ค๋ฉด ๋œ๋‹ค.

# after eval/val, and in training step
model.train()


PyTorch ๊ณต์‹ ๋ฌธ์„œ์—์„œ .eval()์— ๋Œ€ํ•œ ์ž์„ธํ•œ ๋‚ด์šฉ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค. nn.Module.eval()

Categories:

Updated: