Skip to content

Commit ec2ee10

Browse files
committed
update for pytorch 1.0 with nograd
1 parent a13a6b8 commit ec2ee10

File tree

6 files changed

+76
-70
lines changed

6 files changed

+76
-70
lines changed

TCN/adding_problem/add_test.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -100,10 +100,11 @@ def train(epoch):
100100

101101
def evaluate():
102102
model.eval()
103-
output = model(X_test)
104-
test_loss = F.mse_loss(output, Y_test)
105-
print('\nTest set: Average loss: {:.6f}\n'.format(test_loss.item()))
106-
return test_loss.item()
103+
with torch.no_grad():
104+
output = model(X_test)
105+
test_loss = F.mse_loss(output, Y_test)
106+
print('\nTest set: Average loss: {:.6f}\n'.format(test_loss.item()))
107+
return test_loss.item()
107108

108109

109110
for ep in range(1, epochs+1):

TCN/copy_memory/copymem_test.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -85,14 +85,15 @@
8585

8686
def evaluate():
8787
model.eval()
88-
out = model(test_x.unsqueeze(1).contiguous())
89-
loss = criterion(out.view(-1, n_classes), test_y.view(-1))
90-
pred = out.view(-1, n_classes).data.max(1, keepdim=True)[1]
91-
correct = pred.eq(test_y.data.view_as(pred)).cpu().sum()
92-
counter = out.view(-1, n_classes).size(0)
93-
print('\nTest set: Average loss: {:.8f} | Accuracy: {:.4f}\n'.format(
94-
loss.item(), 100. * correct / counter))
95-
return loss.item()
88+
with torch.no_grad():
89+
out = model(test_x.unsqueeze(1).contiguous())
90+
loss = criterion(out.view(-1, n_classes), test_y.view(-1))
91+
pred = out.view(-1, n_classes).data.max(1, keepdim=True)[1]
92+
correct = pred.eq(test_y.data.view_as(pred)).cpu().sum()
93+
counter = out.view(-1, n_classes).size(0)
94+
print('\nTest set: Average loss: {:.8f} | Accuracy: {:.4f}\n'.format(
95+
loss.item(), 100. * correct / counter))
96+
return loss.item()
9697

9798

9899
def train(ep):

TCN/lambada_language/lambada_test.py

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -88,18 +88,19 @@ def evaluate(data_source):
8888
total_loss = 0
8989
processed_data_size = 0
9090
correct = 0
91-
for i in range(len(data_source)):
92-
data, targets = torch.LongTensor(data_source[i]).view(1, -1), torch.LongTensor([data_source[i][-1]]).view(1, -1)
93-
data, targets = Variable(data), Variable(targets)
94-
if args.cuda:
95-
data, targets = data.cuda(), targets.cuda()
96-
output = model(data)
97-
final_output = output[:, -1].contiguous().view(-1, n_words)
98-
final_target = targets[:, -1].contiguous().view(-1)
99-
loss = criterion(final_output, final_target)
100-
total_loss += loss.data
101-
processed_data_size += 1
102-
return total_loss.item() / processed_data_size
91+
with torch.no_grad():
92+
for i in range(len(data_source)):
93+
data, targets = torch.LongTensor(data_source[i]).view(1, -1), torch.LongTensor([data_source[i][-1]]).view(1, -1)
94+
data, targets = Variable(data), Variable(targets)
95+
if args.cuda:
96+
data, targets = data.cuda(), targets.cuda()
97+
output = model(data)
98+
final_output = output[:, -1].contiguous().view(-1, n_words)
99+
final_target = targets[:, -1].contiguous().view(-1)
100+
loss = criterion(final_output, final_target)
101+
total_loss += loss.data
102+
processed_data_size += 1
103+
return total_loss.item() / processed_data_size
103104

104105

105106
def train():

TCN/mnist_pixel/pmnist_test.py

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -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('\nTest 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('\nTest 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

119120
if __name__ == "__main__":

TCN/poly_music/music_test.py

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -68,19 +68,20 @@ def evaluate(X_data, name='Eval'):
6868
eval_idx_list = np.arange(len(X_data), dtype="int32")
6969
total_loss = 0.0
7070
count = 0
71-
for idx in eval_idx_list:
72-
data_line = X_data[idx]
73-
x, y = Variable(data_line[:-1]), Variable(data_line[1:])
74-
if args.cuda:
75-
x, y = x.cuda(), y.cuda()
76-
output = model(x.unsqueeze(0)).squeeze(0)
77-
loss = -torch.trace(torch.matmul(y, torch.log(output).float().t()) +
78-
torch.matmul((1-y), torch.log(1-output).float().t()))
79-
total_loss += loss.item()
80-
count += output.size(0)
81-
eval_loss = total_loss / count
82-
print(name + " loss: {:.5f}".format(eval_loss))
83-
return eval_loss
71+
with torch.no_grad():
72+
for idx in eval_idx_list:
73+
data_line = X_data[idx]
74+
x, y = Variable(data_line[:-1]), Variable(data_line[1:])
75+
if args.cuda:
76+
x, y = x.cuda(), y.cuda()
77+
output = model(x.unsqueeze(0)).squeeze(0)
78+
loss = -torch.trace(torch.matmul(y, torch.log(output).float().t()) +
79+
torch.matmul((1-y), torch.log(1-output).float().t()))
80+
total_loss += loss.item()
81+
count += output.size(0)
82+
eval_loss = total_loss / count
83+
print(name + " loss: {:.5f}".format(eval_loss))
84+
return eval_loss
8485

8586

8687
def train(ep):

TCN/word_cnn/word_cnn_test.py

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -92,23 +92,24 @@ def evaluate(data_source):
9292
model.eval()
9393
total_loss = 0
9494
processed_data_size = 0
95-
for i in range(0, data_source.size(1) - 1, args.validseqlen):
96-
if i + args.seq_len - args.validseqlen >= data_source.size(1) - 1:
97-
continue
98-
data, targets = get_batch(data_source, i, args, evaluation=True)
99-
output = model(data)
100-
101-
# Discard the effective history, just like in training
102-
eff_history = args.seq_len - args.validseqlen
103-
final_output = output[:, eff_history:].contiguous().view(-1, n_words)
104-
final_target = targets[:, eff_history:].contiguous().view(-1)
105-
106-
loss = criterion(final_output, final_target)
107-
108-
# Note that we don't add TAR loss here
109-
total_loss += (data.size(1) - eff_history) * loss.item()
110-
processed_data_size += data.size(1) - eff_history
111-
return total_loss / processed_data_size
95+
with torch.no_grad():
96+
for i in range(0, data_source.size(1) - 1, args.validseqlen):
97+
if i + args.seq_len - args.validseqlen >= data_source.size(1) - 1:
98+
continue
99+
data, targets = get_batch(data_source, i, args, evaluation=True)
100+
output = model(data)
101+
102+
# Discard the effective history, just like in training
103+
eff_history = args.seq_len - args.validseqlen
104+
final_output = output[:, eff_history:].contiguous().view(-1, n_words)
105+
final_target = targets[:, eff_history:].contiguous().view(-1)
106+
107+
loss = criterion(final_output, final_target)
108+
109+
# Note that we don't add TAR loss here
110+
total_loss += (data.size(1) - eff_history) * loss.item()
111+
processed_data_size += data.size(1) - eff_history
112+
return total_loss / processed_data_size
112113

113114

114115
def train():

0 commit comments

Comments
 (0)