Example #1
0
File: base.py Project: mberr/PyKEEN
    def load_triples(self, triples_in: Union[np.ndarray, List[np.ndarray]]):
        """
        Loads triples from arrays, creates mappings and returns the mapped triples
        :param data_paths: The paths for all files that are going to be used for training and testing
        :return: List where each items represents the mapped triples of a file
        """

        if isinstance(triples_in, np.ndarray):
            all_triples = triples_in
            self.entity_label_to_id, self.relation_label_to_id = create_mappings(
                triples=all_triples)
            mapped_triples, _, _ = create_mapped_triples(
                triples=all_triples,
                entity_label_to_id=self.entity_label_to_id,
                relation_label_to_id=self.relation_label_to_id)
        else:
            all_triples: np.ndarray = np.concatenate(triples_in, axis=0)
            self.entity_label_to_id, self.relation_label_to_id = create_mappings(
                triples=all_triples)
            mapped_triples = [
                create_mapped_triples(
                    triples,
                    entity_label_to_id=self.entity_label_to_id,
                    relation_label_to_id=self.relation_label_to_id)[0]
                for triples in triples_in
            ]

        self.num_entities = len(self.entity_label_to_id)
        self.num_relations = len(self.relation_label_to_id)

        return mapped_triples
Example #2
0
    def _get_train_triples(self):
        train_pos = load_data(self.config[pkc.TRAINING_SET_PATH])

        self.entity_label_to_id, self.relation_label_to_id = create_mappings(triples=train_pos)

        mapped_pos_train_triples, _, _ = create_mapped_triples(
            triples=train_pos,
            entity_label_to_id=self.entity_label_to_id,
            relation_label_to_id=self.relation_label_to_id,
        )

        return mapped_pos_train_triples
Example #3
0
def _get_train_triples(data_path: str):
    train_pos = load_data(data_path)

    entity_label_to_id, relation_label_to_id = create_mappings(
        triples=train_pos)

    mapped_pos_train_triples, _, _ = create_mapped_triples(
        triples=train_pos,
        entity_label_to_id=entity_label_to_id,
        relation_label_to_id=relation_label_to_id,
    )

    return mapped_pos_train_triples, entity_label_to_id, relation_label_to_id
Example #4
0
    def _handle_train_and_test(self, train_pos, test_pos) -> Tuple[np.ndarray, np.ndarray]:
        """"""
        all_triples: np.ndarray = np.concatenate([train_pos, test_pos], axis=0)
        self.entity_label_to_id, self.relation_label_to_id = create_mappings(triples=all_triples)

        mapped_pos_train_triples, _, _ = create_mapped_triples(
            triples=train_pos,
            entity_label_to_id=self.entity_label_to_id,
            relation_label_to_id=self.relation_label_to_id,
        )

        mapped_pos_test_triples, _, _ = create_mapped_triples(
            triples=test_pos,
            entity_label_to_id=self.entity_label_to_id,
            relation_label_to_id=self.relation_label_to_id,
        )

        return mapped_pos_train_triples, mapped_pos_test_triples