text
stringlengths
1
93.6k
for k in top_k_path_idx:
path_id = min([i for i in range(beam_size) if edges_scale[i] > k])
step.append(path[l][path_id])
tmp_path.append(step)
path = tmp_path
step = []
expand_edges_scale = [0] + edges_scale
for k in top_k_path_idx:
path_id = min([i for i in range(beam_size) if edges_scale[i] > k])
step_id = k - expand_edges_scale[path_id]
step.append(top_k_idxes[path_id][step_id])
path.append(step)
for i, k in enumerate(top_k_path_idx):
probs[i] = tmp_probs[k]
end_path[i] = False
for l in range(beam_len+1):
if path[l][i] == data_utils.EOS_ID:
end_path[i] = True
path += [[data_utils.PAD_ID for _ in range(beam_size)] for _ in range(_buckets[-1][1]-beam_len-1)]
return path, probs
def MMI(decoder_inputs):
lm_probs = model.lm_prob(sess, decoder_inputs)
lm_probs = lm_probs[0]
lm_prob = []
lens = []
for p in range(len(decoder_inputs[0])):
tmp_prob = 0.0
for l in range(len(decoder_inputs)-1):
if l < 3:
tmp_prob += np.log(lm_probs[l][p][decoder_inputs[l+1][p]])
if decoder_inputs[l+1][p] == data_utils.EOS_ID:
lens.append(l)
break
lm_prob.append(tmp_prob)
if len(lens) < len(lm_prob):
lens.append(len(decoder_inputs)-1)
return lm_prob, lens
if FLAGS.test_type == 'accuracy':
grammar_critic = load_critic(FLAGS.test_critic)
argmax_correct, argmax_count = 0, 0
sample_correct, sample_count = 0, 0
data_dir, file_name = FLAGS.data_path.rsplit('/',1)
test_data = os.path.join(data_dir, 'test_' + file_name)
with open(test_data,'r') as f, open('accuracy_hist_samples.txt','w') as fout:
all_f = f.readlines()#for data only involves inputs
tmp_encoder_inputs = []
tmp_encoder_lens = []
for _, sentence in enumerate(all_f):
# step
token_ids = data_utils.sentence_to_token_ids(tf.compat.as_bytes(sentence), vocab, normalize_digits=False)
encoder_pad = [data_utils.PAD_ID] * (_buckets[-1][0] - len(token_ids))
tmp_encoder_lens.append(len(token_ids))
tmp_encoder_inputs.append(list(reversed(token_ids)) + encoder_pad)
batch_encoder_inputs = []
batch_encoder_lens = []
batch_sentences = []
batch_flag = 0
for _ in range(int(len(tmp_encoder_inputs) / FLAGS.batch_size)):
encoder_inputs = []
for idx in range(_buckets[-1][0]):
encoder_inputs.append(
np.array([tmp_encoder_inputs[batch_flag+batch_idx][idx]
for batch_idx in range(FLAGS.batch_size)],
dtype = np.int32))
batch_encoder_inputs.append(encoder_inputs)
batch_encoder_lens.append(tmp_encoder_lens[batch_flag:batch_flag+FLAGS.batch_size])
batch_sentences.append(all_f[batch_flag:batch_flag+FLAGS.batch_size])
batch_flag += FLAGS.batch_size
decoder_inputs = [[data_utils.GO_ID for _ in range(FLAGS.batch_size)]]
print('batch number:{}'.format(len(batch_encoder_inputs)))
for batch_idx, (encoder_inputs, encoder_lens) in \
enumerate(zip(batch_encoder_inputs, batch_encoder_lens)):
outputs, _ = model.dynamic_decode(sess, encoder_inputs, \
encoder_lens, \
decoder_inputs)
for idx, sentence in enumerate(all_f[batch_idx*FLAGS.batch_size:(batch_idx+1)*FLAGS.batch_size]):
an_output = [output_ids[idx] for output_ids in outputs]
if data_utils.EOS_ID in an_output:
an_output = an_output[:an_output.index(data_utils.EOS_ID)]
ans = " ".join([tf.compat.as_str(rev_vocab[output]) for output in an_output])
if grammar_critic.check_ans(sentence.split(), ans):
argmax_correct += 1
argmax_count += 1
hist_samples = {}
coverage = 0.0
for _ in range(100):
for batch_idx, (encoder_inputs, encoder_lens) in \
enumerate(zip(batch_encoder_inputs, batch_encoder_lens)):
samples, log_prob = model.dynamic_decode(sess, encoder_inputs, \
encoder_lens, \
decoder_inputs, mode='sample')
for idx, sentence in enumerate(batch_sentences[batch_idx]):