仅使用batch_size和仅使用steps_per_epoch的Keras model.fit之间的区别 [英] Difference between Keras model.fit using only batch_size and using only steps_per_epoch

查看:503
本文介绍了仅使用batch_size和仅使用steps_per_epoch的Keras model.fit之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我同时使用batch_size和steps_per_epoch参数运行model.fit时,出现以下错误:

When I run model.fit using both batch_size and steps_per_epoch parameters I receive the following error:

ValueError: If steps_per_epoch is set, the `batch_size` must be None.

因此,从该错误以及以下文档中,来自keras模型(功能性API)的

So, from this error and from the following piece of documentation from keras Model(functional API)

batch_size:整数或无.每个梯度更新的样本数.如果 未指定,batch_size默认为32.

batch_size: Integer or None. Number of samples per gradient update. If unspecified, batch_size will default to 32.

steps_per_epoch :整数或无.总步骤数(一批样品) 在声明一个纪元完成并开始下一个纪元之前.训练时 使用TensorFlow数据张量等输入张量时,默认值None等于数据集中的样本数除以批次大小;如果无法确定,则默认为1.

steps_per_epoch: Integer or None. Total number of steps (batches of samples) before declaring one epoch finished and starting the next epoch. When training with input tensors such as TensorFlow data tensors, the default None is equal to the number of samples in your dataset divided by the batch size, or 1 if that cannot be determined.

我知道这两个参数在某种程度上是等效的.但是,在我的笔记本电脑上(使用带有2GB VRAM的GeForce 940M图形卡并训练cifar10数据集),当我运行epochs参数设置为256的model.fit时,脚本运行良好,并且来自keras的反馈是这样的: /p>

I understand that both parameters are somehow equivalent. But, in my laptop, (with a GeForce 940M graphics card with 2GB of VRAM and training the cifar10 dataset) when I run model.fit with epochs parameter set to 256 the script runs fine and the feedback gave from keras is like this:

4608/50000 [=>............................] - ETA: 1:59 - loss: 0.8167 - acc: 0.7398

更新第一个数字总是加256个单位.但是,当将steps_per_epoch作为number_train//batch_size传递时,我用完了内存,除非我将batch_size传递为1,否则无法运行脚本.

updating the first number always adding 256 units. However, when passing steps_per_epoch as the number_train//batch_size I run out of memory and cannot run my script unless I pass batch_size as 1.

那么,model.fit如何使用这些参数?当我只使用其中一个而不是另一个时,有什么区别?

So, how model.fit work with those parameters? What is the difference when I use just one of those instead the another one?

推荐答案

这是一个很好的问题.我从源代码中观察到的内容( [1 ] [2] )是:

That's a good question. What I observe from the source code ([1] and [2]) is that:

  • 设置batch_size时,将训练数据切成此大小的批次(请参阅 L152 ),以及这就是为什么您会遇到内存错误.
  • When you set batch_size, the training data is sliced into batches of this size (see L184).
  • When you set steps_per_epoch, if the training inputs are not framework-native tensors (this is the most common case), the whole training set is being fed into the network in a single batch (see L152), and that's why you get the memory error.

因此,基于实现,我建议仅在通过框架本机张量(即第一维为批处理大小的TensorFlow张量)馈送时使用参数steps_per_epoch,这确实是一个要求.为此,必须将model.fit中的参数xy设置为None.

Therefore, based on the implementation, I would advise to use the argument steps_per_epoch only when feeding through framework-native tensors (i.e. TensorFlow tensors with the first dimension being the batch size), and that is indeed a requirement. In order to do this, the arguments x and y from model.fit need to be set to None.

这篇关于仅使用batch_size和仅使用steps_per_epoch的Keras model.fit之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