lanpa/tensorboardX

AttributeError: 'NoneType' object has no attribute 'log_metric'

Zessay opened this issue · 13 comments

I have no idea about the following error, looking forward to someone's help, thank you! ~~

Traceback (most recent call last):
  File "disfluency_detection/pipelines/sentseg_train.py", line 427, in <module>
    main()
  File "disfluency_detection/pipelines/sentseg_train.py", line 394, in main
    metrics = trainer.evaluate()
  File "/home/xxx/anaconda3/envs/correction/lib/python3.7/site-packages/transformers/trainer.py", line 2044, in evaluate
    self.log(output.metrics)
  File "/home/xxx/anaconda3/envs/correction/lib/python3.7/site-packages/transformers/trainer.py", line 1709, in log
    self.control = self.callback_handler.on_log(self.args, self.state, self.control, logs)
  File "/home/xxx/anaconda3/envs/correction/lib/python3.7/site-packages/transformers/trainer_callback.py", line 371, in on_log
    return self.call_event("on_log", args, state, control, logs=logs)
  File "/home/xxx/anaconda3/envs/correction/lib/python3.7/site-packages/transformers/trainer_callback.py", line 388, in call_event
    **kwargs,
  File "/home/xxx/anaconda3/envs/correction/lib/python3.7/site-packages/transformers/integrations.py", line 354, in on_log
    self.tb_writer.add_scalar(k, v, state.global_step)
  File "/home/xxx/anaconda3/envs/correction/lib/python3.7/site-packages/tensorboardX/writer.py", line 453, in add_scalar
    self.comet_logger.log_metric(tag, display_name, scalar_value, global_step)
AttributeError: 'NoneType' object has no attribute 'log_metric'

I have found the solution to this problem in one transformers issue

lanpa commented

Thanks for the reporting, seems that it's caused by the new introduced feature. Let me reopen this issue for tracking.

The error still persists! The commit didn't fix it.

cdhx commented

same problem

@cdhx I found the error for my code - as I was using Jupyter notebook, I didn't realize I closed the writer and then tried to write again in another cell.

lanpa commented

@cdhx @CyanideBoy Hi, which version are you using?

I used with summary_writer:summary_writer.add_graph( ) before .add_scalar( )
After deleting with summary_writer: , my code could get correct output.

I encountered a similar problem: 'NoneType' object has no attribute 'log_metrics'.
The code works fine after deleting with writer: like grafaraway suggested.
My package version:
pytorch 1.8.2 py3.8_cpu_0 [cpuonly] pytorch-lts
tensorboardx 2.4 pyhd8ed1ab_0 conda-forge

lanpa commented

@mwck46 Can you provide a code snippet to reproduce the error with with...? Thank you.

@lanpa hope this help

import torch
import torch.nn as nn
import torch.utils.data as dset
from torchvision import datasets, transforms
from tensorboardX import SummaryWriter  

dataset_path = "./data"
log_path = "./log"
device = "cpu"

class ANNModel(nn.Module):
    def __init__(self):
        super(ANNModel, self).__init__()
        self.main = nn.Sequential(
            nn.Linear(in_features=28*28, out_features=128),
            nn.ReLU(),
            nn.Linear(in_features=128, out_features=10),
            nn.LogSoftmax(dim=1))
    def forward(self, input):
        return self.main(input)

def load_data(batch_size, transform):
    trainset = datasets.MNIST(root=dataset_path, download=True, train=True, transform=transform)
    testset = datasets.MNIST(root=dataset_path, download=True, train=False, transform=transform)
    trainloader = dset.DataLoader(trainset, batch_size=batch_size, shuffle=True)
    testloader = dset.DataLoader(testset, batch_size=batch_size, shuffle=False)
    return trainloader, testloader

def main():
    writer = SummaryWriter(log_path)
    
    transform = transforms.Compose([transforms.ToTensor(),])
    trainloader, testloader = load_data(batch_size = 64, transform=transform)
    model = ANNModel().to(device)
    train_data_sample, _ = iter(trainloader).next()
    
    with writer:
        train_data_sample = train_data_sample.view(train_data_sample.shape[0], -1)
        writer.add_graph(model, train_data_sample.to(device))  # model graph, with input

    epochs = 3
    for epoch in range(epochs):
        epoch_loss = 0.1
        
        writer.add_scalars("Loss/Epoch", {"train": epoch_loss}, epoch)
        print("epoch[%02d/%02d] | training loss: %.4f" % (epoch + 1, epochs, epoch_loss))

if __name__ == "__main__":
    main()

My conda env env.txt

lanpa commented

@mwck46 According to your env.txt, maybe you are using the previous tensorboardX (v2.4) from conda-forge
Will pip install 'git+https://github.com/lanpa/tensorboardX' resolve your problem?

Hi everyone
I got the same error with tensorboard 2.4. someone could help me to solve it, please?
I appreciate your time

lanpa commented

@DrraBL Can you try whether pip install git+https://github.com/lanpa/tensorboardX helps?