def test_types_encoded_by_expected_functions(self): things = np.array([[0, 0], [1, 0], [2, 0.5673]], dtype=np.float32) mock_entity_relation_encoder = Mock( return_value=np.array([[0, 0, 0], [0, 0, 0]], dtype=np.float32)) mock_attribute_encoder = Mock(return_value=np.array( [[0.9527, 0.2367, 0.7582]], dtype=np.float32)) encoders_for_types = { lambda: mock_entity_relation_encoder: [0, 1], lambda: mock_attribute_encoder: [2] } tm = TypewiseEncoder(encoders_for_types, 3) encoding = tm(things) # The function under test np.testing.assert_array_equal( [[np.array([[0], [0]], dtype=np.float32)]], get_call_args(mock_entity_relation_encoder)) np.testing.assert_array_equal( [[np.array([[0.5673]], dtype=np.float32)]], get_call_args(mock_attribute_encoder)) expected_encoding = np.array( [[0, 0, 0], [0, 0, 0], [0.9527, 0.2367, 0.7582]], dtype=np.float32) np.testing.assert_array_equal(expected_encoding, encoding.numpy())
def test_encoders_do_not_fulfil_classes(self): mock_entity_relation_encoder = Mock() encoders_for_types = {lambda: mock_entity_relation_encoder: [0, 2]} with self.assertRaises(ValueError) as context: TypewiseEncoder(encoders_for_types, 3) self.assertEqual( 'Encoder categories are inconsistent. Expected [0, 1, 2], but got [0, 2]', str(context.exception))
def test_basic_encoding(self): things = np.array([[0], [1], [2]], dtype=np.float32) mock_entity_relation_encoder = Mock(return_value=np.array( [[0.1, 0, 0], [0.1, 0, 0], [0.1, 0, 0]], dtype=np.float32)) encoders_for_types = {lambda: mock_entity_relation_encoder: [0, 1, 2]} tm = TypewiseEncoder(encoders_for_types, 3) encoding = tm(things) # The function under test expected_encoding = np.array([[0.1, 0, 0], [0.1, 0, 0], [0.1, 0, 0]], dtype=np.float32) np.testing.assert_array_equal(expected_encoding, encoding.numpy())
def test_with_tensors(self): tf.reset_default_graph() tf.set_random_seed(1) things = tf.convert_to_tensor( np.array([[0, 0], [1, 0], [2, 0.5673]], dtype=np.float32)) entity_relation = lambda x: x continuous_attribute = lambda x: x encoders_for_types = { lambda: entity_relation: [0, 1], lambda: continuous_attribute: [2] } tm = TypewiseEncoder(encoders_for_types, 1) encoded_things = tm(things) # The function under test # Check that tensorflow was actually used self.assertEqual(EagerTensor, type(encoded_things))
def embed_attribute(features, attr_encoders, attr_embedding_dim): typewise_attribute_encoder = TypewiseEncoder(attr_encoders, attr_embedding_dim) attr_embedding = typewise_attribute_encoder(features[:, 1:]) tf.summary.histogram('attribute_embedding_histogram', attr_embedding) return attr_embedding
def attribute_embedding(features, attr_encoders, attr_embedding_dim): typewise_attribute_encoder = TypewiseEncoder(attr_encoders, attr_embedding_dim) return typewise_attribute_encoder(features[:, 1:])