我对使用机器学习和keras非常陌生。这是我的第一个CNN模型。我建立了一个模型,将声音分类为UrbanSound8kDataset。当我在一个小的虚拟集上运行模型时,我不断收到以下错误:
ValueError: Error when checking target: expected dense_1 to have shape (16, 1, 2) but got array with shape (2, 1, 1)
我查看了其他存在类似问题的帖子,并尝试将模型损失更改为sparse_categorical_crossentropy而不是categorical_crossentropy。我还在第一个密集层之前添加了model.flatten()。不幸的是,这些都没有解决我的错误。
这是我的模型的相关代码:
num_labels = yy.shape[1]
filter_size = 2
num_rows = 1
num_columns = 1
num_channels = 1
x_train = x_train.reshape(x_train.shape[0], num_rows, num_columns, num_channels)
y_train = y_train.reshape(y_train.shape[0], 2, num_columns, num_channels)
x_test = x_test.reshape(x_test.shape[0], 1, num_columns, num_channels)
y_test = y_test.reshape(y_test.shape[0], 2, num_columns, num_channels)
model = Sequential()
model.add(Conv2D(filters=16, kernel_size=2, input_shape=(num_rows,
num_columns, num_channels), activation='relu', data_format='channels_first', padding="same"))
model.add(MaxPooling2D(pool_size=1))
model.add(Dropout(0.2))
model.add(Conv2D(filters=32, kernel_size=1, activation='relu'))
model.add(MaxPooling2D(pool_size=1))
model.add(Dropout(0.2))
model.add(Conv2D(filters=64, kernel_size=1, activation='relu'))
model.add(MaxPooling2D(pool_size=1))
model.add(Dropout(0.2))
model.add(Conv2D(filters=64, kernel_size=1, activation='relu'))
model.add(MaxPooling2D(pool_size=1))
model.add(Dropout(0.2))
#Flatten layer here:
model.add(Dense(num_labels, activation='softmax'))
model.compile(loss='categorical_crossentropy', metrics=['accuracy'], optimizer='adam')
model.summary()
num_epochs = 100
num_batch_size = 132
start = datetime.now()
model.fit(x_train, y_train, batch_size=num_batch_size, epochs=num_epochs, validation_data=(x_test, y_test), verbose=1)
这是模型摘要:
Layer (type) Output Shape Param #
=================================================================
conv2d_1 (Conv2D) (None, 16, 1, 1) 80
_________________________________________________________________
max_pooling2d_1 (MaxPooling2 (None, 16, 1, 1) 0
_________________________________________________________________
dropout_1 (Dropout) (None, 16, 1, 1) 0
_________________________________________________________________
conv2d_2 (Conv2D) (None, 16, 1, 32) 64
_________________________________________________________________
max_pooling2d_2 (MaxPooling2 (None, 16, 1, 32) 0
_________________________________________________________________
dropout_2 (Dropout) (None, 16, 1, 32) 0
_________________________________________________________________
conv2d_3 (Conv2D) (None, 16, 1, 64) 2112
_________________________________________________________________
max_pooling2d_3 (MaxPooling2 (None, 16, 1, 64) 0
_________________________________________________________________
dropout_3 (Dropout) (None, 16, 1, 64) 0
_________________________________________________________________
conv2d_4 (Conv2D) (None, 16, 1, 64) 4160
_________________________________________________________________
max_pooling2d_4 (MaxPooling2 (None, 16, 1, 64) 0
_________________________________________________________________
dropout_4 (Dropout) (None, 16, 1, 64) 0
_________________________________________________________________
dense_1 (Dense) (None, 16, 1, 2) 130
=================================================================
Total params: 6,546
Trainable params: 6,546
Non-trainable params: 0
____________________________
该错误发生在我得出的代码的最后一行,即model.fit行中。
如果需要添加更多代码,请告诉我。这只是我第二次在这里发布,因此我不确定要包含多少代码。
非常感谢!