Python 模型的输出张量必须是Keras“Layer”的输出(因此保存过去的层元数据)。为CNN LSTM使用函数api时

Python 模型的输出张量必须是Keras“Layer”的输出(因此保存过去的层元数据)。为CNN LSTM使用函数api时,python,keras,Python,Keras,我正在尝试使用时间分布进行简单的cnn lstm分类,但我得到以下错误: 模型的输出张量必须是Keras层的输出(因此保存过去的层元数据)。找到: 我的样本是366个通道和5x5大小的灰度图像。每个样本都有自己独特的标签 model_input = Input(shape=(366,5,5)) model = TimeDistributed(Conv2D(64, (3, 3), activation='relu', padding='same',data_format='channels_fi

我正在尝试使用时间分布进行简单的cnn lstm分类,但我得到以下错误: 模型的输出张量必须是Keras
层的输出(因此保存过去的层元数据)。找到:

我的样本是366个通道和5x5大小的灰度图像。每个样本都有自己独特的标签

model_input = Input(shape=(366,5,5))

model = TimeDistributed(Conv2D(64, (3, 3), activation='relu', padding='same',data_format='channels_first')(model_input))
model = TimeDistributed(MaxPooling2D((2, 2),padding='same',data_format='channels_first'))

model = TimeDistributed(Conv2D(128, (3,3), activation='relu',padding='same',data_format='channels_first'))
model = TimeDistributed(MaxPooling2D((2, 2), strides=(2, 2),padding='same',data_format='channels_first'))


model = Flatten()

model = LSTM(256, return_sequences=False, dropout=0.5)
model =  Dense(128, activation='relu')


model = Dense(6, activation='softmax')

cnnlstm = Model(model_input, model)
cnnlstm.compile(optimizer='adamax',
                loss='sparse_categorical_crossentropy',
                metrics=['accuracy'])
cnnlstm.summary()

您必须在层之间传递张量,因为函数API就是这样工作的,对于所有层,使用
层(参数…(输入)
符号:

model_input = Input(shape=(366,5,5))

model = TimeDistributed(Conv2D(64, (3, 3), activation='relu', padding='same',data_format='channels_first'))(model_input)
model = TimeDistributed(MaxPooling2D((2, 2),padding='same',data_format='channels_first'))(model)

model = TimeDistributed(Conv2D(128, (3,3), activation='relu',padding='same',data_format='channels_first'))(model)
model = TimeDistributed(MaxPooling2D((2, 2), strides=(2, 2),padding='same',data_format='channels_first'))(model)


model = TimeDistributed(Flatten())(model)

model = LSTM(256, return_sequences=False, dropout=0.5)(model)
model =  Dense(128, activation='relu')(model)


model = Dense(6, activation='softmax')(model)

cnnlstm = Model(model_input, model)

请注意,我还更正了第一个
TimeDistributed
层,因为张量位于错误的部分。

您必须在层之间传递张量,因为这是函数API的工作方式,对于所有层,使用
层(参数…(输入)
表示法:

model_input = Input(shape=(366,5,5))

model = TimeDistributed(Conv2D(64, (3, 3), activation='relu', padding='same',data_format='channels_first'))(model_input)
model = TimeDistributed(MaxPooling2D((2, 2),padding='same',data_format='channels_first'))(model)

model = TimeDistributed(Conv2D(128, (3,3), activation='relu',padding='same',data_format='channels_first'))(model)
model = TimeDistributed(MaxPooling2D((2, 2), strides=(2, 2),padding='same',data_format='channels_first'))(model)


model = TimeDistributed(Flatten())(model)

model = LSTM(256, return_sequences=False, dropout=0.5)(model)
model =  Dense(128, activation='relu')(model)


model = Dense(6, activation='softmax')(model)

cnnlstm = Model(model_input, model)

请注意,我还更正了第一个
TimeDistributed
层,因为张量位于错误的部分。

您是否阅读了Functional API指南?因为代码是完全错误的,你没有在层之间传递张量,你只是在第一次分布式层库中才正确地传递张量。我得说,我对这件事还是新手。但据我对timedistributed wrapper的理解,张量仅用于第一层。不,这种理解是错误的。我尝试更新代码,但在将展平输出传递到lstm层时,如:model=timedistributed(flatte())model=lstm(256,return_sequences=False,dropout=0.5)(model)我收到一个错误:调用层lstm_48时使用的输入不是符号张量。同样,您没有将张量传递给层(时间分布展平)。您阅读了函数API指南吗?因为代码是完全错误的,你没有在层之间传递张量,你只是在第一次分布式层库中才正确地传递张量。我得说,我对这件事还是新手。但据我对timedistributed wrapper的理解,张量仅用于第一层。不,这种理解是错误的。我尝试更新代码,但在将展平输出传递到lstm层时,如:model=timedistributed(flatte())model=lstm(256,return_sequences=False,dropout=0.5)(model)我得到一个错误:调用层lstm_48时使用的输入不是符号张量。同样,您没有将张量传递给该层(时间分布展平)。谢谢Matias。但是运行上面的代码时,我得到了一个错误:对于输入形状为[?,5,5],[4]的“时间分布/转置”(op:“转置”),维度必须是3,但是是4。我的输入形状似乎正确366通道5*5。不知道怎么了。非常感谢你,马蒂亚斯,你帮我省了不少时间。现在很好用。问题在于我的输入形状,我没有指定每个时间步的通道数,在我的例子中,通道数是1。因此,model_input=input(shape=(366,1,5,5))修复了这个问题。现在我的准确度没有改变,我希望能解决它。谢谢你,马蒂亚斯。但是运行上面的代码时,我得到了一个错误:对于输入形状为[?,5,5],[4]的“时间分布/转置”(op:“转置”),维度必须是3,但是是4。我的输入形状似乎正确366通道5*5。不知道怎么了。非常感谢你,马蒂亚斯,你帮我省了不少时间。现在很好用。问题在于我的输入形状,我没有指定每个时间步的通道数,在我的例子中,通道数是1。因此,model_input=input(shape=(366,1,5,5))修复了这个问题。现在我的准确度没有改变,我希望能解决它。