can you tell me what is the difference these two?
Difference between fit and fit_generator
The difference between fit and fit_generator
First, the x_train and y_train passed in the fit() function in Keras are completely loaded into the memory. Of course, it is very convenient to use, but if we have a large amount of data, it is impossible to load all the data into the memory. Will cause a memory leak, this time we can use the fit_generator function to train.
Here is an example of a fit argument:
history = model.fit(x_train, y_train, epochs=10,batch_size=32, validation_split=0.2)
Here you need to give epochs and batch_size. epoch is how many times this data set is to be rounded. Batch_size refers to how many batches this data set is divided into.
Finally, the size of the cross-validation set can be given, where 0.2 means 20% of the training set.
The fit_generator function must pass in a generator, and our training data is also generated by the generator. Here is a simple generator function:
batch_size = 128
def generator():
while 1:
row = np.random.randint(0,len(x_train),size=batch_size)
x = np.zeros((batch_size,x_train.shape[-1]))
y = np.zeros((batch_size,))
x = x_train[row]
y = y_train[row]
yield x,y
The generator function here I generated is a batch_size of 128 size data, this is just a demo. If I don’t specify the size of batch_size in the generator, it is to generate a data each time, then the parameter steps_per_epoch is different when using fit_generator. I have been confused for a long time here, although it is not a big problem.
Here is the pass argument to the fit_generator function:
history=model.fit_generator(generator(),epochs=epochs,steps_per_epoch=len(x_train)//(batch_size*epochs))
@chiraggandhi70726 Can you also tell how can we make the validation set as in fit, we have used validation_split or we use validation_data.
This functionality has been added to Keras:
train_datagen = ImageDataGenerator(rescale=1./255,
shear_range=0.2,
zoom_range=0.2,
horizontal_flip=True,
validation_split=0.2)
train_generator = train_datagen.flow_from_directory(
train_data_dir,
target_size=(img_width, img_height),
batch_size=batch_size,
class_mode='binary',
subset='training')
validation_generator = train_datagen.flow_from_directory(
train_data_dir,
target_size=(img_width, img_height),
batch_size=batch_size,
class_mode='binary'
subset='validation')
model.fit_generator(
train_generator,
steps_per_epoch = train_generator.samples // batch_size,
validation_data = validation_generator,
validation_steps = validation_generator.samples // batch_size,
epochs = nb_epochs)