我有一个像这样的Keras模型:
model=Sequential()
'''
filters=number of conv kernel
strides=step
'''
model.add(Convolution1D(filters=1000, kernel_size=11,strides=3,
input_shape=(train_num,data_dim),padding='same',
activation='relu',kernel_initializer='uniform'))
model.add(Convolution1D(filters=1000, kernel_size=7, strides=2, padding='same',
activation='relu',kernel_initializer='uniform'))
model.add(Convolution1D(filters=1000, kernel_size=3, strides=1, padding='same',
activation='relu',kernel_initializer='uniform'))
model.add(Dense(128,activation='relu'))
model.add(Dropout(0.3))
model.add(Dense(128,activation='relu'))
model.add(Dropout(0.3))
model.add(Dense(128,activation='relu'))
model.add(Dropout(0.3))
model.add(Dense(3,activation='softmax'))
model.compile(loss='categorical_crossentropy',optimizer='sgd',metrics=['accuracy'])
model.summary()
print("training starts.....")
model.fit(trX,trY,epochs=30,batch_size=100)
accuracy=model.evaluate(teX,teYY,batch_size=100)
predict_y = model.predict(teX)
print('\nTest accuracy:',accuracy[1])
#predict=model.predict(teY,batch_size=30)
save_model(model,'my_model_com3hl')
我的输入数据形状为(1,855,1000)
。这意味着我有855个样本,每个样本有1000个暗角。但是当我训练时,输出形状的样本暗度像这样下降,并发生以下错误:
检查目标时出错:预期density_101具有形状(143,3) 但得到形状数组(855,3)
这是模型摘要:
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
conv1d_50 (Conv1D) (None, 285, 1000) 11001000
_________________________________________________________________
conv1d_51 (Conv1D) (None, 143, 1000) 7001000
_________________________________________________________________
conv1d_52 (Conv1D) (None, 143, 1000) 3001000
_________________________________________________________________
dense_98 (Dense) (None, 143, 128) 128128
_________________________________________________________________
dropout_71 (Dropout) (None, 143, 128) 0
_________________________________________________________________
dense_99 (Dense) (None, 143, 128) 16512
_________________________________________________________________
dropout_72 (Dropout) (None, 143, 128) 0
_________________________________________________________________
dense_100 (Dense) (None, 143, 128) 16512
_________________________________________________________________
dropout_73 (Dropout) (None, 143, 128) 0
_________________________________________________________________
dense_101 (Dense) (None, 143, 3) 387
=================================================================
答案 0 :(得分:0)
根据您的评论:
是的,这是一个三类分类问题。我有855个培训 样品和214个测试样品。两个样本的尺寸均为1000 字符。标签处于单热模式。 trX,trY的形状 teX,teYY是 (855,1000,1),(214,3,1),(214,1000,1),(214,3,1),分别...
每个形状为(1000,1)
的样本都归为三类之一。因此,要使网络的输出形状与标签形状兼容,应在第一个密集层之前使用Flatten
层:
model.add(Flatten())
model.add(Dense(128,activation='relu'))
model.add(Dropout(0.3))
# ... the rest
现在,模型的输出形状为(None,3)
,这正是我们所需要的。您可能还需要删除标签的最后一个轴,以使它们的形状为(*,3)
(而不是(*,3,*)
):
trY = np.squeeze(trY, axis=2)
teYY = np.squeeze(teYY, axis=2)
此外,别忘了我对input_shape
的评论。您还需要修复它。