intfloat / SimKGC

ACL 2022, SimKGC: Simple Contrastive Knowledge Graph Completion with Pre-trained Language Models
188 stars 36 forks source link

How to get(save) the tensor of the entity (or hr) with the trained SimKGC model? #14

Closed EurusNotes closed 1 year ago

EurusNotes commented 1 year ago

您好,Liang. 请问如何用已经训练好的SimKGC模型获得 entity(或者head+rel)的tensor并保存? 我想要向模型输入字符串然后获得它的tensor,并保存这一对pair。类似于{entity:tensor}

不过这里的 predictor.predict_by_entities 好像也可以获得tensor,对吗?我有点混乱,不知道改用哪一个好。 https://github.com/intfloat/SimKGC/blob/2139ac1573970bd93a4ce261f815da6b31f62b6a/evaluate.py#L108-L125

希望您百忙之中能够回复我,这将能够非常帮助我。


Hi, Liang

How can I get the tensor data of the entity (or hr) with the SimKGC model already trained?

I read your code. Is it possible to use predictor.predict_by_examples() to get the tensor?

And, Is your code only suitable for running in the terminal this time? Because when I try to use Jupyter Notebook I find that it doesn't work in many places.

I hope you can reply to me despite your busy schedule, it would be a great help to me.

intfloat commented 1 year ago

Hi,

The entity_tensor corresponds to the vector representations of all the candidate entities. The hr_tensor corresponds to the vector representation of head entity + relation.

The reason there are two similar functions predict_by_examples and predict_by_entities is that predict_by_entities only computes vector for candidate entities, and can thus speed up the evaluation a bit.

I never tried to run this code base in Jupyter Notebook, but I suspect it should work fine if you import the required functions and libraries.

Thanks.

EurusNotes commented 1 year ago

Hi, Liang. 感谢您的回答,您的回答对我非常有帮助,让我成功地计算了 entity 的 tensor。非常感谢您。

我注意到,在预测 head entity 的时候,应该使用 tail entity + ‘inverse’ + relation 的 tensor。请问您如何获得 tail entity + ‘inverse’ + relation 的 tensor?是否也需要使用 hr_tensor

此外,请问您:candidate entities 的数量是否等于 集合 entity 数量 - 1


Thank you for your response. Your answer has been very helpful to me and I was able to successfully calculate the tensor of the entity. Thank you very much.

I noticed that when predicting the head entity, the tensor of tail entity + 'inverse' + relation should be used. Could you please tell me how to obtain the tensor of tail entity + 'inverse' + relation? Is thehr_tensor also needed?

In addition, could you please confirm if the number of candidate entities is equal to the number of entities in the set minus one?

EurusNotes commented 1 year ago

Hello, Liang I solved the above problems. Thank you very much for your ongoing reply. 🙏 I wish you good health.