Trying to run a gym
environment with RL agent on Mac M1.
import gym
import numpy as np
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Flatten
from tensorflow.keras.optimizers import Adam
from rl.agents import DQNAgent
While running this, it is giving error for the last line:
ImportError: cannot import name 'model_from_config' from 'tensorflow.keras.models' (/Users/username/miniforge3/envs/rl_gym/lib/python3.12/site-packages/keras/_tf_keras/keras/models/__init__.py). Did you mean: 'model_from_json'?
This is on Sonoma 14.4.1
. Most of the other discussions (like this one link) suggested reinstalling tensorflow and other packages. I tried that but it did not work. The error still persists. This is inside a conda environment where I have keras-3.2.1
, tensorflow-2.16.1
and keras-rl2-1.0.5
. What am I dong wrong here?
TensorFlow 2.16 uses Keras 3 by default, which has a slightly different API than Keras 2. See the release notes of TensorFlow 2.16:
Keras 3 will be the default Keras version for TensorFlow 2.16 onwards. You may need to update your script to use Keras 3. Please refer to the new Keras documentation for Keras 3 (https://keras.io/keras_3). Keras 2 will continue to be released alongside TensorFlow as tf_keras. To continue using Keras 2 with TensorFlow 2.16+:
As a result, the tf.keras API also changed, and some functions are not available anymore. If you compare the documentation of the tf.keras.models module from TF 2.15 and the one from TF 2.16, you'll see that some serialization functions were removed.
The solution is to downgrade your version of TensorFlow to the 2.15 which is the latest compatible with the keras-rl
library you're using.