[英]ValueError: Input 0 of layer dense is incompatible with the layer: expected axis -1 to have value 8 but received input with shape [None, 1]
I'm training a model for the OpenAI lunarLander-v2 environment.我正在为 OpenAI lunarLander-v2 环境训练 model。 I've succesfully done this using a Sequential model, but when trying to use the functional model, I get some errors with tensorshapes being incompatible.
我已经使用 Sequential model 成功地做到了这一点,但是当尝试使用功能 model 时,我得到一些错误,因为 tensorshapes 不兼容。 Here is the code for the Agent class, the issue I think has to do with the shape of the done_list and next_states being incompatible, but I'm not sure how to reshape these tensors for it to work.
这是代理 class 的代码,我认为这个问题与 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)
Similar code works fine when creating the model using the Sequential API instead of the functional.使用顺序 API 而不是函数式创建 model 时,类似的代码可以正常工作。 I'm very new to this, and to SO as well, any help is greatly appreciated.
我对此很陌生,对 SO 也很陌生,非常感谢任何帮助。
WARNING:tensorflow:Model was constructed with shape (None, 8) for input Tensor("input_10:0", shape=(None, 8), dtype=float32), but it was called on an input with incompatible shape (None, 1).
警告:tensorflow:Model 是用形状(无,8)构造的输入张量(“input_10:0”,形状=(无,8),dtype=float32),但它是在形状不兼容的输入上调用的(None, 1)。 ValueError: Input 0 of layer dense_72 is incompatible with the layer: expected axis -1 of input shape to have value 8 but received input with shape [None, 1]
ValueError:dense_72 层的输入 0 与该层不兼容:输入形状的预期轴 -1 具有值 8,但接收到形状为 [None,1] 的输入
The model from the sequential implementation, which runs with no issues (the rest of the code is the same)顺序实现中的 model 运行没有问题(代码的 rest 相同)
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
From here, [https://stackoverflow.com/questions/64512293/input-dense-is-incompatible-with-the-layer-invalid-shape][1]从这里开始,[https://stackoverflow.com/questions/64512293/input-dense-is-incompatible-with-the-layer-invalid-shape][1]
Input shape should be (1, )输入形状应为 (1, )
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.