Comments (10)
@mali-git to access the weight data, I have to call
from pykeen.pipeline import pipeline result = pipeline( model='TransE', dataset='umls', ) model = result.model entity_embeddings = model.entity_embeddings._embeddings.weight.data relation_embeddings = model.relation_embeddings._embeddings.weight.dataHas this been updated recently or did I overlook something? smile
Hi, this way should still be possible.
For a more future-proof way you can use
entity_embeddings = model.entity_embeddings()
or more explicitly
entity_embeddings = model.entity_embeddings(indices=None)
which does not depend on the underlying Embedding implementation details, but uses the interface RepresentationModule
.
Also, we are extending our framework to have a unified way to store more than one representation for entities/relations, cf. here:
pykeen/src/pykeen/models/nbase.py
Line 310 in 68af86e
Briefly, the change is to always store a list. In that case, for TransE, you would need to use model.entity_representations[0]()
. We'll update the docs once we actually did migrate the models.
from pykeen.
Hi @xin-zhao0,
pipeline_result
contains the trained model which contains the embeddings:
model = pipeline_result.model
entity_embeddings = model.entity_embeddings.weight.data
relation_embeddings = model.relation_embeddings.weight.data
Each row in entity_embeddings/relation_embeddings
contains the embedding of a specific entity/relation. For instance, entity_embeddings[0]
returns the embedding of the entity with id 0.
To get the ids of the entites/relations, you can use the internal mappings:
entity_to_id = model.triples_factory.entity_to_id
relation_to_id = model.triples_factory.relation_to_id
Putting everything together you can get the entity embeddings for the entities 'brazil' and 'china' contained in the Nations dataset as follows:
import torch
import numpy as np
model = pipeline_result.model
entity_embeddings = model.entity_embeddings.weight.data
relation_embeddings = model.relation_embeddings.weight.data
entity_to_id = model.triples_factory.entity_to_id
entities = ['brazil', 'china']
entity_ids = torch.tensor([entity_to_id[entity] for entity in entities], dtype=torch.long)
embedded_entities = entity_embeddings[entity_ids]
from pykeen.
Thank you. for models like rescal and some others, there is a relation-specific matrix M. Is that returned in the results as part of the model?
The relations matrices are also saved in model.relation_embeddings.weight.data
, but they need to be reshaped. For RESCAL, you can get the relation embedding dimension as follows (please consider that some models have a dedicated relation_dim
which needs to be used when reshaping the relation-embeddings) :
import torch
import numpy as np
# Get the relation-matrix of the relation with the ID 0
relation_embeddings = rescal.relation_embeddings.weight.data
relation = relation_embeddings[0].view(rescal.embedding_dim, rescal.embedding_dim)
# Get the embeddings for the relations 'officialvisits' and 'reldiplomacy' contaiend in the Nations dataset
relation_to_id = model.triples_factory.relation_to_id
relations = ['officialvisits', 'reldiplomacy']
relation_ids = torch.tensor([relation_to_id[relation] for relation in relations], dtype=torch.long)
relation_embeddings = relation_embeddings[relation_ids].view(-1, rescal.embedding_dim, rescal.embedding_dim)
from pykeen.
New docs at https://pykeen.readthedocs.io/en/latest/tutorial/first_steps.html#using-learned-embeddings
from pykeen.
Thanks @cthoyt, very much appreciated. 👍
from pykeen.
@mali-git before we close this, let's add some documentation in the "getting started" tutorial
from pykeen.
Sounds good!
from pykeen.
Thank you. for models like rescal and some others, there is a relation-specific matrix M. Is that returned in the results as part of the model?
from pykeen.
@mali-git to access the weight data, I have to call
from pykeen.pipeline import pipeline
result = pipeline(
model='TransE',
dataset='umls',
)
model = result.model
entity_embeddings = model.entity_embeddings._embeddings.weight.data
relation_embeddings = model.relation_embeddings._embeddings.weight.data
Has this been updated recently or did I overlook something? 😄
from pykeen.
@shimst3r @xin-zhao0 I've started a PR where we translated some of this discussion into the documentation. Thanks for keeping us honest ;)
from pykeen.
Related Issues (20)
- AttributeError: 'Module' object has no attribute 'get' HOT 2
- Question about the use of `create_inverse_triples` HOT 2
- Want to train a model without any evaluate or test dataset HOT 1
- Bug in wandb result tracker HOT 1
- Possible issue with model evaluation when using datasets with inverse triples HOT 1
- RGCN RuntimeError: trying to backward through graph a second time. (has parameters but no reset_parameters) HOT 2
- QuatE: GPU memory is not released per epoch HOT 3
- Training loop does not update relation representations when continuing training HOT 2
- from pykeen.pipeline import pipeline, pipeline issue HOT 3
- Evaluating metrics on many subsets with multiple models HOT 2
- Shape Mismatch upon initializing pretrained ComplEx embeddings HOT 2
- TransE - CUDA out of memory HOT 3
- Importing model_resolver HOT 2
- Getting Embeddings of the Entity and Relations HOT 13
- RGCN Hyper parameter optimization error HOT 1
- MatKG HOT 1
- HPO_Pipeline fails on AutoSF models HOT 1
- Unable to reproduce TransE experiment
- EarlyStopper: show progress bar
- Cosine Annealing with Warm Restart LR Scheduler recieving an unexpected kwarg `T_i`
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from pykeen.