'Kaggle runs code cell and then ends without error

I'm currently using kaggle's notebooks for a convolutional neural network and I ran a cell with the following code:

model = Sequential()

model.add(Conv2D(32, kernel_size=(3,3), activation='relu', input_shape=(28, 28, 1)))
model.add(MaxPooling2D(2,2))
model.add(BatchNormalization())

model.add(Conv2D(64, kernel_size=(3,3), activation='relu'))
model.add(MaxPooling2D(2,2))
model.add(BatchNormalization())

model.add(Dropout(0.1))
model.add(Dense(10, activation='softmax'))

model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])
model.fit(train_images, train_labels, epochs=5)

The cell runs for a second and then stops abruptly and doesn't train, however, it also doesn't give me an error message. It just ends. When I run the following conv neural net structure on a cell above, it works perfectly fine and shows the epochs and training:

model = Sequential()

model.add(Flatten(input_shape=(28, 28)))
model.add(Dropout(0.05))
model.add(Dense(128, activation='relu'))
model.add(Dense(10, activation='softmax'))

model_optimizer = Adam(lr=0.001)

model.compile(optimizer=model_optimizer, loss='sparse_categorical_crossentropy', metrics=['accuracy'])
history = model.fit(train_images, train_labels, epochs=10)

Is there a specific reason or any ideas as to why the first cell block isn't working? Does it have to do with the layers? I noted that the cell seems to run fine when I don't have Conv2D / Maxpooling layers.



Solution 1:[1]

You need to flatten the obtained Convolutional matrix layer using Flatten layer before of feeding it into Dense layer as below:

model.add(Dropout(0.1))
model.add(Flatten())    <------add Flatten layer
model.add(Dense(10, activation='softmax'))

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 TFer2