def initialize_graph(self): if not self.model_settings["static_entity_embeddings"]: self.entity_embedding = VectorEmbedding(self.entity_indexer, self.variables, variable_prefix="entity") self.add_component(self.entity_embedding) else: self.entity_embedding = StaticVectorEmbedding( self.entity_indexer, self.variables, variable_prefix="entity") self.add_component(self.entity_embedding) self.word_embedding = SequenceEmbedding(self.word_indexer, self.variables, variable_prefix="word") self.add_component(self.word_embedding) self.lstms = [ BiLstm(self.variables, self.model_settings["word_embedding_dimension"], variable_prefix="lstm_" + str(i)) for i in range(self.model_settings["n_lstms"]) ] for lstm in self.lstms: self.add_component(lstm) self.target_comparator = TargetComparator( self.variables, variable_prefix="comparison_to_sentence") self.add_component(self.target_comparator) self.decoder = SoftmaxDecoder(self.variables) self.add_component(self.decoder) self.sentence_to_graph_mapper = EmbeddingRetriever( self.variables, duplicate_policy="sum", variable_prefix="mapper") self.add_component(self.sentence_to_graph_mapper) if self.model_settings["use_transformation"]: self.transformation = MultilayerPerceptron( [ self.model_settings["word_embedding_dimension"], self.model_settings["entity_embedding_dimension"] ], self.variables, variable_prefix="transformation", l2_scale=self.model_settings["regularization_scale"]) self.centroid_transformation = MultilayerPerceptron( [ self.model_settings["entity_embedding_dimension"], self.model_settings["word_embedding_dimension"] ], self.variables, variable_prefix="centroid_transformation", l2_scale=self.model_settings["regularization_scale"]) self.add_component(self.centroid_transformation) self.add_component(self.transformation)
def initialize_graph(self): if not self.model_settings["static_entity_embeddings"]: self.entity_embedding = VectorEmbedding(self.entity_indexer, self.variables, variable_prefix="entity") self.add_component(self.entity_embedding) else: self.entity_embedding = StaticVectorEmbedding( self.entity_indexer, self.variables, variable_prefix="entity") self.add_component(self.entity_embedding) self.hypergraph = TensorflowHypergraphRepresentation(self.variables) self.add_component(self.hypergraph) #self.question_sentence = TensorflowSentenceRepresentation(self.variables) #self.add_component(self.question_sentence) self.word_embedding = SequenceEmbedding(self.word_indexer, self.variables, variable_prefix="word") self.add_component(self.word_embedding) self.target_comparator = TargetComparator( self.variables, variable_prefix="comparison_to_sentence") self.add_component(self.target_comparator) self.decoder = SoftmaxDecoder(self.variables) self.add_component(self.decoder) self.sentence_to_graph_mapper = EmbeddingRetriever( self.variables, duplicate_policy="sum", variable_prefix="mapper") self.add_component(self.sentence_to_graph_mapper) if self.model_settings["use_transformation"]: self.transformation = MultilayerPerceptron( [ self.model_settings["word_embedding_dimension"], self.model_settings["entity_embedding_dimension"] ], self.variables, variable_prefix="transformation", l2_scale=self.model_settings["regularization_scale"]) self.centroid_transformation = MultilayerPerceptron( [ self.model_settings["entity_embedding_dimension"], self.model_settings["word_embedding_dimension"] ], self.variables, variable_prefix="centroid_transformation", l2_scale=self.model_settings["regularization_scale"]) self.add_component(self.centroid_transformation) self.add_component(self.transformation)
def initialize_graph(self): self.hypergraph = TensorflowHypergraphRepresentation(self.variables, edge_dropout_rate=self.model_settings["edge_dropout"]) self.add_component(self.hypergraph) self.lstms = [BiLstm(self.variables, self.model_settings["word_embedding_dimension"]+1, self.model_settings["lstm_hidden_state_dimension"], variable_prefix="lstm_" + str(i)) for i in range(self.model_settings["n_lstms"])] for lstm in self.lstms: self.add_component(lstm) self.word_embedding = SequenceEmbedding(self.word_indexer, self.variables, variable_prefix="word", word_dropout_rate=self.model_settings["word_dropout"], is_static=self.model_settings["static_word_embeddings"]) self.add_component(self.word_embedding) #self.attention = Attention(self.model_settings["word_embedding_dimension"], self.variables, variable_prefix="attention", strategy="constant_query") self.attention = MultiheadAttention(self.model_settings["lstm_hidden_state_dimension"], self.variables, attention_heads=4, variable_prefix="attention", strategy="constant_query", attention_dropout=self.model_settings["attention_dropout"]) self.add_component(self.attention) self.target_comparator = TargetComparator(self.variables, variable_prefix="comparison_to_sentence", comparison="concat") self.add_component(self.target_comparator) self.decoder = SoftmaxDecoder(self.variables, self.model_settings["loss"]) self.add_component(self.decoder) self.candidate_scorer = NeuralNetworkOrFactorizationScorer(self.model_settings, self.variables, variable_prefix="scorer") self.add_component(self.candidate_scorer) self.hypergraph_gcn_propagation_units = [None] * self.model_settings["n_layers"] for layer in range(self.model_settings["n_layers"]): self.hypergraph_gcn_propagation_units[layer] = HypergraphGcnPropagationUnit("layer_" + str(layer), self.facts, self.variables, self.model_settings["entity_embedding_dimension"], self.hypergraph, weights="identity", biases="relation_specific", self_weight="identity", self_bias="zero", add_inverse_relations=True) self.add_component(self.hypergraph_gcn_propagation_units[layer]) self.sentence_to_graph_mapper = EmbeddingRetriever(self.variables, duplicate_policy="sum", variable_prefix="mapper") self.add_component(self.sentence_to_graph_mapper) self.final_transformation = MultilayerPerceptron([int(self.model_settings["lstm_hidden_state_dimension"]/2) + self.model_settings["entity_embedding_dimension"], self.model_settings["nn_hidden_state_dimension"], 1], self.variables, variable_prefix="transformation", l2_scale=self.model_settings["regularization_scale"]) self.add_component(self.final_transformation)
class EntityEmbeddingVsBagOfWords(AbstractTensorflowModel): def get_preprocessor_stack_types(self): preprocessor_stack_types = ["hypergraph", "gold", "sentence"] if self.model_settings["static_entity_embeddings"]: preprocessor_stack_types += ["static_entity_embeddings"] return preprocessor_stack_types def initialize_graph(self): if not self.model_settings["static_entity_embeddings"]: self.entity_embedding = VectorEmbedding(self.entity_indexer, self.variables, variable_prefix="entity") self.add_component(self.entity_embedding) else: self.entity_embedding = StaticVectorEmbedding( self.entity_indexer, self.variables, variable_prefix="entity") self.add_component(self.entity_embedding) self.hypergraph = TensorflowHypergraphRepresentation(self.variables) self.add_component(self.hypergraph) #self.question_sentence = TensorflowSentenceRepresentation(self.variables) #self.add_component(self.question_sentence) self.word_embedding = SequenceEmbedding(self.word_indexer, self.variables, variable_prefix="word") self.add_component(self.word_embedding) self.target_comparator = TargetComparator( self.variables, variable_prefix="comparison_to_sentence") self.add_component(self.target_comparator) self.decoder = SoftmaxDecoder(self.variables) self.add_component(self.decoder) self.sentence_to_graph_mapper = EmbeddingRetriever( self.variables, duplicate_policy="sum", variable_prefix="mapper") self.add_component(self.sentence_to_graph_mapper) if self.model_settings["use_transformation"]: self.transformation = MultilayerPerceptron( [ self.model_settings["word_embedding_dimension"], self.model_settings["entity_embedding_dimension"] ], self.variables, variable_prefix="transformation", l2_scale=self.model_settings["regularization_scale"]) self.centroid_transformation = MultilayerPerceptron( [ self.model_settings["entity_embedding_dimension"], self.model_settings["word_embedding_dimension"] ], self.variables, variable_prefix="centroid_transformation", l2_scale=self.model_settings["regularization_scale"]) self.add_component(self.centroid_transformation) self.add_component(self.transformation) def set_indexers(self, indexers): self.word_indexer = indexers.word_indexer self.entity_indexer = indexers.entity_indexer def compute_entity_scores(self): self.hypergraph.entity_vertex_embeddings = self.entity_embedding.get_representations( ) word_embeddings = self.word_embedding.get_representations() word_embedding_shape = tf.shape(word_embeddings) word_embeddings = tf.reshape( word_embeddings, [-1, self.model_settings["word_embedding_dimension"]]) centroid_embeddings = self.sentence_to_graph_mapper.get_forward_embeddings( self.hypergraph.entity_vertex_embeddings) centroid_embeddings = self.centroid_transformation.transform( centroid_embeddings) word_embeddings += self.sentence_to_graph_mapper.map_backwards( centroid_embeddings) word_embeddings = tf.reshape(word_embeddings, word_embedding_shape) bag_of_words = tf.reduce_sum(word_embeddings, 1) if self.model_settings["use_transformation"]: bag_of_words = self.transformation.transform(bag_of_words) entity_embeddings = self.target_comparator.get_comparison_scores( bag_of_words, self.hypergraph.entity_vertex_embeddings) return entity_embeddings
class PathBagWithGatesVsLstm(AbstractTensorflowModel): def get_preprocessor_stack_types(self): preprocessor_stack_types = ["hypergraph", "gold", "sentence"] if self.model_settings["static_entity_embeddings"]: preprocessor_stack_types += ["static_entity_embeddings"] return preprocessor_stack_types def initialize_graph(self): self.hypergraph = TensorflowHypergraphRepresentation(self.variables, edge_dropout_rate=self.model_settings["edge_dropout"]) self.add_component(self.hypergraph) self.lstms = [BiLstm(self.variables, self.model_settings["word_embedding_dimension"]+1, self.model_settings["lstm_hidden_state_dimension"], variable_prefix="lstm_" + str(i)) for i in range(self.model_settings["n_lstms"])] for lstm in self.lstms: self.add_component(lstm) self.word_embedding = SequenceEmbedding(self.word_indexer, self.variables, variable_prefix="word", word_dropout_rate=self.model_settings["word_dropout"], is_static=self.model_settings["static_word_embeddings"]) self.add_component(self.word_embedding) #self.attention = Attention(self.model_settings["word_embedding_dimension"], self.variables, variable_prefix="attention", strategy="constant_query") self.attention = MultiheadAttention(self.model_settings["lstm_hidden_state_dimension"], self.variables, attention_heads=self.model_settings["n_attention_heads"], variable_prefix="attention", strategy="constant_query", attention_dropout=self.model_settings["attention_dropout"]) self.add_component(self.attention) self.target_comparator = TargetComparator(self.variables, variable_prefix="comparison_to_sentence", comparison="concat") self.add_component(self.target_comparator) self.decoder = SoftmaxDecoder(self.variables) self.add_component(self.decoder) self.candidate_scorer = NeuralNetworkOrFactorizationScorer(self.model_settings, self.variables, variable_prefix="scorer") self.add_component(self.candidate_scorer) if self.model_settings["gate_input"] == "GCN": gate_input_dim = self.model_settings["gate_input_dim"] self.gate_transform = MultilayerPerceptron([1, gate_input_dim], self.variables, variable_prefix="gate_transformation", l2_scale=self.model_settings["regularization_scale"], dropout_rate=self.model_settings["transform_dropout"]) self.add_component(self.gate_transform) self.gate_gcns = [None] * self.model_settings["gate_input_layers"] for layer in range(self.model_settings["gate_input_layers"]): self.gate_gcns[layer] = HypergraphGcnPropagationUnit("gate_layer_" + str(layer), self.facts, self.variables, gate_input_dim, self.hypergraph, weights="single", biases="constant", self_weight="full", self_bias="constant", add_inverse_relations=True,) self.add_component(self.gate_gcns[layer]) else: gate_input_dim = 1 self.hypergraph_gcn_propagation_units = [None] * self.model_settings["n_layers"] for layer in range(self.model_settings["n_layers"]): self.hypergraph_gcn_propagation_units[layer] = HypergraphGcnPropagationUnit("layer_" + str(layer), self.facts, self.variables, self.model_settings["entity_embedding_dimension"], self.hypergraph, weights="identity", biases="relation_specific", self_weight="identity", self_bias="zero", add_inverse_relations=True, gate_mode="features_given", gate_input_dim=gate_input_dim) self.add_component(self.hypergraph_gcn_propagation_units[layer]) self.sentence_to_graph_mapper = EmbeddingRetriever(self.variables, duplicate_policy="sum", variable_prefix="mapper") self.add_component(self.sentence_to_graph_mapper) if False: #self.model_settings["use_transformation"]: self.transformation = MultilayerPerceptron([self.model_settings["word_embedding_dimension"], self.model_settings["entity_embedding_dimension"]], self.variables, variable_prefix="transformation", l2_scale=self.model_settings["regularization_scale"]) self.centroid_transformation = MultilayerPerceptron([self.model_settings["entity_embedding_dimension"], self.model_settings["word_embedding_dimension"]], self.variables, variable_prefix="centroid_transformation", l2_scale=self.model_settings["regularization_scale"]) self.add_component(self.centroid_transformation) self.add_component(self.transformation) self.final_transformation = MultilayerPerceptron([int(self.model_settings["lstm_hidden_state_dimension"]/2) + self.model_settings["entity_embedding_dimension"], self.model_settings["nn_hidden_state_dimension"], 1], self.variables, variable_prefix="transformation", l2_scale=self.model_settings["regularization_scale"]) self.add_component(self.final_transformation) def set_indexers(self, indexers): self.word_indexer = indexers.word_indexer self.relation_indexer = indexers.relation_indexer self.entity_indexer = indexers.entity_indexer def compute_entity_scores(self, mode="train"): #entity_vertex_embeddings = self.entity_embedding.get_representations() #word_embedding_shape = tf.shape(word_embeddings) #word_embeddings = tf.reshape(word_embeddings, [-1, self.model_settings["word_embedding_dimension"]]) #centroid_embeddings = self.sentence_to_graph_mapper.get_forward_embeddings(entity_vertex_embeddings) #centroid_embeddings = self.centroid_transformation.transform(centroid_embeddings) #word_embeddings += self.sentence_to_graph_mapper.map_backwards(centroid_embeddings) #word_embeddings = tf.reshape(word_embeddings, word_embedding_shape) if self.model_settings["gate_input"] == "GCN": v_features = self.gate_transform.transform(tf.expand_dims(self.hypergraph.get_vertex_scores(),1), mode=mode) e_features = self.gate_transform.transform(tf.expand_dims(self.hypergraph.get_event_scores(),1), mode=mode) self.hypergraph.update_entity_embeddings(v_features, self.model_settings["gate_input_dim"]) self.hypergraph.update_event_embeddings(e_features, self.model_settings["gate_input_dim"]) for gate_layer in self.gate_gcns: gate_layer.propagate() v_features = self.hypergraph.entity_vertex_embeddings e_features = self.hypergraph.event_vertex_embeddings else: v_features = tf.expand_dims(self.hypergraph.get_vertex_scores(),1) e_features = tf.expand_dims(self.hypergraph.get_event_scores(),1) self.hypergraph.initialize_zero_embeddings(self.model_settings["entity_embedding_dimension"]) for hgpu in self.hypergraph_gcn_propagation_units: hgpu.set_gate_features(v_features, "entities") hgpu.set_gate_features(e_features, "events") hgpu.propagate() entity_scores = self.hypergraph.entity_vertex_embeddings entity_scores = tf.concat([entity_scores, tf.expand_dims(self.hypergraph.get_vertex_scores(),1)], axis=1) word_embeddings = self.word_embedding.get_representations(mode=mode) ### word_embedding_shape = tf.shape(word_embeddings) word_embeddings = tf.reshape(word_embeddings, [-1, self.model_settings["word_embedding_dimension"]]) centroid_embeddings = self.sentence_to_graph_mapper.get_forward_embeddings(tf.ones([tf.shape(entity_scores)[0], 1])) word_embeddings = tf.concat([word_embeddings, self.sentence_to_graph_mapper.map_backwards(centroid_embeddings)], axis=1) word_embeddings = tf.reshape(word_embeddings, [word_embedding_shape[0],-1,self.model_settings["word_embedding_dimension"]+1]) ### for lstm in self.lstms: word_embeddings = lstm.transform_sequences(word_embeddings) sentence_vector = self.attention.attend(word_embeddings, mode=mode) return self.candidate_scorer.score(sentence_vector, entity_scores, mode=mode) #if self.model_settings["use_transformation"]: # bag_of_words = self.transformation.transform(bag_of_words) #hidden = self.target_comparator.get_comparison_scores(sentence_vector, entity_scores) #entity_scores = tf.squeeze(self.final_transformation.transform(hidden)) #entity_scores = self.target_comparator.get_comparison_scores(bag_of_words, # entity_scores) #return entity_scores
def initialize_graph(self): self.hypergraph = TensorflowHypergraphRepresentation( self.variables, edge_dropout_rate=self.model_settings["edge_dropout"]) self.add_component(self.hypergraph) self.lstms = [ BiLstm(self.variables, self.model_settings["word_embedding_dimension"] + 1, self.model_settings["lstm_hidden_state_dimension"], variable_prefix="lstm_" + str(i)) for i in range(self.model_settings["n_lstms"]) ] for lstm in self.lstms: self.add_component(lstm) self.word_embedding = SequenceEmbedding( self.word_indexer, self.variables, variable_prefix="word", word_dropout_rate=self.model_settings["word_dropout"], is_static=self.model_settings["static_word_embeddings"]) self.add_component(self.word_embedding) #self.attention = Attention(self.model_settings["word_embedding_dimension"], self.variables, variable_prefix="attention", strategy="constant_query") self.attention = MultiheadAttention( self.model_settings["lstm_hidden_state_dimension"], self.variables, attention_heads=self.model_settings["n_attention_heads"], variable_prefix="attention", strategy="constant_query", attention_dropout=self.model_settings["attention_dropout"]) self.add_component(self.attention) self.target_comparator = TargetComparator( self.variables, variable_prefix="comparison_to_sentence", comparison="concat") self.add_component(self.target_comparator) self.decoder = SoftmaxDecoder(self.variables, self.model_settings["loss"]) self.add_component(self.decoder) self.candidate_scorer = NeuralNetworkOrFactorizationScorer( self.model_settings, self.variables, variable_prefix="scorer") self.add_component(self.candidate_scorer) gcn_factory = GcnFactory() gcn_settings = { "n_layers": self.model_settings["n_layers"], "embedding_dimension": self.model_settings["entity_embedding_dimension"], "n_relation_types": self.facts.number_of_relation_types } self.hypergraph_gcn_propagation_units = gcn_factory.get_gated_type_only_gcn( self.hypergraph, self.variables, gcn_settings) for layer in self.hypergraph_gcn_propagation_units: self.add_component(layer) self.sentence_to_graph_mapper = EmbeddingRetriever( self.variables, duplicate_policy="sum", variable_prefix="mapper") self.add_component(self.sentence_to_graph_mapper) self.final_transformation = MultilayerPerceptron( [ int(self.model_settings["lstm_hidden_state_dimension"] / 2) + self.model_settings["entity_embedding_dimension"], self.model_settings["nn_hidden_state_dimension"], 1 ], self.variables, variable_prefix="transformation", l2_scale=self.model_settings["regularization_scale"]) self.add_component(self.final_transformation)
class PathBagWithTypeGatesVsLstm(AbstractTensorflowModel): def get_preprocessor_stack_types(self): preprocessor_stack_types = ["hypergraph", "gold", "sentence"] if self.model_settings["static_entity_embeddings"]: preprocessor_stack_types += ["static_entity_embeddings"] return preprocessor_stack_types edge_gates = None def get_edge_gates(self): if self.edge_gates is None: self.edge_gates = [ hpgu.get_edge_gates() for hpgu in self.hypergraph_gcn_propagation_units ] return self.edge_gates def initialize_graph(self): self.hypergraph = TensorflowHypergraphRepresentation( self.variables, edge_dropout_rate=self.model_settings["edge_dropout"]) self.add_component(self.hypergraph) self.lstms = [ BiLstm(self.variables, self.model_settings["word_embedding_dimension"] + 1, self.model_settings["lstm_hidden_state_dimension"], variable_prefix="lstm_" + str(i)) for i in range(self.model_settings["n_lstms"]) ] for lstm in self.lstms: self.add_component(lstm) self.word_embedding = SequenceEmbedding( self.word_indexer, self.variables, variable_prefix="word", word_dropout_rate=self.model_settings["word_dropout"], is_static=self.model_settings["static_word_embeddings"]) self.add_component(self.word_embedding) #self.attention = Attention(self.model_settings["word_embedding_dimension"], self.variables, variable_prefix="attention", strategy="constant_query") self.attention = MultiheadAttention( self.model_settings["lstm_hidden_state_dimension"], self.variables, attention_heads=self.model_settings["n_attention_heads"], variable_prefix="attention", strategy="constant_query", attention_dropout=self.model_settings["attention_dropout"]) self.add_component(self.attention) self.target_comparator = TargetComparator( self.variables, variable_prefix="comparison_to_sentence", comparison="concat") self.add_component(self.target_comparator) self.decoder = SoftmaxDecoder(self.variables, self.model_settings["loss"]) self.add_component(self.decoder) self.candidate_scorer = NeuralNetworkOrFactorizationScorer( self.model_settings, self.variables, variable_prefix="scorer") self.add_component(self.candidate_scorer) gcn_factory = GcnFactory() gcn_settings = { "n_layers": self.model_settings["n_layers"], "embedding_dimension": self.model_settings["entity_embedding_dimension"], "n_relation_types": self.facts.number_of_relation_types } self.hypergraph_gcn_propagation_units = gcn_factory.get_gated_type_only_gcn( self.hypergraph, self.variables, gcn_settings) for layer in self.hypergraph_gcn_propagation_units: self.add_component(layer) self.sentence_to_graph_mapper = EmbeddingRetriever( self.variables, duplicate_policy="sum", variable_prefix="mapper") self.add_component(self.sentence_to_graph_mapper) self.final_transformation = MultilayerPerceptron( [ int(self.model_settings["lstm_hidden_state_dimension"] / 2) + self.model_settings["entity_embedding_dimension"], self.model_settings["nn_hidden_state_dimension"], 1 ], self.variables, variable_prefix="transformation", l2_scale=self.model_settings["regularization_scale"]) self.add_component(self.final_transformation) def set_indexers(self, indexers): self.word_indexer = indexers.word_indexer self.relation_indexer = indexers.relation_indexer self.entity_indexer = indexers.entity_indexer def compute_entity_scores(self, mode="train"): self.hypergraph.initialize_zero_embeddings( self.model_settings["entity_embedding_dimension"]) word_embeddings = self.word_embedding.get_representations(mode=mode) word_embedding_shape = tf.shape(word_embeddings) word_embeddings = tf.reshape( word_embeddings, [-1, self.model_settings["word_embedding_dimension"]]) centroid_embeddings = self.sentence_to_graph_mapper.get_forward_embeddings( tf.ones([tf.shape(self.hypergraph.entity_vertex_embeddings)[0], 1])) word_embeddings = tf.concat([ word_embeddings, self.sentence_to_graph_mapper.map_backwards(centroid_embeddings) ], axis=1) word_embeddings = tf.reshape(word_embeddings, [ word_embedding_shape[0], -1, self.model_settings["word_embedding_dimension"] + 1 ]) for lstm in self.lstms: word_embeddings = lstm.transform_sequences(word_embeddings) sentence_vector = self.attention.attend(word_embeddings, mode=mode) for hgpu in self.hypergraph_gcn_propagation_units: hgpu.set_gate_key(sentence_vector) hgpu.propagate() entity_scores = self.hypergraph.entity_vertex_embeddings if self.model_settings["concatenate_scores"]: entity_scores = tf.concat([ entity_scores, tf.expand_dims(self.hypergraph.get_vertex_scores(), 1) ], axis=1) return self.candidate_scorer.score(sentence_vector, entity_scores, mode=mode)
def initialize_graph(self): if not self.model_settings["static_entity_embeddings"]: self.entity_embedding = VectorEmbedding(self.entity_indexer, self.variables, variable_prefix="entity") self.add_component(self.entity_embedding) else: self.entity_embedding = StaticVectorEmbedding( self.entity_indexer, self.variables, variable_prefix="entity") self.add_component(self.entity_embedding) self.hypergraph = TensorflowHypergraphRepresentation(self.variables) self.add_component(self.hypergraph) self.word_embedding = SequenceEmbedding(self.word_indexer, self.variables, variable_prefix="word") self.add_component(self.word_embedding) self.target_comparator = TargetComparator( self.variables, variable_prefix="comparison_to_sentence", comparison="concat") self.add_component(self.target_comparator) self.decoder = SoftmaxDecoder(self.variables) self.add_component(self.decoder) self.hypergraph_gcn_propagation_units = [ None ] * self.model_settings["n_layers"] for layer in range(self.model_settings["n_layers"]): self.hypergraph_gcn_propagation_units[ layer] = HypergraphGcnPropagationUnit( "layer_" + str(layer), self.facts, self.variables, self.model_settings["entity_embedding_dimension"], self.hypergraph, weights="identity", biases="relation_specific", self_weight="identity", self_bias="zero", add_inverse_relations=False) self.add_component(self.hypergraph_gcn_propagation_units[layer]) self.sentence_to_graph_mapper = EmbeddingRetriever( self.variables, duplicate_policy="sum", variable_prefix="mapper") self.add_component(self.sentence_to_graph_mapper) if False: #self.model_settings["use_transformation"]: self.transformation = MultilayerPerceptron( [ self.model_settings["word_embedding_dimension"], self.model_settings["entity_embedding_dimension"] ], self.variables, variable_prefix="transformation", l2_scale=self.model_settings["regularization_scale"]) self.centroid_transformation = MultilayerPerceptron( [ self.model_settings["entity_embedding_dimension"], self.model_settings["word_embedding_dimension"] ], self.variables, variable_prefix="centroid_transformation", l2_scale=self.model_settings["regularization_scale"]) self.add_component(self.centroid_transformation) self.add_component(self.transformation) self.final_transformation = MultilayerPerceptron( [ self.model_settings["word_embedding_dimension"] + self.model_settings["entity_embedding_dimension"], 4 * self.model_settings["entity_embedding_dimension"], 1 ], self.variables, variable_prefix="transformation", l2_scale=self.model_settings["regularization_scale"]) self.add_component(self.final_transformation)
class PathBagVsBagOfWords(AbstractTensorflowModel): def get_preprocessor_stack_types(self): preprocessor_stack_types = ["hypergraph", "gold", "sentence"] if self.model_settings["static_entity_embeddings"]: preprocessor_stack_types += ["static_entity_embeddings"] return preprocessor_stack_types def initialize_graph(self): if not self.model_settings["static_entity_embeddings"]: self.entity_embedding = VectorEmbedding(self.entity_indexer, self.variables, variable_prefix="entity") self.add_component(self.entity_embedding) else: self.entity_embedding = StaticVectorEmbedding( self.entity_indexer, self.variables, variable_prefix="entity") self.add_component(self.entity_embedding) self.hypergraph = TensorflowHypergraphRepresentation(self.variables) self.add_component(self.hypergraph) self.word_embedding = SequenceEmbedding(self.word_indexer, self.variables, variable_prefix="word") self.add_component(self.word_embedding) self.target_comparator = TargetComparator( self.variables, variable_prefix="comparison_to_sentence", comparison="concat") self.add_component(self.target_comparator) self.decoder = SoftmaxDecoder(self.variables) self.add_component(self.decoder) self.hypergraph_gcn_propagation_units = [ None ] * self.model_settings["n_layers"] for layer in range(self.model_settings["n_layers"]): self.hypergraph_gcn_propagation_units[ layer] = HypergraphGcnPropagationUnit( "layer_" + str(layer), self.facts, self.variables, self.model_settings["entity_embedding_dimension"], self.hypergraph, weights="identity", biases="relation_specific", self_weight="identity", self_bias="zero", add_inverse_relations=False) self.add_component(self.hypergraph_gcn_propagation_units[layer]) self.sentence_to_graph_mapper = EmbeddingRetriever( self.variables, duplicate_policy="sum", variable_prefix="mapper") self.add_component(self.sentence_to_graph_mapper) if False: #self.model_settings["use_transformation"]: self.transformation = MultilayerPerceptron( [ self.model_settings["word_embedding_dimension"], self.model_settings["entity_embedding_dimension"] ], self.variables, variable_prefix="transformation", l2_scale=self.model_settings["regularization_scale"]) self.centroid_transformation = MultilayerPerceptron( [ self.model_settings["entity_embedding_dimension"], self.model_settings["word_embedding_dimension"] ], self.variables, variable_prefix="centroid_transformation", l2_scale=self.model_settings["regularization_scale"]) self.add_component(self.centroid_transformation) self.add_component(self.transformation) self.final_transformation = MultilayerPerceptron( [ self.model_settings["word_embedding_dimension"] + self.model_settings["entity_embedding_dimension"], 4 * self.model_settings["entity_embedding_dimension"], 1 ], self.variables, variable_prefix="transformation", l2_scale=self.model_settings["regularization_scale"]) self.add_component(self.final_transformation) def set_indexers(self, indexers): self.word_indexer = indexers.word_indexer self.relation_indexer = indexers.relation_indexer self.entity_indexer = indexers.entity_indexer def compute_entity_scores(self): #entity_vertex_embeddings = self.entity_embedding.get_representations() word_embeddings = self.word_embedding.get_representations() #word_embedding_shape = tf.shape(word_embeddings) #word_embeddings = tf.reshape(word_embeddings, [-1, self.model_settings["word_embedding_dimension"]]) #centroid_embeddings = self.sentence_to_graph_mapper.get_forward_embeddings(entity_vertex_embeddings) #centroid_embeddings = self.centroid_transformation.transform(centroid_embeddings) #word_embeddings += self.sentence_to_graph_mapper.map_backwards(centroid_embeddings) #word_embeddings = tf.reshape(word_embeddings, word_embedding_shape) self.hypergraph.initialize_zero_embeddings( self.model_settings["entity_embedding_dimension"]) for hgpu in self.hypergraph_gcn_propagation_units: hgpu.propagate() entity_scores = self.hypergraph.entity_vertex_embeddings bag_of_words = tf.reduce_sum(word_embeddings, 1) #if self.model_settings["use_transformation"]: # bag_of_words = self.transformation.transform(bag_of_words) hidden = self.target_comparator.get_comparison_scores( bag_of_words, entity_scores) entity_scores = tf.squeeze(self.final_transformation.transform(hidden)) #entity_scores = tf.Print(entity_scores, [entity_scores], summarize=25, message="entity_scores: ") #entity_scores = self.target_comparator.get_comparison_scores(bag_of_words, # entity_scores) return entity_scores
class EntityEmbeddingVsLstm(AbstractTensorflowModel): def get_preprocessor_stack_types(self): preprocessor_stack_types = ["hypergraph", "gold", "sentence"] if self.model_settings["static_entity_embeddings"]: preprocessor_stack_types += ["static_entity_embeddings"] return preprocessor_stack_types def set_indexers(self, indexers): self.word_indexer = indexers.word_indexer self.entity_indexer = indexers.entity_indexer def initialize_graph(self): if not self.model_settings["static_entity_embeddings"]: self.entity_embedding = VectorEmbedding(self.entity_indexer, self.variables, variable_prefix="entity") self.add_component(self.entity_embedding) else: self.entity_embedding = StaticVectorEmbedding( self.entity_indexer, self.variables, variable_prefix="entity") self.add_component(self.entity_embedding) self.word_embedding = SequenceEmbedding(self.word_indexer, self.variables, variable_prefix="word") self.add_component(self.word_embedding) self.lstms = [ BiLstm(self.variables, self.model_settings["word_embedding_dimension"], variable_prefix="lstm_" + str(i)) for i in range(self.model_settings["n_lstms"]) ] for lstm in self.lstms: self.add_component(lstm) self.target_comparator = TargetComparator( self.variables, variable_prefix="comparison_to_sentence") self.add_component(self.target_comparator) self.decoder = SoftmaxDecoder(self.variables) self.add_component(self.decoder) self.sentence_to_graph_mapper = EmbeddingRetriever( self.variables, duplicate_policy="sum", variable_prefix="mapper") self.add_component(self.sentence_to_graph_mapper) if self.model_settings["use_transformation"]: self.transformation = MultilayerPerceptron( [ self.model_settings["word_embedding_dimension"], self.model_settings["entity_embedding_dimension"] ], self.variables, variable_prefix="transformation", l2_scale=self.model_settings["regularization_scale"]) self.centroid_transformation = MultilayerPerceptron( [ self.model_settings["entity_embedding_dimension"], self.model_settings["word_embedding_dimension"] ], self.variables, variable_prefix="centroid_transformation", l2_scale=self.model_settings["regularization_scale"]) self.add_component(self.centroid_transformation) self.add_component(self.transformation) def compute_entity_scores(self): entity_vertex_embeddings = self.entity_embedding.get_representations() word_embeddings = self.word_embedding.get_representations() word_embedding_shape = tf.shape(word_embeddings) word_embeddings = tf.reshape( word_embeddings, [-1, self.model_settings["word_embedding_dimension"]]) centroid_embeddings = self.sentence_to_graph_mapper.get_forward_embeddings( entity_vertex_embeddings) centroid_embeddings = self.centroid_transformation.transform( centroid_embeddings) word_embeddings += self.sentence_to_graph_mapper.map_backwards( centroid_embeddings) word_embeddings = tf.reshape(word_embeddings, word_embedding_shape) for lstm in self.lstms: word_embeddings = lstm.transform_sequences(word_embeddings) attention_scores = self.lstm_attention.transform_sequences( word_embeddings) attention_values = tf.nn.softmax(attention_scores, dim=1) attention_weighted_word_scores = word_embeddings * attention_values target_vector = tf.reduce_sum(attention_weighted_word_scores, 1) if self.model_settings["use_transformation"]: target_vector = tf.nn.relu(target_vector) target_vector = self.transformation.transform(target_vector) entity_scores = self.target_comparator.get_comparison_scores( target_vector, entity_vertex_embeddings) return entity_scores