[问题笔记]PyTorch使用tensorboard报错‘Tensor‘ object has no attribute ‘items‘

for tag, scalar_value in tag_scalar_dict.items():
AttributeError: ‘Tensor’ object has no attribute ‘items’
makefile:12: recipe for target ‘train’ failed
make: *** [train] Error 1

问题出现原因:

误将 writer.add_scalar写成 writer.add_scalars,这是两个不同的函数

    def add_scalars(self, main_tag, tag_scalar_dict, global_step=None, walltime=None):
        """Adds many scalar data to summary.

        Args:
            main_tag (string): The parent name for the tags
            tag_scalar_dict (dict): Key-value pair storing the tag and corresponding values
            global_step (int): Global step value to record
            walltime (float): Optional override default walltime (time.time())
              seconds after epoch of event

add_scalars第二个参数传入的需要是dict

解决方案:
根据实际需求,修改为 writer.add_scalar或将数据合并为字典

猜你喜欢

转载自blog.csdn.net/ftimes/article/details/120995760