TypeError: Keras symbolic inputs/outputs do not implement `__len__`

i installed a tensorflow following all instructions for macbook with m1/m2. (that is that i need)
and code below contiuning to give me the same error.
" TypeError: Keras symbolic inputs/outputs do not implement __len__. You may be trying to pass Keras symbolic inputs/outputs to a TF API that does not register dispatching, preventing Keras from automatically converting the API call to a lambda layer in the Functional Model. This error will also get raised if you try asserting a symbolic input/output directly "

py charm do not see these imports
from tensorflow.keras.layers import InputLayer, Dense
from tensorflow.keras.optimizers import Adam

any changes on these imports that pycharm can see give the error above.
tensorflow version is 2.11 / python 3.9 / pip unpgraded / keras installed and similar error on forums not wasnt helpful.

I cannot figure out what should i do to solve the error.

Here i am trying to create a custom environment and set up dqn agent.

import csv
import numpy as np
import gym
from gym import spaces
from typing import List
import tensorflow as tf
from tensorflow.keras.layers import InputLayer, Dense
from tensorflow.keras.optimizers import Adam
from rl.agents.dqn import DQNAgent
from rl.policy import EpsGreedyQPolicy
from rl.memory import SequentialMemory

class MyEnv(gym.Env):
    def __init__(self):
        # initialize your environment here
        self.profit = None
        self.params = None
        self.database = download_market_data()
        self.observation_space = gym.spaces.Box(low=np.array([0.0, 1]), high=np.array([0.1, 25]), dtype=np.float32)
        self.action_space = gym.spaces.Discrete(2)
        self.current_step = 0
        self.max_steps = 1000

    def step(self, action):
        # take an action (which corresponds to selecting some random parameters)
        # and return the next state, reward, and done flag
        err_msg = f"{action!r} ({type(action)}) invalid"
        assert self.action_space.contains(action), err_msg
        assert self.current_step < self.max_steps, "Max steps reached. Call reset to start again."

        self.current_step += 1

        params = self.observation_space.sample()
        flat = params.flatten()

        database = find_extrema(self.database, flat[0], flat[1])
        profit = calculate_profitability(database)

        reward = profit
        done = self.current_step == self.max_steps
        self.params = params
        self.profit = profit
        return params, reward, done, {}

    def reset(self, **kwargs):
        # reset the environment to its initial state
        self.current_step = 0
        return self.observation_space.sample()

    def render(self, mode='human'):
        print(f"params: {self.params}, profit: {self.profit}")

# Define the environment
env = MyEnv()

# Define the number of actions and the number of observations
nb_actions = env.action_space.n
nb_observations = env.observation_space.shape[0]

# Define the model architecture
model = tf.keras.models.Sequential()
model.add(Dense(64, activation="relu"))
model.add(Dense(64, activation="relu"))
model.add(Dense(nb_actions, activation="linear"))

# Define the memory buffer
memory = SequentialMemory(limit=1000000, window_length=1)

# Define the policy for choosing actions
policy = EpsGreedyQPolicy(0.1)

# Define the optimizer
optimizer = tf.keras.optimizers.Adam(learning_rate=0.001)

# Define the DQN agent
dqn = DQNAgent(model=model, nb_actions=nb_actions, memory=memory, nb_steps_warmup=1000,
               target_model_update=1e-2, policy=policy, enable_double_dqn=True, enable_dueling_network=True,

# Compile the DQN agent with optimizer

# Train the DQN agent
dqn.fit(env, nb_steps=10000, visualize=False, verbose=2)


Welcome to the Tensorflow Forum!

As mentioned here, you need to install a newer version of keras-rl.

!pip install keras-rl2

Thank you!

thank you for replying . earlier i tried to install keras rl2 but i am getting this :

ERROR: Cannot install keras-rl2==1.0.0, keras-rl2==1.0.1, keras-rl2==1.0.2, keras-rl2==1.0.3, keras-rl2==1.0.4 and keras-rl2==1.0.5 because these package versions have conflicting dependencies.

The conflict is caused by:
keras-rl2 1.0.5 depends on tensorflow
keras-rl2 1.0.4 depends on tensorflow>=2.1.0
keras-rl2 1.0.3 depends on tensorflow==2.0.0-beta1
keras-rl2 1.0.2 depends on tensorflow==2.0.0-beta0
keras-rl2 1.0.1 depends on tf-nightly-2.0-preview
keras-rl2 1.0.0 depends on tf-nightly-2.0-preview


This error message indicates that you are trying to install multiple versions of the keras-rl2 package but they have conflicting dependencies on the tensorflow package.

If you need to use a specific version of keras-rl2 that has conflicting dependencies, you can create a virtual environment for that version of the package and its dependencies. For example, to install keras-rl2 1.0.4, you can create a new virtual environment and then install the required version of tensorflow as shown below

python -m venv myenv
source myenv/bin/activate
pip install keras-rl2==1.0.4 tensorflow>=2.1.0,<2.2.0

Could you also let us know the steps that you have followed to install tensorflow?

Thank you!