waleedka / hiddenlayer

Neural network graphs and training metrics for PyTorch, Tensorflow, and Keras.
MIT License
1.79k stars 266 forks source link

Not displaying graph as expected in Jupyter Notebook (either during training or after training) #87

Closed jasperhyp closed 3 years ago

jasperhyp commented 3 years ago

I followed the tutorial and put the HL functions in my validation part, but the graph is not showing at all... Matplotlib is working and running the demo pytorch_train.ipynb has no problem.

I packed the plotting part in my evaluation function and there are other string outputs during training. Should I delete those string outputs?

Code snippet

def evaluate(loader):
    model.eval()

    predictions = []
    labels = []
    correct = 0
    loss = 0
    with torch.no_grad():
        for i, data in enumerate(loader,0):
            data = data.to(device)
            pred = model(data.x,data.batch).detach().cpu()
            label = data.y.detach().cpu()

            predictions.append(pred.numpy())
            labels.append(label.numpy())
            loss += loss_func(pred, label).item()

            pred = pred.numpy().squeeze()
            label = label.numpy().squeeze()        
            correct += (abs(pred-label)<0.5).sum()

            global_iter_num = epoch * len(loader) + i + 1
            print(global_iter_num)
            if (global_iter_num % log_step_interval == 0):
                print("o")

                history.log((epoch, i),
                            val_loss=loss/(i+1),
                            val_acc=correct/(i+1),
                            hidden_weight1=model.conv1.weight,
                            hidden_weight2=model.conv2.weight
                           )
                with canvas:
                    canvas.draw_plot(history["val_loss"])
                    canvas.draw_plot(history["val_acc"])
                    canvas.draw_image(history["hidden_weight1"])
                    canvas.draw_image(history["hidden_weight2"])
                time.sleep(0.1)
        return predictions, labels, loss/len(loader.dataset), correct/len(loader.dataset)

history = hl.History()
canvas = hl.Canvas()
log_step_interval = 20

device = torch.device('cuda')
model = GCN().to(device)
optimizer = torch.optim.Adam(model.parameters(), lr=learning_rate, weight_decay = l2_reg)
loss_func = torch.nn.BCELoss()  # binary cross-entropy
train_loader = DataLoader(train_dataset, batch_size=batch_size, drop_last = True)
val_loader = DataLoader(val_dataset, batch_size=batch_size)
for epoch in range(num_epochs):
    gc.collect()
    train_loss, train_acc = train()
    _, _, val_loss, val_acc = evaluate(val_loader)
    print('Epoch: {:03d}, Train Loss: {:.5f}, Train Acc: {:.5f}, Val Loss: {:.5f}, Val Acc: {:.5f}'.
          format(epoch, train_loss, train_acc, val_loss, val_acc))

Typical output:

23
Epoch: 000, Train Loss: 0.02185, Train Acc: 0.52832, Val Loss: 0.02351, Val Acc: 0.51029
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
o
/public/workspace/z/miniconda3/envs/ST-Torch/lib/python3.7/site-packages/torch/nn/modules/loss.py:498: UserWarning: Using a target size (torch.Size([32])) that is different to the input size (torch.Size([32, 1])) is deprecated. Please ensure they have the same size.
  return F.binary_cross_entropy(input, target, weight=self.weight, reduction=self.reduction)
41
42
43
44
45
46
Epoch: 001, Train Loss: 0.02081, Train Acc: 0.58771, Val Loss: 0.02304, Val Acc: 0.53498
/public/workspace/z/miniconda3/envs/ST-Torch/lib/python3.7/site-packages/torch/nn/modules/loss.py:498: UserWarning: Using a target size (torch.Size([25])) that is different to the input size (torch.Size([25, 1])) is deprecated. Please ensure they have the same size.
  return F.binary_cross_entropy(input, target, weight=self.weight, reduction=self.reduction)
47
48
49
50
51
52
53
54
55
56
57
58
59
60
o
jasperhyp commented 3 years ago

Aaaaaaaaah... Somewhere within my code I have a matplotlib.use('Agg') and I do not even remember I wrote this... Silly mistake. Solved.