visdom 绘制神经网络的曲线图 TypeError: a float is required

问题

Traceback (most recent call last):
  File "E:/wj-lab/expStad/train.py", line 441, in 
    
  File "E:\wj-lab\expStad\plot.py", line 38, in plot
    'title': axis_name})

  File "D:\Anaconda3\envs\python35\lib\site-packages\visdom\__init__.py", line 288, in wrapped_f
    return f(*args, **kwargs)
  File "D:\Anaconda3\envs\python35\lib\site-packages\visdom\__init__.py", line 1207, in line
    update=update, name=name)

  File "D:\Anaconda3\envs\python35\lib\site-packages\visdom\__init__.py", line 288, in wrapped_f
    return f(*args, **kwargs)
  File "D:\Anaconda3\envs\python35\lib\site-packages\visdom\__init__.py", line 1087, in scatter
    'x': nan2none(X.take(0, 1)[ind].tolist()),
  File "D:\Anaconda3\envs\python35\lib\site-packages\visdom\__init__.py", line 82, in nan2none
    if math.isnan(val):

TypeError: a float is required

出错源码

from visdom import Visdom
import numpy as np
   
 viz = Visdom(env='expStad')   
           
 for axis_name, value in self.name.items():                     
# 若传入新参数,不在窗口中,则在visdom 环境 exp 创建画板
    if axis_name not in loss_windows: 
           loss_windows[axis_name]=viz.line(
                  X=np.array([epoch]),
                  Y=np.array([value]),
                  opts={'xlabel': 'epochs', 'ylabel': axis_name,'title': axis_name}
               )
# 传入参数 在画板窗口中 在原曲线图上追加数据
    else:
          viz.line(X=np.array([epoch]), 
                   Y=np.array([value]),
                   win=loss_windows[axis_name], update='append')
    # 初始化
    self.name[axis_name] = 0.0


输入 》》》 {'train_loss_Avg': 5.274}, {'epoch': 1}

原因

传入 dict 型,使用时应取数值部分,而不是整体

解决办法

# 添加程序,取到 dict 的数值部分

for epoch_name,val in self.epoch.items():
       epo=val
       
for i, axis_name in enumerate(name.keys()):
       if axis_name not in name:
             name[axis_name] = name[axis_name]
       else:      
             name[axis_name] += name[axis_name]   

for axis_name, value in self.name.items():                     
    if axis_name not in loss_windows: 
           loss_windows[axis_name]=viz.line(
                  X=np.array([epo]), # 修改处
                  Y=np.array([value]),
                  opts={'xlabel': 'epochs', 'ylabel': axis_name,'title': axis_name}
               )
    else:
          viz.line(X=np.array([epo]), # 修改处
                   Y=np.array([value]),
                   win=loss_windows[axis_name], update='append')
    self.name[axis_name] = 0.0

 

你可能感兴趣的:(bug)