Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Keras: What if the size of data is not divisible by batch_size?

I am new to Keras and just started working on some examples. I am dealing with the following problem: I have 4032 samples and use about 650 of them as for the fit or basically the training state and then use the rest for testing the model. The problem is that I keep getting the following error:

Exception: In a stateful network, you should only pass inputs with a number of samples that can be divided by the batch size.

I understand why I am getting this error, my question is, what if the size of my data is not divisible by batch_size? I used to work with Deeplearning4j LSTM and did not have to deal with this problem. Is there anyway to get around with this?

Thanks

like image 808
ahajib Avatar asked Jun 22 '16 17:06

ahajib


People also ask

What happens if the dataset does not divide evenly by batch size?

What if the dataset does not divide evenly by the batch size? This can and does happen often when training a model. It simply means that the final batch has fewer samples than the other batches.

What does Batch_size do in Keras?

From my experience with Keras and assuming you are familiar enough with neural networks: batch_size denotes the subset size of your training sample (e.g. 100 out of 1000) which is going to be used in order to train the network during its learning process.

Does batch size affect accuracy Keras?

Using too large a batch size can have a negative effect on the accuracy of your network during training since it reduces the stochasticity of the gradient descent.

How do I choose the right size epochs batch?

Generally batch size of 32 or 25 is good, with epochs = 100 unless you have large dataset. in case of large dataset you can go with batch size of 10 with epochs b/w 50 to 100. Again the above mentioned figures have worked fine for me. Value for batch size should be (preferred) in powers of 2.

What is the default batch size for keras?

The documentation for Keras about batch size can be found under the fit function in the Models (functional API) page batch_size: Integer or None. Number of samples per gradient update. If unspecified, batch_size will default to 32. If you have a small dataset, it would be best to make the batch size equal to the size of the training data.

What is the back-end of a keras model?

Keras uses fast symbolic mathematical libraries as a backend, such as TensorFlow and Theano. A downside of using these libraries is that the shape and size of your data must be defined once up front and held constant regardless of whether you are training your network or making predictions.

What is Max_queue_size in keras?

Used for generator or keras.utils.Sequence input only. Maximum size for the generator queue. If unspecified, max_queue_size will default to 10. workers: Integer. Used for generator or keras.utils.Sequence input only.

What are the advantages and disadvantages of using keras?

A benefit of using Keras is that it is built on top of symbolic mathematical libraries such as TensorFlow and Theano for fast and efficient computation. This is needed with large neural networks. A downside of using these efficient libraries is that you must define the scope of your data upfront and for all time.


1 Answers

The simplest solution is to use fit_generator instead of fit. I write a simple dataloader class that can be inherited to do more complex stuff. It would look something like this with get_next_batch_data redefined to whatever your data is including stuff like augmentation etc..

class BatchedLoader():
    def __init__(self):
        self.possible_indices = [0,1,2,...N] #(say N = 33)
        self.cur_it = 0
        self.cur_epoch = 0

    def get_batch_indices(self):
        batch_indices = self.possible_indices [cur_it : cur_it + batchsize]
        # If len(batch_indices) < batchsize, the you've reached the end
        # In that case, reset cur_it to 0 and increase cur_epoch and shuffle possible_indices if wanted
        # And add remaining K = batchsize - len(batch_indices) to batch_indices


    def get_next_batch_data(self):
        # batch_indices = self.get_batch_indices()
        # The data points corresponding to those indices will be your next batch data
like image 75
Prophecies Avatar answered Oct 04 '22 13:10

Prophecies