【问题标题】:whats the difference in batch_size in fit() and image_dataset_from_directory()?fit() 和 image_dataset_from_directory() 中的 batch_size 有什么区别?
【发布时间】:2021-03-03 17:29:01
【问题描述】:

我在训练时尝试更改批量大小,因此我在循环中运行 fit 方法并更改批量大小,但我们已经在 image_dataset_from_directory() 中定义了批量大小,如果我在这两个函数中放置不同的批量大小会怎样,有没有办法让我在 fit 方法中控制批量大小并且不受image_dataset_from_directory() 的影响 这是我的代码

  train_ds = tf.keras.preprocessing.image_dataset_from_directory(
  data_dir,
  validation_split=0.2,
  subset="training",
  seed=123,
  image_size=(img_height, img_width),
  batch_size=batch_size)

  model.fit(
      
            train_ds,
            validation_data=val_ds,
            epochs=epoch,
            batch_size=batch_size,
            steps_per_epoch=10,
            callbacks=call

            )

代码取自 tensorflow 的 tutorial

【问题讨论】:

    标签: python tensorflow keras deep-learning


    【解决方案1】:

    image_dataset_from_directory 是一个生成器,因此在model.fit() 中指定batch_size 将无济于事。请参阅model.fit() 上的文档:

    batch_size 整数或无。每次梯度更新的样本数。如果未指定,batch_size 将默认为 32。如果您的数据是数据集、生成器或 keras.utils.Sequence 实例的形式(因为它们会生成批次),请不要指定 batch_size。

    正如您在提到的示例中看到的那样,Tensorflow 教程没有在 model.fit() 中指定 batch_size:

    epochs=10
    history = model.fit(
      train_ds,
      validation_data=val_ds,
      epochs=epochs
    )
    

    批量大小将是您在生成器中指定的大小,model.fit() 中的参数将被忽略

    【讨论】:

      猜你喜欢
      • 2021-11-21
      • 2019-12-05
      • 1970-01-01
      • 2015-08-22
      • 2018-08-25
      • 2020-10-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多