For small, simplistic datasets it’s perfectly acceptable to use Keras’ .fit function.

These datasets are often not very challenging and do not require any data augmentation.

However, real-world datasets are rarely that simple:

  • Real-world datasets are often too large to fit into memory.
  • They also tend to be challenging, requiring us to perform data augmentation to avoid overfitting and increase the ability of our model to generalize.

In those situations we need to utilize Keras’ .fit_generator function:

# initialize the number of epochs and batch size
EPOCHS = 100
BS = 32# construct the training image generator for data augmentation
aug = ImageDataGenerator(rotation_range=20, zoom_range=0.15,width_shift_range=0.2, height_shift_range=0.2, shear_range=0.15,horizontal_flip=True, fill_mode="nearest")# train the network
H = model.fit_generator(aug.flow(trainX, trainY, batch_size=BS),validation_data=(testX, testY), steps_per_epoch=len(trainX) // BS,epochs=EPOCHS)

Here we start by first initializing the number of epochs we are going to train our network for along with the batch size.

We then initialize aug , a Keras ImageDataGenerator object that is used to apply data augmentation, randomly translating, rotating, resizing, etc. images on the fly.

Performing data augmentation is a form of regularization, enabling our model to generalize better.

However, applying data augmentation implies that our training data is no longer “static” — the data is constantly changing.

Each new batch of data is randomly adjusted according to the parameters supplied to ImageDataGenerator.

Thus, we now need to utilize Keras’ .fit_generator function to train our model.

As the name suggests, the .fit_generator function assumes there is an underlying function that is generating the data for it.

The function itself is a Python generator.

Internally, Keras is using the following process when training a model with .fit_generator:

  1. Keras calls the generator function supplied to .fit_generator (in this case, aug.flow ).
  2. The generator function yields a batch of size BS to the .fit_generator function.
  3. The .fit_generator function accepts the batch of data, performs backpropagation, and updates the weights in our model.
    This process is repeated until we have reached the desired number of epochs.
    You’ll notice we now need to supply a steps_per_epoch parameter when calling .fit_generator (the .fit method had no such parameter).

Why do we need steps_per_epoch ?

Keep in mind that a Keras data generator is meant to loop infinitely — it should never return or exit.

Since the function is intended to loop infinitely, Keras has no ability to determine when one epoch starts and a new epoch begins.

Therefore, we compute the steps_per_epoch value as the total number of training data points divided by the batch size. Once Keras hits this step count it knows that it’s a new epoch.

.fit VS .fit_generator in Keras相关推荐

  1. [深度学习] Keras 如何使用fit和fit_generator

    介绍 在本教程中,您将了解Keras .fit和.fit_generator函数的工作原理,包括它们之间的差异.为了帮助您获得实践经验,我已经提供了一个完整的示例,向您展示如何从头开始实现Keras数 ...

  2. fit,fit_generator的使用区别

    fit函数 根据[1],当你使用.fit()函数时,意味着如下两个假设: 训练数据可以 完整地 放入到内存(RAM)里 数据已经不需要再进行任何处理了 ######################## ...

  3. Tensorflow学习之tf.keras(一) tf.keras.layers.Model(另附compile,fit)

    模型将层分组为具有训练和推理特征的对象. 继承自:Layer, Module tf.keras.Model(*args, **kwargs ) 参数 inputs 模型的输入:keras.Input ...

  4. keras和tensorflow使用 fit_generator 批次训练

    fit_generator 是 keras 提供的用来进行批次训练的函数,使用方法如下: model.fit_generator(generator, steps_per_epoch=None, ep ...

  5. keras优化算法_目标检测算法 - CenterNet - 代码分析

    代码出处 吃水不忘打井人,分析github上的基于keras的实现: xuannianz/keras-CenterNet​github.com 代码主体结构 模型训练的主函数流程如下所示,该流程也是使 ...

  6. keras train_on_batch详解(train_on_batch的输出输入详解,train_on_batch多GPU训练详解,自定义学习率调整策略)

    利用 train_on_batch 精细管理训练过程 大部分使用 keras 的同学使用 fit() 或者 fit_generator() 进行模型训练, 这两个 api 对于刚接触深度学习的同学非常 ...

  7. tf.keras下常用模块 activations、applications、datasets、layers、losses、optimizers、regularizers、Sequential

    日萌社 人工智能AI:Keras PyTorch MXNet TensorFlow PaddlePaddle 深度学习实战(不定时更新) 1.activations:tf.keras.activati ...

  8. 神经网络贷款风险评估(base on keras and python )

    用我儿子的话说,有一天啊,小乌龟遇见小兔子--- 有一天,我在网上看到这样一片文章,决策书做贷款决策分析. 贷还是不贷:如何用Python和机器学习帮你决策? import pandas as pd ...

  9. 神经网络贷款风险评估(base on keras and python ) 原创 2017年08月18日 14:35:17 标签: python / 神经网络 / keras 300 用我

    神经网络贷款风险评估(base on keras and python ) 原创 2017年08月18日 14:35:17 标签: python / 神经网络 / keras / 300 编辑 删除 ...

  10. PYTHON链家租房数据分析:岭回归、LASSO、随机森林、XGBOOST、KERAS神经网络、KMEANS聚类、地理可视化...

    全文下载链接:http://tecdat.cn/?p=29480 作者:Xingsheng Yang 1 利用 python 爬取链家网公开的租房数据: 2 对租房信息进行分析,主要对房租相关特征进行 ...

最新文章

  1. SpringBoot第十八篇: 定时任务(Scheduling Tasks)
  2. RadioGroup结合RadioButton使用切换Fragment片段
  3. IBM专家谈(一)能源效率是POWER7的另一个优势
  4. Java学习之Iterator(迭代器)的一般用法(转)
  5. 宏定义函数container_of的解释
  6. 使用PostgREST构建PostgreSQL数据库的REST风格API
  7. 超赞Transformer+CNN=SOTA!
  8. 谈谈Winform程序的界面设计
  9. python信息安全书籍_信息安全从业者书单推荐
  10. FeatureLayer到本地shapefile文件
  11. 相机裁剪旋转_感受大画幅相机随心所欲的景深控制
  12. java用那个软件编,java编译软件 编写java程序用什么软件?
  13. 前端开发程序员的月薪到底有多高?
  14. 求职面试技巧_开始求职的7个技巧
  15. oho,找工作有点难度
  16. python ui界面设计(二)
  17. JavaRSAJS加密解密(整合版,仅供自己参考)
  18. win10修复ubuntu18.04引导
  19. 如何写好科研论文笔记 李玉军 清华大学 学堂在线课程
  20. Fortran进行t检验后使用GrADS画打点图

热门文章

  1. 如何清除浏览器历史记录-在Chrome,Firefox和Safari中删除浏览历史记录
  2. 我在阿里工作的这段时间里,都学到了哪些东西
  3. 开发者如何提升和推销自己
  4. 一周热图|倪妮现身1664法蓝晚宴;贾静雯代言艾美特;刘嘉玲助力苏州国际设计周...
  5. d : 无法将“d”项识别为 cmdlet、函数、脚本文件或可运行程序的名称
  6. VS2017--无法添加引用--提示“未能完成操作。不支持此接口”
  7. appcan代码迁出失败
  8. 华为太极magisk安装教程_【极客教程】如何让不支持指纹支付的手机也能用上指纹支付?...
  9. 盘点CSV文件在Excel中打开后乱码问题的两种处理方法
  10. 服务器cpu型号E5,超强悍服务器CPU:Intel 18核心至强E5