Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ImportError: cannot import name 'CuDNNLSTM' from 'keras.layers' #9

Open
muammar opened this issue Mar 25, 2021 · 1 comment
Open

ImportError: cannot import name 'CuDNNLSTM' from 'keras.layers' #9

muammar opened this issue Mar 25, 2021 · 1 comment

Comments

@muammar
Copy link

muammar commented Mar 25, 2021

I tried using SMILES-X with a recent environment, and the example notebook is failing with the following error:

---------------------------------------------------------------------------
ImportError                               Traceback (most recent call last)
<ipython-input-4-84592a845c88> in <module>
      1 import pandas as pd
      2 
----> 3 from SMILESX import main, inference
      4 get_ipython().run_line_magic('matplotlib', 'inline')

~/git/SMILES-X/SMILESX/main.py in <module>
     24 from sklearn.metrics import r2_score
     25 
---> 26 from SMILESX import utils, token, augm, model
     27 
     28 np.set_printoptions(precision=3)

~/git/SMILES-X/SMILESX/model.py in <module>
      4 from keras.layers import Embedding
      5 from keras.layers.wrappers import Bidirectional
----> 6 from keras.layers import CuDNNLSTM, TimeDistributed
      7 
      8 from keras.engine.topology import Layer

ImportError: cannot import name 'CuDNNLSTM' from 'keras.layers' (/home/elkhatim/miniconda3/envs/py38/lib/python3.8/site-packages/keras/layers/__init__.py)

This is due to the latest version of Keras deprecating CuDNNLSTM. As I am under a conda environment with Python3.8, I tried installing the version 2.3.0 of Keras but not possible:

conda install -c conda-forge keras==2.3
UnsatisfiableError: The following specifications were found
to be incompatible with the existing python installation in your environment:

Specifications:

  - keras==2.3.0 -> python[version='>=2.7,<2.8.0a0|>=3.6,<3.7.0a0|>=3.7,<3.8.0a0']

Your python: python=3.8

It seems the only way to use SMILES-X would be to install an environment with Python3.6 and use the Keras version posted above, which becomes very inconvenient on my setup to access my jupyterlab instance.

@muammar
Copy link
Author

muammar commented Mar 26, 2021

More advances, after installing a conda environment with python3.6, and trying to run the example I am getting this error:

---------------------------------------------------------------------------
InvalidArgumentError                      Traceback (most recent call last)
~/miniconda3/envs/py36/lib/python3.6/site-packages/tensorflow/python/client/session.py in _do_call(self, fn, *args)
   1355     try:
-> 1356       return fn(*args)
   1357     except errors.OpError as e:

~/miniconda3/envs/py36/lib/python3.6/site-packages/tensorflow/python/client/session.py in _run_fn(feed_dict, fetch_list, target_list, options, run_metadata)
   1338       # Ensure any changes to the graph are reflected in the runtime.
-> 1339       self._extend_graph()
   1340       return self._call_tf_sessionrun(

~/miniconda3/envs/py36/lib/python3.6/site-packages/tensorflow/python/client/session.py in _extend_graph(self)
   1373     with self._graph._session_run_lock():  # pylint: disable=protected-access
-> 1374       tf_session.ExtendSession(self._session)
   1375 

InvalidArgumentError: No OpKernel was registered to support Op 'CudnnRNN' used by {{node bidirectional_1/CudnnRNN}}with these attrs: [rnn_mode="lstm", seed2=0, is_training=true, dropout=0, seed=87654321, T=DT_FLOAT, input_mode="linear_input", direction="unidirectional"]
Registered devices: [CPU, XLA_CPU]
Registered kernels:
  <no registered kernels>

	 [[bidirectional_1/CudnnRNN]]

During handling of the above exception, another exception occurred:

InvalidArgumentError                      Traceback (most recent call last)
<ipython-input-44-ad7b10e344ac> in <module>
     18           alpha_ref = 22,
     19           patience = 50,
---> 20           n_epochs = 100)

~/git/SMILES-X/SMILESX/main.py in Main(data, data_name, bayopt_bounds, data_units, k_fold_number, augmentation, outdir, bayopt_n_epochs, bayopt_n_rounds, bayopt_it_factor, bayopt_on, lstmunits_ref, denseunits_ref, embedding_ref, batch_size_ref, alpha_ref, n_gpus, bridge_type, patience, n_epochs)
    304                                                             exact_feval = False,
    305                                                             normalize_Y = True,
