Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to use a trained model to obtain entity embeddings #28

Open
xga0 opened this issue Aug 10, 2023 · 1 comment
Open

How to use a trained model to obtain entity embeddings #28

xga0 opened this issue Aug 10, 2023 · 1 comment

Comments

@xga0
Copy link

xga0 commented Aug 10, 2023

Thank you for sharing the code!

After we have trained a model, how to use this trained model to obtain entity embeddings? Should we use the following function:

    def predict_by_entities(self, entity_exs) -> torch.tensor:
        examples = []
        for entity_ex in entity_exs:
            examples.append(Example(head_id='', relation='',
                                    tail_id=entity_ex.entity_id))
        data_loader = torch.utils.data.DataLoader(
            Dataset(path='', examples=examples, task=args.task),
            num_workers=2,
            batch_size=max(args.batch_size, 1024),
            collate_fn=collate,
            shuffle=False)

        ent_tensor_list = []
        for idx, batch_dict in enumerate(tqdm.tqdm(data_loader)):
            batch_dict['only_ent_embedding'] = True
            if self.use_cuda:
                batch_dict = move_to_cuda(batch_dict)
            outputs = self.model(**batch_dict)
            ent_tensor_list.append(outputs['ent_vectors'])

        return torch.cat(ent_tensor_list, dim=0)

Also, is the vectors.json file mentioned in this issue generated by the following line?

entity_tensor = predictor.predict_by_entities(entity_dict.entity_exs)

Thank you!

@intfloat
Copy link
Owner

Yes, that's correct.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants