def generative_predict(test_model, word_index, batch, embed_indices, class_indices, batch_size = 64, prem_len = 22, hypo_len = 12): prem, _, _ = load_data.prepare_split_vec_dataset(batch, word_index) padded_p = load_data.pad_sequences(prem, maxlen=prem_len, dim = -1) core_model, premise_func, noise_func = test_model premise = premise_func(padded_p) noise = noise_func(embed_indices, load_data.convert_to_one_hot(class_indices, 3)) core_model.reset_states() core_model.nodes['attention'].set_state(noise) word_input = np.zeros((batch_size, 1)) result = [] for i in range(hypo_len): data = {'premise' :premise, 'creative': noise, 'hypo_input': word_input, 'train_input': np.zeros((batch_size,1))} preds = core_model.predict_on_batch(data)['output'] result.append(preds) word_input = np.argmax(preds, axis=2) result = np.transpose(np.array(result)[:,:,-1,:], (1,0,2)) return result
def single_generate(premise, label, gen_test, beam_size, hypo_len, noise_size, noise_input = None): batch_size = gen_test[0].input_layers[0].input_shape[0] per_batch = batch_size / beam_size premises = [premise] * per_batch if noise_input is None: noise_input = np.random.normal(scale=0.11, size=(per_batch, 1, noise_size)) class_indices = np.ones(per_batch) * label class_indices = load_data.convert_to_one_hot(class_indices, 3) words, loss = generative_predict_beam(gen_test, premises, noise_input, class_indices, True, hypo_len) return words
def generation_predict_embed(model, word_index, batch, embed_indices, batch_size = 64, hs = True, class_indices = None): prem, hypo, y = load_data.prepare_split_vec_dataset(batch, word_index) X_p = load_data.pad_sequences(prem, maxlen=PREM_LEN, dim = -1) data = {'premise_input': X_p, 'embed_input': embed_indices[:,None]} if class_indices is not None: C = load_data.convert_to_one_hot(class_indices, 3) data['class_input'] = C if hs: data['train_input'] = np.zeros((batch_size, HYPO_LEN)) model_pred = model.predict_on_batch(data) return model_pred['output']
def print_hypos(premise, label, gen_test, beam_size, hypo_len, noise_size, wi): words = single_generate(premise, label, gen_test, beam_size, hypo_len, noise_size) batch_size = gen_test[0].input_layers[0].input_shape[0] per_batch = batch_size / beam_size premises = [premise] * per_batch noise_input = np.random.normal(scale=0.11, size=(per_batch, 1, noise_size)) class_indices = np.ones(per_batch) * label class_indices = load_data.convert_to_one_hot(class_indices, 3) words, loss = generative_predict_beam(gen_test, premises, noise_input, class_indices, True, hypo_len) print 'Premise:', wi.print_seq(premise) print 'Label:', load_data.LABEL_LIST[label] print print 'Hypotheses:' for h in words: print wi.print_seq(h)
def single_generate(premise, label, gen_test, beam_size, hypo_len, noise_size, noise_input=None): batch_size = gen_test[0].input_layers[0].input_shape[0] per_batch = batch_size / beam_size premises = [premise] * per_batch if noise_input is None: noise_input = np.random.normal(scale=0.11, size=(per_batch, 1, noise_size)) class_indices = np.ones(per_batch) * label class_indices = load_data.convert_to_one_hot(class_indices, 3) words, loss = generative_predict_beam(gen_test, premises, noise_input, class_indices, True, hypo_len) return words
def get_explanation(premise, label, gen_test, beam_size, hypo_len, noise_size, wi): words = single_generate(premise, label, gen_test, beam_size, hypo_len, noise_size) batch_size = gen_test[0].input_layers[0].input_shape[0] per_batch = batch_size / beam_size premises = [premise] * per_batch noise_input = np.random.normal(scale=0.11, size=(per_batch, 1, noise_size)) class_indices = np.ones(per_batch) * label class_indices = load_data.convert_to_one_hot(class_indices, 3) words, loss = generative_predict_beam(gen_test, premises, noise_input, class_indices, True, hypo_len) # sentence = wi.print_seq(premise) # print 'Label:', load_data.LABEL_LIST[label] # print explanation = '' for h in words: explanation = wi.print_seq(h) return explanation.encode('utf-8')