I was trying to train a model on a gym environment when I encountered the following error:
TypeError: Argument `fetch` = <tf.Variable 'dense/kernel:0' shape=(4, 24) dtype=float32> has invalid type "ResourceVariable" must be a string or Tensor. (Can not convert a ResourceVariable into a Tensor or Operation.)
Here is the relevant code:
env = gym.make("CartPole-v1")
env.observation_space.sample()
from tensorflow.python.keras.models import Sequential
from tensorflow.python.keras.layers import Dense, Flatten
from tensorflow.keras.optimizers import Adam
states = env.observation_space.shape
actions = env.action_space.n
print("Actions: " + str(actions))
def build_model(states, actions):
model = Sequential()
model.add(Dense(24, activation='relu', input_shape=states))
model.add(Dense(24, activation='relu'))
model.add(Dense(actions, activation='linear'))
return model
model = build_model(states, actions)
model.summary()
from rl.agents import DQNAgent
from rl.policy import BoltzmannQPolicy
from rl.memory import SequentialMemory
def build_agent(model, actions):
policy = BoltzmannQPolicy()
memory = SequentialMemory(limit=50000, window_length=1)
dqn = DQNAgent(model=model, memory=memory, policy=policy,
nb_actions=actions, nb_steps_warmup=10, target_model_update=1e-2)
return dqn
dqn = build_agent(model, actions)
dqn.compile(Adam(lr = 0.001), metrics= ["mae"])
dqn.fit(env, nb_steps=10000, visualize=False, verbose=1)
I tried change my versions to what others say is compatible.
tensorflow: 2.12.0
gym: 0.25.2
keras-rl2: 1.0.5
I also know this is not a cartpole problem since I was originally trying to get this to work on a custom gym environment which gave the same error. Changing how the optimizer is passed in also does not affect the error.