fit()和image_dataset_from_directory()中batch_size的区别是什么? [英] whats the difference in batch_size in fit() and image_dataset_from_directory()?
问题描述
我尝试在训练时更改批次大小,因此我在循环中运行fit方法并更改批次大小,但是我们已经在 image_dataset_from_directory()
中定义了批次大小,如果我放不同这两个函数中的批处理大小,并且有一种方法可以让我用fit方法控制批处理大小,而不受 image_dataset_from_directory()
的影响这是我的代码
I was trying to change batch size while training so I run the fit method in a loop and change the batch size, but we already define batch size in image_dataset_from_directory()
, What if I put different batch sizes in these two functions, and is there a way that I get to control the batch size in fit method and not be affected by image_dataset_from_directory()
Heres my code
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
)
代码已从此张量流的教程中提取
the code has been taken from this tutorial of tensorflow
推荐答案
image_dataset_from_directory
是生成器,因此在 model.fit()中指定
batch_size
代码>不会执行任何操作.请参阅 model.fit()
上的文档:
image_dataset_from_directory
is a generator and so specifying batch_size
in model.fit()
will do nothing. See the docs on model.fit()
:
batch_size 整数或无.每个梯度更新的样本数.如果未指定,batch_size将默认为32.如果您的数据采用数据集,生成器或keras.utils.Sequence实例的形式(因为它们生成批次),则不要指定batch_size.
batch_size Integer or None. Number of samples per gradient update. If unspecified, batch_size will default to 32. Do not specify the batch_size if your data is in the form of datasets, generators, or keras.utils.Sequence instances (since they generate batches).
在您所引用的示例中可以看到,Tensorflow教程未在 model.fit()
中指定 batch_size
:
As you can see in the example you referred, the Tensorflow tutorial doesn't specify batch_size
in model.fit()
:
epochs=10
history = model.fit(
train_ds,
validation_data=val_ds,
epochs=epochs
)
批次大小将是您在生成器中指定的大小,并且在 model.fit()
The batch size will be what you specified in the generator, and the argument will be ignored in model.fit()
这篇关于fit()和image_dataset_from_directory()中batch_size的区别是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!