我正在为 OpenAI lunarLander-v2 环境训练模型。我已经使用 Sequential 模型成功地做到了这一点,但是在尝试使用功能模型时,我遇到了一些与 tensorshapes 不兼容的错误。 这是 Agent 类的代码,我认为这个问题与 done_list 和 next_states 的形状不兼容,但我不确定如何重塑这些张量以使其工作。
class DQAgent(Agent):
def __init__(self, env, config):
Agent.__init__(self, env, config)
self.memory = deque(maxlen=self.config.memory_size)
self.model = self.initialize()
def initialize(self):
inputs = Input(shape=(8,))
dense = Dense(self.config.layer_size * self.config.input_layer_mult, activation = relu)
x = dense(inputs)
x = Dense(self.config.layer_size, activation = relu)(x)
outputs = layers.Dense(self.action_space_size, activation = linear)(x)
model = keras.Model(inputs = inputs, outputs = outputs, name = self.name)
model.compile(loss = mean_squared_error, optimizer = Adam(lr = self.config.learning_rate))
model.summary()
return model
def policyAct(self, state):
predicted_actions = self.model.predict(state)
return np.argmax(predicted_actions[0])
def addToMemory(self, state, action, reward, next_state, done):
self.memory.append((self, state, action, reward, next_state, done))
def sampleFromMemory(self):
sample = np.random.sample(self.memory, self.config.batch_size)
return sample
def extractFromSample(self, sample):
states = np.array([i[0] for i in sample])
actions = np.array([i[1] for i in sample])
rewards = np.array([i[2] for i in sample])
next_states = np.array([i[3] for i in sample])
done_list = np.array([i[4] for i in sample])
states = np.squeeze(states)
next_states = np.squeeze(next_states)
return np.squeeze(states), actions, rewards, next_states, done_list
def updateReplayCount(self):
self.config.replay_counter += 1
self.config.replay_counter = self.replay_counter % self.config.replay_step_size
def learnFromMemory(self):
if len(self.memory) < self.config.batch_size or self.config.replay_counter != 0:
return
if np.mean(self.training_episode_rewards[-10:]) > 100:
return
sample = self.sampleFromMemory()
states, actions, rewards, next_states, done_list = self.extractFromSample(sample)
targets = rewards + self.config.gamma * (np.amax(self.model.predict_on_batch(next_states),
axis=1)) * (1 - (done_list))
target_vec = self.model.predict_on_batch(states)
indexes = np.array([i for i in range(self.config.batch_size)])
target_vec[[indexes], [actions]] = targets
self.model.fit(states, target_vec, epochs=1, verbose=0)
def save(self, name):
self.model.save(name)
在使用 Sequential API 而不是函数式 API 创建模型时,类似的代码工作正常。 我对此很陌生,对 SO 也是如此,非常感谢任何帮助。
<块引用>警告:tensorflow:Model 是用形状 (None, 8) 构建的,用于输入 Tensor("input_10:0", shape=(None, 8), dtype=float32),但它在形状不兼容的输入上被调用(无,1)。 值错误:dense_72 层的输入 0 与层不兼容:输入形状的预期轴 -1 具有值 8,但收到的输入形状为 [None, 1]
来自顺序实现的模型,运行没有问题(其余代码相同)
def initialize_model(self):
model = Sequential()
model.add(Dense(self.config.layer_size*self.config.input_layer_mult, input_dim = self.observation_space_dim, activation=relu))
for i in range(self.config.deep_layers):
model.add(Dense(self.config.layer_size, activation=relu))
model.add(Dense(self.action_space_dim, activation=linear))
model.compile(loss=mean_squared_error, optimizer=Adam(lr=self.config.learning_rate))
print(model.summary())
return model
答案 0 :(得分:0)
从这里开始,[https://stackoverflow.com/questions/64512293/input-dense-is-incompatible-with-the-layer-invalid-shape][1]
输入形状应该是 (1, )