pythontensorflowmachine-learninganacondareinforcement-learning

Keras-rl2 error Compability with Tensorflow


I'm Currently Facing a problem when it comes to use the keras-rl2 with tensorflow and i dont know why, I just search on the internet and the keras-rl2, tensorflow, and keras documentation and didn't found the solution.

Currently, I want to use the keras-rl2 with the newest version of tensorflow which is 2.16.1 and keras 3 and got some error like this when using

from rl.agents import DKQAgent
ModuleNotFoundError                       Traceback (most recent call last)
Cell In[37], line 1
----> 1 import rl.agents
      3 print("RL agents library version:", rl.agents.__version__)

File D:\Anaconda\Lib\site-packages\rl\agents\__init__.py:2
      1 from .dqn import DQNAgent, NAFAgent, ContinuousDQNAgent
----> 2 from .ddpg import DDPGAgent
      3 from .cem import CEMAgent
      4 from .sarsa import SarsaAgent, SARSAAgent

File D:\Anaconda\Lib\site-packages\rl\agents\dqn.py:8
      5 from tensorflow.keras.layers import Lambda, Input, Layer, Dense
      7 from rl.core import Agent
----> 8 from rl.policy import EpsGreedyQPolicy, GreedyQPolicy
      9 from rl.util import *
     12 def mean_q(y_true, y_pred):

File D:\Anaconda\Lib\site-packages\rl\core.py:8
      4 import numpy as np
      5 from tensorflow.keras.callbacks import History
      7 from rl.callbacks import (
----> 8     CallbackList,
      9     TestLogger,
     10     TrainEpisodeLogger,
     11     TrainIntervalLogger,
     12     Visualizer
     13 )
     16 class Agent:
     17     """Abstract base class for all implemented agents.
     18 
     19     Each agent interacts with the environment (as defined by the `Env` class) by first observing the
   (...)
     37         processor (`Processor` instance): See [Processor](#processor) for details.
     38     """

File D:\Anaconda\Lib\site-packages\rl\callbacks.py:12
      9 from tensorflow.python.keras.callbacks import Callback as KerasCallback, CallbackList as KerasCallbackList
     10 from tensorflow.python.keras.utils.generic_utils import Progbar
---> 12 class Callback(KerasCallback):
     13     def _set_env(self, env):
     14         self.env = env

ModuleNotFoundError: No module named 'keras.utils.generic_utils'

and when I thought I just need to downgrade it to some version like 2.13.0 and keras 2.13.0 it still error like this

---------------------------------------------------------------------------
ImportError                               Traceback (most recent call last)
Cell In[18], line 1
----> 1 from rl.agents.dqn import DQNAgent

File D:\Anaconda\envs\AI\Lib\site-packages\rl\agents\__init__.py:1
----> 1 from .dqn import DQNAgent, NAFAgent, ContinuousDQNAgent
      2 from .ddpg import DDPGAgent
      3 from .cem import CEMAgent

File D:\Anaconda\envs\AI\Lib\site-packages\rl\agents\dqn.py:7
      4 from tensorflow.keras.models import Model
      5 from tensorflow.keras.layers import Lambda, Input, Layer, Dense
----> 7 from rl.core import Agent
      8 from rl.policy import EpsGreedyQPolicy, GreedyQPolicy
      9 from rl.util import *

File D:\Anaconda\envs\AI\Lib\site-packages\rl\core.py:7
      4 import numpy as np
      5 from tensorflow.keras.callbacks import History
----> 7 from rl.callbacks import (
      8     CallbackList,
      9     TestLogger,
     10     TrainEpisodeLogger,
     11     TrainIntervalLogger,
     12     Visualizer
     13 )
     16 class Agent:
     17     """Abstract base class for all implemented agents.
     18 
     19     Each agent interacts with the environment (as defined by the `Env` class) by first observing the
   (...)
     37         processor (`Processor` instance): See [Processor](#processor) for details.
     38     """

File D:\Anaconda\envs\AI\Lib\site-packages\rl\callbacks.py:8
      6 import numpy as np
      7 import tensorflow as tf
----> 8 from tensorflow.keras import __version__ as KERAS_VERSION
      9 from tensorflow.python.keras.callbacks import Callback as KerasCallback, CallbackList as KerasCallbackList
     10 from tensorflow.python.keras.utils.generic_utils import Progbar

ImportError: cannot import name '__version__' from 'tensorflow.keras' (D:\Anaconda\envs\AI\Lib\site-packages\keras\api\_v2\keras\__init__.py)

Can Anyone Give me an Explanation or Solution why it is always Error?

Thank you for your concern


Solution

  • Apparently I found the solution for Tensorflow <= 2.15.0 to use tke Keras-rl2, which is to add this code on your base code

    import tensorflow as tf
    tf.keras.__version__ = __version__
    

    so that it will not error if using keras-rl2, and for the newer tensorflow which is now currently at 2.16.0 I still dont know how to solve it