keras ValueError:密集图层的输入0与图层不兼容:预期轴-1的值为8,但收到的输入的形状为[None,1]

m1m5dgzv  于 2022-11-13  发布在  其他
关注(0)|答案(1)|浏览(201)

我正在为OpenAI lunarLander-v2环境训练一个模型。我已经使用Sequential模型成功地完成了这个任务,但是当我尝试使用函数模型时,我得到了一些Tensor形状不兼容的错误。这是Agent类的代码,我认为这个问题与done_list和next_states的形状不兼容有关,但是我不知道如何重新塑造这些Tensor使它工作。

  1. class DQAgent(Agent):
  2. def __init__(self, env, config):
  3. Agent.__init__(self, env, config)
  4. self.memory = deque(maxlen=self.config.memory_size)
  5. self.model = self.initialize()
  6. def initialize(self):
  7. inputs = Input(shape=(8,))
  8. dense = Dense(self.config.layer_size * self.config.input_layer_mult, activation = relu)
  9. x = dense(inputs)
  10. x = Dense(self.config.layer_size, activation = relu)(x)
  11. outputs = layers.Dense(self.action_space_size, activation = linear)(x)
  12. model = keras.Model(inputs = inputs, outputs = outputs, name = self.name)
  13. model.compile(loss = mean_squared_error, optimizer = Adam(lr = self.config.learning_rate))
  14. model.summary()
  15. return model
  16. def policyAct(self, state):
  17. predicted_actions = self.model.predict(state)
  18. return np.argmax(predicted_actions[0])
  19. def addToMemory(self, state, action, reward, next_state, done):
  20. self.memory.append((self, state, action, reward, next_state, done))
  21. def sampleFromMemory(self):
  22. sample = np.random.sample(self.memory, self.config.batch_size)
  23. return sample
  24. def extractFromSample(self, sample):
  25. states = np.array([i[0] for i in sample])
  26. actions = np.array([i[1] for i in sample])
  27. rewards = np.array([i[2] for i in sample])
  28. next_states = np.array([i[3] for i in sample])
  29. done_list = np.array([i[4] for i in sample])
  30. states = np.squeeze(states)
  31. next_states = np.squeeze(next_states)
  32. return np.squeeze(states), actions, rewards, next_states, done_list
  33. def updateReplayCount(self):
  34. self.config.replay_counter += 1
  35. self.config.replay_counter = self.replay_counter % self.config.replay_step_size
  36. def learnFromMemory(self):
  37. if len(self.memory) < self.config.batch_size or self.config.replay_counter != 0:
  38. return
  39. if np.mean(self.training_episode_rewards[-10:]) > 100:
  40. return
  41. sample = self.sampleFromMemory()
  42. states, actions, rewards, next_states, done_list = self.extractFromSample(sample)
  43. targets = rewards + self.config.gamma * (np.amax(self.model.predict_on_batch(next_states),
  44. axis=1)) * (1 - (done_list))
  45. target_vec = self.model.predict_on_batch(states)
  46. indexes = np.array([i for i in range(self.config.batch_size)])
  47. target_vec[[indexes], [actions]] = targets
  48. self.model.fit(states, target_vec, epochs=1, verbose=0)
  49. def save(self, name):
  50. self.model.save(name)

当使用Sequential API而不是函数式API创建模型时,类似的代码可以很好地工作。我对这一点非常陌生,对SO也是如此,任何帮助都非常感谢。
警告:tensorflow:模型是使用输入Tensor(“input_10:0”,shape=(None,8),dtype= float 32)的形状(None,8)构建的,但在使用不兼容形状(None,1)的输入上调用了该模型。值错误:层dense_72的输入0与层不兼容:输入shape的轴-1的值应为8,但收到的输入的shape为[None,1]
来自顺序实现的模型,运行时没有问题(其余代码相同)

  1. def initialize_model(self):
  2. model = Sequential()
  3. model.add(Dense(self.config.layer_size*self.config.input_layer_mult, input_dim = self.observation_space_dim, activation=relu))
  4. for i in range(self.config.deep_layers):
  5. model.add(Dense(self.config.layer_size, activation=relu))
  6. model.add(Dense(self.action_space_dim, activation=linear))
  7. model.compile(loss=mean_squared_error, optimizer=Adam(lr=self.config.learning_rate))
  8. print(model.summary())
  9. return model
cgvd09ve

cgvd09ve1#

here开始,输入形状应为(1,)

相关问题