I have tried to use tensorboard pytorch to plot loss graphs and accuracy. I have used Summary Writer here.
But, as I run my training loop, I face the following error at the end of first epoch.
Following snippet shows the error:
Following is the python code containing summary writer for reference:
loss_fn = torch.nn.BCELoss()
lr = 1e-3
optimizer = optim.SGD(model.parameters(), lr=lr)
scheduler = lr_scheduler.StepLR(optimizer, 8, gamma=0.1, last_epoch=-1)
n_epochs = 20
log_interval = 1
writer = SummaryWriter()
fit(train_loader, test_loader, model, loss_fn, optimizer, scheduler, n_epochs, cuda, log_interval, [metrics.Binary_accuracy()], writer)
writer.close()
def fit(train_loader, val_loader, model, loss_fn, optimizer, scheduler, n_epochs, cuda, log_interval, metrics, writer,
start_epoch=0, message=None) -> object:
for epoch in range(0, start_epoch):
scheduler.step()
for epoch in range(start_epoch, n_epochs):
scheduler.step()
# Train stage
train_loss, metrics = train_epoch(train_loader, model, loss_fn, optimizer, cuda, log_interval, metrics, writer)
#log_data['train_loss'].append(train_loss)
writer.add_scalar('loss/train',train_loss,epoch)
message = 'Epoch: {}/{}. Train set: Average loss: {:.4f}'.format(epoch + 1, n_epochs, train_loss)
for metric in metrics:
message += '\t{}: {}'.format(metric.name(), metric.value())
writer.add_scalar(f'{metric.name()}/train', metric.value(), epoch)
val_loss, metrics = test_epoch(val_loader, model, loss_fn, cuda, metrics, log_interval)
val_loss /= len(val_loader)
writer.add_scalar(f'loss/test', val_loss, epoch)
message += '\nEpoch: {}/{}. Test set: Average loss: {:.4f}'.format(epoch + 1, n_epochs,
val_loss)
for metric in metrics:
message += '\t{}: {}'.format(metric.name(), metric.value())
writer.add_scalar(f'{metric.name()}/test', metric.value(), epoch)
print(message)
writer.flush()
I also want to plot confusion matrix in tensorboard, please can I have some insights in this as well.
Any help is most appreciated! Thank you in advance