--> 306                                                             num_cores = multiprocessing.cpu_count()-1)
    307             print("Optimization:\n")
    308             Bayes_opt.run_optimization(max_iter=bayopt_n_rounds)

~/miniconda3/envs/py36/lib/python3.6/site-packages/GPyOpt/methods/bayesian_optimization.py in __init__(self, f, domain, constraints, cost_withGradients, model_type, X, Y, initial_design_numdata, initial_design_type, acquisition_type, normalize_Y, exact_feval, acquisition_optimizer_type, model_update_interval, evaluator_type, batch_size, num_cores, verbosity, verbosity_model, maximize, de_duplication, **kwargs)
    117         self.initial_design_type  = initial_design_type
    118         self.initial_design_numdata = initial_design_numdata
--> 119         self._init_design_chooser()
    120 
    121         # --- CHOOSE the model type. If an instance of a GPyOpt model is passed (possibly user defined), it is used.

~/miniconda3/envs/py36/lib/python3.6/site-packages/GPyOpt/methods/bayesian_optimization.py in _init_design_chooser(self)
    193         if self.X is None:
    194             self.X = initial_design(self.initial_design_type, self.space, self.initial_design_numdata)
--> 195             self.Y, _ = self.objective.evaluate(self.X)
    196         # Case 2
    197         elif self.X is not None and self.Y is None:

~/miniconda3/envs/py36/lib/python3.6/site-packages/GPyOpt/core/task/objective.py in evaluate(self, x)
     48 
     49         if self.n_procs == 1:
---> 50             f_evals, cost_evals = self._eval_func(x)
     51         else:
     52             try:

~/miniconda3/envs/py36/lib/python3.6/site-packages/GPyOpt/core/task/objective.py in _eval_func(self, x)
     72         for i in range(x.shape[0]):
     73             st_time    = time.time()
---> 74             rlt = self.func(np.atleast_2d(x[i]))
     75             f_evals     = np.vstack([f_evals,rlt])
     76             cost_evals += [time.time()-st_time]

~/git/SMILES-X/SMILESX/main.py in create_mod(params)
    285                                                     shuffle = True,
    286                                                     initial_epoch = 0,
--> 287                                                     verbose = 0)
    288 
    289                 best_epoch = np.argmin(history.history['val_loss'])

~/miniconda3/envs/py36/lib/python3.6/site-packages/keras/legacy/interfaces.py in wrapper(*args, **kwargs)
     89                 warnings.warn('Update your `' + object_name + '` call to the ' +
     90                               'Keras 2 API: ' + signature, stacklevel=2)
---> 91             return func(*args, **kwargs)
     92         wrapper._original_function = func
     93         return wrapper

~/miniconda3/envs/py36/lib/python3.6/site-packages/keras/engine/training.py in fit_generator(self, generator, steps_per_epoch, epochs, verbose, callbacks, validation_data, validation_steps, validation_freq, class_weight, max_queue_size, workers, use_multiprocessing, shuffle, initial_epoch)
   1730             use_multiprocessing=use_multiprocessing,
   1731             shuffle=shuffle,
-> 1732             initial_epoch=initial_epoch)
   1733 
   1734     @interfaces.legacy_generator_methods_support

~/miniconda3/envs/py36/lib/python3.6/site-packages/keras/engine/training_generator.py in fit_generator(model, generator, steps_per_epoch, epochs, verbose, callbacks, validation_data, validation_steps, validation_freq, class_weight, max_queue_size, workers, use_multiprocessing, shuffle, initial_epoch)
     40 
     41     do_validation = bool(validation_data)
---> 42     model._make_train_function()
     43     if do_validation:
     44         model._make_test_function()

~/miniconda3/envs/py36/lib/python3.6/site-packages/keras/engine/training.py in _make_train_function(self)
    331                     updates=updates + metrics_updates,
    332                     name='train_function',
--> 333                     **self._function_kwargs)
    334 
    335     def _make_test_function(self):

~/miniconda3/envs/py36/lib/python3.6/site-packages/keras/backend/tensorflow_backend.py in function(inputs, outputs, updates, **kwargs)
   3004 def function(inputs, outputs, updates=None, **kwargs):
   3005     if _is_tf_1():
