I am training GCN to predict link. for tensor data I have taken similarities generated between every pairs of nodes of a graph. due to size mismatch this error showing i have also tried other solution also but showing same error.
my code is this
import torch
import torch.nn as nn
import torch.optim as optim
class GCN(nn.Module):
def __init__(self, num_entities, num_features, hidden_size):
super(GCN, self).__init__()
self.embedding = nn.Embedding(num_entities, num_features)
self.gcn_layer = nn.Linear(num_features, hidden_size)
self.activation = nn.ReLU()
def forward(self, adjacency_matrix):
embedded = self.embedding.weight
gcn_output = self.gcn_layer(adjacency_matrix)
gcn_output = self.activation(gcn_output)
return gcn_output
# Define parameters
num_entities = len(entities)
num_features = num_entities # Set num_features to the same value as num_entities
hidden_size = 20
learning_rate = 0.01
num_epochs = 100
# Initialize GCN model
gcn_model = GCN(num_entities, num_features, hidden_size)
# Define optimizer and loss function
optimizer = optim.Adam(gcn_model.parameters(), lr=learning_rate)
criterion = nn.MSELoss()
# Convert similarity dictionaries to tensors
train_similarity_tensor = torch.Tensor([[train_similarities.get(entity1,
{}).get(entity2, 0.0) for entity2 in entities] for entity1 in entities])
test_similarity_tensor = torch.Tensor([[test_similarities.get(entity1, {}).get(entity2,
0.0) for entity2 in entities] for entity1 in entities])
# Resize similarity tensors to match adjacency matrix dimensions
train_similarity_tensor = train_similarity_tensor.unsqueeze(0).repeat(num_features, 1,
1)
test_similarity_tensor = test_similarity_tensor.unsqueeze(0).repeat(num_features, 1, 1)
# Training loop
for epoch in range(num_epochs):
# Forward pass
train_output = gcn_model(train_similarity_tensor)
# Compute loss
loss = criterion(train_output, train_similarity_tensor)
# Backward pass and optimization
optimizer.zero_grad()
loss.backward()
optimizer.step()
# Print loss for each epoch
print(f"Epoch {epoch+1}/{num_epochs}, Loss: {loss.item()}")
# Compute output for test set
test_output = gcn_model(test_similarity_tensor)
# Intelligence Computation
# Find the most similar entities to each entity in the train set
for i, entity in enumerate(entities):
_, indices = torch.topk(train_output[i], k=5)
similar_entities_train = [entities[j] for j in indices]
print(f"Most similar entities to '{entity}' in the train set:")
for similar_entity in similar_entities_train:
print(similar_entity)
print()
# Find the most similar entities to each entity in the test set
for i, entity in enumerate(entities):
_, indices = torch.topk(test_output[i], k=5)
similar_entities_test = [entities[j] for j in indices]
print(f"Most similar entities to '{entity}' in the test set:")
for similar_entity in similar_entities_test:
print(similar_entity)
print()
this part of code showing error
# Compute loss
loss = criterion(train_output, train_similarity_tensor)
this error showing
RuntimeError Traceback (most recent call last)
<ipython-input-10-0a3de1c57857> in <cell line: 41>()
44
45 # Compute loss
---> 46 loss = criterion(train_output, train_similarity_tensor)
47
48 # Backward pass and optimization
3 frames
/usr/local/lib/python3.10/dist-packages/torch/functional.py in
broadcast_tensors(*tensors)
72 if has_torch_function(tensors):
73 return handle_torch_function(broadcast_tensors, tensors, *tensors)
---> 74 return _VF.broadcast_tensors(tensors) # type: ignore[attr-defined]
75
76
RuntimeError: The size of tensor a (20) must match the size of tensor b (10) at non-
singleton dimension 2
this error very irritating same error ii face earlier in mnist datset can some one suggest what should be learn to tackle these error or avoid them
As per the documentation of nn.MSELoss, the input and target of that function must have the same shape. However, train_output
has last dimension 20 and train_similarity_tensor
has last dimension 10.
I'm not entirely sure what you're trying to achieve (I think you're trying to build an autoencoder) or indeed why you call this a GCN (I think it's just a feed forward neural network on the node properties). If you change the hidden_size
to 10, I think your NN will just learn to pass through the input unchanged, except you will never be able to achieve negative numbers in the ouput because of the activation function.