如何解决python中的theano-循环导入问题?

How to solve theano - circular import problem in python?

提问人:Jan Nowak 提问时间:11/17/2023 更新时间:11/17/2023 访问量:34

问:

我尝试在我的jupyternotebook中运行2019年的一些代码。我有我不知道如何解决的错误:



----> 8 import theano.tensor as T

...


File ~/my-jupyter-env/lib/python3.11/site-packages/theano/__init__.py:124
    120 from theano.misc.safe_asarray import _asarray
    122 from theano.printing import pprint, pp
--> 124 from theano.scan_module import (scan, map, reduce, foldl, foldr, clone,
    125                                 scan_checkpoints)
    127 from theano.updates import OrderedUpdates
    129 # scan_module import above initializes tensor and scalar making these imports
    130 # redundant
    131 
   (...)
    136 
    137 # import sparse

File ~/my-jupyter-env/lib/python3.11/site-packages/theano/scan_module/__init__.py:41
     38 __copyright__ = "(c) 2010, Universite de Montreal"
     39 __contact__ = "Razvan Pascanu <r.pascanu@gmail>"
---> 41 from theano.scan_module import scan_opt
     42 from theano.scan_module.scan import scan
     43 from theano.scan_module.scan_checkpoints import scan_checkpoints

File ~/my-jupyter-env/lib/python3.11/site-packages/theano/scan_module/scan_opt.py:60
     57 import numpy as np
     59 import theano
---> 60 from theano import tensor, scalar
     61 from theano.tensor import opt, get_scalar_constant_value, Alloc, AllocEmpty
     62 from theano import gof

File ~/my-jupyter-env/lib/python3.11/site-packages/theano/tensor/__init__.py:8
      4 __docformat__ = "restructuredtext en"
      6 import warnings
----> 8 from theano.tensor.basic import *
      9 from theano.tensor.subtensor import *
     10 from theano.tensor.type_other import *

File ~/my-jupyter-env/lib/python3.11/site-packages/theano/tensor/basic.py:20
     17 from theano.gof import Apply, Constant, Op, Variable, ParamsType
     18 from theano.gof.type import Generic
---> 20 from theano.scalar import int32 as int32_t
     21 from theano.tensor import elemwise
     22 from theano.tensor.var import (AsTensorError, TensorVariable,
     23                                TensorConstant, TensorConstantSignature,
     24                                _tensor_py_operators)

File ~/my-jupyter-env/lib/python3.11/site-packages/theano/scalar/__init__.py:3
      1 from __future__ import absolute_import, print_function, division
----> 3 from .basic import *
      5 from .basic_scipy import *

File ~/my-jupyter-env/lib/python3.11/site-packages/theano/scalar/basic.py:657
    654         return shape_info
    656 # Register C code for ViewOp on Scalars.
--> 657 theano.compile.register_view_op_c_code(
    658     Scalar,
    659     """
    660     %(oname)s = %(iname)s;
    661     """,
    662     1)
    665 bool = get_scalar_type('bool')
    666 int8 = get_scalar_type('int8')

AttributeError: partially initialized module 'theano' has no attribute 'compile' (most likely due to a circular import)

在 readme.md 中,据说它需要 Theano (1.0.0) 和 numpy (1.13.3)。我应该将我的版本降级到这些版本来运行它吗?我试过了,但没有帮助。我听说 theano 现在已经死了,我看到很多错误,例如 theano 使用例如来自“collections”库的“MulatbleMaps”,但现在由于一些更新,它应该是“collections.abc”。因此,我尝试更改例如theano源代码中的此集合错误,但现在没有收到此错误。但是我有错误,我不知道如何解决我在这里展示的。那么我应该在代码或安装中更改什么才能摆脱此错误?或者,也许我应该做一些不同的事情,在 2023 年安全地运行 theano?我知道我可以使用tensorflow进行检查,但是这段代码很长,我不想重写它。

jupyter-notebook python-import importerror theano

评论

0赞 Péter Szilvási 11/17/2023
我建议你创建一个新的虚拟环境:.激活虚拟环境后,仅在其中安装必要的依赖项。或者,可以创建新的 conda 环境,安装依赖项,并在 Jupyter 笔记本中选择它作为 Python 解释器。python -m venv .venv

答:

0赞 0x00 11/17/2023 #1

查看 PyPi,看起来他们更新了库以支持 Python 3.9,但您运行的是 Python 3.11,这可能会导致问题。

另外,如果您从他们的存储库中检查 README 文件,请说:

MILA将停止开发Theano: https://groups.google.com/d/msg/theano-users/7Poq8BZutbY/rNCIfvAEAwAJ [2017年9月28日]

您有两种选择:

  • 推荐:Theano 被分叉到一个名为 Aesara 的新项目中,该项目正在积极维护,并且支持 Python 3.11。您必须更新 Jupyternotebook 代码才能使用 Aesara。
  • 不推荐:安装 Python 3.9,安装依赖项并尝试运行 jupyternotebook。不建议这样做,因为您必须安装旧版本的 Python,并且您将使用停止获取更新的旧项目 (Theano)。