Raising "list index out of range" error in torch_geometric/data/collate.py
See original GitHub issueš Bug
When I used torch_geometric.data.Data as data structure to build an InMemoryDataset, the following error raised in the collate function.
File ".../torch_geometric/data/collate.py", line 184, in _collate
and isinstance(elem[0], (Tensor, SparseTensor))):
IndexError: list index out of range
This error also ocurred in
File ".../torch_geometric/data/separate.py", line 92, in _separate
and isinstance(value[0][0], (Tensor, SparseTensor))):
IndexError: list index out of range
To Reproduce
import torch
from torch_geometric.data import InMemoryDataset, download_url
from torch_geometric.data import Data
class MyOwnDataset(InMemoryDataset):
def __init__(self, root, transform=None, pre_transform=None):
super().__init__(root, transform, pre_transform)
self.data, self.slices = torch.load(self.processed_paths[0])
@property
def raw_file_names(self):
return ['some_file_1', 'some_file_2', ...]
@property
def processed_file_names(self):
return ['data.pt']
def download(self):
# Download to `self.raw_dir`.
download_url(url, self.raw_dir)
def process(self):
# Read data into huge `Data` list.
data_list = []
graph_input = Data(
x=torch.from_numpy(x).float(),
y=torch.from_numpy(y).float(),
cluster=torch.from_numpy(cluster).short(),
edge_index=torch.from_numpy(edge_index).long(),
identifier=torch.from_numpy(identifier).float(),
traj_len=torch.tensor([traj_lens[ind]]).int(),
valid_len=torch.tensor([valid_lens[ind]]).int(),
time_step_len=torch.tensor([num_valid_len_max]).int(),
candidate_len_max=torch.tensor([num_candidate_max]).int(),
candidate_mask=[],
candidate=torch.from_numpy(raw_data['tar_candts'].values[0]).float(),
candidate_gt=torch.from_numpy(raw_data['gt_candts'].values[0]).bool(),
offset_gt=torch.from_numpy(raw_data['gt_tar_offset'].values[0]).float(),
target_gt=torch.from_numpy(raw_data['gt_preds'].values[0][0][-1, :]).float(),
)
data_list.append(graph_input)
data, slices = self.collate(data_list)
torch.save((data, slices), self.processed_paths[0])
Expected behavior
No error raised.
Environment
- PyG version: torch_geometric.2.0.4
- PyTorch version: torch.1.10.1
- OS: Ubuntu16.04
- Python version: 3.9
- CUDA/cuDNN version: cuda11.3
- How you installed PyTorch and PyG: pip
- Any other relevant information: torch-scatter.2.0.9
Additional context
Issue Analytics
- State:
- Created 2 years ago
- Reactions:1
- Comments:7 (4 by maintainers)
Top Results From Across the Web
List Index Out of Range ā Python Error Message Solved
You'll get the Indexerror: list index out of range error when iterating through a list and trying to access an item that doesn't...
Read more >How to Fix IndexError in Python - Rollbar
The IndexError in Python occurs when an item from a list is attempted to be accessed that is outside the index range of...
Read more >Python indexerror: list index out of range Solution
They're raised when you try to access an index value inside a Python list that does not exist. In most cases, index errors...
Read more >Does "IndexError: list index out of range" when trying to ...
If you have a list with 53 items, the last one is thelist[52] because indexing starts at 0. From Real Python: Understanding the...
Read more >Indexerror: list Index Out of Range in Python - STechies
The only way to avoid this error is to mention the indexes of list elements properly. Example: # Declaring list list_fruits = ['apple',...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I think Iāve found whatās causing the issue with
collate
. I had an empty, non-tensor list as one of the data stores of the entries of my customDataset
. I can see that in @McDifferenceās example, they also have an empty list:candidate_mask=[]
. Hereās a small example that generates the same error:This generates the following error:
I still need some empty lists in my dataset, so I was able to fix it by changing them from normal lists to empty tensors. I think the error is caused by the following in
_collate
:https://github.com/pyg-team/pytorch_geometric/blob/57c88c0c7b86fb38715b72f5914bb208c7aa11e7/torch_geometric/data/collate.py#L183-L184
When one of the data stores is an empty list, it is a
Sequence
. However, it doesnāt have any elements, soelem[0]
throws an index out of range error. The issue with_separate
might be similar, but I didnāt encounter it myself.Itās seems that your code is based on a lower version of torch-geometric. I just solved the same problem by uninstalling pyg(from conda) and reinstall torch-geometric=1.7.2 with pip.