当前位置:网站首页>Pytorch_ Geometric (pyg) uses dataloader to report an error runtimeerror: sizes of tenants must match except in dimension 0

Pytorch_ Geometric (pyg) uses dataloader to report an error runtimeerror: sizes of tenants must match except in dimension 0

2022-06-25 09:41:00 Deno_ V

Use Pytorch_Geometric(PyG) Time to build DataLoader, from DataLoader Take samples Batch Times wrong :RuntimeError: Sizes of tensors must match except in dimension 0.
 Insert picture description here

The error is caused by data alignment error ,1 individual batch Is a collection of multiple samples , An error occurred when the samples were spliced into a set , The law is as follows :

  • Use pytorch-geometric Of dataloader when ,batch Merge rules for each sample of
    • attribute edge_index Special rules , Each sample edge_index by 2 × e i 2\times e_i 2×ei, Then merge n Samples form a batch After that batch.edge_index The size is 2 × ( ∑ i = 1 n e i ) 2\times(\sum_{i=1}^n e_i) 2×(i=1nei)
    • If all other attributes are tensor, Expand according to the first dimension , For example, for attributes x x x, The first sample size is d 1 × d 2 d_1\times d_2 d1×d2, The second sample size is d 3 × d 2 d_3\times d_2 d3×d2, If there is one batch These two samples are included ,batch.x The size of will be ( d 3 + d 1 ) × d 2 (d_3+d_1)\times d_2 (d3+d1)×d2. Here is a huge pit , It requires that in addition to the first dimension , All other dimensions must be the same size !! Otherwise, an error will be reported RuntimeError: Sizes of tensors must match except in dimension 0.
    • Other properties if not tensor, It will return normally according to the list ,batch.x=[ sample 1 Of x, sample 2 Of x, sample 3 Of x]

How to solve :

  • If using torch tensor Caused by the , Think of ways to align other than the first dimension , Width of other dimensions .
  • If there is no way to align , Use non tensor Data type substitution , For example, a list of .
  • The last choice , Appoint batch_size=1 To avoid .dataloader=DataLoader(MyData,batch_size=1)
原网站

版权声明
本文为[Deno_ V]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/176/202206250913446431.html