简体   繁体   English

如何在keras中使用model.fit_generator

[英]How to use model.fit_generator in keras

When and how should I use fit_generator? 何时以及如何使用fit_generator? What is the difference between fit and fit_generator? fit和fit_generator有什么区别?

If you have prepared your data and labels in all necessary aspects and simply can assign these to an array x and y, than use model.fit(x, y). 如果您已准备好所有必要方面的数据和标签,只需将这些数据和标签分配给数组x和y,而不是使用model.fit(x,y)。

If you need to preprocess and/or augment your data while training, than you can take advantage of the generators that Keras provides. 如果您需要训练时预处理和/或扩充数据,那么您可以利用Keras提供的生成器。

You could for example augment images by applying random transforms (very helpful if you only have little data to train with), pad sequences, tokenize text, let Keras automagically read your data from a folder and assign appropiate classes (flow_from_directory) and much more. 例如,您可以通过应用随机变换来增强图像(如果您只有很少的数据可以训练,非常有用),填充序列,标记化文本,让Keras自动从文件夹中读取数据并分配适当的类(flow_from_directory)等等。

See here for examples and boilerplate code for image preprocessing: https://keras.io/preprocessing/image/ 请参阅此处以获取图像预处理的示例和样板代码: https//keras.io/preprocessing/image/

or here for text preprocessing: https://keras.io/preprocessing/text/ 或者在这里进行文本预处理: https//keras.io/preprocessing/text/

fit_generator also will help you to train in a more memory efficient way since you load data only when needed. fit_generator还将帮助您以更高效的内存方式进行训练,因为您只在需要时加载数据。 The generator function yields (aka "delivers") data to your model batch by batch on demand, so to say. 生成器函数按需批量生成(也称为“交付”)数据到您的模型批次,可以这么说。

They are useful for on-the-fly augmentations, which the previous poster mentioned. 它们对于前面提到的海报中提到的即时增强非常有用。 This however is not neccessarily restricted to generators, because you can fit for one epoch and then augment your data and fit again. 然而,这并不一定仅限于生成器,因为您可以适应一个纪元,然后再增加数据并再次适合。

What does not work with fit is using too much data per epoch though. 不合适的是每个时期使用过多的数据。 This means that if you have a dataset of 1 TB and only 8 GB of RAM you can use the generator to load the data on the fly and only hold a couple of batches in memory. 这意味着如果您拥有1 TB的数据集且只有8 GB的RAM,您可以使用生成器即时加载数据,并且只在内存中保留几个批次。 This helps tremendously on scaling to huge datasets. 这有助于扩展到大型数据集。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用 model.fit_generator 时,keras val 非常慢 - keras val very slow when use model.fit_generator 如何解决keras model.fit_generator 中的名称更改? - How to solve the name changing in keras model.fit_generator? CNN Keras model.fit 和 model.fit_generator - CNN Keras model.fit and model.fit_generator 在 Python 中使用生成器输入 Keras model.fit_generator - Using generator in Python to feed into Keras model.fit_generator Keras上的model.compile和model.fit_generator出错 - Error with model.compile and model.fit_generator on Keras Keras:我可以使用model.predict而不使用model.predict_generator来预测是否使用model.fit_generator训练模型 - Keras: can I use model.predict but not model.predict_generator to predict if I train the model with model.fit_generator 使用 tf.keras.utils.Sequence 和 model.fit_generator 和 use_multiprocessing=True 生成警告 - Using tf.keras.utils.Sequence with model.fit_generator with use_multiprocessing=True generated warning class_weight ='auto'for model.fit_generator keras - class_weight='auto' for model.fit_generator keras Keras model.fit_generator()给出0.0%的验证精度 - Keras model.fit_generator() gives 0.0% validation accuracy Keras:model.fit()和model.fit_generator()返回历史对象。 如何获得Keras模型? - Keras: model.fit() and model.fit_generator() return history objects. How do I get Keras models?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM