-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathevaluate.py
More file actions
173 lines (148 loc) · 5.34 KB
/
evaluate.py
File metadata and controls
173 lines (148 loc) · 5.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# Copyright 2018 Dua, Logan and Matsubara
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ==============================================================================
"""Evaluation script."""
import argparse
import os
import logging
import shutil
import sys
import torch
import torch.nn.functional as F
import yaml
from math import exp, log
from multiprocessing import cpu_count
from tensorboardX import SummaryWriter
from torch.utils.data import DataLoader
from data import TextDataset
from model import RNNTextInferenceNetwork, RNNTextGenerativeModel
from utils import Vocab, configure_logging, word_dropout
def suml2p(logp, target, pad_idx):
"""Computes the sum of log2 probs."""
suml2p = 0.0
n = 0
for pred, idx in zip(logp, target):
if idx == pad_idx:
continue
else:
n += 1
# Convert base of log
lp = pred[idx]
l2p = lp / log(2)
suml2p += l2p
return suml2p, n
def main(_):
# Set up logging
configure_logging(FLAGS.debug_log)
# Load configuration
with open(FLAGS.config, 'r') as f:
config = yaml.load(f)
# Get the checkpoint path
ckpt_dir = os.path.join(config['training']['ckpt_dir'],
config['experiment_name'])
# Load vocab and datasets
logging.info('Loading the vocabulary.')
with open(config['data']['vocab'], 'r') as f:
vocab = Vocab.load(f)
logging.info('Loading test data.')
test_data = TextDataset(config['data']['test'],
vocab=vocab,
max_length=config['training']['max_length'])
test_loader = DataLoader(
dataset=test_data,
batch_size=config['training']['batch_size'],
shuffle=False,
num_workers=cpu_count(),
pin_memory=torch.cuda.is_available())
# Initialize models
logging.info('Initializing the inference network and generative model.')
inference_network = RNNTextInferenceNetwork(
dim=config['model']['dim'],
vocab_size=len(vocab),
encoder_kwargs=config['model']['encoder'],
normalizing_flow_kwargs=config['model']['normalizing_flow'])
generative_model = RNNTextGenerativeModel(
dim=config['model']['dim'],
vocab_size=len(vocab),
max_length=config['training']['max_length'],
sos_idx=vocab.sos_idx,
**config['model']['generator'])
if torch.cuda.is_available():
inference_network = inference_network.cuda()
generative_model = generative_model.cuda()
# Restore
ckpt = os.path.join(ckpt_dir, 'model.pt.best')
if os.path.exists(ckpt):
logging.info('Model checkpoint detected at: `%s`. Restoring.' % ckpt)
checkpoint = torch.load(ckpt)
inference_network.load_state_dict(checkpoint['state_dict_in'])
generative_model.load_state_dict(checkpoint['state_dict_gm'])
else:
logging.error('No model checkpoint found. Terminating.')
sys.exit(1)
# Init test summaries
test_nll = 0.0
test_kl = 0.0
test_loss = 0.0
test_suml2p = 0.0
test_n = 0.0
# Evaluate
inference_network.eval()
generative_model.eval()
for batch in test_loader:
x = batch['input']
target = batch['target']
lengths = batch['lengths']
if torch.cuda.is_available():
x = x.cuda()
target = target.cuda()
lengths = lengths.cuda()
# Forward pass of inference network
z, kl = inference_network(x, lengths)
# Teacher forcing
logp, _ = generative_model(z, x, lengths)
# Compute loss
length = logp.shape[1]
logp = logp.view(-1, len(vocab))
target = target[:,:length].contiguous().view(-1)
nll = F.nll_loss(logp, target, ignore_index=vocab.pad_idx,
size_average=False)
loss = nll + kl
l2p, n = suml2p(logp, target, vocab.pad_idx)
# Update summaries
test_nll += nll.data
test_kl += kl.data
test_loss += loss.data
test_suml2p += l2p.data
test_n += n
# Normalize losses
test_nll /= len(test_data)
test_kl /= len(test_data)
test_loss /= len(test_data)
H = -test_suml2p / test_n
test_perplexity = 2**H
# Log output
logging.info('NLL: %0.4f' % test_nll)
logging.info('KL: %0.4f' % test_kl)
logging.info('ELBO: %0.4f' % test_loss)
logging.info('Perplexity: %0.4f' % test_perplexity)
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('--config', type=str, required=True,
help='Path to configuration file.')
parser.add_argument('--debug_log', type=str, default=None,
help='If given, write DEBUG level logging events to '
'this file.')
FLAGS, _ = parser.parse_known_args()
main(_)