PyTorch Geometric: Fixing Dimension Mismatch in Node Feature Loading
The error message suggests that there is a dimension mismatch in the line 'node_features[j] = image' inside the 'load_node_features' function.
The error occurs because you are trying to assign a 3-dimensional tensor 'image' to a 2-dimensional tensor 'node_features[j]'. This happens because the 'image' tensor is of shape [4, 40, 40] (assuming that the image size is 40) and the 'node_features[j]' tensor is of shape [40, 40] (assuming that the image size is 40).
To fix this, you need to make sure that the 'node_features' tensor has the appropriate shape to accommodate the 'image' tensor. One way to do this is to initialize 'node_features' with an additional dimension for the number of channels in the image (which is 1 in this case). Here's an updated version of the 'load_node_features' function:
def load_node_features(num_graphs, num_nodes, image_size):
all_node_features = torch.empty(num_graphs, num_nodes, 1, image_size, image_size, dtype=torch.float)
for i in range(num_graphs):
node_features = torch.empty(num_nodes, 1, image_size, image_size, dtype=torch.float)
for j in range(num_nodes):
file_path = f'C:\Users\jh\Desktop\data\input\images\{i+1}.png_{j}.png'
# Load image and convert to tensor
image = PIL.Image.open(file_path)
image = transforms.ToTensor()(image)
node_features[j] = image
all_node_features[i] = node_features
return all_node_features
In this version, 'node_features' is initialized as a 4-dimensional tensor with shape [num_nodes, 1, image_size, image_size]. This allows you to assign the 3-dimensional 'image' tensor to 'node_features[j]' correctly.
原文地址: https://www.cveoy.top/t/topic/pk7i 著作权归作者所有。请勿转载和采集!