Quantcast
Channel: Active questions tagged python - Stack Overflow
Viewing all articles
Browse latest Browse all 17331

ValueError: Error when checking input: expected flatten_input to have shape (1, 4) but got array with shape (1, 2)

$
0
0

I'm fairly new to RL and i can't really understand why I'm getting this error.

import randomimport numpy as npimport numpy as npfrom tensorflow.keras.models import Sequentialfrom tensorflow.keras.layers import Dense, Flattenfrom tensorflow.keras.optimizers import Adamfrom rl.agents import DQNAgentfrom rl.policy import BoltzmannQPolicyfrom rl.memory import SequentialMemorydef build_model(states, actions):    model = Sequential()    model.add(Flatten(input_shape=(1,states)))    model.add(Dense(24, activation='relu'))    model.add(Dense(24, activation='relu'))    model.add(Dense(actions, activation='linear'))    return modeldef 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 dqndef main():    env = gym.make('CartPole-v1')    states = env.observation_space.shape[0]    actions = env.action_space.n    #print(env.reset())    #print(env.action_space.sample())    print(env.observation())    model = build_model(states, actions)    dqn = build_agent(model, actions)    dqn.compile(Adam(learning_rate=1e-3),metrics=['mae'])    dqn.fit(env, nb_steps=50000, visualize=False, verbose=1)main()

I can't understand why it's getting an array with shape (1,2). I've looked through some people's similar questions but I can't apply it to mine.It starts the training but it fails immediately with 0 steps performed.

Thanks in advance!


Viewing all articles
Browse latest Browse all 17331

Latest Images

Trending Articles



Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>