简体   繁体   English

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

[英]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.

相关问题 层密集的输入 0 与层不兼容:输入形状的预期轴 -1 具有值 3,但收到的输入形状为 (None, 1) - Input 0 of layer dense is incompatible with the layer: expected axis -1 of input shape to have value 3 but received input with shape (None, 1) 密集层的输入 0 与该层不兼容:输入形状的预期轴 -1 具有值 8192,但接收到的输入具有形状(无,61608) - Input 0 of layer dense is incompatible with the layer: expected axis -1 of input shape to have value 8192 but received input with shape (None, 61608) ValueError: ...与图层不兼容:输入形状的预期轴 -1 的值为 20,但接收到的输入形状为 (None, 20, 637) - ValueError: ...incompatible with the layer: expected axis -1 of input shape to have value 20 but received input with shape (None, 20, 637) 密集层的输入 0 与该层不兼容:输入形状的预期轴 -1 的值为 2700,但接收到的输入形状为 [None, 900] - Input 0 of layer dense is incompatible with the layer: expected axis -1 of input shape to have value 2700 but received input with shape [None, 900] “ValueError:……与图层不兼容:输入形状的预期轴 -1 的值为 8,但接收到的输入形状为(无、7、169)” - “ValueError: …incompatible with the layer: expected axis -1 of input shape to have value 8 but received input with shape (None, 7, 169)” 层密集_18 的输入 0 与层不兼容:输入形状的预期轴 -1 具有值 3500,但接收到形状为 [None, 7] 的输入 - Input 0 of layer dense_18 is incompatible with the layer: expected axis -1 of input shape to have value 3500 but received input with shape [None, 7] ValueError:dense_24 层的输入 0 不兼容:输入形状的预期轴 -1 具有值 1024,但接收到形状为 [16, 512] 的输入 - ValueError: Input 0 of layer dense_24 is incompatible: expected axis -1 of input shape to have value 1024 but received input with shape [16, 512] ValueError: 层序 7 的输入 0 与层不兼容:输入形状的预期轴 -1 具有值 5,但接收到的形状(无,21) - ValueError: Input 0 of layer sequential_7 incompatible with the layer: expected axis -1 of input shape to have value 5 but received shape (None, 21) ValueError:层顺序的输入 0 与层不兼容:输入形状的预期轴 -1 具有值 3,但接收到的输入具有形状 - ValueError: Input 0 of layer sequential is incompatible with the layer: expected axis -1 of input shape to have value 3 but received input with shape 层顺序的输入 0 与层不兼容:输入形状的预期轴 -1 具有值 8,但收到的输入具有形状(无,71) - Input 0 of layer sequential is incompatible with the layer: expected axis -1 of input shape to have value 8 but received input with shape (None, 71)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM