Problem in concatenating edges of graphs in PyTorch

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:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<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

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<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:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<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]))

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật