Fixing IndexError in Graph Neural Network: Reshaping Masks for Compatible Indexing
The error 'IndexError: The shape of the mask [42, 37] at index 0 does not match the shape of the indexed tensor [1554, 800] at index 0' arises due to an incompatibility in the shape of the mask and the tensor being indexed. This often occurs when using masks to split data into training and validation sets in a GNN model. The provided code snippet highlights the issue where the mask has a shape of (42, 37), while the indexed tensor 'data.x' has a shape of (1554, 800). To fix this, we need to reshape the mask tensors to match the dimensions of the data.x tensor. This can be achieved by using the 'view()' method in PyTorch, which allows us to rearrange the tensor's dimensions without changing the underlying data. The updated code snippet demonstrates the solution: ```python
Reshape the mask tensors
mask_train = mask_train.view(-1) mask_val = mask_val.view(-1)
Create train_data and val_data objects
train_data = Data(x=data.x[mask_train], edge_index=data.edge_index) val_data = Data(x=data.x[mask_val], edge_index=data.edge_index)
原文地址: https://www.cveoy.top/t/topic/pfTY 著作权归作者所有。请勿转载和采集!