pytorch_geometric icon indicating copy to clipboard operation
pytorch_geometric copied to clipboard

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

Open WGDMS opened this issue 2 years ago • 18 comments

😵 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

  • PyG version: 2.0.4
  • PyTorch version:1.10.0
  • OS: Windows
  • Python version: 3.7.13
  • CUDA/cuDNN version:11.1
  • How you installed PyTorch and PyG (conda, pip, source): !pip install torch torch-scatter torch-sparse torch-cluster torch-spline-conv torchaudio torch-geometric -f https://data.pyg.org/whl/torch-1.10.0+cu111.html
  • Any other relevant information (e.g., version of torch-scatter):

WGDMS avatar Mar 27 '22 11:03 WGDMS

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

rusty1s avatar Mar 28 '22 07:03 rusty1s

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?

Abbas009 avatar Apr 28 '22 07:04 Abbas009

Did you apply the above suggestion?

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

rusty1s avatar Apr 28 '22 07:04 rusty1s

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?

Abbas009 avatar Apr 28 '22 07:04 Abbas009

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.

rusty1s avatar Apr 28 '22 11:04 rusty1s

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?

yuxinl915 avatar Aug 29 '22 03:08 yuxinl915

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

rusty1s avatar Aug 29 '22 09:08 rusty1s

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)

yuxinl915 avatar Sep 03 '22 17:09 yuxinl915

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.

rusty1s avatar Sep 05 '22 06:09 rusty1s

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

yuxinl915 avatar Sep 05 '22 14:09 yuxinl915

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.

rusty1s avatar Sep 06 '22 14:09 rusty1s

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.

yuxinl915 avatar Sep 06 '22 15:09 yuxinl915

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.

rusty1s avatar Sep 07 '22 05:09 rusty1s

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: @.***>

yuxinl915 avatar Sep 09 '22 14:09 yuxinl915

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

sirlddf avatar Apr 20 '23 02:04 sirlddf

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

zaplm avatar May 19 '23 15:05 zaplm

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.

Jctrp avatar Jun 06 '23 03:06 Jctrp

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!!

rankupup avatar Apr 22 '24 13:04 rankupup