I am working on a variable-length classification problem. I want to utilize multiple Deep learning methods in combination, like CNN, LSTM, attention, etc. Now I'm quite confused and having difficulty preparing and feeding data into the model.  I am receiving plenty of errors but had no idea why. As a result, I am eagerly looking for help. I am doing implementation Tensorflow Keras libraries.  Is there any help or guidance available implementation wise as well as theoretical? 


2 Answers 2


By padding and this is not a necessarily a deep learning thing. In general if inputs are from variable lengths you may fix them to a specific size which let's you input all your data. Theoretically that size is the length of longest sequence in your data and any shorter sequence gets some zeros to become as long as the fixed size.

Approach above is complicated as you may need to use the model on an even longer sequence e.g. an unseen longer input during test phase. That's why zero padding is combined with cutting sequences e.g. all pertained language model has a fixed input size. If your sequence is shorter than that, they add it with 0 in preprocessing/tokenization step and if input is longer than fixed size, they cut it. You may have a look at this question and its answers.


If you are able to crop consistently, that's a great step to do to throw away useless information. For example, if you are expecting a single bright object on a dark background. If this is not possible - e.g. there may be multiple objects in the corners of the image, image resizing (squishing/stretching) is a good idea. But, you have to make sure to use resizing in augmentation while training the network because the image properties - blur (pixel cross correlation)/quantization - will change.

Finally, you can use a convolutional neural net with a max pool down to a 1x1 height/width dimension and the number of channels equal to your number of classes. https://wandb.ai/ayush-thakur/dl-question-bank/reports/How-to-Handle-Images-of-Different-Sizes-in-a-Convolutional-Neural-Network--VmlldzoyMDk3NzQ

The last step will be equivalent to resizing the final feature map e.g. with nearest neighbors if you are using max pool, or 1st order interpolation if you are using average pool. You should also augment images when training this way.

My preference is for cropping (when possible) and resizing the input images to the same size with cubic/spline interpolation (or lower order if you are speed-limited) for minimal information loss.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.