Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
Zessay opened this issue Aug 31, 2021 · 13 comments
Open

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

Zessay opened this issue Aug 31, 2021 · 13 comments

Comments

@Zessay
Copy link

Zessay commented Aug 31, 2021

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'
@Zessay
Copy link
Author

Zessay commented Aug 31, 2021

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

@Zessay Zessay closed this as completed Aug 31, 2021
@lanpa
Copy link
Owner

lanpa commented Aug 31, 2021

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

@lanpa lanpa reopened this Aug 31, 2021
lanpa added a commit that referenced this issue Sep 12, 2021
@lanpa lanpa closed this as completed in 054f1f3 Sep 12, 2021
@syomantak
Copy link

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

@cdhx
Copy link

cdhx commented Sep 29, 2021

same problem

@syomantak
Copy link

@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
Copy link
Owner

lanpa commented Oct 1, 2021

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

@grafaraway
Copy link

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

@mwck46
Copy link

mwck46 commented Nov 2, 2021

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
Copy link
Owner

lanpa commented Nov 9, 2021

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

@lanpa lanpa reopened this Nov 9, 2021
@mwck46
Copy link

mwck46 commented Nov 11, 2021

@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
Copy link
Owner

lanpa commented Nov 14, 2021

@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?

@DrraBL
Copy link

DrraBL commented Nov 16, 2021

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

@lanpa
Copy link
Owner

lanpa commented Nov 16, 2021

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

lanpa added a commit that referenced this issue Nov 20, 2021
lanpa added a commit that referenced this issue Dec 19, 2021
* prepare for release

* add test case to reproduce #639

* fix description
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants