对 Keras 中的 steps_per_epoch 和 validation_steps 感到困惑

数据挖掘 喀拉斯 张量流
2022-01-29 12:12:34

根据 TF 2 中的文档:

       steps_per_epoch: Integer or `None`.
            Total number of steps (batches of samples)
            before declaring one epoch finished and starting the
            next epoch. When training with input tensors such as
            TensorFlow data tensors, the default `None` is equal to
            the number of samples in your dataset divided by
            the batch size, or 1 if that cannot be determined. If x is a
            `tf.data` dataset, and 'steps_per_epoch'
            is None, the epoch will run until the input dataset is exhausted.
            This argument is not supported with array inputs.
        validation_steps: Only relevant if `validation_data` is provided and
            is a `tf.data` dataset. Total number of steps (batches of
            samples) to draw before stopping when performing validation
            at the end of every epoch. If validation_data is a `tf.data` dataset
            and 'validation_steps' is None, validation
            will run until the `validation_data` dataset is exhausted.

我不明白这部分:

Total number of steps (batches of samples)
                before declaring one epoch finished and starting the
                next epoch.

如果我有 1000 个样本,批量大小 = 100,那么一个 epoch 将需要 10 个步骤才能达到。为什么需要另一个steps_per_epoch?如果两者都使用,则它们是冲突的。不是吗?如果批量大小为 100,则需要 10 个步骤。如果steps_per_epoch = 20,则意味着一个epoch需要20个批次,这与通过批次大小参数100计算的“10”步相冲突。

我哪里错了?

1个回答

这种方法还有一个额外的功能,如果损失停滞太多,可以考虑使用迷你时代

通常它等于n_samples // batch_size,但是

steps_per_epoch在更新学习率时,让您有机会使用ReduceLROnPlateuau(). 此回调在每个 epoch 完成后检查损失的下降,如果损失在patience多个连续的 epoch 中停滞不前,则回调会降低学习率以“慢煮”网络。如果您的数据集很大,通常需要使用生成器时,您可能希望在单个 epoch 内衰减学习率(因为它包含大量数据)。这可以通过设置steps_per_epoch一个小于 n_samples // batch_size而不影响模型训练周期总数的值来实现。