Validation data batch_size of size one? (Keras)

≯℡__Kan透↙ 提交于 2021-02-08 07:19:47

问题


In Keras there is an option to set the size of the validation set batches to one:

valid_batches = ImageDataGenerator().flow_from_directory(valid_path, ... batch_size=1)

Is it correct that the model then just uses one object from the validation data to validate the model after each training data epoch? If that is the case then my model should not get a very good validation score. But when I run the model it runs without any problems, keeps improving and seems to be using many validation objects for evaluation. Can someone explain this please? Thank you.


回答1:


You really give us little information there so I'll try to give you the information that you need in order to understand and maybe fix it by yourself.

valid_batches = ImageDataGenerator().flow_from_directory(valid_path, ... batch_size=1)

This gives you a generator. It'll do what python generators do : yield batches of validation data, those batches will be of size 1 in your case.

However, when you do :

model.fit_generator(train_batches, validation_data=valid_batches, ... )

What keras does is to use your generator to get some validation data. But it will not stop after one call to valid_batches, it will make several setps. A batch isn't your whole validation set, keras assumes that you will give it its validation data set by batches. So it iterates on your validation_data generator as many times as asked.

The way to specify how many times you want to generate batches in one validation step is to use the validation_steps argument like this :

model.fit_generator(train_batches, validation_data=valid_batches,  validation_steps=10... )

That way, your validation will be using validation_steps * batch_size = 10 * 1 = 10 samples for each validation executed.

I hope it helps a bit :-) If not, please be more specific about your code. A good stack overflow question should contain a minimal reproducible code for your error.



来源:https://stackoverflow.com/questions/48724122/validation-data-batch-size-of-size-one-keras

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!