This repository has been archived by the owner on Sep 10, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
/
kernel_model.py
126 lines (108 loc) · 4.09 KB
/
kernel_model.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
# Copyright 2017 Google Inc.
#
# 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
#
# https://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.
"""Kernel model.
This model allows interactions between the two inputs, such as attention.
"""
import sys
import tensorflow as tf
import tensorflow.contrib.learn as learn
import tf_utils
from common_model import embedding_layer
def kernel_model(features, mode, params, scope=None):
"""Kernel models that allow interaction between question and context.
This is handler for all kernel models in this script. Models are called via
`params.model_id` (e.g. `params.model_id = m00`).
Function requirement for each model is in:
https://www.tensorflow.org/extend/estimators
This function does not have any dependency on FLAGS. All parameters must be
passed through `params` argument.
Args:
features: A dict of feature tensors.
mode: https://www.tensorflow.org/api_docs/python/tf/contrib/learn/ModeKeys
params: `params` passed during initialization of `Estimator` object.
scope: Variable name scope.
Returns:
`(logits_start, logits_end, tensors)` pair. Tensors is a dictionary of
tensors that can be useful outside of this function, e.g. visualization.
"""
this_module = sys.modules[__name__]
model_fn = getattr(this_module, '_model_%s' % params.model_id)
return model_fn(
features, mode, params, scope=scope)
def _model_m00(features, mode, params, scope=None):
"""Simplified BiDAF, reaching 74~75% F1.
Args:
features: A dict of feature tensors.
mode: https://www.tensorflow.org/api_docs/python/tf/contrib/learn/ModeKeys
params: `params` passed during initialization of `Estimator` object.
scope: Variable name scope.
Returns:
`(logits_start, logits_end, tensors)` pair. Tensors is a dictionary of
tensors that can be useful outside of this function, e.g. visualization.
"""
with tf.variable_scope(scope or 'kernel_model'):
training = mode == learn.ModeKeys.TRAIN
tensors = {}
x, q = embedding_layer(features, mode, params)
x0 = tf_utils.bi_rnn(
params.hidden_size,
x,
sequence_length_list=features['context_num_words'],
scope='x_bi_rnn_0',
training=training,
dropout_rate=params.dropout_rate)
q0 = tf_utils.bi_rnn(
params.hidden_size,
q,
sequence_length_list=features['question_num_words'],
scope='q_bi_rnn_0',
training=training,
dropout_rate=params.dropout_rate)
xq = tf_utils.att2d(
q0,
x0,
mask=features['question_num_words'],
tensors=tensors,
scope='xq')
xq = tf.concat([x0, xq, x0 * xq], 2)
x1 = tf_utils.bi_rnn(
params.hidden_size,
xq,
sequence_length_list=features['context_num_words'],
training=training,
scope='x1_bi_rnn',
dropout_rate=params.dropout_rate)
x2 = tf_utils.bi_rnn(
params.hidden_size,
x1,
sequence_length_list=features['context_num_words'],
training=training,
scope='x2_bi_rnn',
dropout_rate=params.dropout_rate)
x3 = tf_utils.bi_rnn(
params.hidden_size,
x2,
sequence_length_list=features['context_num_words'],
training=training,
scope='x3_bi_rnn',
dropout_rate=params.dropout_rate)
logits_start = tf_utils.exp_mask(
tf.squeeze(
tf.layers.dense(tf.concat([x1, x2], 2), 1, name='logits1'), 2),
features['context_num_words'])
logits_end = tf_utils.exp_mask(
tf.squeeze(
tf.layers.dense(tf.concat([x1, x3], 2), 1, name='logits2'), 2),
features['context_num_words'])
return logits_start, logits_end, tensors