Theano运行时大量报错求助:Python与包版本兼容问题排查
Theano与NumPy版本兼容性问题解决
问题描述
创建Python 3.9的conda环境并安装Theano 1.0.5后,运行以下代码出现错误:
import theano from theano import tensor as T x = T.vector('x') W = T.matrix('W') b = T.vector('b') dot = T.dot(x, W) out = T.nnet.sigmoid(dot + b)
报错信息如下:
F:\Projects\Python\Environments\condaTheano\lib\site-packages\theano\scalar\basic.py:2324: FutureWarning: In the future `np.bool` will be defined as the corresponding NumPy scalar. self.ctor = getattr(np, o_type.dtype) Traceback (most recent call last): File "f:/Projects/Python/theanoTutorial/firstExample.py", line 1, in <module> import theano File "F:\Projects\Python\Environments\condaTheano\lib\site-packages\theano\__init__.py", line 124, in <module> from theano.scan_module import (scan, map, reduce, foldl, foldr, clone, File "F:\Projects\Python\Environments\condaTheano\lib\site-packages\theano\scan_module\__init__.py", line 41, in <module> from theano.scan_module import scan_opt File "F:\Projects\Python\Environments\condaTheano\lib\site-packages\theano\scan_module\scan_opt.py", line 60, in <module> from theano import tensor, scalar File "F:\Projects\Python\Environments\condaTheano\lib\site-packages\theano\tensor\__init__.py", line 8, in <module> from theano.tensor.basic import * File "F:\Projects\Python\Environments\condaTheano\lib\site-packages\theano\tensor\basic.py", line 20, in <module> from theano.scalar import int32 as int32_t File "F:\Projects\Python\Environments\condaTheano\lib\site-packages\theano\scalar\__init__.py", line 3, in <module> from .basic import * File "F:\Projects\Python\Environments\condaTheano\lib\site-packages\theano\scalar\basic.py", line 2371, in <module> convert_to_bool = Cast(bool, name='convert_to_bool') File "F:\Projects\Python\Environments\condaTheano\lib\site-packages\theano\scalar\basic.py", line 2324, in __init__ self.ctor = getattr(np, o_type.dtype) File "F:\Projects\Python\Environments\condaTheano\lib\site-packages\numpy\__init__.py", line 305, in __getattr__ raise AttributeError(__former_attrs__[attr]) AttributeError: module 'numpy' has no attribute 'bool'. `np.bool` was a deprecated alias for the builtin `bool`. To avoid this error in existing code, use `bool` by itself. Doing this will not modify any behavior and is safe. If you specifically wanted the numpy scalar type, use `np.bool_` here. The aliases was originally deprecated in NumPy 1.20; for more details and guidance see the original release note at: https://numpy.org/devdocs/release/1.20.0-notes.html#deprecations
已尝试更换Python 3.8、Theano 1.0.4,调整mkl版本,问题仍未解决。
解决办法
降级NumPy到1.19.x版本
Theano 1.0.x正式版不兼容NumPy 1.20+,因为np.bool在NumPy 1.20后被移除。安装兼容版本:conda install numpy=1.19.5或用pip:
pip install numpy==1.19.5修改Theano源码临时修复
找到报错文件F:\Projects\Python\Environments\condaTheano\lib\site-packages\theano\scalar\basic.py,定位到2324行,修改代码:if o_type.dtype == 'bool': self.ctor = np.bool_ else: self.ctor = getattr(np, o_type.dtype)同时确保类型定义部分兼容
np.bool_。使用Theano开发版
Theano开发分支已修复NumPy 1.20+的兼容性问题,安装最新版本:pip install git+https://github.com/Theano/Theano.git
内容的提问来源于stack exchange,提问作者amirabbas Gheisary
相关产品推荐
相关产品推荐

