在softmax铺设之前添加LSTM层

2024-06-16 12:09:13 发布

您现在位置:Python中文网/ 问答频道 /正文

我想在softmax层之前添加一个LSTM层,这样我就可以跟踪序列的上下文并将其用于预测。以下是我的实现,但我每次都会遇到以下错误。请帮我解决这个错误。在

值错误:输入0与层lstm\u 1不兼容:预期的ndim=3,找到的ndim=2

    common_model = Sequential()
    common_model.add(Conv2D(32, (3, 3), input_shape=self.state_size, padding='same', activation='relu'))
    common_model.add(Dropout(0.2))
    common_model.add(Conv2D(32, (3, 3), activation='relu', padding='same'))
    common_model.add(MaxPooling2D(pool_size=(2, 2)))
    common_model.add(Flatten())
    common_model.add(Dense(512, activation='relu'))
    common_model.add(Dropout(0.5))
    common_model.add(Dense(512, activation='relu'))
    common_model.add(Dropout(0.5))
    common_model.add(Dense(512, activation='relu'))
    common_model.add(Dropout(0.5))


    agent_model = Sequential()
    agent_model.add(common_model)
    agent_model.add(LSTM(512, return_sequences=False))
    agent_model.add(Dense(self.action_size, activation='softmax'))
    agent_model.compile(loss='categorical_crossentropy', optimizer=Adam(lr=self.agent_learning_rate))

    critic_model = Sequential()
    critic_model.add(common_model)
    critic_model.add(Dense(1, activation='linear'))
    critic_model.compile(loss="mse", optimizer=Adam(lr=self.critic_learning_rate))

Tags: selfaddsizemodel错误commonactivationdropout
1条回答
网友
1楼 · 发布于 2024-06-16 12:09:13

我仍然不太明白在稠密之后附加LSTM的目的,但是这个错误可以解释为:

因为在Keras中,LSTM接受的输入张量为(?, m, n),需要有3个维数,而稠密的输出是(?)?,p)有2个尺寸。在

您可能需要尝试嵌入或重塑层,例如:

model.add(Embedding(512, 64, input_length=512))

或者

model.add(Reshape((512, 64)))

另外,还可以检查一些使用LSTM的示例:https://github.com/keras-team/keras/tree/master/examples

相关问题 更多 >