This repository was archived by the owner on Sep 7, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathfft_tree_constrained_inference_test.py
186 lines (149 loc) · 7.4 KB
/
fft_tree_constrained_inference_test.py
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
# Copyright 2016 The TensorFlow Authors All Rights Reserved.
#
# 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.
# ==============================================================================
"""Tests for fft_tree_constrained_inference."""
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import numpy as np
import tensorflow as tf
from tensorflow.python.framework import test_util
import data
import fft_tree_constrained_inference as ffttci
import tf_lib
import vocabulary
class FFTTreeConstrainedTest(test_util.TensorFlowTestCase):
def test_tree_constrained_inference(self):
self.shared_tree_constrained_inference(get_testing_hps())
def test_tree_constrained_inference_single_sentence_concat(self):
self.shared_tree_constrained_inference(
get_single_sentence_concat_testing_hps())
def shared_tree_constrained_inference(self, testing_hps):
np.set_printoptions(precision=4, suppress=True)
testing_sent_tokens_1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
testing_sent_tokens_1 = [str(t) for t in testing_sent_tokens_1]
testing_sent_edu_ids_1 = [0, 0, 1, 1, 2, 2, 3, 3, 4, 4]
testing_sent_parent_ids_1 = [1, 1, 2, 2, -1, -1, 2, 2, 3, 3]
testing_sent_tokens_2 = [1, 2, 3, 4, 5, 6, 7, 8, 9]
testing_sent_tokens_2 = [str(t) for t in testing_sent_tokens_2]
testing_sent_edu_ids_2 = [5, 5, 5, 6, 7, 7, 7, 8, 8]
testing_sent_parent_ids_2 = [6, 6, 6, -1, 6, 6, 6, 6, 6]
dummy_abstract_sentence_1 = ["0", "0", "0", "0", "0", "0", "0", "0", "0",
"0"]
dummy_abstract_sentence_2 = ["0", "0", "0"]
extract_labels_1 = [0 for _ in testing_sent_tokens_1]
extract_labels_2 = [0 for _ in testing_sent_tokens_2]
vocab_word_indices = range(11)
dummy_vocab_indices = dict([(str(i), i) for i in vocab_word_indices])
stem_indices = dummy_vocab_indices
word_stems = dict([(str(w), str(w)) for w in vocab_word_indices])
is_stop = [False for _ in vocab_word_indices]
vocab = vocabulary.Vocabulary(dummy_vocab_indices, stem_indices, word_stems,
is_stop, testing_hps.vocab_size)
if testing_hps.single_sentence_concat:
combined_toks = [["0"] + testing_sent_tokens_1 + testing_sent_tokens_2]
combined_labels = [[1] + extract_labels_1 + extract_labels_2]
combined_edu_ids = [-1] + testing_sent_edu_ids_1 + testing_sent_edu_ids_2
combined_parent_ids = [
-2
] + testing_sent_parent_ids_1 + testing_sent_parent_ids_2
combined_edu_ids = [[i + 1 for i in combined_edu_ids]]
combined_parent_ids = [[i + 1 for i in combined_parent_ids]]
nyt_ex_1 = data.SummaryExample(0, combined_toks, combined_edu_ids,
combined_parent_ids, combined_labels,
dummy_abstract_sentence_1)
nyt_ex_2 = data.SummaryExample(1, combined_toks, combined_edu_ids,
combined_parent_ids, combined_labels,
dummy_abstract_sentence_2)
else:
nyt_ex_1 = data.SummaryExample(
0, [testing_sent_tokens_1, testing_sent_tokens_2],
[testing_sent_edu_ids_1, testing_sent_edu_ids_2],
[testing_sent_parent_ids_1, testing_sent_parent_ids_2],
[extract_labels_1, extract_labels_2], dummy_abstract_sentence_1)
nyt_ex_2 = data.SummaryExample(
1, [testing_sent_tokens_1, testing_sent_tokens_2],
[testing_sent_edu_ids_1, testing_sent_edu_ids_2],
[testing_sent_parent_ids_1, testing_sent_parent_ids_2],
[extract_labels_1, extract_labels_2], dummy_abstract_sentence_2)
with self.test_session() as session:
tf.set_random_seed(12)
model_inp = ffttci.TreeInferenceInputs(testing_hps)
ex_batch = ffttci.TreeInferenceBatch(testing_hps, model_inp,
[nyt_ex_1, nyt_ex_2], vocab)
inferencer = ffttci.TreeConstrainedInferencer()
logit_shape = [testing_hps.batch_size, testing_hps.num_art_steps]
word_logits = tf.constant(
np.full(logit_shape, 0.0), dtype=tf.float32, shape=logit_shape)
margs, samples, logz = inferencer.do_tree_inference(
testing_hps, model_inp, word_logits)
margs = tf.reshape(margs, [testing_hps.batch_size, -1])
grad_logz = tf.gradients(logz, word_logits)[0]
margs_np, samples_np, logz_np, grad_logz_np = session.run(
[margs, samples, logz, grad_logz],
ex_batch.feeds)
emp_marg = np.average(samples_np, axis=1)
emp_marg = np.reshape(emp_marg, [testing_hps.batch_size, -1])
# sampled marginals should be pretty close to marginals calculated from BP
self.assertNDArrayNear(margs_np, emp_marg, 0.05)
# gradient of logz should be _very_ close to marginals calculated from BP
self.assertNDArrayNear(margs_np, grad_logz_np, 0.001)
# for k=3 example, logz should equal log(3)
self.assertNear(1.08961229, logz_np[1], 0.01)
def get_single_sentence_concat_testing_hps():
return tf_lib.HParams(
init_scale=0.08,
vocab_size=20,
hidden_size=200,
word_embedding_size=300,
batch_size=2,
min_art_steps=0, # min/max length for article
num_art_steps=20,
min_abs_steps=0, # min/max length for abstract
num_abs_steps=1,
num_samples=2000, # number of sampled extractions
single_sentence_concat=True, # make one tree over all sentences
max_num_spans=10, # number of token spans in constraints
max_num_sentences=1,
max_tree_nodes_any_level=32, # max width for BP tree
# lambda for message damping at each level of BP tree,
# improves numerical stability
fft_tree_msg_damp_lambdas=[0.0, 0.0, 0.0, 0.0],
tree_widths_at_level=[32, 32, 32, 32], # width for each BP tree level
fft_tree_widths_at_level=[32, 32, 32], # width for each sum tree level
global_fft_tree_width=32) # width of global sum tree
def get_testing_hps():
return tf_lib.HParams(
init_scale=0.08,
vocab_size=20,
hidden_size=200,
word_embedding_size=300,
batch_size=2,
min_art_steps=0, # min/max length for article
num_art_steps=19,
min_abs_steps=0, # min/max length for abstract
num_abs_steps=1,
num_samples=2000, # number of sampled extractions
single_sentence_concat=False, # make one tree over all sentences
max_num_spans=10, # number of token spans in constraints
max_num_sentences=2,
max_tree_nodes_any_level=11, # max width for BP tree
# lambda for message damping at each level of BP tree,
# improves numerical stability
fft_tree_msg_damp_lambdas=[0.0, 0.0, 0.0],
tree_widths_at_level=[16, 16, 16], # width for each BP tree level
fft_tree_widths_at_level=[16, 16], # width for each sum tree level
global_fft_tree_width=32) # width of global sum tree
if __name__ == "__main__":
tf.test.main()