def score_function(self, head_entity_id, relationship, tail_entity_id): """ """ with tf.name_scope('entity_embedding'): head_entity_embedded = self.embed_entity(head_entity_id) tail_entity_embedded = self.embed_entity(tail_entity_id) score_result = self._scoring_function(head_entity_embedded, relationship, tail_entity_embedded) assert int_shapes(score_result) == [ -1, 1 ], 'Score result was: {}, expected [-1, 1]'.format( int_shapes(score_result)) return score_result
def relationship_shape_correct(self, relationship_embeddings, name=None): dims = int_shapes(relationship_embeddings) if len(dims) == 3: return tf.reshape(relationship_embeddings, [-1, 1, self.relationship_embed_dim], name=name) elif len(dims) == 4: # vectors/matrices if dims[2] == 1: return tf.reshape(relationship_embeddings, [-1, 1, self.relationship_embed_dim], name=name) else: return tf.reshape( relationship_embeddings, [-1, self.relationship_embed_dim, self.entity_embed_dim], name=name) elif len(dims) == 5: # tensors return tf.reshape(relationship_embeddings, [ -1, self.entity_embed_dim, self.entity_embed_dim, self.relationship_embed_dim ]) else: raise ValueError( 'The relationship embeddings are vectors, matrices or tensors')
def _scoring_function(self, embedded_head, relationship, embedded_tail): with tf.variable_scope('relationship'): relationship_vectors = tf.nn.embedding_lookup(self.W_relationship_embedding, relationship) relationship_matrices = tf.nn.embedding_lookup(self.W_bilinear_relationship, relationship) with tf.name_scope('scoringfunction'): print(tensorutils.int_shapes(relationship_vectors)) linear_part = 2 * self.g_linear(embedded_head, relationship_vectors, -1.0 * relationship_vectors, embedded_tail) bilinear_part = -2 * self.g_bilinear(embedded_head, relationship_matrices, embedded_tail) norm = tf.square(tf.norm(relationship_vectors, ord='euclidean', axis=2)) return linear_part + bilinear_part + norm
def test_bilinear_product_with_tensor(): # define the relationships tensor_relationship = tf.placeholder(tf.float32, shape=(None, entity_dims, entity_dims, relationship_dims)) # define the tensor result tensor_result = tensorutils.bilinear_product(left, tensor_relationship, right) # assert that the result has to be a vector assert tensorutils.int_shapes(tensor_result) == [-1, relationship_dims] feed_dict_local = dict(single_batch_feed_dict) feed_dict_local[tensor_relationship] = np.linspace( 0, 1, entity_dims * entity_dims * relationship_dims).reshape( -1, entity_dims, entity_dims, relationship_dims) tf_result = sess.run(tensor_result, feed_dict_local) # without batches numpy_result = manual_bilinear_products_for_tensors( feed_dict_local[left], feed_dict_local[tensor_relationship], feed_dict_local[right]) assert np.allclose(numpy_result, tf_result) feed_dict_local = dict(multi_batch_feed_dict) feed_dict_local[tensor_relationship] = np.linspace( 0, 1, 2 * entity_dims * entity_dims * relationship_dims).reshape( 2, entity_dims, entity_dims, relationship_dims) tf_result = sess.run(tensor_result, feed_dict_local) # with batches numpy_result = manual_bilinear_products_for_tensors( feed_dict_local[left], feed_dict_local[tensor_relationship], feed_dict_local[right]) assert np.allclose(numpy_result, tf_result)
def test_bilinear_product_with_matrix(): # define the relationships matrix_relationship = tf.placeholder(tf.float32, shape=(None, entity_dims, entity_dims)) # matrix result matrix_result = tensorutils.bilinear_product(left, matrix_relationship, right) # assert that the result must be a scalar. assert tensorutils.int_shapes(matrix_result) == [-1, 1] feed_dict_local = dict(single_batch_feed_dict) feed_dict_local[matrix_relationship] = np.linspace( 0, 1, entity_dims * entity_dims).reshape(-1, entity_dims, entity_dims) tf_result = sess.run(matrix_result, feed_dict_local) # without batches numpy_result = np.matmul( feed_dict_local[left].squeeze(0).T, np.matmul(feed_dict_local[matrix_relationship].squeeze(0), feed_dict_local[right].squeeze(0))) assert np.allclose(numpy_result, tf_result) feed_dict_local = dict(multi_batch_feed_dict) feed_dict_local[matrix_relationship] = np.linspace( 0, 1, entity_dims * entity_dims * 2).reshape(2, entity_dims, entity_dims) tf_result = sess.run(matrix_result, feed_dict_local) # with batches numpy_result = np.matmul( np.transpose(feed_dict_local[left], (0, 2, 1)), np.matmul(feed_dict_local[matrix_relationship], feed_dict_local[right])) assert np.allclose(numpy_result.squeeze(), tf_result.squeeze())