def test_dtrnn_train(): from Model_Trainer import DT_RNN_Train as dttrain from Helpers import utils initializations = [ 'glorot_normal', 'glorot_uniform', 'he_uniform', 'he_normal' ] for optimization in ['adadelta', 'adam']: for initialization in initializations: sent1 = "this is my house" sent2 = "this is my home" score = 5 inputs1 = utils.get_dtree(sent1).get_rnn_input() inputs2 = utils.get_dtree(sent2).get_rnn_input() model = dttrain(n=1, epochs=2, hid_dim=200, optimization=optimization, initialization=initialization) model.train(inputs1[0], inputs1[1], inputs1[2], inputs1[3], inputs2[0], inputs2[1], inputs2[2], inputs2[3], score) return
def test_dt_rnn(): import numpy as np from Models import DT_RNN from Models import np_dt_rnn from Helpers import utils model = DT_RNN(dim=3, word_vector_size=3) np_W_dep = model.W_dep.get_value() np_W_x = model.W_x.get_value() np_b = model.b.get_value() sentence = "welcome to my house" dtree = utils.get_dtree(sentence, dim=3) vectors, parent_indices, is_leaf, dep_tags = dtree.get_rnn_input() np_ans = np_dt_rnn(vectors, parent_indices, is_leaf, dep_tags, np_W_x, np_W_dep, np_b) theano_ans = model.get_hidden_states(vectors, parent_indices, is_leaf, dep_tags) print(np_ans) print(theano_ans) assert (np.allclose(np_ans, theano_ans, rtol=1e-04, atol=1e-07)) return
def _extract_answer_from_sentence(sentence, question_tree, nlp, config, verbose=False): # check_configurations() # config = get_config('dtrnn.cfg') if verbose: print('SpaCy: Generating Dependency Tree for ["{0}"]'.format(sentence)) sentence_tree = utils.get_dtree(sentence, nlp, dim=config['word_vector_size']) sentence_text_traversal = sentence_tree.get_tree_traversal('text') sentence_hidden_states = get_tree_hidden_states( sentence_tree, config, verbose, ) sentence_tree.update_hidden_states(sentence_hidden_states) answers = get_answer_nodes(sentence_tree, question_tree, verbose) answers = [(sentence_text_traversal[i], score) for i, score in answers] return answers
def process_context(self, sentences): vectors_list = [] parent_indices_list = [] is_leaf_list = [] dep_tags_list = [] dtree_nodes = [] for sentence in sentences: dtree_nodes.append(utils.get_dtree(sentence)) max_len = max([node.count_nodes() for node in dtree_nodes]) for node in dtree_nodes: vectors, parent_indices, is_leaf, dep_tags = node.get_rnn_input() pad_width = max_len - node.count_nodes() vectors_list.append( utils.pad_matrix_with_zeros(vectors, pad_width=pad_width)) parent_indices_list.append( utils.pad_vector_with_zeros(parent_indices, pad_width=pad_width)) is_leaf_list.append( utils.pad_vector_with_zeros(is_leaf, pad_width=pad_width)) dep_tags_list.append( utils.pad_vector_with_zeros(dep_tags, pad_width=pad_width)) self.vectors_list = vectors_list self.parent_indices_list = parent_indices_list self.is_leaf_list = is_leaf_list self.dep_tags_list = dep_tags_list return
def _extract_answer_from_sentence(sentence, question, nlp, config): # check_configurations() # config = get_config('dtrnn.cfg') sentence_tree = utils.get_dtree(sentence, nlp, dim=config['word_vector_size']) question_tree = utils.get_dtree(question, nlp, dim=config['word_vector_size']) sentence_text_traversal = sentence_tree.get_tree_taversal('text') temp = get_tree_hidden_states(sentence_tree, question_tree, config) sentence_hidden_states = temp[0] question_hidden_states = temp[1] sentence_tree.update_hidden_states(sentence_hidden_states) question_tree.update_hidden_states(question_hidden_states) answers = get_answer_nodes(sentence_tree, question_tree) answers = [(sentence_text_traversal[i], score) for i, score in answers] return answers
def _extract_answer_from_sentence_vis(sentence, question_tree, nlp, config, hidden_states): output = {} output['sentence'] = sentence sentence_tree = utils.get_dtree(sentence, nlp, dim=config['word_vector_size']) sentence_text_traversal = sentence_tree.get_tree_traversal('text') sentence_hidden_states = get_tree_hidden_states( sentence_tree, config, verbose=False, ) hidden_states.extend(sentence_hidden_states) sentence_tree.update_hidden_states(sentence_hidden_states) answers = get_answer_nodes(sentence_tree, question_tree, verbose=False) answers = [(i, sentence_text_traversal[i], score) for i, score in answers] sentence_tree.update_node_scores(answers) output['tree'] = sentence_tree return answers, output
def extract_answer_from_sentences(sentences, question, verbose=False, vis=False): check_configurations() config = get_config('dtrnn.cfg') if verbose: print('Spacy: Initializing...') import spacy nlp = spacy.load('en') if verbose: print('SpaCy: Generating Dependency Tree for ["{0}"]'.format(question)) question_tree = utils.get_dtree(question, nlp, dim=config['word_vector_size']) question_hidden_states = get_tree_hidden_states( question_tree, config, ) question_tree.update_hidden_states(question_hidden_states) tree_list = [ { 'sentence': question, 'tree': question_tree, }, ] if vis: hidden_states = [] hidden_states.extend(question_hidden_states) ans_sent_list = [] final_list = [] for sent_score_tuple in sentences: sentence, score = sent_score_tuple if vis: node_scores, tree_dict = _extract_answer_from_sentence_vis( sentence, question_tree, nlp, config, hidden_states, ) tree_list.append(tree_dict) else: node_scores = _extract_answer_from_sentence( sentence, question_tree, nlp, config, verbose, ) if verbose: print('') for ns in node_scores: if vis: _, node, n_score = ns else: node, n_score = ns f_score = score * n_score final_list.append((node, f_score)) ans_node, score = max(final_list, key=operator.itemgetter(1)) final_list = sorted( final_list, key=operator.itemgetter(1), reverse=True) # Uncomment this if want list of all nodes with scores if vis: return ans_node, score, final_list, tree_list, hidden_states else: return ans_node, score, final_list