我在创建Keras CNN + LSTM模型时遇到问题:
ValueError:检查目标时出错:预期density_2具有2 尺寸,但数组的形状为()
我已经删除了一些图层以测试问题。但是什么都没有改变。
背景: 我正在尝试分析4D数据:3D图像+ 1D时间序列。 我的工作方式是一次添加1张图片,然后由CNN + LSTM模型进行分析。我设法使尺寸正确并流经模型。但是后来我遇到了上面提到的错误。
# define CNN model
model = Sequential()
#Layer 1
model.add(TimeDistributed(Conv3D(32, kernel_size=(5, 5, 5), strides=(1, 1, 1),
activation='relu'),
input_shape=input_shape))
model.add(TimeDistributed(MaxPooling3D(pool_size=(2, 2, 2), strides=(2, 2, 2))))
#Layer 2
model.add(TimeDistributed(Conv3D(64, (5, 5, 5), activation='relu')))
model.add(TimeDistributed(MaxPooling3D(pool_size=(2, 2, 2))))
#Layer 3
model.add(TimeDistributed(Conv3D(128, (5, 5, 5), activation='relu')))
model.add(TimeDistributed(MaxPooling3D(pool_size=(2, 2, 2))))
#Flatten
model.add(TimeDistributed(Flatten()))
# LSTM
model.add(LSTM(512, return_sequences=True))
model.add(LSTM(512))
model.add(Dense(1201))
#Dense
model.add(Dense(num_classes, activation='sigmoid'))
#Compile
model.compile(loss='categorical_crossentropy',
optimizer='adam',
metrics=['accuracy'])
model.summary()
train_gen = generate_images(train_dataset)
model.fit_generator(train_gen, samples_per_epoch=5, nb_epoch=10)
###################################################################
#Data Generator
def generate_images(dataframe):
while True:
sub_dataframe = dataframe.sample(n=1)
batch_input = []
# batch_output = []
# for index, row in sub_dataframe.iterrows(): # iterate through each row
input_path = os.path.join(base_directory, sub_dataframe['Image'].values[0])
img = get_fmri_sequence(input_path)
img = np.expand_dims(img, axis=-1)
batch_input.append(img)
batch_input = np.array(batch_input)
yield (batch_input, sub_dataframe["DX"].values[0])
###################################################################
这是模型摘要:
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
time_distributed_1 (TimeDist (None, None, 43, 54, 45, 4032
_________________________________________________________________
time_distributed_2 (TimeDist (None, None, 21, 27, 22, 0
_________________________________________________________________
time_distributed_3 (TimeDist (None, None, 17, 23, 18, 256064
_________________________________________________________________
time_distributed_4 (TimeDist (None, None, 8, 11, 9, 64 0
_________________________________________________________________
time_distributed_5 (TimeDist (None, None, 4, 7, 5, 128 1024128
_________________________________________________________________
time_distributed_6 (TimeDist (None, None, 2, 3, 2, 128 0
_________________________________________________________________
time_distributed_7 (TimeDist (None, None, 1536) 0
_________________________________________________________________
lstm_1 (LSTM) (None, None, 512) 4196352
_________________________________________________________________
lstm_2 (LSTM) (None, 512) 2099200
_________________________________________________________________
dense_1 (Dense) (None, 1201) 616113
_________________________________________________________________
dense_2 (Dense) (None, 2) 2404
=================================================================
Total params: 8,198,293
Trainable params: 8,198,293
Non-trainable params: 0
_________________________________________________________________