Python 验证损失和验证精度曲线随预训练模型波动
我目前正在学习神经网络,当我试图学习CNN时遇到了问题,我试图训练包含音乐类型谱图的数据。我的数据由27000个光谱图组成,分为3类(类型)。我的数据按9:1的比例分割,用于培训和验证 有人能帮我吗?为什么我的验证损失/准确性会波动?我正在使用Keras的MobileNet V2,并将其与3个致密层连接。以下是我的代码片段:Python 验证损失和验证精度曲线随预训练模型波动,python,tensorflow,keras,conv-neural-network,mobilenet,Python,Tensorflow,Keras,Conv Neural Network,Mobilenet,我目前正在学习神经网络,当我试图学习CNN时遇到了问题,我试图训练包含音乐类型谱图的数据。我的数据由27000个光谱图组成,分为3类(类型)。我的数据按9:1的比例分割,用于培训和验证 有人能帮我吗?为什么我的验证损失/准确性会波动?我正在使用Keras的MobileNet V2,并将其与3个致密层连接。以下是我的代码片段: train_datagen = ImageDataGenerator( preprocessing_function=preprocess_input, v
train_datagen = ImageDataGenerator(
preprocessing_function=preprocess_input,
validation_split=0.1)
train_generator = train_datagen.flow_from_dataframe(
dataframe=traindf,
directory="...",
color_mode='rgb',
x_col="ID",
y_col="Class",
subset="training",
batch_size=32,
seed=42,
shuffle=True,
class_mode="categorical",
target_size=(64, 64))
valid_generator = train_datagen.flow_from_dataframe(
dataframe=traindf,
directory="...",
color_mode='rgb',
x_col="ID",
y_col="Class",
subset="validation",
batch_size=32,
seed=42,
shuffle=True,
class_mode="categorical",
target_size=(64, 64))
base_model = MobileNetV2(weights='imagenet', include_top=False)
x = base_model.output
x = GlobalAveragePooling2D()(x)
x = Dense(1025, activation='relu')(x)
x = Dense(1025, activation='relu')(x)
x = Dense(512, activation='relu')(x)
preds = Dense(3, activation='softmax')(x)
model = Model(inputs=base_model.input, outputs=preds)
model.compile(optimizer='adam', loss='categorical_crossentropy',
metrics=['accuracy'])
step_size_train = train_generator.n//train_generator.batch_size
step_size_valid = valid_generator.n//valid_generator.batch_size
history = model.fit_generator(
generator=train_generator,
steps_per_epoch=step_size_train,
validation_data=valid_generator,
validation_steps=step_size_valid,
epochs=75)
这是我的验证损失和验证精度曲线波动太大的图片
有没有办法减少波动或使其更好?我在这里是否有过盈或过盈的问题?我尝试过使用Dropout(),但它只会让情况变得更糟。我需要做什么来解决这个问题
谢谢你,
Aquilla Setiawan Kanadi.首先,验证损失和验证准确性的图片缺失 为了回答您的问题,下面可能是您的验证丢失和验证精度波动的原因-
import tensorflow as tf
from tensorflow.keras.models import Model
from tensorflow.keras.layers import GlobalAveragePooling2D, Dense
from keras.utils.layer_utils import count_params
class color:
PURPLE = '\033[95m'
CYAN = '\033[96m'
DARKCYAN = '\033[36m'
BLUE = '\033[94m'
GREEN = '\033[92m'
YELLOW = '\033[93m'
RED = '\033[91m'
BOLD = '\033[1m'
UNDERLINE = '\033[4m'
END = '\033[0m'
base_model = tf.keras.applications.MobileNetV2(weights='imagenet', include_top=False)
#base_model.summary()
trainable_count = count_params(base_model.trainable_weights)
non_trainable_count = count_params(base_model.non_trainable_weights)
print("\n",color.BOLD + ' base_model Statistics !' + color.END)
print("Trainable Parameters :", color.BOLD + str(trainable_count) + color.END)
print("Non Trainable Parameters :", non_trainable_count,"\n")
x = base_model.output
x = GlobalAveragePooling2D()(x)
x = Dense(1025, activation='relu')(x)
x = Dense(1025, activation='relu')(x)
x = Dense(512, activation='relu')(x)
preds = Dense(3, activation='softmax')(x)
model = Model(inputs=base_model.input, outputs=preds)
#model.summary()
trainable_count = count_params(model.trainable_weights)
non_trainable_count = count_params(model.non_trainable_weights)
print(color.BOLD + ' model Statistics !' + color.END)
print("Trainable Parameters :", color.BOLD + str(trainable_count) + color.END)
print("Non Trainable Parameters :", non_trainable_count,"\n")
new_weights_added = count_params(model.trainable_weights) - count_params(base_model.trainable_weights)
print("Additional trainable weights added to the model excluding basel model trainable weights :", color.BOLD + str(new_weights_added) + color.END)
输出-
WARNING:tensorflow:`input_shape` is undefined or non-square, or `rows` is not in [96, 128, 160, 192, 224]. Weights for input shape (224, 224) will be loaded as the default.
base_model Statistics !
Trainable Parameters : 2223872
Non Trainable Parameters : 34112
model Statistics !
Trainable Parameters : 5115398
Non Trainable Parameters : 34112
Additional trainable weights added to the model excluding basel model trainable weights : 2891526
base\u model.trainable=False
冻结基本模型,只需训练添加到MobileNetV2层顶部的新层# Let's take a look to see how many layers are in the base model
print("Number of layers in the base model: ", len(base_model.layers))
# Fine-tune from this layer onwards
fine_tune_at = 100
# Freeze all the layers before the `fine_tune_at` layer
for layer in base_model.layers[:fine_tune_at]:
layer.trainable = False
输出-
Number of layers in the base model: 155
我认为损失的画面不见了。此外,在模型训练期间共享日志也是一个好主意,无论有无辍学。如果数据是非机密的,您可以共享这些数据,这样我们就可以尝试在我们这边重现这个问题。谢谢