Tensorflow:损失和准确度曲线显示类似的行为

时间:2018-03-18 07:52:21

标签: python tensorflow machine-learning deep-learning keras

我正在使用带有Tensorflow后端的Keras来训练修改后的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
)

1 个答案:

答案 0 :(得分:1)

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

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)

    let monster = monsters[indexPath.row]
    cell.textLabel?.text = monster.name
    cell.detailTextLabel?.text = monster.subtitle
    let label = UILabel.init(frame: CGRect(x:0,y:0,width:100,height:20))
    label.text = monster.name
    cell.accessoryView = label

    return cell
}

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