-> 3006         v1_variable_initialization()
   3007     return tf_keras_backend.function(inputs, outputs,
   3008                                      updates=updates,

~/miniconda3/envs/py36/lib/python3.6/site-packages/keras/backend/tensorflow_backend.py in v1_variable_initialization()
    418 
    419 def v1_variable_initialization():
--> 420     session = get_session()
    421     with session.graph.as_default():
    422         variables = tf.global_variables()

~/miniconda3/envs/py36/lib/python3.6/site-packages/keras/backend/tensorflow_backend.py in get_session()
    383             '`get_session` is not available when '
    384             'TensorFlow is executing eagerly.')
--> 385     return tf_keras_backend.get_session()
    386 
    387 

~/miniconda3/envs/py36/lib/python3.6/site-packages/tensorflow/python/keras/backend.py in get_session(op_input_list)
    460   if not _MANUAL_VAR_INIT:
    461     with session.graph.as_default():
--> 462       _initialize_variables(session)
    463   return session
    464 

~/miniconda3/envs/py36/lib/python3.6/site-packages/tensorflow/python/keras/backend.py in _initialize_variables(session)
    877     # marked as initialized.
    878     is_initialized = session.run(
--> 879         [variables_module.is_variable_initialized(v) for v in candidate_vars])
    880     uninitialized_vars = []
    881     for flag, v in zip(is_initialized, candidate_vars):

~/miniconda3/envs/py36/lib/python3.6/site-packages/tensorflow/python/client/session.py in run(self, fetches, feed_dict, options, run_metadata)
    948     try:
    949       result = self._run(None, fetches, feed_dict, options_ptr,
--> 950                          run_metadata_ptr)
    951       if run_metadata:
    952         proto_data = tf_session.TF_GetBuffer(run_metadata_ptr)

~/miniconda3/envs/py36/lib/python3.6/site-packages/tensorflow/python/client/session.py in _run(self, handle, fetches, feed_dict, options, run_metadata)
   1171     if final_fetches or final_targets or (handle and feed_dict_tensor):
   1172       results = self._do_run(handle, final_targets, final_fetches,
-> 1173                              feed_dict_tensor, options, run_metadata)
   1174     else:
   1175       results = []

~/miniconda3/envs/py36/lib/python3.6/site-packages/tensorflow/python/client/session.py in _do_run(self, handle, target_list, fetch_list, feed_dict, options, run_metadata)
   1348     if handle is None:
   1349       return self._do_call(_run_fn, feeds, fetches, targets, options,
-> 1350                            run_metadata)
   1351     else:
   1352       return self._do_call(_prun_fn, handle, feeds, fetches)

~/miniconda3/envs/py36/lib/python3.6/site-packages/tensorflow/python/client/session.py in _do_call(self, fn, *args)
   1368           pass
   1369       message = error_interpolation.interpolate(message, self._graph)
-> 1370       raise type(e)(node_def, op, message)
   1371 
   1372   def _extend_graph(self):

InvalidArgumentError: No OpKernel was registered to support Op 'CudnnRNN' used by node bidirectional_1/CudnnRNN (defined at /home/elkhatim/miniconda3/envs/py36/lib/python3.6/site-packages/keras/layers/cudnn_recurrent.py:517) with these attrs: [rnn_mode="lstm", seed2=0, is_training=true, dropout=0, seed=87654321, T=DT_FLOAT, input_mode="linear_input", direction="unidirectional"]
Registered devices: [CPU, XLA_CPU]
Registered kernels:
  <no registered kernels>

	 [[bidirectional_1/CudnnRNN]]

Errors may have originated from an input operation.
Input Source operations connected to node bidirectional_1/CudnnRNN:
 bidirectional_1/ExpandDims_2 (defined at /home/elkhatim/miniconda3/envs/py36/lib/python3.6/site-packages/keras/layers/cudnn_recurrent.py:488)	
 bidirectional_1/transpose (defined at /home/elkhatim/miniconda3/envs/py36/lib/python3.6/site-packages/keras/layers/cudnn_recurrent.py:484)	
 bidirectional_1/ExpandDims_1 (defined at /home/elkhatim/miniconda3/envs/py36/lib/python3.6/site-packages/keras/layers/cudnn_recurrent.py:487)	
 bidirectional_1/concat (defined at /home/elkhatim/miniconda3/envs/py36/lib/python3.6/site-packages/keras/layers/cudnn_recurrent.py:60)

I would love trying to help with this but I am just fluent in PyTorch, and have not used keras too much. Any ideas how to solve this issue?

Best,

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant