我的theano计划中有一个导致NaN值的错误。该文档建议使用nanguardmode
来追踪问题的根源。
当我从doc网页复制/粘贴此行时:
from theano.compile.nanguardmode import NanGuardMode
我明白了:
ImportError: No module named nanguardmode
键入时找不到nanguardmode
的任何迹象:
help(theano.compile)
知道为什么nanguardmode
不存在?我该如何解决这个问题?
编辑:
感谢您的回复。
关于我的Theano版本,我找不到如何检查它。但我认为它是最新的:大约一个月前我在安装网页上安装了它。我在Windows 64bit上。
关于detect_nan hack:事情变得更加怪异了!
首先:如果我尝试使用:
post_func=theano.compile.monitormode.detect_nan
我明白了:
File "C:\SciSoft\WinPython-64bit-2.7.9.4\python-2.7.10.amd64\lib\site-packages\theano\compile\monitormode.py", line 87, in detect_nan
if (not isinstance(numpy.random.RandomState, output[0]) and
NameError: global name 'numpy' is not defined
确实,numpy没有在monitormode模块中导入......这是一个已知的错误吗?
第二:如果我尝试使用detect_nan的复制/粘贴,那么NaN会神奇地消失。其他一切保持不变,在我的theano函数中没有detect_nan(迭代地训练模型),我在迭代5得到NaN:
epoch 1, valid 28.582677 %, train 27.723320 % 0.546633
epoch 2, valid 27.814961 %, train 25.681751 % 0.500522
epoch 3, valid 27.263780 %, train 24.262972 % 0.478799
epoch 4, valid 26.938976 %, train 23.209021 % 0.463017
epoch 5, valid 50.000000 %, train 50.000000 % nan
(最后一个数字是成本价值)
当我添加
时mode=theano.compile.MonitorMode(post_func=detect_nan)
到函数,没有NaN出现至少迭代100(可能更多)。
epoch 1, valid 28.582677 %, train 27.723320 % 0.546633
epoch 2, valid 27.814961 %, train 25.681751 % 0.500522
epoch 3, valid 27.263780 %, train 24.262972 % 0.478799
epoch 4, valid 26.938976 %, train 23.209021 % 0.463017
epoch 5, valid 26.289370 %, train 22.320902 % 0.450454
... etc ...
这里发生了什么?
答案 0 :(得分:2)
NanGuardMode
被转移到Theano的前沿版本(来自PyLearn2)。这是在3月26日发布0.7版本之后,因此您需要从GitHub upgrade to the bleeding edge version使用NanGuardMode。
或者,您可以使用debug FAQ中的detect_nan
示例:
import numpy
import theano
# This is the current suggested detect_nan implementation to
# show you how it work. That way, you can modify it for your
# need. If you want exactly this method, you can use
# ``theano.compile.monitormode.detect_nan`` that will always
# contain the current suggested version.
def detect_nan(i, node, fn):
for output in fn.outputs:
if (not isinstance(output[0], numpy.random.RandomState) and
numpy.isnan(output[0]).any()):
print '*** NaN detected ***'
theano.printing.debugprint(node)
print 'Inputs : %s' % [input[0] for input in fn.inputs]
print 'Outputs: %s' % [output[0] for output in fn.outputs]
break
x = theano.tensor.dscalar('x')
f = theano.function([x], [theano.tensor.log(x) * x],
mode=theano.compile.MonitorMode(
post_func=detect_nan))