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

2.3k Views Asked by At

I am importing pymc3 and theano. Using jupyter notebook, python version 3.11.5.I have installed theano 1.0.5 and pymc3 3.11.4. However when I import theano and pymc3 as below:

import numpy as np
import arviz as az
import theano
import pymc3 as pm

Error is as below:

AttributeError      Traceback (most recent call last)
   Input In [76], in <module>
      1 import numpy as np
      2 import arviz as az
----> 3 import theano
      4 import pymc3 as pm
      5 #print("NumPy version:", np.__version__)
      6 #print("ArviZ version:", az.__version__)

File ~/opt/anaconda3/lib/python3.9/site-packages/theano/__init__.py:124, in <module>
    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 ~/opt/anaconda3/lib/python3.9/site-packages/theano/scan_module/__init__.py:41, in <module>
     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 ~/opt/anaconda3/lib/python3.9/site-packages/theano/scan_module/scan_opt.py:60, in <module>
     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 ~/opt/anaconda3/lib/python3.9/site-packages/theano/tensor/__init__.py:38, in <module>
     34 from theano.tensor.sharedvar import tensor_constructor as _shared
     36 from theano.tensor.io import *
---> 38 from theano.tensor import nnet  # used for softmax, sigmoid, etc.
     40 from theano.gradient import Rop, Lop, grad, numeric_grad, verify_grad, \
     41     jacobian, hessian, consider_constant
     43 from theano.tensor.sort import sort, argsort, topk, argtopk, topk_and_argtopk

File ~/opt/anaconda3/lib/python3.9/site-packages/theano/tensor/nnet/__init__.py:2, in <module>
      1 from __future__ import absolute_import, print_function, division
----> 2 from .nnet import (
      3     CrossentropyCategorical1Hot, CrossentropyCategorical1HotGrad,
      4     CrossentropySoftmax1HotWithBiasDx, CrossentropySoftmaxArgmax1HotWithBias,
      5     LogSoftmax, Prepend_scalar_constant_to_each_row,
      6     Prepend_scalar_to_each_row, Softmax,
      7     SoftmaxGrad, SoftmaxWithBias,
      8     binary_crossentropy, sigmoid_binary_crossentropy,
      9     categorical_crossentropy, crossentropy_categorical_1hot,
     10     crossentropy_categorical_1hot_grad, crossentropy_softmax_1hot,
     11     crossentropy_softmax_1hot_with_bias,
     12     crossentropy_softmax_1hot_with_bias_dx,
     13     crossentropy_softmax_argmax_1hot_with_bias,
     14     crossentropy_softmax_max_and_argmax_1hot,
     15     crossentropy_softmax_max_and_argmax_1hot_with_bias,
     16     crossentropy_to_crossentropy_with_softmax,
     17     crossentropy_to_crossentropy_with_softmax_with_bias,
     18     graph_merge_softmax_with_crossentropy_softmax, h_softmax,
     19     logsoftmax, logsoftmax_op, prepend_0_to_each_row, prepend_1_to_each_row,
     20     prepend_scalar_to_each_row, relu, softmax, softmax_grad, softmax_graph,
     21     softmax_op, softmax_simplifier, softmax_with_bias, elu, selu,
     22     confusion_matrix, softsign)
     23 from . import opt
     24 from .conv import ConvOp

File ~/opt/anaconda3/lib/python3.9/site-packages/theano/tensor/nnet/nnet.py:32, in <module>
     29 from theano.compile import optdb
     30 from theano.gof import Apply
---> 32 from theano.tensor.nnet.sigm import sigmoid, softplus
     33 from theano.gradient import DisconnectedType
     34 from theano.gradient import grad_not_implemented`

File ~/opt/anaconda3/lib/python3.9/site-packages/theano/tensor/nnet/sigm.py:275, in <module>
    273         out.tag.values_eq_approx = values_eq_approx_remove_low_prec
    274         return [out]
--> 275 theano.compile.optdb['uncanonicalize'].register("local_ultra_fast_sigmoid",
    276                                                 local_ultra_fast_sigmoid)
    279 def hard_sigmoid(x):
    280     """
    281     An approximation of sigmoid.
    282 
   (...)
    288 
    289    
AttributeError: partially initialized module 'theano' has no attribute 'compile' (most likely due to a circular import)

I have also used other version of python but have not been successful. I also installed pygpu and also used numpy monkey patch which solved the initial errors but could not solve the above attribute error.

1

There are 1 best solutions below

0
Darshan Bhandari On

To fix this error is to use the theano.config.optimizer attribute. This attribute allows you to specify the optimizer that Theano should use. By setting this attribute to None, Theano will not try to import itself, and the circular import error will not occur.

import theano
theano.config.optimizer = 'None'