Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

is it possible to specify different batch sizes for train and validation? #8550

Closed
ophiry opened this issue Nov 21, 2017 · 7 comments
Closed

Comments

@ophiry
Copy link

ophiry commented Nov 21, 2017

for train data, there are reasons to keep batches relatively small (batch size can effect training results)
however for the validation set, using a single reasonably big batch, allows simple use of metric functions that don't work well when averaging batches (precision \ recall \ f1)

@cbensimon
Copy link
Contributor

If you don't specify any batch_size in your model definition (I think that you never have to, but I may be wrong), your model has a dynamic batch_size. You then use the batch_size parameter of the fit and predict methods of your keras models.

@ophiry
Copy link
Author

ophiry commented Nov 22, 2017

I use fit_generator
the batch size of the train data is constant, though not explicitly defined in keras
the validation data is sent as a numpy arrays (not as a generator) which are larger then the batch size of the train data

I see that event though no batch size was defined, keras will break the test data to batches in the size of the train set batch (I added a dummy metric that calculates the length of the batch to confirm this)

@cbensimon
Copy link
Contributor

For that scenario, my advice is to do the epochs loop yourself :
for each epoch, you call fit_generator with epochs=1,
and then you call predict on your validation data with the desired batch size.

Another solution is to use a generator for the validation data, so you have control over the batch_size of your validation data

You could also use a custom Keras Callback
(in which you call predict inside on_epoch_end method)

@cbensimon
Copy link
Contributor

But you're right, maybe a validation_batch_size parameter is missing for the case where validation_data is not a generator

@boulderZ
Copy link

boulderZ commented Jun 23, 2018

I need this feature now. I am implementing a custom metric that calculates correlation. If I use the training batch size of 32 it is too small for my custom metric and I can get runs that are all zeros resulting in nan output for the validation metric. I would think this could be a common issue for any metric that needs longer string of data to process accurately or in a stable way. Seems like this would be an easy feature to add?

@MatthewScholefield
Copy link

@boulderZ I'm not sure if this would help your issue, but when I ran into an Nan issue, I was able to fix it with value / K.minimum(1.0, other_value). However, this didn't fix my issue of metric being averaged across the batch size. This issue has some relevant posts that details more of what @cbensimon and @ophiry are talking about.

@chmn106
Copy link

chmn106 commented Jul 27, 2019

my advice is to define your validate function,use self define metric function

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants