2015-09-15 83 views
1

我有一個導致NaN值的theano程序錯誤。文檔建議使用nanguardmode來追蹤問題的根源。沒有名爲nanguardmode的模塊

當我複製/粘貼該行從DOC網頁:

from theano.compile.nanguardmode import NanGuardMode 

我得到:

ImportError: No module named nanguardmode 

當我輸入找不到nanguardmode任何跡象:

help(theano.compile) 

任何想法爲什麼nanguardmode缺席?我怎樣才能解決這個問題?

編輯:

感謝您的答覆。

關於我的Theano版本,我無法找到如何檢查它。但我認爲這是最新的:大約一個月前,我在安裝網頁上安裝了它。我在Windows 64位上。

關於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的未在監聽模式模塊進口...是一個已知的bug?

第二:如果我嘗試使用detect_nan的複製/粘貼,NaN會神奇地消失。一切保持不變,沒有detect_nan我theano功能(即反覆訓練模型),我在迭代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

你是什麼theano版本? –

+0

不知道發生了什麼,但重新啓動控制檯後,一切都恢復正常...... – Julien

+0

因此,您不再需要您的問題的更新部分的答案?如果是這樣,也許你可以添加自己的答案與你發現幫助你? –

回答

2

NanGuardMode於5月1日轉移到了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))