1. 程式人生 > >AttributeError:’module’ object has no attribute ‘ifelse’錯誤資訊的解決方法

AttributeError:’module’ object has no attribute ‘ifelse’錯誤資訊的解決方法

在執行訓練卷積神經網路CNN模型進行分類的Python程式碼過程中,出現如下錯誤資訊:

AttributeError:’module’object has no attribute ‘ifelse’


問題分析:

    根據錯誤資訊提示,錯誤是在theano_backend.py這個檔案中發生,可見應該是keras的backend(後端) theano出現了問題,進一步查詢發現是kera的版本和theano版本不匹配所致,即所使用的theano版本太新,而keras尚不支援該版本中theano的ifelse介面。

解決方法一(此種方法未經實測)

降級所使用的theano版本,重新安裝0.9版本的theano

解決方法二

修改theano_backend.py這個檔案。

1)      首先切換到theano_backend.py檔案所在目錄:

      cd  /usr/local/lib/python2.7/dist-packages/keras/backend

2)      然後執行:sudo gedit theano_backend.py &

3)      在檔案頭新增:from theano import ifelse

4)      將第1482行程式碼:


修改為:


儲存後,再次執行Python程式碼,不再報錯,問題成功解決。