keras-rl / keras-rl

Deep Reinforcement Learning for Keras.

Home Page:http://keras-rl.readthedocs.io/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Cannot import CallbackList

FitMachineLearning opened this issue · comments

Getting the following error while trying to run the sample DQN code after installation of keras-rl.

Example DQN with Keras-rl

File "keras_rl_test.py", line 8, in
from rl.agents.dqn import DQNAgent
File "C:\Users\eljed\anaconda3\lib\site-packages\rl\agents_init
.py", line 2, in
from .dqn import DQNAgent, NAFAgent, ContinuousDQNAgent
File "C:\Users\eljed\anaconda3\lib\site-packages\rl\agents\dqn.py", line 8, in
from rl.core import Agent
File "C:\Users\eljed\anaconda3\lib\site-packages\rl\core.py", line 8, in
from rl.callbacks import (
File "C:\Users\eljed\anaconda3\lib\site-packages\rl\callbacks.py", line 12, in
from keras.callbacks import Callback as KerasCallback, CallbackList as KerasCallbackList
ImportError: cannot import name 'CallbackList' from 'keras.callbacks' (C:\Users\eljed\anaconda3\lib\site-packages\keras\callbacks.py)_

Please make sure that the boxes below are checked before you submit your issue. If your issue is an implementation question, please ask your question in the Keras-RL Google group or join the Keras-RL Gitter channel and ask there instead of filing a GitHub issue.

Thank you!
KerasRL error

  • Check that you are up-to-date with the master branch of Keras-RL. You can update with:
    pip install git+git://github.com/keras-rl/keras-rl.git --upgrade --no-deps

  • Check that you are up-to-date with the master branch of Keras. You can update with:
    pip install git+git://github.com/fchollet/keras.git --upgrade --no-deps

  • Provide a link to a GitHub Gist of a Python script that can reproduce your issue (or just copy the script here if it is short). If you report an error, please include the error message and the backtrace.

I noticed the same problem when I upgraded the keras module. The recent version of keras now supports tensorflow 2.2 or higher, meaning that it might have changed a lot, including CallbackList gone. AFAIK, keras-rl does not support such a recent change in keras yet. Down-grade keras may solve the issue. The following command line works for me with tensorflow < 2.0:

pip install tensorflow==1.9 keras==2.2.4 keras-rl

Thanks @jyounglee .
Unfortunately downgrading is not an option here as TF is setup to work with CUDA and CUDNN specific version needed for my project.

Any support for TF 2.2 coming soon?

Using tf-nightly fixed this particular problem for me:
pip install tf-nightly

Of course it can break something else, but it is a temporary fix until the next version of tensorflow.

I struggled with this issue as well in TF 2.2. In the end, what works for me was to import the CallbackList class from the keras module that hides in the python directory of tensorflow (that is, tensorflow.python.keras) instead of the tensorflow.keras module.

In a nutshell, you can fix the CallbackList import error by changing from

from tensorflow.keras.callbacks import CallbackList as KerasCallbackList

to

from tensorflow.python.keras.callbacks import CallbackList as KerasCallbackList

commented

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

from tensorflow.python.keras.callbacks import CallbackList as KerasCallbackList

Thank you so much

My Code:
import cv2
import numpy as np
from keras.datasets import cifar10
from keras import backend as K
from keras.utils import np_utils
from keras.datasets import cifar10
import math
from keras.optimizers import SGD
from keras.callbacks import LearningRateSchedule

Error:

ImportError Traceback (most recent call last)
in <cell line: 9>()
7 import math
8 from keras.optimizers import SGD
----> 9 from keras.callbacks import LearningRateSchedule

ImportError: cannot import name 'LearningRateSchedule' from 'keras.callbacks' (/usr/local/lib/python3.9/dist-packages/keras/callbacks.py)