-
Notifications
You must be signed in to change notification settings - Fork 150
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
A problem when the sample_size is not divisible by the batch_size #5
Comments
As far as I know, the problem lies in keras-data-generator/my_classes.py Lines 42 to 46 in 866cce8
|
FWIW, I think you have 2 options:
If you use approach (2), then you have to cap the upper bound of the batch, i.e., instead of: keras-data-generator/my_classes.py Lines 25 to 26 in 866cce8
which can be rewritten as: low = index * self.batch_size
high = (index+1) * self.batch_size
indexes = self.indexes[low:high] you may want to use something like: low = index * self.batch_size
high = min((index + 1) * self.batch_size, len(self.list_IDs))
indexes = self.indexes[low:high] to cap at the length of the array; the last batch may be smaller And if you want to micro-optimize, you can replace a multiplication with an addition: low = index * self.batch_size
high = min(low + self.batch_size, len(self.list_IDs))
indexes = self.indexes[low:high] I wrote some helper classes (with tests) which can be dropped in to provide the data slicing, or you can incorporate them into your |
@mbrukman Thank you for the nice indexing suggestion!
would still result in
|
@gwirn wrote:
I think it returns
Just to clarify, I am not creating too big X or y because this is not my repo and it's not my code, but you're correct, if we're going to make the last batch smaller than the rest, then we have to also fix the initialization to create an array that's the correct size. My implementation of I think some of the sample code here and in the blog post need to be updated to handle these cases; I opened a separate issue #7 to ask about the license for this repo (since there isn't one now) so that we can contribute some fixes for the code. |
@mbrukman Yes you are right |
I adapted DataGenerator to my Deep Learning pipeline.
When the sample size is not divisible by the batch_size, the DataGenerator seems to return to the first batch without taking into account the last (smaller) batch.
Example
Let A be an array of train samples, and batch_size = 4.
A = [4,7,8,7,9,78,8,4,78,51,6,5,1,0]. Here A.size = 14
It is clear, in this situation, that A.size is not divisible by batch_size.
The batches the DataGenerator yields during the training process are the following :
Here is a situation where an other generator behaves well when the sample_size is not divisible by the batch_size https://stackoverflow.com/questions/54159034/what-if-the-sample-size-is-not-divisible-by-batch-size-in-keras-model
For your information, I kept as is the following instruction
int(np.floor(len(self.list_IDs) / self.batch_size))
If I change np.floor to np.ceil, it seems to bug during the training/validation phases.
The text was updated successfully, but these errors were encountered: