How to resolve the issue of Input layer expects different dimensions

20 views Asked by At
--------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
Cell In[92], line 5
      3 dqn_only_embedding = DQNAgent(model=model, nb_actions=action_size, memory=memory, nb_steps_warmup=500, target_model_update=1e-2, policy=policy)
      4 dqn_only_embedding.compile(Adam(lr=1e-3), metrics=['mae'])
----> 5 dqn_only_embedding.fit(env, nb_steps=1000000, visualize=False, verbose=1, nb_max_episode_steps=99, log_interval=100000)

File c:\users\nimas\myenv\lib\site-packages\rl\core.py:169, in Agent.fit(self, env, nb_steps, action_repetition, callbacks, verbose, visualize, nb_max_start_steps, start_step_policy, log_interval, nb_max_episode_steps)
    166 callbacks.on_step_begin(episode_step)
    167 # This is were all of the work happens. We first perceive and compute the action
    168 # (forward step) and then use the reward to improve (backward step).
--> 169 action = self.forward(observation)
    170 if self.processor is not None:
    171     action = self.processor.process_action(action)

File c:\users\nimas\myenv\lib\site-packages\rl\agents\dqn.py:225, in DQNAgent.forward(self, observation)
    222 def forward(self, observation):
    223     # Select an action.
    224     state = self.memory.get_recent_state(observation)
--> 225     q_values = self.compute_q_values(state)
    226     if self.training:
    227         action = self.policy.select_action(q_values=q_values)

File c:\users\nimas\myenv\lib\site-packages\rl\agents\dqn.py:69, in AbstractDQNAgent.compute_q_values(self, state)
     68 def compute_q_values(self, state):
---> 69     q_values = self.compute_batch_q_values([state]).flatten()
     70     assert q_values.shape == (self.nb_actions,)
     71     return q_values

File c:\users\nimas\myenv\lib\site-packages\rl\agents\dqn.py:64, in AbstractDQNAgent.compute_batch_q_values(self, state_batch)
     62 def compute_batch_q_values(self, state_batch):
     63     batch = self.process_state_batch(state_batch)
---> 64     q_values = self.model.predict_on_batch(batch)
     65     assert q_values.shape == (len(state_batch), self.nb_actions)
     66     return q_values

File c:\users\nimas\myenv\lib\site-packages\tensorflow\python\keras\engine\training_v1.py:1199, in Model.predict_on_batch(self, x)
   1195   raise NotImplementedError(
   1196       '`predict_on_batch` is not supported for models distributed with'
   1197       ' tf.distribute.Strategy.')
   1198 # Validate and standardize user data.
-> 1199 inputs, _, _ = self._standardize_user_data(
   1200     x, extract_tensors_from_dataset=True)
   1201 # If `self._distribution_strategy` is True, then we are in a replica context
   1202 # at this point.
   1203 if self.run_eagerly or self._distribution_strategy:

File c:\users\nimas\myenv\lib\site-packages\tensorflow\python\keras\engine\training_v1.py:2330, in Model._standardize_user_data(self, x, y, sample_weight, class_weight, batch_size, check_steps, steps_name, steps, validation_split, shuffle, extract_tensors_from_dataset)
   2326 if (not run_eagerly and is_build_called and is_compile_called and
   2327     not is_dataset  and any(_is_symbolic_tensor(v) for v in all_inputs)):
   2328   return [], [], None
-> 2330 return self._standardize_tensors(
   2331     x, y, sample_weight,
   2332     run_eagerly=run_eagerly,
   2333     dict_inputs=dict_inputs,
   2334     is_dataset=is_dataset,
   2335     class_weight=class_weight,
   2336     batch_size=batch_size)

File c:\users\nimas\myenv\lib\site-packages\tensorflow\python\keras\engine\training_v1.py:2358, in Model._standardize_tensors(self, x, y, sample_weight, run_eagerly, dict_inputs, is_dataset, class_weight, batch_size)
   2355 # Standardize the inputs.
   2356 if not isinstance(x, (dataset_ops.DatasetV1, dataset_ops.DatasetV2)):
   2357   # TODO(fchollet): run static checks with dataset output shape(s).
-> 2358   x = training_utils_v1.standardize_input_data(
   2359       x,
   2360       feed_input_names,
   2361       feed_input_shapes,
   2362       check_batch_axis=False,  # Don't enforce the batch size.
   2363       exception_prefix='input')
   2365 # Get typespecs for the input data and sanitize it if necessary.
   2366 # TODO(momernick): This should be capable of doing full input validation
   2367 # at all times - validate that this is so and refactor the standardization
   2368 # code.
   2369 if isinstance(x, dataset_ops.DatasetV2):

File c:\users\nimas\myenv\lib\site-packages\tensorflow\python\keras\engine\training_utils_v1.py:528, in standardize_input_data(data, names, shapes, check_batch_axis, exception_prefix)
    526 shape = shapes[i]
    527 if len(data_shape) != len(shape):
--> 528   raise ValueError('Error when checking ' + exception_prefix +
    529                    ': expected ' + names[i] + ' to have ' +
    530                    str(len(shape)) + ' dimensions, but got array '
    531                    'with shape ' + str(data_shape))
    532 if not check_batch_axis:
    533   data_shape = data_shape[1:]

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

I am currently implementing DQN and following the instructions outlined in this post: 'https://tiewkh.github.io/blog/deepqlearning-openaitaxi/'. However, I am using 'taxi-v3' instead of 'taxi-v2'. I encountered the following error, and in an attempt to address it, I modified the model as shown below. Unfortunately, I am still facing an issue and receiving the error message: 'TypeError: float() argument must be a string or a number, not 'dict'. How to resolve this issue.

    model = Sequential()
    model.add(Flatten(input_shape=(1,2)))
    model.add(Dense(24, activation='relu'))
    model.add(Dense(24, activation='relu'))
    model.add(Dense(6, activation='linear'))
0

There are 0 answers