Repository: pykeen/pykeen
Issue: Get scores of every triple in the dataset
Root cause: Documentation Deficiency

Hi, I tried to get scores of every triple in my dataset using ` predictions_df = model.get_all_prediction_df(triples_factory=result.training)` in [the documentation](https://pykeen.readthedocs.io/en/latest/tutorial/making_predictions.html#:~:text=predictions_df%20%3D%20model.get_all_prediction_df(triples_factory%3Dresult.training)). The codes are as follow:

```python
import ...

TRIPLE = "triple.txt"
MODEL = "results_transR/trained_model.pkl"
tf = TriplesFactory.from_path(TRIPLE)
training, testing = tf.split()

traning the model...

model = torch.load(MODEL)
predictions_df = model.get_all_prediction_df(triples_factory=tf)
print(predictions_df)

```
the number of my triples is 2764, but the output of the predictions_df contains 1746404 scores, as follows.

```
head_id          head_label  relation_id relation_label  tail_id  \
0            590     wall_top_313753            4           join      590   
1            411   column_top_313657            4           join      411   
2            286       column_313647            4           join      286   
3            117  beam_bottom_313661            4           join      117   
4            417         slab_306057            4           join      417   
...          ...                 ...          ...            ...      ...   
1746400      259       column_299440            0   contact_with      139   
1746401      256       column_299430            0   contact_with      455   
1746402      289       column_313653            0   contact_with      455   
1746403      411   column_top_313657            2    has_surface      257   
1746404      289       column_313653            0   contact_with      479   

                 tail_label     score  in_training  
0           wall_top_313753 -0.029119        False  
1         column_top_313657 -0.029119        False  
2             column_313647 -0.029119        False  
3        beam_bottom_313661 -0.029119        False  
4               slab_306057 -0.029119        False  
...                     ...       ...          ...  
1746400    beam_side_300341 -6.431433        False  
1746401  slab_bottom_313611 -6.446237        False  
1746402  slab_bottom_313611 -6.468115        False  
1746403       column_299436 -6.477124        False  
1746404    slab_side_313611 -6.494636        False  

[1746405 rows x 8 columns]
```

How can I get 2764 scores for my 2764 triples in my dataset (i.e., one triple one score)? Thanks!



Hi @Hao-666 ,

`get_all_prediction_df` computes score for *all possible triples* rather than just the existing ones. There is a more explicit documentation when looking at the particular method: https://pykeen.readthedocs.io/en/latest/api/pykeen.models.predict.get_all_prediction_df.html#pykeen.models.predict.get_all_prediction_df

To compute scores for a given tensor of ID-based triples, you can use the `score_hrt` method, e.g.,
```python
scores = model.score_hrt(triples_factory.mapped_triples.to(model.device))
```

Notice that depending on your model and GPU size, you may see out-of-memory problems, and then need to add some batching mechanism, e.g.,
```python
scores = []
for batch in triples_factory.mapped_triples.split(batch_size, dim=0):
  scores.append(model.score_hrt(batch.to(model.device)).cpu())
scores = torch.cat(scores, dim=0)
```

Another trick is to make sure to disable gradient tracking when doing so (which allows PyTorch to discard intermediate values at the cost of not being able to compute gradients), cf. [torch.no_grad](https://pytorch.org/docs/stable/generated/torch.no_grad.html?highlight=no_grad#torch.no_grad).

Hi @mberr ,

Thanks for such a helpful answer! `scores = model.score_hrt(triples_factory.mapped_triples.to(model.device))` worked for me :)