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

TypeError: missing required positional argument when using CoreTriplesFactory.from_path_binary() #1182

Closed
3 tasks done
uier opened this issue Dec 7, 2022 · 5 comments · Fixed by #1185
Closed
3 tasks done
Labels
bug Something isn't working

Comments

@uier
Copy link

uier commented Dec 7, 2022

Describe the bug

According to the document about saving pipeline result to directory, it said:

training_triples contains the training triples factory, including label-to-id mappings, if used. It has been saved via pykeen.triples.CoreTriplesFactory.to_path_binary(), and can re-loaded via pykeen.triples.CoreTriplesFactory.from_path_binary().

But error occurred when I want to load the triplets_factory from the pipeline result saved before.

triplets_factory = CoreTriplesFactory.from_path_binary('RESCAL_FB15k237/training_triples')
Traceback (most recent call last):
  File "/home/tzuwei/developing/kge-loss/main.py", line 32, in <module>
    triplets_factory = CoreTriplesFactory.from_path_binary(f'{save_path}/training_triples')
  File "/home/tzuwei/.pyenv/versions/loss/lib/python3.9/site-packages/pykeen/triples/triples_factory.py", line 816, in from_path_binary
    return cls(**cls._from_path_binary(path=path))
TypeError: __init__() missing 2 required positional arguments: 'num_entities' and 'num_relations'

Seems like the data return by cls._from_path_binary does not contain num_entities and num_relations at line 816.

@classmethod
# docstr-coverage: inherited
def from_path_binary(
cls,
path: Union[str, pathlib.Path, TextIO],
) -> "CoreTriplesFactory": # noqa: D102
"""
Load triples factory from a binary file.
:param path:
The path, pointing to an existing PyTorch .pt file.
:return:
The loaded triples factory.
"""
path = normalize_path(path)
logger.info(f"Loading from {path.as_uri()}")
return cls(**cls._from_path_binary(path=path))
@classmethod
def _from_path_binary(
cls,
path: pathlib.Path,
) -> MutableMapping[str, Any]:
# load base
data = dict(torch.load(path.joinpath(cls.base_file_name)))
# load numeric triples
data["mapped_triples"] = torch.as_tensor(
pd.read_csv(path.joinpath(cls.triples_file_name), sep="\t", dtype=int).values,
dtype=torch.long,
)
return data

How to reproduce

The following code can reproduce the problem on my machine

from pykeen.pipeline import pipeline
from pykeen.models import RESCAL
from pykeen.datasets import FB15k237
from pykeen.triples import CoreTriplesFactory

result = pipeline(
    model=RESCAL,
    dataset=FB15k237,
)
result.save_to_directory('RESCAL_FB15k237')

triplets_factory = CoreTriplesFactory.from_path_binary('RESCAL_FB15k237/training_triples')

Environment

Unable to handle parameter in CooccurrenceFilteredModel: base

Key Value
OS posix
Platform Linux
Release 5.15.0-52-generic
Time Wed Dec 7 18:02:29 2022
Python 3.9.14
PyKEEN 1.9.0
PyKEEN Hash 1f526edb
PyKEEN Branch master
PyTorch 1.13.0+cu117
CUDA Available? true
CUDA Version 11.7
cuDNN Version 8500

Additional information

Thank you for providing this open source project. Your work is very helpful to me. 😄

Issue Template Checks

  • This is not a feature request (use a different issue template if it is)
  • This is not a question (use the discussions forum instead)
  • I've read the text explaining why including environment information is important and understand if I omit this information that my issue will be dismissed
@uier uier added the bug Something isn't working label Dec 7, 2022
@mberr
Copy link
Member

mberr commented Dec 10, 2022

Hi @uier ,

thanks for reporting the issue. I could locally reproduce the error with the code you provided, and extract a test case from it for #1185 using a reduced setting.

@mberr
Copy link
Member

mberr commented Dec 10, 2022

@uier this seems to be resolved in #1185 now - if you already want to try it out, you can install PyKEEN from the given branch (cf. here):

pip install https://github.com/pykeen/pykeen/archive/fix-from-path-binary.zip

Note that after #1185 has been merged, you would need to update to

pip install git+https://github.com/pykeen/pykeen.git

(as explained in the repo's README).

@uier
Copy link
Author

uier commented Dec 12, 2022

Hi @mberr

Thanks for fixing the bug and providing a patch. I tested it and it works for me too.

Thanks for your hard work!

@Karimsaber1
Copy link

@mberr
when I make the same to load the saved train tripletfactory I get this error. I preferred to write it here as it might be related.

Traceback (most recent call last):
  File "/home/bio/groupshare/kshalaby/drug_repurposing/pykeen/NP_RotatE/NP_RotatE_Prediction_wwl_ns.py", line 60, in <module>
    pred = predict_target(
           ^^^^^^^^^^^^^^^
  File "/home/bio/groupshare/kshalaby/drep/lib/python3.11/site-packages/torch/utils/_contextlib.py", line 115, in decorate_context
    return func(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^
  File "/home/bio/groupshare/kshalaby/drep/lib/python3.11/site-packages/pykeen/predict.py", line 1077, in predict_target
    target, batch, other_col_ids = _get_input_batch(factory=triples_factory, head=head, relation=relation, tail=tail)
                                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/bio/groupshare/kshalaby/drep/lib/python3.11/site-packages/pykeen/predict.py", line 563, in _get_input_batch
    head = factory.entity_to_id[head]
           ^^^^^^^^^^^^^^^^^^^^
AttributeError: 'CoreTriplesFactory' object has no attribute 'entity_to_id'. Did you mean: 'entities_to_ids'?

my code:

tf = CoreTriplesFactory.from_path_binary(
    "./NP_RotatE_model_results_wwl_ns/training_triples"
)
my_model = torch.load("./NP_RotatE_model_results_wwl_ns/trained_model.pkl")
pred = predict_target(
        model=my_model, head=str(head), tail=str(tail), triples_factory=tf
    )

@mberr
Copy link
Member

mberr commented Aug 24, 2023

@Karimsaber1 , would you mind sharing on which PyKEEN version your code runs (and with which version you wrote the training factory into a file)? You can get this via

python -m pykeen version 

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants