steps_per_epoch 和 validation_steps 在 Keras 模型中的行为

Behavior of steps_per_epoch and validation_steps in Keras Model

我对 fit 函数中 steps_per_epochvalidation_steps 的行为有点困惑。更具体地说,如果我将 steps_per_epoch 设置为小于 total_records/batch_size,会不会是 a) 模型只在每个时期训练相同的训练数据子集或 b) 模型将使用不同的训练每个时期的数据,并最终覆盖所有训练数据?

validation_steps同样的问题。

例如,如果我有 10000 行用于训练,批量大小为 10,并且 steps_per_epoch 设置为 10,那么模型是只在所有时期的前 100 行上训练还是将是在每个时期训练不同的 100 行,并在 100 个时期后遍历所有行?

我试图在文档和网上搜索它,但没有找到任何提及。

谢谢。

由于 docs state that using the parameter shuffle in model.fit(...) has no effect when using a generator and when steps_per_epoch is not None, it is essentially up to your data generator to shuffle the rows everytime it is called otherwise you will always get the same results. Check for example, how the ImageDataGenerator 有效:

import tensorflow as tf

BATCH_SIZE = 2

flowers = tf.keras.utils.get_file(
    'flower_photos',
    'https://storage.googleapis.com/download.tensorflow.org/example_images/flower_photos.tgz',
    untar=True)

img_gen = tf.keras.preprocessing.image.ImageDataGenerator(rescale=1./255, rotation_range=20)

ds = img_gen.flow_from_directory(flowers, batch_size=BATCH_SIZE, shuffle=True)

for x, y in ds:
  print(x.shape, y)
  break

使用 shuffle=True 每次都会产生不同的值,而 shuffle=False 总是 returns 相同的值。