MCPcopy Create free account
hub / github.com/chinawithfrank/ChatBotCourse / decode

Function decode

chatbotv4/translate.py:201–246  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

199
200
201def decode():
202 with tf.Session() as sess:
203 # Create model and load parameters.
204 model = create_model(sess, True)
205 model.batch_size = 1 # We decode one sentence at a time.
206
207 # Load vocabularies.
208 en_vocab_path = os.path.join(FLAGS.data_dir,
209 "vocab%d.input" % FLAGS.input_vocab_size)
210 fr_vocab_path = os.path.join(FLAGS.data_dir,
211 "vocab%d.output" % FLAGS.output_vocab_size)
212 en_vocab, _ = data_utils.initialize_vocabulary(en_vocab_path)
213 _, rev_fr_vocab = data_utils.initialize_vocabulary(fr_vocab_path)
214
215 # Decode from standard input.
216 sys.stdout.write("> ")
217 sys.stdout.flush()
218 sentence = sys.stdin.readline()
219 while sentence:
220 # Get token-ids for the input sentence.
221 token_ids = data_utils.sentence_to_token_ids(tf.compat.as_bytes(sentence), en_vocab)
222 # Which bucket does it belong to?
223 bucket_id = len(_buckets) - 1
224 for i, bucket in enumerate(_buckets):
225 if bucket[0] >= len(token_ids):
226 bucket_id = i
227 break
228 else:
229 logging.warning("Sentence truncated: %s", sentence)
230
231 # Get a 1-element batch to feed the sentence to the model.
232 encoder_inputs, decoder_inputs, target_weights = model.get_batch(
233 {bucket_id: [(token_ids, [])]}, bucket_id)
234 # Get output logits for the sentence.
235 _, _, output_logits = model.step(sess, encoder_inputs, decoder_inputs,
236 target_weights, bucket_id, True)
237 # This is a greedy decoder - outputs are just argmaxes of output_logits.
238 outputs = [int(np.argmax(logit, axis=1)) for logit in output_logits]
239 # If there is an EOS symbol in outputs, cut them at that point.
240 if data_utils.EOS_ID in outputs:
241 outputs = outputs[:outputs.index(data_utils.EOS_ID)]
242 # Print out French sentence corresponding to outputs.
243 print(" ".join([tf.compat.as_str(rev_fr_vocab[output]) for output in outputs]))
244 print("> ", end="")
245 sys.stdout.flush()
246 sentence = sys.stdin.readline()
247
248
249def self_test():

Callers 1

mainFunction · 0.85

Calls 3

get_batchMethod · 0.80
stepMethod · 0.80
create_modelFunction · 0.70

Tested by

no test coverage detected