@@ -97,23 +97,24 @@ def test():
9797 model .eval ()
9898 test_loss = 0
9999 correct = 0
100- for data , target in test_loader :
101- if args .cuda :
102- data , target = data .cuda (), target .cuda ()
103- data = data .view (- 1 , input_channels , seq_length )
104- if args .permute :
105- data = data [:, :, permute ]
106- data , target = Variable (data , volatile = True ), Variable (target )
107- output = model (data )
108- test_loss += F .nll_loss (output , target , size_average = False ).item ()
109- pred = output .data .max (1 , keepdim = True )[1 ]
110- correct += pred .eq (target .data .view_as (pred )).cpu ().sum ()
100+ with torch .no_grad ():
101+ for data , target in test_loader :
102+ if args .cuda :
103+ data , target = data .cuda (), target .cuda ()
104+ data = data .view (- 1 , input_channels , seq_length )
105+ if args .permute :
106+ data = data [:, :, permute ]
107+ data , target = Variable (data , volatile = True ), Variable (target )
108+ output = model (data )
109+ test_loss += F .nll_loss (output , target , size_average = False ).item ()
110+ pred = output .data .max (1 , keepdim = True )[1 ]
111+ correct += pred .eq (target .data .view_as (pred )).cpu ().sum ()
111112
112- test_loss /= len (test_loader .dataset )
113- print ('\n Test set: Average loss: {:.4f}, Accuracy: {}/{} ({:.0f}%)\n ' .format (
114- test_loss , correct , len (test_loader .dataset ),
115- 100. * correct / len (test_loader .dataset )))
116- return test_loss
113+ test_loss /= len (test_loader .dataset )
114+ print ('\n Test set: Average loss: {:.4f}, Accuracy: {}/{} ({:.0f}%)\n ' .format (
115+ test_loss , correct , len (test_loader .dataset ),
116+ 100. * correct / len (test_loader .dataset )))
117+ return test_loss
117118
118119
119120if __name__ == "__main__" :
0 commit comments