简体   繁体   中英

How to use model.fit_generator in keras

When and how should I use fit_generator? What is the difference between fit and 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).

If you need to preprocess and/or augment your data while training, than you can take advantage of the generators that Keras provides.

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.

See here for examples and boilerplate code for image preprocessing: https://keras.io/preprocessing/image/

or here for text preprocessing: 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. 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. This helps tremendously on scaling to huge datasets.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM