【问题标题】:Combining Keras model.fit's `steps_per_epoch` with TensorFlow's Dataset API's `batch()`将 Keras model.fit 的 `steps_per_epoch` 与 TensorFlow 的 Dataset API 的 `batch()` 相结合
【发布时间】:2019-02-07 14:35:42
【问题描述】:

我正在研究使用 Keras+TensorFlow 训练 CNN 模型期间的性能和 GPU 使用情况。与this question 类似,我很难理解Keras model.fitsteps_per_epoch 和TensorFlow 的Dataset API 的.batch() 的组合使用:我在输入管道dataset = dataset.batch(batch_size) 和后来我用

fit = model.fit(dataset, epochs=num_epochs, steps_per_epoch=training_set_size//batch_size)

但我发现实际上可以在每个 epoch 设置任意数量的步数,甚至比 training_set_size//batch_size 还要多。从文档中我了解到,在 Keras 上,一个时代不一定像往常一样通过整个训练集,但无论如何我有点困惑,现在我不完全确定我是否正确使用它。

dataset.batch(batch_size) + steps_per_epoch=training_set_size//batch_size 是否定义了一个小批量 SGD,它通过小批量 batch_size 样本在整个训练集上运行?如果 steps_per_epoch 设置为大于 training_set_size//batch_size,则训练集的 epoch 是否大于 1 次?

【问题讨论】:

    标签: tensorflow keras tensorflow-datasets


    【解决方案1】:

    steps_per_epoch 是您设置的批量大小在一个时期内通过网络运行的批次数。

    您将steps_per_epoch 设置为training_set_size//batch_size 是有充分理由的。这确保了所有数据都在一个 epoch 中进行训练,提供精确除数的数字(如果不是,则由 // 运算符四舍五入)。

    也就是说,如果您的批大小为 10,训练集大小为 30,则steps_per_epoch = 3 确保使用所有数据。

    并引用您的问题:

    “如果steps_per_epoch设置为大于training_set_size//batch_size,训练集的epoch是否大于一次?”

    是的。一些数据会在同一个epoch中再次通过。

    【讨论】:

    • 感谢您的回答!我现在看到我的脚本确实做了我想做的事。
    • 我多么希望早点找到你的答案!
    猜你喜欢
    • 2018-02-18
    • 2019-03-02
    • 2018-02-23
    • 2019-08-21
    • 1970-01-01
    • 1970-01-01
    • 2018-10-21
    • 2016-10-19
    • 1970-01-01
    相关资源
    最近更新 更多