我如何在keras中使用batchsize?

How do I use batchsize in keras?

早上好!

我只是想澄清一下 - model.fit 中的 batch_size 参数是声明有多少样本进入,还是一次进入的样本数量 x/batch_size,其中总共有 x 个样本?

也就是说,假设我们有 20,000 个样本,我们将批量大小设置为 100。这是否意味着一次传入 200 个样本(意味着 100 个批次),或者 100 个样本?

我问是因为 https://deeplizard.com/learn/video/Skc8nqJirJg 说“如果我们立即将整个训练集传递给模型 (batch_size=1),那么我们刚才计算损失的过程将会发生在训练期间的每个时期结束时”,暗示它是一批。然而,batch_size 似乎根据它的名字有不同的意思,所以我想澄清一下。

谢谢!

注意:还有一个类似的问题,但没有回答 - How BatchSize in Keras works ? LSTM-WithState-Time Series

那补充说:这些样本是如何选择的?

来自 TensorFlow documentation:

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).

所以这是梯度更新前使用的样本数。如果 batch_size 等于 1,那么每个样本都会有一个梯度更新(因此,每个 epoch num_samples)

例如,对于您引用的示例:如果我们有 20,000 个样本,并且我们将批量大小设置为 100,则一次传递 100 个样本。

That adds: how are those samples chosen?

这取决于fit方法的shuffle参数是否为True。如果是,它们将被随机抽取,直到所有样本都被选中(时代结束)。如果不是,则按顺序进行