I have a code like the following. I need to retrieve edges from multiple graphs that are saved in data_loader and doing something with the nodes of the graphs inside my method (while the edges will remained the same as before) and return them from the method to use as input to a deep learning. Here’s what I have tried to implement it:
<code>`def generate_node_embeddings(node_embeddings,data_loader):
edge_index = []
with torch.no_grad():
for data in data_loader:
edge_index.append(data.edge_index.cpu())
#Doing sth with node_embeddings
# Concatenate edge_index and edge_weight
edge_index = torch.cat(edge_index, dim=1) # Concatenate edge indices along columns
# Verify the concatenated edge_index shape
print('edge_index.shape after concatenation:', edge_index.shape)
return edge_index`
</code>
<code>`def generate_node_embeddings(node_embeddings,data_loader):
edge_index = []
with torch.no_grad():
for data in data_loader:
edge_index.append(data.edge_index.cpu())
#Doing sth with node_embeddings
# Concatenate edge_index and edge_weight
edge_index = torch.cat(edge_index, dim=1) # Concatenate edge indices along columns
# Verify the concatenated edge_index shape
print('edge_index.shape after concatenation:', edge_index.shape)
return edge_index`
</code>
`def generate_node_embeddings(node_embeddings,data_loader):
edge_index = []
with torch.no_grad():
for data in data_loader:
edge_index.append(data.edge_index.cpu())
#Doing sth with node_embeddings
# Concatenate edge_index and edge_weight
edge_index = torch.cat(edge_index, dim=1) # Concatenate edge indices along columns
# Verify the concatenated edge_index shape
print('edge_index.shape after concatenation:', edge_index.shape)
return edge_index`
here is where I am going to use those returned edges
<code>`g_edge_index = generate_node_embeddings(embedding_model, total_data_loader)
print('g_edge_index[0]:',g_edge_index[0])
for _,e1 in enumerate(g_edge_index):
# Ensure `e1` is in the correct shape for edge_index: [2, num_edges]
print('before:',e1)
edge_index = e1.t().contiguous()
print('after e1:',edge_index)
# Create Data object
data = Data(edge_index=e1.t().contiguous())
# Validate the Data object
data.validate(raise_on_error=True)`
</code>
<code>`g_edge_index = generate_node_embeddings(embedding_model, total_data_loader)
print('g_edge_index[0]:',g_edge_index[0])
for _,e1 in enumerate(g_edge_index):
# Ensure `e1` is in the correct shape for edge_index: [2, num_edges]
print('before:',e1)
edge_index = e1.t().contiguous()
print('after e1:',edge_index)
# Create Data object
data = Data(edge_index=e1.t().contiguous())
# Validate the Data object
data.validate(raise_on_error=True)`
</code>
`g_edge_index = generate_node_embeddings(embedding_model, total_data_loader)
print('g_edge_index[0]:',g_edge_index[0])
for _,e1 in enumerate(g_edge_index):
# Ensure `e1` is in the correct shape for edge_index: [2, num_edges]
print('before:',e1)
edge_index = e1.t().contiguous()
print('after e1:',edge_index)
# Create Data object
data = Data(edge_index=e1.t().contiguous())
# Validate the Data object
data.validate(raise_on_error=True)`
Here is what I see in th console after running the code:
<code>data.edge_index.shape: torch.Size([2, 3544])
data.edge_index.shape: torch.Size([2, 3448])
...
edge_index[0].shape: torch.Size([2, 3544])
edge_index[1].shape: torch.Size([2, 3448])
...
edge_index.shape after concatenation: torch.Size([2, 24084])
g_edge_index[0]: tensor([ 0, 0, 1, ..., 351, 352, 353])
before: tensor([ 0, 0, 1, ..., 351, 352, 353])
after: tensor([ 0, 0, 1, ..., 351, 352, 353])
Traceback (most recent call last):
File "/Users/Documents/conference-code/metric-learningGNN.py", line 1009, in <module>
data.validate(raise_on_error=True)
File "/Users/anaconda3/envs/GNN/lib/python3.12/site-packages/torch_geometric/data/data.py", line 676, in validate
warn_or_raise(
File "/Users/anaconda3/envs/GNN/lib/python3.12/site-packages/torch_geometric/data/data.py", line 1186, in warn_or_raise
raise ValueError(msg)
ValueError: 'edge_index' needs to be of shape [2, num_edges] in 'Data' (found torch.Size([24084]))
</code>
<code>data.edge_index.shape: torch.Size([2, 3544])
data.edge_index.shape: torch.Size([2, 3448])
...
edge_index[0].shape: torch.Size([2, 3544])
edge_index[1].shape: torch.Size([2, 3448])
...
edge_index.shape after concatenation: torch.Size([2, 24084])
g_edge_index[0]: tensor([ 0, 0, 1, ..., 351, 352, 353])
before: tensor([ 0, 0, 1, ..., 351, 352, 353])
after: tensor([ 0, 0, 1, ..., 351, 352, 353])
Traceback (most recent call last):
File "/Users/Documents/conference-code/metric-learningGNN.py", line 1009, in <module>
data.validate(raise_on_error=True)
File "/Users/anaconda3/envs/GNN/lib/python3.12/site-packages/torch_geometric/data/data.py", line 676, in validate
warn_or_raise(
File "/Users/anaconda3/envs/GNN/lib/python3.12/site-packages/torch_geometric/data/data.py", line 1186, in warn_or_raise
raise ValueError(msg)
ValueError: 'edge_index' needs to be of shape [2, num_edges] in 'Data' (found torch.Size([24084]))
</code>
data.edge_index.shape: torch.Size([2, 3544])
data.edge_index.shape: torch.Size([2, 3448])
...
edge_index[0].shape: torch.Size([2, 3544])
edge_index[1].shape: torch.Size([2, 3448])
...
edge_index.shape after concatenation: torch.Size([2, 24084])
g_edge_index[0]: tensor([ 0, 0, 1, ..., 351, 352, 353])
before: tensor([ 0, 0, 1, ..., 351, 352, 353])
after: tensor([ 0, 0, 1, ..., 351, 352, 353])
Traceback (most recent call last):
File "/Users/Documents/conference-code/metric-learningGNN.py", line 1009, in <module>
data.validate(raise_on_error=True)
File "/Users/anaconda3/envs/GNN/lib/python3.12/site-packages/torch_geometric/data/data.py", line 676, in validate
warn_or_raise(
File "/Users/anaconda3/envs/GNN/lib/python3.12/site-packages/torch_geometric/data/data.py", line 1186, in warn_or_raise
raise ValueError(msg)
ValueError: 'edge_index' needs to be of shape [2, num_edges] in 'Data' (found torch.Size([24084]))