我使用 tensorflow 创建了一个自定义 CNN 来执行 3 类分类。我尝试了一个没有类不平衡的数据集。但是 CNN 分类报告(来自 sklearn)显示 CNN 仅预测单个类别。
CNN的架构:
model = tf.keras.Sequential([
Conv2D(32, (3, 3), activation='relu', input_shape=(150, 150, 1), padding='same', data_format = 'channels_last'),
BatchNormalization(),
MaxPooling2D((2, 2), padding='same'),
Conv2D(64, (3, 3), activation='relu', padding='same'),
BatchNormalization(),
MaxPooling2D((2, 2), padding='same'),
Conv2D(128, (3, 3), activation='relu', padding='same'),
BatchNormalization(),
MaxPooling2D((2, 2), padding='same'),
Conv2D(256, (3, 3), activation='relu', padding='same'),
BatchNormalization(),
MaxPooling2D((2, 2), padding='same'),
Flatten(),
Dense(256, activation='relu', kernel_regularizer=tf.keras.regularizers.l2(0.01)),
Dropout(0.5),
Dense(128, activation='relu', kernel_regularizer=tf.keras.regularizers.l2(0.01)),
Dropout(0.5),
Dense(3, activation='softmax')
])
我尝试更改 lr,确实添加了 BatchNormalization 和 dropout。还添加了 L2 正则化项,但没有改变结果。
这是运行 10 个 epoch 后的分类报告。每个班级都有 10000 个样本。当我尝试使用 pytorch 时也发生了同样的情况。
precision recall f1-score support
no 0.00 0.00 0.00 10000
sphere 0.33 1.00 0.50 10000
vort 0.00 0.00 0.00 10000
accuracy 0.33 30000
macro avg 0.11 0.33 0.17 30000
weighted avg 0.11 0.33 0.17 30000
有趣的是,有时被预测的类别发生变化但结果保持不变。
我也洗牌了我的数据集:
BATCH_SIZE = 32
train_dataset = train_dataset.shuffle(buffer_size=train_len).batch(BATCH_SIZE)
val_dataset = val_dataset.batch(BATCH_SIZE)
我该怎么办?