Man*_*rma 3 python lstm keras tensorflow keras-layer
我需要训练基于序列的 10x10 图像分割。以下是我要使用的 lstm 和 convlstm 模型:
def lstmModel():
# Model definition
model = Sequential()
model.add(LSTM(50, batch_input_shape=(1, None, inp.shape[1]*inp.shape[2]), return_sequences=True, stateful=True))
model.add(Dense(out.shape[1]*out.shape[2], activation='softmax'))
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
model.summary()
return model
def convlstmModel():
# Model definition
model = Sequential()
model.add(ConvLSTM2D(12, kernel_size=5, padding = "same", batch_input_shape=(1, None, inp.shape[1], inp.shape[2], 1), return_sequences=True, stateful=True))
model.add(Conv2D(20, 3, padding='same', activation='softmax'))
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
model.summary()
return model
Run Code Online (Sandbox Code Playgroud)
我为 10 个随机 10x10 图像序列的序列训练模型。LSTM 模型对我来说似乎工作正常,但 ConvLSTM 模型显示 Conv2D 层的维度不匹配:
ValueError: Input 0 is incompatible with layer conv2d_1: expected ndim=4, found ndim=5
Run Code Online (Sandbox Code Playgroud)
任何帮助都非常感谢。谢谢!
LSTM
层用于“时间序列”。
Conv
图层用于“静止图像”。
一种需要形状(batch, steps, features)
,另一种需要:(batch, witdh, height, features)
现在,ConvLSTM2D
混合两者并需要(batch, steps, width, height, features)
离开时,ConvLSTM2D
您有一个steps
不支持的额外维度Conv2D
。
如果要保留此维度,请使用带有TimeDistributed
包装器的卷积:
model.add(TimeDistributed(Conv2D(...))
Run Code Online (Sandbox Code Playgroud)
请注意,与只有 3 个维度的其他模型相比,您仍将拥有所有 5 个维度。
您应该使用某种重塑或其他操作来使其适合您的训练数据。
由于您的问题没有显示任何内容,我们现在只能回答这些。