@@ -51,19 +51,19 @@ def __init__(
51
51
52
52
def encode (self , input_str ):
53
53
"""Input str to features dict, ready for inference."""
54
- inputs = self .encoders [' inputs' ].encode (input_str ) + [EOS_ID ]
54
+ inputs = self .encoders [" inputs" ].encode (input_str ) + [EOS_ID ]
55
55
batch_inputs = np .reshape (inputs , [1 , - 1 , 1 , 1 ]) # Make it 3D.
56
56
return batch_inputs
57
57
58
58
def decode (self , integers ):
59
59
"""List of ints to str."""
60
60
integers = list (np .squeeze (integers ))
61
- return self .encoders [' inputs' ].decode (integers )
61
+ return self .encoders [" inputs" ].decode (integers )
62
62
63
63
def decode_list (self , integers ):
64
64
"""List of ints to list of str."""
65
65
integers = list (np .squeeze (integers ))
66
- return self .encoders [' inputs' ].decode_list (integers )
66
+ return self .encoders [" inputs" ].decode_list (integers )
67
67
68
68
def get_vis_data_from_string (self , sess , input_string ):
69
69
"""Constructs the data needed for visualizing attentions.
@@ -135,11 +135,11 @@ def build_model(hparams_set, model_name, data_dir, problem_name, beam_size=1):
135
135
translate_model = registry .model (model_name )(
136
136
hparams , tf .estimator .ModeKeys .EVAL )
137
137
138
- inputs = tf .placeholder (tf .int32 , shape = (1 , None , 1 , 1 ), name = ' inputs' )
139
- targets = tf .placeholder (tf .int32 , shape = (1 , None , 1 , 1 ), name = ' targets' )
138
+ inputs = tf .placeholder (tf .int32 , shape = (1 , None , 1 , 1 ), name = " inputs" )
139
+ targets = tf .placeholder (tf .int32 , shape = (1 , None , 1 , 1 ), name = " targets" )
140
140
translate_model ({
141
- ' inputs' : inputs ,
142
- ' targets' : targets ,
141
+ " inputs" : inputs ,
142
+ " targets" : targets ,
143
143
})
144
144
145
145
# Must be called after building the training graph, so that the dict will
@@ -150,8 +150,8 @@ def build_model(hparams_set, model_name, data_dir, problem_name, beam_size=1):
150
150
151
151
with tf .variable_scope (tf .get_variable_scope (), reuse = True ):
152
152
samples = translate_model .infer ({
153
- ' inputs' : inputs ,
154
- }, beam_size = beam_size )[' outputs' ]
153
+ " inputs" : inputs ,
154
+ }, beam_size = beam_size )[" outputs" ]
155
155
156
156
return inputs , targets , samples , att_mats
157
157
@@ -182,19 +182,22 @@ def get_att_mats(translate_model):
182
182
dec_atts = []
183
183
encdec_atts = []
184
184
185
- prefix = ' transformer/body/'
186
- postfix_self_attention = ' /multihead_attention/dot_product_attention'
185
+ prefix = " transformer/body/"
186
+ postfix_self_attention = " /multihead_attention/dot_product_attention"
187
187
if translate_model .hparams .self_attention_type == "dot_product_relative" :
188
- postfix_self_attention = '/multihead_attention/dot_product_attention_relative'
189
- postfix_encdec = '/multihead_attention/dot_product_attention'
188
+ postfix_self_attention = ("/multihead_attention/"
189
+ "dot_product_attention_relative" )
190
+ postfix_encdec = "/multihead_attention/dot_product_attention"
190
191
191
192
for i in range (translate_model .hparams .num_hidden_layers ):
192
193
enc_att = translate_model .attention_weights [
193
- '%sencoder/layer_%i/self_attention%s' % (prefix , i , postfix_self_attention )]
194
+ "%sencoder/layer_%i/self_attention%s"
195
+ % (prefix , i , postfix_self_attention )]
194
196
dec_att = translate_model .attention_weights [
195
- '%sdecoder/layer_%i/self_attention%s' % (prefix , i , postfix_self_attention )]
197
+ "%sdecoder/layer_%i/self_attention%s"
198
+ % (prefix , i , postfix_self_attention )]
196
199
encdec_att = translate_model .attention_weights [
197
- ' %sdecoder/layer_%i/encdec_attention%s' % (prefix , i , postfix_encdec )]
200
+ " %sdecoder/layer_%i/encdec_attention%s" % (prefix , i , postfix_encdec )]
198
201
enc_atts .append (enc_att )
199
202
dec_atts .append (dec_att )
200
203
encdec_atts .append (encdec_att )
0 commit comments