pyg-team / pytorch_geometric

Graph Neural Network Library for PyTorch
https://pyg.org
MIT License
21.37k stars 3.66k forks source link

TypeError: __inc__() takes 3 positional arguments but 4 were given +torch_geometric/data/collate.py #4358

Open WGDMS opened 2 years ago

WGDMS commented 2 years ago

😵 Describe the installation problem

Dear Team: I received the following error. Hope that it is a version problem.

---> 19 run_classification(train_dataset, val_dataset, test_dataset, model, num_tasks, epochs, batch_size, vt_batch_size, lr, lr_decay_factor, lr_decay_step_size, weight_decay, early_stopping, loss, metric, log_dir, save_dir,True)

10 frames /usr/local/lib/python3.7/dist-packages/torch_geometric/data/collate.py in (.0) 223 repeats = [ 224 data.inc(key, value, store) --> 225 for value, data, store in zip(values, data_list, stores) 226 ] 227 if isinstance(repeats[0], Tensor):

TypeError: inc() takes 3 positional arguments but 4 were given

Please help me to solve this.

Environment

rusty1s commented 2 years ago

Which could do you try to run? With PyG>=2.0, there was a change in the expected input arguments of data.__inc__ and data.__cat_dim__` (which now expect four arguments rather than three). This is documented here. You can easily fix this by adjusting the function headers to:

def __cat_dim__(self, key, value, *args, **kwargs):
    pass

def __inc__(self, key, value, *args, **kwargs):
    pass
Abbas009 commented 2 years ago

I am using pyg>=2.0.5 torch =1.10.0 Python = 3.7.11 OS: Linux And I am still getting this error. Pair data is what data contains and i have created sub_g as subgraph from whole graph. PairData(x=[200, 52], y=[1], pos=[200, 2], sub_g=[16, 50, 52], edge_index=[2, 1335], patch_idx=[1]) PairData(x=[296, 52], y=[1], pos=[296, 2], sub_g=[16, 50, 52], edge_index=[2, 2138], patch_idx=[1]) PairData(x=[279, 52], y=[1], pos=[279, 2], sub_g=[16, 50, 52], edge_index=[2, 2021], patch_idx=[1]) Traceback (most recent call last): File "train.py", line 491, in main() File "train.py", line 483, in main cell_graph(prog_args, writer=writer) File "train.py", line 364, in cell_graph writer=writer) File "train.py", line 243, in train for idx, data in enumerate(dataset): File "/compuworks/anaconda3/envs/tandoan2/lib/python3.7/site-packages/torch/utils/data/dataloader.py", line 521, in next data = self._next_data() File "/compuworks/anaconda3/envs/tandoan2/lib/python3.7/site-packages/torch/utils/data/dataloader.py", line 561, in _next_data data = self._dataset_fetcher.fetch(index) # may raise StopIteration File "/compuworks/anaconda3/envs/tandoan2/lib/python3.7/site-packages/torch/utils/data/_utils/fetch.py", line 52, in fetch return self.collate_fn(data) File "/compuworks/anaconda3/envs/tandoan2/lib/python3.7/site-packages/torch_geometric/loader/dataloader.py", line 20, in call self.exclude_keys) File "/compuworks/anaconda3/envs/tandoan2/lib/python3.7/site-packages/torch_geometric/data/batch.py", line 74, in from_data_list exclude_keys=exclude_keys, File "/compuworks/anaconda3/envs/tandoan2/lib/python3.7/site-packages/torch_geometric/data/collate.py", line 85, in collate increment) File "/compuworks/anaconda3/envs/tandoan2/lib/python3.7/site-packages/torch_geometric/data/collate.py", line 128, in _collate cat_dim = data_list[0].cat_dim(key, elem, stores[0]) TypeError: cat_dim() takes 3 positional arguments but 4 were given

I have checked def __cat_dim and def inc__ as similar in the newer version as you have described above but still getting this error. Any solution?

rusty1s commented 2 years ago

Did you apply the above suggestion?

def __cat_dim__(self, key, value, *args, **kwargs):
    pass

def __inc__(self, key, value, *args, **kwargs):
    pass
Abbas009 commented 2 years ago

Yes, In the newer version it already as it is mentioned by you. What other reason could be for this error, is this related to the version or related to the dataset?

rusty1s commented 2 years ago

I'm not sure which code you are running. But if you changed it as it is mentioned above, you may need to ensure to re-install the package such that the modifications are taken into account.

yuxinl915 commented 2 years ago

I'm not sure which code you are running. But if you changed it as it is mentioned above, you may need to ensure to re-install the package such that the modifications are taken into account.

Hi! Thanks so much for your help! But could please provide more details on how to re-install the package?

rusty1s commented 2 years ago

Do you see the same error? Does adjusting the __cat_dim__ and __inc__ properties as mentioned here not help? You can re-install the package via

pip install --upgrade torch-geometric
yuxinl915 commented 2 years ago

Do you see the same error? Does adjusting the __cat_dim__ and __inc__ properties as mentioned here not help? You can re-install the package via

pip install --upgrade torch-geometric

Thanks so much for your reply! But if do so, new problems occur shown as below. This problem is not accidental that I encountered it every time I did the operation you mentioned when dealing with several different projects. Could you please give me some tips about that? Really appreciate it!

env/lib/python3.8/site-packages/torch_geometric/data/separate.py", line 65, in _separate value = value.narrow(cat_dim or 0, start, end - start) IndexError: Dimension out of range (expected to be in range of [-2, 1], but got 43204744)

rusty1s commented 2 years ago

Is it possible for you to share some of the data objects with a small reproducible script? I am sure the error is easy to solve.

yuxinl915 commented 2 years ago

@rusty1s Thanks so much for your reply! Could you please provide me with an email, so that I can share some files?

rusty1s commented 2 years ago

At best, you can share the link here so others can chime in as well. Alternatively, you can find my mail on my github profile.

yuxinl915 commented 2 years ago

data.txt This is my current data.py file from env/lib/python3.8/site-packages/torch_geometric/data/data.py. I modified the original code from lines 502 - 516.

rusty1s commented 2 years ago

Oh, it looks like there is some mis-understanding. You do not need to modify the PyG source code (which will in fact result in breaking PyG) - instead, there seems to exist a data object in your code that inherits from torch_geometric.data.Data and which re-implements the __cat_dim__ and __inc__ interface. Simply changing these functions to the new function headers

def __cat_dim__(self, key, value, *args, **kwargs):
def __inc__(self, key, value, *args, **kwargs):

should resolve this.

yuxinl915 commented 2 years ago

Cool! I will try it out! Thanks so much!!!

On Wed, Sep 7, 2022 at 1:48 AM Matthias Fey @.***> wrote:

Oh, it looks like there is some mis-understanding. You do not need to modify the PyG source code (and will in fact result in breaking PyG) - instead, there seems to be a data object in your code that inherits from torch_geometric.data.Data and which re-implements the __cat_dim and inc__ interface. Simply changing these functions to the new function headers

def __cat_dim(self, key, value, *args, **kwargs):def inc__(self, key, value, *args, **kwargs):

should resolve this.

— Reply to this email directly, view it on GitHub https://github.com/pyg-team/pytorch_geometric/issues/4358#issuecomment-1238934670, or unsubscribe https://github.com/notifications/unsubscribe-auth/ARSRTYYBIR3BIMDDR6FT3JDV5AUEBANCNFSM5RYUEXWQ . You are receiving this because you commented.Message ID: @.***>

sirlddf commented 1 year ago

In PyG version 2.3, the solution mentioned before doesn't work. However, I solve the problem by arrange the input of the function inc in the collate.py file.

The relative of the function I modified is as follows: anaconda3/envs/${your env name}/lib/python3.9/site-pacakages/torch_geometric/data/collate.py/get_incs

def get_incs(key, values: List[Any], data_list: List[BaseData]), stores: List[BaseStorage]) -> Tensor: """

original version which will cause TypeError...

repeats = [
    data.__inc__(key, value, store) 
    for value, data, store in zip(values, data_list, stores)
]
"""
### modified version, while may cause other problems we do not expect currently
repeats = [
    data.__inc__(key, value) 
    for value, data, store in zip(values, data_list, stores)
]
...
return cumsum(repeats[:-1])

my environment details: python=3.9 torch=1.13.1 + cu116 torch-geometric=2.3

Hope this can help you

zaplm commented 1 year ago

Thank you @sirlddf, this problem has been bothering me for a long time, and I finally managed to solve it by using your method.

Jctrp commented 1 year ago

In PyG version 2.3, the solution mentioned before doesn't work. However, I solve the problem by arrange the input of the function inc in the collate.py file.

The relative of the function I modified is as follows: anaconda3/envs/${your env name}/lib/python3.9/site-pacakages/torch_geometric/data/collate.py/get_incs

def get_incs(key, values: List[Any], data_list: List[BaseData]), stores: List[BaseStorage]) -> Tensor: """ ### original version which will cause TypeError... repeats = [ data.inc(key, value, store) for value, data, store in zip(values, data_list, stores) ] """ ### modified version, while may cause other problems we do not expect currently repeats = [ data.inc(key, value) for value, data, store in zip(values, data_list, stores) ] ... return cumsum(repeats[:-1])

my environment details: python=3.9 torch=1.13.1 + cu116 torch-geometric=2.3

Hope this can help you

It works, thank you.

rankupup commented 6 months ago

In PyG version 2.3, the solution mentioned before doesn't work. However, I solve the problem by arrange the input of the function inc in the collate.py file.

The relative of the function I modified is as follows: anaconda3/envs/${your env name}/lib/python3.9/site-pacakages/torch_geometric/data/collate.py/get_incs

def get_incs(key, values: List[Any], data_list: List[BaseData]), stores: List[BaseStorage]) -> Tensor: """ ### original version which will cause TypeError... repeats = [ data.inc(key, value, store) for value, data, store in zip(values, data_list, stores) ] """ ### modified version, while may cause other problems we do not expect currently repeats = [ data.inc(key, value) for value, data, store in zip(values, data_list, stores) ] ... return cumsum(repeats[:-1])

my environment details: python=3.9 torch=1.13.1 + cu116 torch-geometric=2.3

Hope this can help you

It works!!!Thank you very much!!

Jingbo-gao commented 1 week ago

In PyG version 2.3, the solution mentioned before doesn't work. However, I solve the problem by arrange the input of the function inc in the collate.py file.

The relative of the function I modified is as follows: anaconda3/envs/${your env name}/lib/python3.9/site-pacakages/torch_geometric/data/collate.py/get_incs

def get_incs(key, values: List[Any], data_list: List[BaseData]), stores: List[BaseStorage]) -> Tensor: """ ### original version which will cause TypeError... repeats = [ data.inc(key, value, store) for value, data, store in zip(values, data_list, stores) ] """ ### modified version, while may cause other problems we do not expect currently repeats = [ data.inc(key, value) for value, data, store in zip(values, data_list, stores) ] ... return cumsum(repeats[:-1])

my environment details: python=3.9 torch=1.13.1 + cu116 torch-geometric=2.3

Hope this can help you

I solved the problem sucessfully, thx!!!