I’ve tried to create a simple graph neural network with pytorch geometric. However, I’m getting the same loss for every epoch while training. Here’s the code for the network:
train_loader = DataLoader(train_dataset, batch_size=batch_size)
val_loader = DataLoader(val_dataset, batch_size=batch_size)
test_loader = DataLoader(test_dataset, batch_size=batch_size)
## Building the Graph Neural Network
class Net(torch.nn.Module):
def __init__(self):
super(Net, self).__init__()
self.conv1 = GCNConv(dataset.num_node_features, 50)
self.conv2 = GCNConv(50, 1)
def forward(self, data):
x, edge_index = data.x, data.edge_index
x = F.relu(self.conv1(x, edge_index))
x = F.dropout(x, training=self.training)
x = self.conv2(x, edge_index)
return F.log_softmax(x, dim=1)
device = torch.device('cuda')
model = Net().double().to(device)
optimizer = torch.optim.Adam(model.parameters(), lr=0.1)
crit = torch.nn.BCELoss()
def train():
model.train()
loss_all = 0
for data in train_loader:
optimizer.zero_grad()
data = data.to(device)
output = model(data)
label = data.y.to(device)
loss = crit(output.double(), label.double())
loss.backward()
loss_all += data.num_graphs * loss.item()
optimizer.step()
return loss_all / len(train_dataset)
from sklearn.metrics import zero_one_loss
def evaluate(loader):
model.eval()
predictions = []
labels = []
with torch.no_grad():
for data in loader:
data = data.to(device)
pred = model(data).detach().cpu().numpy().astype('uint8').flatten()
label = data.y.detach().cpu().numpy().astype('uint8')
predictions.append(pred)
labels.append(label)
predictions, labels = np.concatenate(predictions), np.concatenate(labels)
# Calculate accuracy
acc = zero_one_loss(labels, predictions)
return acc
for epoch in range(1, 200):
loss = train()
train_acc = evaluate(train_loader)
val_acc = evaluate(val_loader)
test_acc = evaluate(test_loader)
print('Epoch: {:03d}, Loss: {:.5f}, Train Auc: {:.5f}, Val Auc: {:.5f}, Test Auc: {:.5f}'.
format(epoch, loss, train_acc, val_acc, test_acc))
Here’s the output:
Epoch: 001, Loss: 19391.92254, Train Auc: 0.54593, Val Auc: 0.53062, Test Auc: 0.56377
Epoch: 002, Loss: 19391.92254, Train Auc: 0.54593, Val Auc: 0.53062, Test Auc: 0.56377
Epoch: 003, Loss: 19391.92254, Train Auc: 0.54593, Val Auc: 0.53062, Test Auc: 0.56377
Epoch: 004, Loss: 19391.92254, Train Auc: 0.54593, Val Auc: 0.53062, Test Auc: 0.56377
Epoch: 005, Loss: 19391.92254, Train Auc: 0.54593, Val Auc: 0.53062, Test Auc: 0.56377
Epoch: 006, Loss: 19391.92254, Train Auc: 0.54593, Val Auc: 0.53062, Test Auc: 0.56377
Epoch: 007, Loss: 19391.92254, Train Auc: 0.54593, Val Auc: 0.53062, Test Auc: 0.56377
Epoch: 008, Loss: 19391.92254, Train Auc: 0.54593, Val Auc: 0.53062, Test Auc: 0.56377
Epoch: 009, Loss: 19391.92254, Train Auc: 0.54593, Val Auc: 0.53062, Test Auc: 0.56377
Epoch: 010, Loss: 19391.92254, Train Auc: 0.54593, Val Auc: 0.53062, Test Auc: 0.56377
Another thing that I’ve noticed is that I’m getting only zeros in the train function when calling output = model(data). Does this mean that I have issue with the data or with my neural network?
I appreciate the help