Keras model.fit runs out of memory in Google Colab

Viewed 28

I'm running out of memory in a simple CNN keras model. Here's the model summary:

Model: "sequential"
_________________________________________________________________
 Layer (type)                Output Shape              Param #   
=================================================================
 conv1d (Conv1D)             (None, 398, 250)          225250    
                                                                 
 global_max_pooling1d (Globa  (None, 250)              0         
 lMaxPooling1D)                                                  
                                                                 
 dense (Dense)               (None, 250)               62750     
                                                                 
 dropout (Dropout)           (None, 250)               0         
                                                                 
 activation (Activation)     (None, 250)               0         
                                                                 
 dense_1 (Dense)             (None, 1)                 251       
                                                                 
 activation_1 (Activation)   (None, 1)                 0         
                                                                 
=================================================================
Total params: 288,251
Trainable params: 288,251
Non-trainable params: 0
_________________________________________________________________

I have a 20,000 x 400 x 300 embedding matrix as the x_train input (python nested list), all values are np.float16 (the total size is less than 5GB). I tried all Colab runtimes but when I run

model.fit(x_train, y_train,
batch_size=32,
epochs=2,
verbose=1,
validation_data=(x_test, y_test),)

Colab crashes with message 'you are out of RAM'. It doesn't even start to output the verbose messages.

1 Answers

You should try with a smaller batch_size, you can start with a small one like 4 and if your memory doesn't crash try incrementing it like 8, 16

Related