model.train() : 使得droupout 層起作用, 前相傳播的時候會更新means 和var,而且會自動計算梯度
model.eval(): 會使得droupout 和前向傳播的時候, droupout 不起作用, means 和var這樣的統計數據值不會更新,但是依然會計算梯度。
with torch.no_grad() :則代表着不會計算梯度了,節省計算量。
因此在測試階段,應該model.eval()和 with torch.no_grad() 同時使用。
在每個測試的mini_batch中使用with torch.no_grad():
with torch.no_grad():
for data in dataloader:
imgs, labels = data
imgs = imgs.cuda()
temp_features = model(imgs)[0]