gpt4 book ai didi

python - Tensorflow:损失和准确度曲线表现出相似的行为

转载 作者:太空宇宙 更新时间:2023-11-04 04:47:00 24 4
gpt4 key购买 nike

我正在使用 Keras 和 Tensorflow 后端来训练经过修改的 Resnet-50,它将对象分为 15 个类别。我正在使用 Adam 优化器,我尝试了 0.001 和 0.01 的学习率,但得到了相似的结果。

我面临的问题是损失和准确性都表现出相似的行为(在训练和验证数据集中)。它们在相似的时间上升或下降,我希望随着损失的减少而获得更高的准确度。是什么导致了这种行为?

这是我上次运行的一些 Tensorboard 曲线: Training Set Accuracy and Loss

Validation Set Accuracy and Loss

编辑:该模型的代码如下:

#Model creation:
def create_model(possible_labels):
rn50 = ResNet50(include_top=True, weights=None)
layer_name = rn50.layers[-2].name
model = Model(rn50.input,
Dense(len(possible_labels))(rn50.get_layer(layer_name).output))
adam = Adam(lr=0.0001)
model.compile(loss='categorical_crossentropy',
optimizer=adam, metrics=['accuracy'])
checkpointer = ModelCheckpoint(filepath='the_best_you_ever_had',
verbose=1, save_best_only=True)
tensorboard = TensorBoard()
return model, [checkpointer, tensorboard]

model, checkpointers = create_model(labels)

#Dataset generation:
train_datagen = ImageDataGenerator(
featurewise_center=True,
featurewise_std_normalization=True,
rotation_range=20,
width_shift_range=0.2,
height_shift_range=0.2,
horizontal_flip=True,
vertical_flip=True,
channel_shift_range=0.2,
shear_range=0.2,
zoom_range=0.2
)

val_datagen = ImageDataGenerator()

train_generator = train_datagen.flow_from_directory(
'data\\train',
target_size=(224, 224),
batch_size=32,
class_mode='categorical'
)

val_generator = val_datagen.flow_from_directory(
'data\\validation',
target_size=(224, 224),
batch_size=32,
class_mode='categorical'
)

#Model training:
model.fit_generator(
train_generator,
steps_per_epoch=5000,
epochs=50,
validation_data=val_generator,
callbacks=checkpointers
)

最佳答案

我在我的代码中发现了错误,事实上我只是在我添加的最后一层中使用了默认(线性)激活。我通过在代码中执行此操作将其切换为 softmax 激活(因为它是分类问题而不是回归问题):

model = Model(rn50.input, 
Dense(len(possible_labels), activation='softmax')
(rn50.get_layer(layer_name).output))

然后曲线开始按预期运行,我达到了 96% 的准确率。

关于python - Tensorflow:损失和准确度曲线表现出相似的行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49345671/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com