Keras的loss_weights和class_weight

loss_weights

是model.compile的参数,对应于模型的每个输出的损失的权重。loss_weights是一个列表,对应于每个输出的权重,默认1.

Model.compile(
    optimizer="rmsprop",
    loss=None,
    metrics=None,
    loss_weights=None,
    weighted_metrics=None,
    run_eagerly=None,
    steps_per_execution=None,
    **kwargs
)
  • loss_weights: Optional list or dictionary specifying scalar coefficients (Python floats) to weight the loss contributions of different model outputs. The loss value that will be minimized by the model will then be the weighted sum of all individual losses, weighted by the loss_weights coefficients. If a list, it is expected to have a 1:1 mapping to the model's outputs. If a dict, it is expected to map output names (strings) to scalar coefficients.

class_weight

是model.fit的参数,对应于样本类别的权重,可以更加关注数量少得样本。

当数据集不平衡时,可以为每个类设置类权重。假设有 5000 个类狗样本和 45000 个类非狗样本,class_weight= [0:5,1:0.5],这给类"狗"10倍的权重作用在损失函数。

Model.fit(
    x=None,
    y=None,
    batch_size=None,
    epochs=1,
    verbose="auto",
    callbacks=None,
    validation_split=0.0,
    validation_data=None,
    shuffle=True,
    class_weight=None,
    sample_weight=None,
    initial_epoch=0,
    steps_per_epoch=None,
    validation_steps=None,
    validation_batch_size=None,
    validation_freq=1,
    max_queue_size=10,
    workers=1,
    use_multiprocessing=False,
)
  • class_weight: Optional dictionary mapping class indices (integers) to a weight (float) value, used for weighting the loss function (during training only). This can be useful to tell the model to "pay more attention" to samples from an under-represented class.

猜你喜欢

转载自blog.csdn.net/dou3516/article/details/121086133