0

I'm training a simple CNN on the MNIST dataset. This is form a tutorial (https://machinelearningmastery.com/how-to-develop-a-convolutional-neural-network-from-scratch-for-mnist-handwritten-digit-classification/).

def define_model():
    model = Sequential()
    model.add(Conv2D(32, (3, 3), activation='relu', kernel_initializer='he_uniform', input_shape=(28, 28, 1)))
    model.add(MaxPooling2D((2, 2)))
    model.add(Flatten())
    model.add(Dense(100, activation='relu', kernel_initializer='he_uniform'))
    model.add(Dense(10, activation='softmax'))
    # compile model
    opt = SGD(lr=0.01, momentum=0.9)
    model.compile(optimizer=opt, loss='categorical_crossentropy', metrics=['accuracy'])
    return model

I actually want to be able to step through the training process. So starting with an example from the dataset:

one example from MNIST dataset

I want to see the 32 initialised kernels and the resulting output of the convolutions. Then maybe I want to see these things again a bunch of iterations later when the model has started to converge. I want to then be able to see the outputs of MaxPooling, Flatten, and the Dense layers.

So my question is not around how to visualise things (I'll use print and pypolot.imshow for that), but more about how I can code it up so that I can look into all of these things wherever I want.

Alexander Soare
  • 2,825
  • 3
  • 25
  • 53
  • 1
    [This](https://stackoverflow.com/questions/58193588/keras-how-to-get-model-predictions-or-last-layer-output-in-a-custom-generator) may be of help (scroll for custom train loop); also [here](https://stackoverflow.com/questions/58193588/keras-how-to-get-model-predictions-or-last-layer-output-in-a-custom-generator) – OverLordGoldDragon Dec 26 '19 at 15:42
  • 1
    You can use `model.layers` to iterate over all the layers of the model and then feed the images to see the output at each layer, after the training has completed. – Raj Dec 26 '19 at 16:01

0 Answers0