我在这里要使python中的输入通道数等于过滤器尺寸的问题
这是我尝试过的代码
我已经尝试过重塑,但是它给了我同样的错误..因为我是python的新手,所以我不明白如何解决我的错误。我的模型是将cnn与lstm层结合在一起,我有2892个训练图像和测试1896张图像,总共4788张图像,每张图像尺寸为128 * 128
cnn_model = Sequential()
cnn_model.add(Conv2D(32, (3, 3), activation='relu', input_shape=(128,128,3),data_format='channels_last'))
print (cnn_model.output_shape)
cnn_model.add(MaxPooling2D(pool_size=(2, 2)))
print (cnn_model.output_shape)
cnn_model.add(Conv2D(32, (3, 3), activation='relu',data_format='channels_last'))
print (cnn_model.output_shape)
cnn_model.add(MaxPooling2D(pool_size=(2, 2)))
print (cnn_model.output_shape)
cnn_model.add(Conv2D(64, (3, 3), activation='relu',data_format='channels_last'))
print (cnn_model.output_shape)
cnn_model.add(MaxPooling2D(pool_size=(2, 2)))
print (cnn_model.output_shape)
cnn_model.add(Conv2D(128, (3, 3), activation='relu',data_format='channels_last'))
print (cnn_model.output_shape)
cnn_model.add(MaxPooling2D(pool_size=(2, 2)))
print ("MP",cnn_model.output_shape)
cnn_model.add(Flatten())
print (cnn_model.output_shape)
cnn_model.summary()
model = Sequential()
model.add(cnn_model)
print (model.output_shape)
model.add(Reshape((4608,1)))
model.add(LSTM(16,return_sequences=True, dropout=0.5))
print ("LSTM",model.output_shape)
model.add(Dense(1, activation='softmax'))
model.compile(optimizer='adadelta', loss='categorical_crossentropy', metrics=['accuracy'])
model.summary()
X_data = np.array(X_data)
X_datatest = np.array(X_datatest)
model.add(Reshape((-1,128,128,3)))
hist=model.fit(X_data, X_data,epochs=15,batch_size=128,verbose = 2,validation_data=(X_data, X_data))
但是它给我以下错误
回溯(最近通话最近):文件 “ C:\ Users \ bdyssm \ Desktop \ Master \ LSTMCNN2.py”,第193行,在 model.add(Reshape((2892,1)))文件“ C:\ Users \ bdyssm \ AppData \ Local \ Programs \ Python \ Python35 \ lib \ site-packages \ keras \ engine \ sequential.py”, 第181行,添加 output_tensor = layer(self.outputs [0])文件“ C:\ Users \ bdyssm \ AppData \ Local \ Programs \ Python \ Python35 \ lib \ site-packages \ keras \ engine \ base_layer.py”, 第474行,在致电中 output_shape = self.compute_output_shape(input_shape)文件“ C:\ Users \ bdyssm \ AppData \ Local \ Programs \ Python \ Python35 \ lib \ site-packages \ keras \ layers \ core.py”, 第398行,在compute_output_shape中 input_shape [1:],self.target_shape)文件“ C:\ Users \ bdyssm \ AppData \ Local \ Programs \ Python \ Python35 \ lib \ site-packages \ keras \ layers \ core.py”, _fix_unknown_dimension中的第386行 引发ValueError(msg)ValueError:新数组的总大小必须保持不变
答案 0 :(得分:0)
添加密集层后,输出形状为:function get_colonn3($data) {
$this->db->select("table.colonn3")
->where([
"colonn3" => $data
]);
return $this->db->get("table")->result_array();
}
,因此总大小为(None, 4608, 1)
但是随后您想要添加一个重塑图层batch_size * 4608
,其总大小为Reshape((-1,128,128,3))
这是您的问题所在。
答案 1 :(得分:0)
如果您的第一个密集形状为60,则Reshape()必须成比例,例如3,20:
def create_model():
clf = Sequential()
clf.add(Dense(60, input_dim=Input_Dim, activation='relu'))
print('clf.output_shape1: ',clf.output_shape) # clf.output_shape1: (None, 60)
clf.add(Reshape((3,20)))
clf.add(LSTM(LSTM_out, dropout=0.5, recurrent_dropout=0.5))