In [1]:
import keras
import numpy as np
import keras.backend as K
from keras.datasets import mnist
from keras.utils import np_utils
K.set_learning_phase(False)
import matplotlib.pyplot as plt
plt.rcParams['image.cmap'] = 'gray'
%matplotlib inline


Using TensorFlow backend.

In [2]:
model = keras.models.load_model('example_keras_mnist_model.h5')
model.summary()


_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
conv2d_1 (Conv2D)            (None, 26, 26, 32)        320       
_________________________________________________________________
max_pooling2d_1 (MaxPooling2 (None, 13, 13, 32)        0         
_________________________________________________________________
conv2d_2 (Conv2D)            (None, 11, 11, 32)        9248      
_________________________________________________________________
dropout_1 (Dropout)          (None, 11, 11, 32)        0         
_________________________________________________________________
flatten_1 (Flatten)          (None, 3872)              0         
_________________________________________________________________
dense_1 (Dense)              (None, 64)                247872    
_________________________________________________________________
dropout_2 (Dropout)          (None, 64)                0         
_________________________________________________________________
dense_2 (Dense)              (None, 10)                650       
=================================================================
Total params: 258,090
Trainable params: 258,090
Non-trainable params: 0
_________________________________________________________________

In [3]:
dataset = mnist.load_data()
train_data = dataset[0][0] / 255
train_data = train_data[..., np.newaxis].astype('float32')
train_labels = np_utils.to_categorical(dataset[0][1]).astype('float32')
test_data = dataset[1][0] / 255
test_data = test_data[..., np.newaxis].astype('float32')
test_labels = np_utils.to_categorical(dataset[1][1]).astype('float32')
plt.imshow(train_data[0, ..., 0])


Out[3]:
<matplotlib.image.AxesImage at 0x7f97d91e7ba8>

Keras model are serialzed in a JSON format.


In [4]:
model.get_config()


Out[4]:
[{'class_name': 'Conv2D',
  'config': {'activation': 'relu',
   'activity_regularizer': None,
   'batch_input_shape': (None, 28, 28, 1),
   'bias_constraint': None,
   'bias_initializer': {'class_name': 'Zeros', 'config': {}},
   'bias_regularizer': None,
   'data_format': 'channels_last',
   'dilation_rate': (1, 1),
   'dtype': 'float32',
   'filters': 32,
   'kernel_constraint': None,
   'kernel_initializer': {'class_name': 'VarianceScaling',
    'config': {'distribution': 'uniform',
     'mode': 'fan_avg',
     'scale': 1.0,
     'seed': None}},
   'kernel_regularizer': None,
   'kernel_size': (3, 3),
   'name': 'conv2d_1',
   'padding': 'valid',
   'strides': (1, 1),
   'trainable': True,
   'use_bias': True}},
 {'class_name': 'MaxPooling2D',
  'config': {'data_format': 'channels_last',
   'name': 'max_pooling2d_1',
   'padding': 'valid',
   'pool_size': (2, 2),
   'strides': (2, 2),
   'trainable': True}},
 {'class_name': 'Conv2D',
  'config': {'activation': 'relu',
   'activity_regularizer': None,
   'bias_constraint': None,
   'bias_initializer': {'class_name': 'Zeros', 'config': {}},
   'bias_regularizer': None,
   'data_format': 'channels_last',
   'dilation_rate': (1, 1),
   'filters': 32,
   'kernel_constraint': None,
   'kernel_initializer': {'class_name': 'VarianceScaling',
    'config': {'distribution': 'uniform',
     'mode': 'fan_avg',
     'scale': 1.0,
     'seed': None}},
   'kernel_regularizer': None,
   'kernel_size': (3, 3),
   'name': 'conv2d_2',
   'padding': 'valid',
   'strides': (1, 1),
   'trainable': True,
   'use_bias': True}},
 {'class_name': 'Dropout',
  'config': {'name': 'dropout_1', 'rate': 0.25, 'trainable': True}},
 {'class_name': 'Flatten', 'config': {'name': 'flatten_1', 'trainable': True}},
 {'class_name': 'Dense',
  'config': {'activation': 'relu',
   'activity_regularizer': None,
   'bias_constraint': None,
   'bias_initializer': {'class_name': 'Zeros', 'config': {}},
   'bias_regularizer': None,
   'kernel_constraint': None,
   'kernel_initializer': {'class_name': 'VarianceScaling',
    'config': {'distribution': 'uniform',
     'mode': 'fan_avg',
     'scale': 1.0,
     'seed': None}},
   'kernel_regularizer': None,
   'name': 'dense_1',
   'trainable': True,
   'units': 64,
   'use_bias': True}},
 {'class_name': 'Dropout',
  'config': {'name': 'dropout_2', 'rate': 1.0, 'trainable': True}},
 {'class_name': 'Dense',
  'config': {'activation': 'softmax',
   'activity_regularizer': None,
   'bias_constraint': None,
   'bias_initializer': {'class_name': 'Zeros', 'config': {}},
   'bias_regularizer': None,
   'kernel_constraint': None,
   'kernel_initializer': {'class_name': 'VarianceScaling',
    'config': {'distribution': 'uniform',
     'mode': 'fan_avg',
     'scale': 1.0,
     'seed': None}},
   'kernel_regularizer': None,
   'name': 'dense_2',
   'trainable': True,
   'units': 10,
   'use_bias': True}}]

Getting the weights

Weights can be retrieved either directly from the model or from each individual layer.


In [5]:
# Weights and biases of the entire model.
model.get_weights()


Out[5]:
[array([[[[-0.33856323,  0.20390955, -0.11109605,  0.27959093,  0.00380358,
            0.06809236,  0.13476925, -0.06905884, -0.00914569,  0.01789398,
           -0.15531571,  0.1061649 , -0.16948256,  0.07929865,  0.16483295,
           -0.07207379,  0.24873769, -0.19941764,  0.16662872,  0.19174086,
           -0.01943381,  0.0066115 ,  0.0134623 ,  0.04628464,  0.00221355,
           -0.23785631,  0.05527339, -0.32550618, -0.12803005,  0.00084405,
           -0.21533267, -0.18520311]],
 
         [[-0.05132618,  0.11048742, -0.01836612, -0.12476675, -0.18518658,
            0.03234763,  0.19836517, -0.11385031, -0.22344072,  0.17603368,
            0.14256321,  0.085033  ,  0.00895712,  0.06350163,  0.1005916 ,
            0.13216837, -0.05474935, -0.13029362, -0.05059361,  0.10186421,
            0.13374192, -0.11962113, -0.10992999,  0.08803006, -0.03906573,
           -0.3092995 , -0.01672952, -0.23022896, -0.30201221,  0.10596731,
           -0.27116138, -0.35430175]],
 
         [[ 0.33610955,  0.033969  ,  0.00095919, -0.17490396,  0.03093159,
            0.10593548,  0.28105086,  0.12002508, -0.25334483, -0.03389421,
            0.19349942,  0.32704428,  0.1018483 ,  0.04974011,  0.15481295,
            0.12925673, -0.14330553,  0.10459574, -0.16167818, -0.00957763,
           -0.17980698, -0.19601411, -0.00238411, -0.30377474, -0.15770112,
           -0.04087055,  0.02476479,  0.00098312, -0.24966806, -0.09523545,
           -0.15778606, -0.22892013]]],
 
 
        [[[-0.35785612,  0.11476454,  0.04976963,  0.05825217, -0.10467952,
            0.02363232, -0.07825409,  0.14956443,  0.12504648,  0.01157873,
           -0.06974483, -0.13124345,  0.11080556,  0.10724898, -0.10358807,
           -0.03627256,  0.05047637, -0.14007384, -0.01534044,  0.14637068,
            0.14159171,  0.07099191,  0.04750481,  0.23509285,  0.00062798,
           -0.28756508, -0.00313633, -0.18350028,  0.06473579,  0.23929332,
            0.0193444 , -0.11831183]],
 
         [[-0.00380083,  0.23089294, -0.21173477, -0.21526209,  0.10552941,
            0.16956028,  0.01564491,  0.1375519 ,  0.16510786,  0.07982598,
            0.19161849, -0.21977969, -0.02409861,  0.157625  ,  0.01468054,
            0.12768054, -0.27593395,  0.12159296,  0.10541939, -0.02288091,
            0.18439572, -0.09308205, -0.04813813, -0.11125364,  0.17840828,
            0.02104141,  0.23276028,  0.05439105, -0.08695178,  0.08954187,
            0.13792819,  0.01321854]],
 
         [[ 0.19480787,  0.12171868, -0.27793065,  0.068828  ,  0.12536857,
           -0.04126728, -0.06341767,  0.10707708,  0.10291349,  0.12810974,
            0.04934532, -0.18099618, -0.02886892,  0.12866126,  0.18573213,
            0.20035776, -0.07830612,  0.12074151,  0.11779081,  0.11454585,
           -0.04120336, -0.20274398, -0.00399038, -0.11202653,  0.13420886,
            0.21578619,  0.14358513,  0.1243699 ,  0.13599069, -0.04502468,
            0.13634589,  0.15575847]]],
 
 
        [[[-0.20989153,  0.15227273,  0.23237251, -0.08367927,  0.14982514,
            0.16587329, -0.28325978, -0.08602066,  0.13957199,  0.19290963,
            0.00193023, -0.12849583,  0.12334183,  0.16267049, -0.13655755,
           -0.12450543, -0.2113706 , -0.08632083,  0.02195895, -0.0963969 ,
            0.06236559,  0.22840671,  0.1670882 ,  0.087584  ,  0.06964725,
           -0.11409627,  0.08816697, -0.03587545,  0.18001753,  0.0437397 ,
            0.22911704,  0.0286596 ]],
 
         [[-0.04831968, -0.09599441,  0.16779834, -0.30507049,  0.19729429,
            0.06221648, -0.19661918,  0.03457066,  0.18631683,  0.17452584,
            0.18820918,  0.04805256,  0.06722377,  0.26525283, -0.00758747,
           -0.04513836,  0.16339223,  0.00425898,  0.22618811,  0.00424452,
           -0.11183094,  0.16458337,  0.03243278,  0.13851871,  0.03470041,
            0.07749669,  0.25952315,  0.22436245,  0.2598435 , -0.09826418,
            0.05551234,  0.27973706]],
 
         [[ 0.1098677 , -0.08579756, -0.05499269,  0.00762451, -0.09339599,
            0.07986619, -0.14637941, -0.11159059,  0.09420574, -0.07074815,
            0.09719007, -0.10575279,  0.08875547,  0.23332612,  0.09377708,
            0.20497514,  0.24949729,  0.1880891 ,  0.05041798, -0.13240333,
            0.09229264,  0.14950337,  0.16152637, -0.00753751,  0.02833049,
            0.21858366,  0.21298586,  0.30639312,  0.12163855, -0.26328984,
            0.1449654 ,  0.08858766]]]], dtype=float32),
 array([-0.00504222, -0.03398184,  0.03041283,  0.05144327, -0.02014533,
        -0.03628516,  0.00899984, -0.0141687 , -0.00900476, -0.04701506,
        -0.03609759,  0.00830616, -0.03162324, -0.04258636, -0.03593863,
        -0.02859137,  0.00309902, -0.00644259, -0.01920764, -0.01361663,
        -0.02481366, -0.00387063, -0.0365628 , -0.00746764, -0.0200652 ,
         0.0303475 , -0.03546616,  0.00537372, -0.00621093, -0.00206236,
        -0.00341022, -0.00076332], dtype=float32),
 array([[[[ -4.01226431e-02,   5.65311350e-02,   1.97778791e-02, ...,
            -7.24663138e-02,  -8.53474513e-02,  -2.47050971e-02],
          [  8.85965973e-02,  -4.05703671e-02,  -2.93839648e-02, ...,
            -1.11803887e-02,  -4.80645746e-02,  -6.58439025e-02],
          [ -2.13793498e-02,  -1.59590065e-01,  -8.00972506e-02, ...,
             2.53647882e-02,  -1.91419214e-01,   3.48039232e-02],
          ..., 
          [  2.99461465e-02,  -3.43047753e-02,   1.23317085e-01, ...,
             6.12316839e-02,  -8.19578245e-02,  -7.98792988e-02],
          [  4.35201935e-02,  -1.55795738e-01,  -1.04933493e-02, ...,
            -5.25219925e-02,   8.30645412e-02,  -7.37955049e-02],
          [ -9.04319659e-02,  -1.58308387e-01,  -2.26154979e-02, ...,
             6.65647686e-02,   1.10460976e-02,  -2.82318033e-02]],
 
         [[ -3.56494114e-02,   6.75998405e-02,  -2.15384793e-02, ...,
             1.28065869e-01,   4.45998460e-03,   8.36961064e-03],
          [ -7.25646615e-02,   3.11616454e-02,  -1.86694935e-02, ...,
             4.91630882e-02,   4.57360819e-02,   9.56004660e-04],
          [ -4.07712013e-02,   9.61609706e-02,  -1.65225174e-02, ...,
            -1.47591745e-02,  -2.37694815e-01,   4.33482118e-02],
          ..., 
          [  5.81468642e-02,  -9.16257575e-02,   1.40851319e-01, ...,
             1.78829525e-02,   6.83607161e-02,  -1.08090660e-03],
          [ -6.07242808e-03,   6.49580061e-02,   8.39213654e-02, ...,
             8.62157065e-03,  -7.82900006e-02,  -2.93575227e-02],
          [ -1.05463199e-01,  -1.23982310e-01,   1.32971138e-01, ...,
            -2.68109202e-01,   2.37438716e-02,  -1.14829145e-01]],
 
         [[  1.52936447e-02,   3.90354618e-02,  -1.12520829e-02, ...,
             5.79406582e-02,   5.32508604e-02,  -1.24649880e-02],
          [ -3.43148150e-02,   3.13750766e-02,  -1.05846170e-02, ...,
             1.08574599e-01,   1.15808979e-01,  -7.92817995e-02],
          [ -7.19422176e-02,  -5.68333268e-03,  -2.10240290e-01, ...,
             1.33076042e-01,  -1.08591720e-01,  -1.09124787e-01],
          ..., 
          [  5.17075434e-02,  -2.11005285e-02,  -4.09693345e-02, ...,
             9.19375867e-02,   7.10872039e-02,  -5.03213182e-02],
          [  9.40999091e-02,   9.44437981e-02,  -1.03572838e-01, ...,
             2.79077142e-02,  -5.97504824e-02,  -2.05431551e-01],
          [ -7.51482621e-02,   7.23841935e-02,   3.87758017e-02, ...,
            -2.46702153e-02,   1.12753557e-02,  -2.08663657e-01]]],
 
 
        [[[  1.15424432e-01,   3.41603756e-02,   1.08134672e-01, ...,
            -1.66405186e-01,   4.99275289e-02,  -2.19747081e-01],
          [ -3.20136435e-02,  -5.79421893e-02,  -8.25085863e-02, ...,
            -2.49465201e-02,   1.06344424e-01,   7.58055821e-02],
          [  1.02315649e-01,  -1.01954013e-01,   3.53149064e-02, ...,
             1.38127521e-01,  -9.03331339e-02,   1.92770675e-01],
          ..., 
          [ -5.88744693e-02,  -5.12662753e-02,   2.82629263e-02, ...,
             1.52217850e-01,   2.74222121e-02,  -9.50521231e-02],
          [ -2.22033262e-02,   3.10577564e-02,   8.29979330e-02, ...,
            -1.15540944e-01,  -1.28794432e-01,   5.08924276e-02],
          [  4.54525985e-02,  -1.19983852e-01,  -8.90542660e-03, ...,
            -5.80082051e-02,  -1.50292337e-01,  -1.08759878e-02]],
 
         [[  6.48164973e-02,  -5.26220948e-02,  -4.96042520e-02, ...,
             1.98234692e-01,  -6.57171905e-02,  -1.37150943e-01],
          [  8.02129358e-02,  -3.08953878e-02,   8.13097209e-02, ...,
            -1.24222282e-02,   4.78967503e-02,  -1.02946989e-01],
          [  8.18985254e-02,   1.98033173e-02,   2.51242854e-02, ...,
            -1.15361534e-01,  -1.79735914e-01,   1.08657740e-01],
          ..., 
          [  7.45853335e-02,  -2.57881824e-02,   4.53389212e-02, ...,
            -3.20457429e-01,   3.10847461e-02,   1.06562242e-01],
          [ -5.41368127e-02,  -1.29561439e-01,  -1.43217482e-02, ...,
            -1.39163021e-04,  -8.92032683e-02,   2.46253498e-02],
          [  1.01359107e-01,  -1.87046140e-01,  -1.38537094e-01, ...,
            -1.56840369e-01,  -1.18709743e-01,   1.70156091e-01]],
 
         [[ -1.00295348e-02,  -1.71230417e-02,   3.40224281e-02, ...,
             3.12297851e-01,   8.12264234e-02,  -1.20560721e-01],
          [ -4.38664705e-02,  -5.42960018e-02,   3.29849757e-02, ...,
            -3.37751694e-02,   3.49974521e-02,  -6.98371083e-02],
          [  2.68943105e-02,   1.00566104e-01,  -1.07213087e-01, ...,
             1.53350428e-01,  -1.30984128e-01,   1.69847399e-01],
          ..., 
          [  9.87360924e-02,   5.75206839e-02,   7.33780786e-02, ...,
             8.71890560e-02,  -1.02836840e-01,  -8.32794830e-02],
          [  3.67187522e-02,   4.47824895e-02,   7.92319477e-02, ...,
            -1.02265298e-01,  -1.57988831e-01,  -3.84410620e-02],
          [ -5.98737821e-02,  -3.85037228e-03,   1.18370377e-01, ...,
            -1.12961163e-03,  -5.13557009e-02,  -1.23469085e-02]]],
 
 
        [[[ -1.54174656e-01,  -7.29809925e-02,   1.03580609e-01, ...,
            -5.83949573e-02,   1.42159043e-02,   1.08536510e-02],
          [ -1.77145645e-03,   4.64351624e-02,  -2.20793635e-02, ...,
            -7.95024633e-02,   1.24868646e-01,  -6.13103956e-02],
          [ -1.45229772e-01,  -2.58118659e-02,  -1.83774874e-01, ...,
             1.88135952e-02,  -1.80301592e-01,  -1.35331944e-01],
          ..., 
          [  1.82130724e-01,   9.95685235e-02,  -1.57544315e-01, ...,
             2.75112838e-01,   3.98966558e-02,  -6.07371852e-02],
          [ -1.00632012e-01,  -1.26577348e-01,  -7.93468356e-02, ...,
             4.82924879e-02,  -3.96377295e-02,   6.26751035e-02],
          [ -1.03973649e-01,  -1.27423242e-01,  -1.73042938e-02, ...,
            -3.28851081e-02,  -1.20088654e-02,  -7.24502802e-02]],
 
         [[  2.47026756e-02,   1.50272861e-01,  -5.51312901e-02, ...,
             2.03319445e-01,  -1.50788695e-01,   9.36137587e-02],
          [  6.49152249e-02,   5.40793985e-02,   1.83095150e-02, ...,
            -1.57246575e-01,  -7.47760162e-02,  -1.87514033e-02],
          [ -1.75388202e-01,   2.85918899e-02,  -1.62405036e-02, ...,
             6.12527579e-02,  -1.74887124e-02,   1.34213299e-01],
          ..., 
          [  3.40895131e-02,   1.51363742e-02,  -1.39256850e-01, ...,
            -3.53645802e-01,   5.38300797e-02,   7.85057340e-03],
          [ -2.48833075e-02,   1.08410008e-01,   3.59907746e-03, ...,
            -2.35926500e-03,   6.96492493e-02,   1.25792444e-01],
          [  1.34210382e-02,   1.61334034e-02,  -9.85880964e-04, ...,
            -2.10113078e-01,  -3.63990366e-02,   3.79806198e-02]],
 
         [[ -8.78810361e-02,  -4.22350578e-02,  -3.16658437e-01, ...,
             2.75519103e-01,  -6.21331148e-02,  -1.87287226e-01],
          [ -8.78605098e-02,   7.86287859e-02,  -5.87010793e-02, ...,
             1.08604282e-01,  -2.26945728e-02,   8.21494609e-02],
          [ -1.43985510e-01,   5.48310615e-02,  -4.04641591e-02, ...,
            -3.45682283e-03,   1.79626510e-01,   1.64892867e-01],
          ..., 
          [  1.46391522e-02,   8.34961683e-02,   2.14303434e-01, ...,
             8.81534740e-02,  -4.87429611e-02,   3.87322716e-02],
          [  4.44919541e-02,   1.07672445e-01,   1.14316538e-01, ...,
            -1.46668136e-01,   1.10305361e-01,   6.61478117e-02],
          [  9.13153067e-02,  -6.36789277e-02,  -3.22604664e-02, ...,
            -1.87554494e-01,   6.95533380e-02,  -6.87294453e-02]]]], dtype=float32),
 array([-0.01067298, -0.02657567, -0.04066884, -0.01147611, -0.00863655,
        -0.01902861, -0.020127  , -0.00405021, -0.0179966 ,  0.01227409,
        -0.00634154, -0.02403696, -0.04557842,  0.00490331,  0.03980603,
        -0.03981873, -0.02845218, -0.00918497, -0.00231124, -0.01461604,
        -0.03896464, -0.02953302, -0.011937  ,  0.01655394, -0.01250598,
        -0.01699266, -0.01369387, -0.02693589, -0.01247327, -0.01274089,
        -0.01558687, -0.00631594], dtype=float32),
 array([[-0.03399933, -0.00636341, -0.03531976, ..., -0.002253  ,
          0.00117791, -0.02263132],
        [ 0.01083158, -0.01456495,  0.05681583, ...,  0.01127636,
         -0.050655  ,  0.00019235],
        [-0.03372994,  0.01775249,  0.02735798, ...,  0.04454331,
         -0.02833169, -0.02742446],
        ..., 
        [-0.00682181,  0.08876038,  0.04527185, ..., -0.03760593,
         -0.00575223,  0.00523976],
        [ 0.02149359,  0.00826968, -0.06222196, ...,  0.02651198,
          0.0340944 , -0.02549834],
        [-0.04021263,  0.06992488, -0.07317327, ..., -0.05559838,
          0.09858176, -0.02161136]], dtype=float32),
 array([-0.00720194,  0.028562  , -0.00160787,  0.0215813 , -0.01914285,
         0.00030446, -0.000688  , -0.0106398 ,  0.0158753 , -0.00254661,
        -0.00949155, -0.00823552, -0.02121146,  0.00452077, -0.00509617,
         0.00947366,  0.00698859, -0.00662428,  0.00705692,  0.00142634,
         0.01124116, -0.02187956,  0.00824096,  0.009196  ,  0.01629323,
         0.00308878, -0.01798662,  0.00442771,  0.00855005,  0.03406683,
        -0.01220048,  0.00496649,  0.01438117,  0.00992037, -0.0087936 ,
         0.00814831,  0.00288912,  0.0097445 , -0.00083011, -0.00278994,
        -0.03658567, -0.00311721,  0.00624681, -0.00189713, -0.02947262,
         0.00626109,  0.00474429, -0.00600845, -0.00600886,  0.01489487,
         0.01527645,  0.01390779, -0.00211709,  0.01082167, -0.01084373,
         0.00533908, -0.0060085 ,  0.0022535 ,  0.01792572, -0.00195908,
         0.02661596,  0.01152584,  0.02012873, -0.02058013], dtype=float32),
 array([[  2.27199554e-01,  -2.57113457e-01,  -1.77013442e-01,
          -1.70919269e-01,   2.31279701e-01,   1.13302425e-01,
           1.14091136e-01,   2.63918564e-03,   3.90625605e-03,
          -1.68767616e-01],
        [ -2.75812924e-01,   2.79425323e-01,   1.92961514e-01,
          -1.08527146e-01,   2.78711379e-01,  -2.27263138e-01,
          -3.38807404e-02,  -2.40667358e-01,  -8.83584395e-02,
           1.53998345e-01],
        [ -1.87436461e-01,  -4.15476151e-02,   2.64846772e-01,
           2.28108838e-01,  -1.16850495e-01,  -1.30943835e-01,
           1.97022613e-02,   2.19481960e-01,   1.45279542e-01,
          -3.36738437e-01],
        [  1.54272169e-01,  -1.46834508e-01,  -1.16244793e-01,
          -2.04680264e-01,   2.36481279e-01,  -9.02779996e-02,
           1.58134744e-01,  -2.16092333e-01,   2.21976891e-01,
          -3.06478560e-01],
        [ -9.13032368e-02,  -1.29526496e-01,  -1.72021091e-01,
           2.71623433e-01,   2.36334607e-01,  -1.80978313e-01,
           2.07542077e-01,   2.05281064e-01,   1.39271945e-01,
           7.34422877e-02],
        [  2.68887013e-01,  -3.84743214e-01,  -4.53814492e-02,
          -5.39208949e-02,  -2.73803681e-01,   1.84203058e-01,
          -1.05920538e-01,  -2.20152661e-01,   1.00724861e-01,
           5.49882688e-02],
        [  1.18764728e-01,  -2.30835870e-01,  -7.18775988e-02,
           1.80881187e-01,  -2.02770699e-02,  -2.64777631e-01,
           1.13848254e-01,  -3.53392184e-01,  -1.00222595e-01,
          -2.43887305e-01],
        [ -2.95966685e-01,  -3.58311266e-01,  -3.15606207e-01,
           3.40228468e-01,  -2.89055914e-01,  -1.91512540e-01,
           2.03709789e-02,  -7.33947381e-02,  -2.68885285e-01,
          -1.45314097e-01],
        [  1.44678444e-01,  -2.20216185e-01,  -2.88866371e-01,
          -2.95819670e-01,   5.21352440e-02,   2.33309120e-01,
           1.24131337e-01,  -1.58179134e-01,   1.68531343e-01,
           1.86730519e-01],
        [  2.71883756e-01,   1.05057903e-01,  -1.25644505e-02,
           2.32350394e-01,  -3.28531742e-01,   6.20941706e-02,
          -2.09419176e-01,  -1.40810311e-01,  -2.11457014e-01,
          -6.35151714e-02],
        [ -5.34111112e-02,   3.45380306e-02,  -1.35113165e-01,
          -1.62397116e-01,  -3.87337625e-01,   1.53405577e-01,
          -1.89499617e-01,  -3.02919894e-02,  -1.88102841e-01,
          -1.79173410e-01],
        [  5.58387488e-02,   3.73322852e-02,   2.18576595e-01,
           1.54009044e-01,  -1.49218917e-01,  -7.06033260e-02,
           5.68501391e-02,   1.75858587e-01,   2.57969290e-01,
          -1.19513152e-02],
        [  2.98790485e-01,  -2.07670555e-01,  -2.64700621e-01,
           1.41980592e-02,   1.96788266e-01,  -2.92604357e-01,
           2.67412931e-01,  -1.59053043e-01,   7.32314866e-03,
           2.94690371e-01],
        [  3.02103758e-01,   1.33932143e-01,   2.19203219e-01,
          -8.17389339e-02,  -6.89085945e-02,  -1.85268641e-01,
          -5.57305105e-02,  -2.38308385e-01,   1.50871933e-01,
          -2.67988741e-01],
        [ -1.54590219e-01,   3.98003496e-02,  -1.17358334e-01,
           5.13160974e-02,  -2.91683137e-01,  -2.43264034e-01,
          -3.13334912e-01,  -1.41014576e-01,  -3.17830741e-01,
           8.59333798e-02],
        [ -4.30263169e-02,   2.44828593e-02,  -1.19065076e-01,
          -5.45082963e-04,   2.86619633e-01,  -1.58150509e-01,
           3.01434007e-02,   2.16535300e-01,  -4.14474696e-01,
           1.30073011e-01],
        [  1.41079938e-02,   6.40332699e-02,   1.13467045e-01,
           2.65778422e-01,  -3.05125743e-01,   1.33101374e-01,
          -2.71364599e-01,   8.35645869e-02,   2.77368367e-01,
           4.82500345e-02],
        [  1.77251622e-01,   1.69934928e-01,  -3.78782116e-02,
           1.28735751e-01,   4.08700109e-02,  -3.49745117e-02,
           1.33898169e-01,  -2.12117910e-01,   2.17818007e-01,
          -1.08952440e-01],
        [  2.08975598e-01,   2.92818509e-02,   4.39116469e-04,
           1.52113438e-01,   1.09986857e-01,  -2.49650523e-01,
          -3.36861283e-01,   2.92183936e-01,   7.72883510e-03,
          -1.66138023e-01],
        [ -8.57517347e-02,   1.51478425e-01,   9.25557613e-02,
           1.75205812e-01,  -1.53200909e-01,   2.30352297e-01,
           1.44412622e-01,  -2.22771123e-01,   2.65094221e-01,
          -3.02121460e-01],
        [  1.90764427e-01,   1.86336830e-01,   2.34901085e-01,
          -1.84299782e-01,   2.99284607e-02,   1.03449292e-01,
          -3.14840861e-02,   2.23225802e-01,  -1.83406517e-01,
           1.16176300e-01],
        [  2.99407423e-01,   1.66783825e-01,  -2.72359461e-01,
           2.09762976e-01,   7.45339543e-02,  -1.10964462e-01,
           2.56580144e-01,   1.62176803e-01,   1.29176736e-01,
          -1.82499975e-01],
        [ -1.33700222e-01,  -3.72590609e-02,   1.82002023e-01,
           2.19805032e-01,   2.83537209e-01,   1.76458687e-01,
          -3.19795877e-01,   2.10146323e-01,  -2.56385803e-01,
          -2.00856581e-01],
        [ -2.30001807e-02,   2.72753388e-01,   2.17077553e-01,
          -1.46781653e-02,  -1.89189509e-01,   2.63100863e-01,
          -1.45978451e-01,  -2.95014471e-01,   2.60790616e-01,
           7.96748176e-02],
        [ -1.45796776e-01,   2.18175560e-01,  -1.29518166e-01,
           9.38024819e-02,  -2.64606148e-01,  -2.43423119e-01,
           1.52353927e-01,  -2.70566523e-01,  -2.14991629e-01,
          -4.63702306e-02],
        [ -1.16731159e-01,  -2.61305839e-01,   1.41764313e-01,
           3.56869958e-02,  -3.41079116e-01,   2.94116229e-01,
           1.34668708e-01,   1.22645430e-01,  -1.36504471e-01,
          -1.18513688e-01],
        [  1.20217004e-03,   2.51902997e-01,   2.03765199e-01,
          -1.46404192e-01,   2.50091702e-01,   1.27985388e-01,
           1.61354810e-01,   2.19981059e-01,  -1.42366877e-02,
          -2.15715915e-01],
        [ -7.67053813e-02,   4.60804664e-02,  -3.43521357e-01,
           1.63204268e-01,  -1.26871467e-01,   2.16075912e-01,
          -2.33200535e-01,  -2.16597155e-01,  -3.11601192e-01,
          -2.42482632e-01],
        [ -3.37412685e-01,   1.39879122e-01,  -3.44309896e-01,
           2.80438691e-01,  -2.89053053e-01,   1.50709882e-01,
           2.17663646e-01,   2.04893306e-01,  -1.61889970e-01,
          -2.46577784e-01],
        [ -2.52028376e-01,  -6.51162416e-02,   1.55622199e-01,
          -1.66314587e-01,   8.94625783e-02,  -2.97883451e-01,
          -3.20139304e-02,   1.32251456e-02,   2.46251047e-01,
           1.02616869e-01],
        [ -1.52793825e-01,  -2.75805861e-01,  -1.23228341e-01,
           1.81739569e-01,   1.87746882e-01,   2.29500711e-01,
          -2.49644015e-02,  -9.11383182e-02,  -7.09892884e-02,
           5.00184447e-02],
        [  6.75391359e-03,  -6.09791055e-02,  -1.12856217e-01,
          -3.04151088e-01,  -1.42996525e-02,   1.92872524e-01,
          -1.49397224e-01,   3.32208574e-02,  -7.48034567e-02,
           9.24338698e-02],
        [  6.62017539e-02,   2.01928973e-01,  -6.13440797e-02,
          -3.13703448e-01,  -1.45382747e-01,   1.00455351e-01,
          -2.03057215e-01,   1.52138369e-02,   5.82579989e-04,
           1.41213372e-01],
        [  1.78411976e-01,  -2.04576895e-01,  -3.28929961e-01,
          -1.31248146e-01,  -4.82596830e-02,  -7.84068741e-03,
           3.06355208e-01,  -4.80976980e-03,   9.12943259e-02,
           2.35946048e-02],
        [ -2.45069265e-01,  -3.82708721e-02,  -2.39063770e-01,
           2.82954238e-02,  -4.67224829e-02,  -6.85872361e-02,
           1.95125248e-02,   2.31484860e-01,  -1.90710992e-01,
           1.43905729e-01],
        [  1.02160767e-01,  -2.09685296e-01,  -2.97667772e-01,
          -2.79983670e-01,  -5.31976484e-02,  -1.28686979e-01,
          -2.93887556e-02,  -2.72684902e-01,  -1.01809919e-01,
           1.90626122e-02],
        [  2.09710270e-01,   5.25735319e-02,  -2.52488911e-01,
           4.13311720e-02,  -2.03798786e-02,  -1.92051589e-01,
          -2.67291605e-01,  -2.34322045e-02,  -2.81463534e-01,
           2.25570142e-01],
        [  2.72732694e-02,  -1.75066695e-01,  -3.54795247e-01,
          -7.11853504e-02,   9.38303322e-02,   8.26030225e-02,
          -8.28038827e-02,  -3.30001004e-02,   1.35767087e-01,
          -3.29228878e-01],
        [  1.68258116e-01,   3.01740855e-01,   2.52543360e-01,
          -3.23650315e-02,   2.48474196e-01,   1.90074593e-01,
          -2.62334883e-01,   1.95796430e-01,  -2.34018415e-01,
          -2.27477089e-01],
        [  2.35338911e-01,  -1.89718008e-01,   1.60330772e-01,
           9.89737734e-02,   6.57345280e-02,   1.58491418e-01,
          -8.12683105e-02,   1.55207485e-01,  -1.86909646e-01,
          -6.21964000e-02],
        [ -5.81616424e-02,  -2.01229006e-02,   5.58019243e-02,
          -2.47738481e-01,  -1.54453859e-01,  -3.96426544e-02,
          -2.27877647e-01,  -5.62080927e-02,   3.15649249e-02,
          -1.73713788e-01],
        [  7.49514699e-02,  -2.77292281e-01,   2.22893342e-01,
           1.16635233e-01,   9.38363522e-02,  -1.34366795e-01,
           2.23353907e-01,  -2.00244188e-01,  -2.67709136e-01,
           2.05649465e-01],
        [  1.03751928e-01,   3.34706120e-02,   1.86501697e-01,
          -6.89404383e-02,   1.41317785e-01,  -1.95601150e-01,
           2.91108161e-01,   2.30690151e-01,  -1.63213566e-01,
          -1.59612030e-01],
        [ -8.35805461e-02,   2.18266286e-02,  -2.86656410e-01,
           1.37796119e-01,   2.23043516e-01,   2.77650446e-01,
           7.18319416e-02,   1.53950155e-01,  -2.91653812e-01,
           1.19053805e-02],
        [  1.72093913e-01,   2.29707524e-01,  -1.17506295e-01,
           1.30034834e-01,   1.80434555e-01,  -1.56087831e-01,
          -2.22451761e-01,   3.20497081e-02,  -1.10695111e-02,
          -1.71327293e-01],
        [ -2.13076711e-01,   1.08537488e-01,   1.13791622e-01,
           1.22002147e-01,   2.02805191e-01,   1.21919490e-01,
          -1.72846273e-01,  -6.84125274e-02,   2.39307716e-01,
          -2.37989485e-01],
        [ -8.37182719e-03,  -1.36512801e-01,  -1.16388679e-01,
           2.37981945e-01,  -3.74585629e-01,  -2.29619578e-01,
           5.06465994e-02,   2.86864191e-01,  -3.43668431e-01,
          -7.74058849e-02],
        [ -2.54211187e-01,  -1.15798600e-01,  -4.35124664e-03,
           1.58266146e-02,   2.57185489e-01,   2.66794950e-01,
           4.69176471e-02,  -1.09862396e-02,   2.16736197e-01,
           4.63430136e-02],
        [ -1.12226360e-01,  -2.57873416e-01,   2.03919604e-01,
           2.37405211e-01,  -1.14845850e-01,  -2.25683510e-01,
          -5.84898219e-02,  -1.14973262e-01,  -5.48982807e-02,
          -2.44586676e-01],
        [  2.62387484e-01,   1.32674947e-01,   2.13866547e-01,
          -2.27707893e-01,   1.25895411e-01,  -1.05258614e-01,
          -3.03990573e-01,   2.62807518e-01,   3.13323997e-02,
          -2.31100023e-01],
        [ -2.16944516e-01,   1.73459291e-01,  -1.94933429e-01,
           1.15096428e-01,   8.75729024e-02,  -7.18995184e-02,
           2.71084141e-02,  -2.76733994e-01,   3.05548757e-01,
          -1.03211693e-01],
        [ -3.38558376e-01,  -8.02640617e-02,  -2.94061929e-01,
           1.95091590e-01,   1.20830260e-01,  -2.09004775e-01,
          -3.21991175e-01,   2.23950669e-01,   2.92522777e-02,
           2.93040216e-01],
        [  1.61847219e-01,  -2.60404140e-01,  -1.90269932e-01,
           4.46208306e-02,  -3.45205277e-01,  -8.88504311e-02,
           9.66997072e-02,   2.68198580e-01,   2.35251158e-01,
          -9.43500698e-02],
        [ -4.74630669e-02,  -1.00912772e-01,  -1.90052122e-01,
          -2.87345827e-01,  -4.61423844e-01,   5.81858493e-02,
          -4.37013544e-02,   4.21729416e-01,  -1.11786038e-01,
          -2.49078587e-01],
        [  1.18645810e-01,  -6.85039386e-02,   1.55644864e-01,
           2.29464993e-01,  -1.80532515e-01,  -8.93409736e-03,
          -1.62305683e-01,  -2.86069185e-01,  -1.42704383e-01,
           1.89492881e-01],
        [ -2.44077027e-01,  -2.60956317e-01,   2.56882429e-01,
           3.07674799e-02,  -2.59813249e-01,  -2.20379010e-01,
          -4.19678949e-02,  -1.50883436e-01,  -1.74878091e-02,
          -1.16253763e-01],
        [  6.90871477e-02,  -2.45860338e-01,   2.62299716e-01,
          -5.13328090e-02,   1.93846926e-01,  -2.64902890e-01,
           1.15024939e-01,  -1.52685434e-01,  -1.66082922e-02,
           1.14513434e-01],
        [  9.71112996e-02,   7.44211301e-02,   1.00341365e-01,
           1.26194835e-01,   6.81405738e-02,   8.84246156e-02,
          -1.99568883e-01,  -8.18875134e-02,   2.33137131e-01,
           2.77939826e-01],
        [  1.80132892e-02,   2.66947508e-01,  -2.59600766e-02,
           1.23937383e-01,   1.51239052e-01,   1.39441639e-01,
           1.49141625e-01,  -3.60500254e-02,  -6.87832609e-02,
          -3.05294424e-01],
        [  8.04231465e-02,   1.13841267e-02,  -1.75071552e-01,
           2.01330662e-01,  -1.65964499e-01,   2.06818536e-01,
          -3.37303489e-01,  -2.05087006e-01,   2.93599963e-02,
           3.05837661e-01],
        [ -2.07948431e-01,   2.31808141e-01,  -1.98289439e-01,
           6.13826253e-02,   4.66298833e-02,   2.81204760e-01,
           1.34726942e-01,   1.16115034e-01,   5.52287288e-02,
           1.89750865e-01],
        [ -1.20472945e-01,  -2.93984950e-01,   1.58886924e-01,
           1.74282473e-02,  -2.52005219e-01,   5.79595864e-02,
           2.49596745e-01,   1.89382896e-01,   1.43527210e-01,
          -1.18359759e-01],
        [ -3.73746753e-01,  -2.16751620e-01,  -2.86016434e-01,
          -2.48870552e-01,   2.60464787e-01,   1.04101159e-01,
          -2.18318805e-01,   1.19532302e-01,  -6.86946288e-02,
           2.96406239e-01],
        [  1.23343788e-01,   2.01594979e-01,  -1.73242949e-02,
           2.27668643e-01,  -7.74656236e-02,   2.04612225e-01,
           7.65966251e-02,   1.42209932e-01,   1.78642780e-01,
          -5.97998910e-02]], dtype=float32),
 array([ -3.31726447e-02,   2.15315968e-02,   9.42673360e-05,
         -3.13507393e-02,   1.74332801e-02,  -1.34902140e-02,
          4.82997042e-04,   1.06283929e-02,   1.36456415e-02,
          9.77588817e-04], dtype=float32)]

In [6]:
# Weights and bias for a single layer.
conv_layer = model.get_layer('conv2d_1')
conv_layer.get_weights()


Out[6]:
[array([[[[-0.33856323,  0.20390955, -0.11109605,  0.27959093,  0.00380358,
            0.06809236,  0.13476925, -0.06905884, -0.00914569,  0.01789398,
           -0.15531571,  0.1061649 , -0.16948256,  0.07929865,  0.16483295,
           -0.07207379,  0.24873769, -0.19941764,  0.16662872,  0.19174086,
           -0.01943381,  0.0066115 ,  0.0134623 ,  0.04628464,  0.00221355,
           -0.23785631,  0.05527339, -0.32550618, -0.12803005,  0.00084405,
           -0.21533267, -0.18520311]],
 
         [[-0.05132618,  0.11048742, -0.01836612, -0.12476675, -0.18518658,
            0.03234763,  0.19836517, -0.11385031, -0.22344072,  0.17603368,
            0.14256321,  0.085033  ,  0.00895712,  0.06350163,  0.1005916 ,
            0.13216837, -0.05474935, -0.13029362, -0.05059361,  0.10186421,
            0.13374192, -0.11962113, -0.10992999,  0.08803006, -0.03906573,
           -0.3092995 , -0.01672952, -0.23022896, -0.30201221,  0.10596731,
           -0.27116138, -0.35430175]],
 
         [[ 0.33610955,  0.033969  ,  0.00095919, -0.17490396,  0.03093159,
            0.10593548,  0.28105086,  0.12002508, -0.25334483, -0.03389421,
            0.19349942,  0.32704428,  0.1018483 ,  0.04974011,  0.15481295,
            0.12925673, -0.14330553,  0.10459574, -0.16167818, -0.00957763,
           -0.17980698, -0.19601411, -0.00238411, -0.30377474, -0.15770112,
           -0.04087055,  0.02476479,  0.00098312, -0.24966806, -0.09523545,
           -0.15778606, -0.22892013]]],
 
 
        [[[-0.35785612,  0.11476454,  0.04976963,  0.05825217, -0.10467952,
            0.02363232, -0.07825409,  0.14956443,  0.12504648,  0.01157873,
           -0.06974483, -0.13124345,  0.11080556,  0.10724898, -0.10358807,
           -0.03627256,  0.05047637, -0.14007384, -0.01534044,  0.14637068,
            0.14159171,  0.07099191,  0.04750481,  0.23509285,  0.00062798,
           -0.28756508, -0.00313633, -0.18350028,  0.06473579,  0.23929332,
            0.0193444 , -0.11831183]],
 
         [[-0.00380083,  0.23089294, -0.21173477, -0.21526209,  0.10552941,
            0.16956028,  0.01564491,  0.1375519 ,  0.16510786,  0.07982598,
            0.19161849, -0.21977969, -0.02409861,  0.157625  ,  0.01468054,
            0.12768054, -0.27593395,  0.12159296,  0.10541939, -0.02288091,
            0.18439572, -0.09308205, -0.04813813, -0.11125364,  0.17840828,
            0.02104141,  0.23276028,  0.05439105, -0.08695178,  0.08954187,
            0.13792819,  0.01321854]],
 
         [[ 0.19480787,  0.12171868, -0.27793065,  0.068828  ,  0.12536857,
           -0.04126728, -0.06341767,  0.10707708,  0.10291349,  0.12810974,
            0.04934532, -0.18099618, -0.02886892,  0.12866126,  0.18573213,
            0.20035776, -0.07830612,  0.12074151,  0.11779081,  0.11454585,
           -0.04120336, -0.20274398, -0.00399038, -0.11202653,  0.13420886,
            0.21578619,  0.14358513,  0.1243699 ,  0.13599069, -0.04502468,
            0.13634589,  0.15575847]]],
 
 
        [[[-0.20989153,  0.15227273,  0.23237251, -0.08367927,  0.14982514,
            0.16587329, -0.28325978, -0.08602066,  0.13957199,  0.19290963,
            0.00193023, -0.12849583,  0.12334183,  0.16267049, -0.13655755,
           -0.12450543, -0.2113706 , -0.08632083,  0.02195895, -0.0963969 ,
            0.06236559,  0.22840671,  0.1670882 ,  0.087584  ,  0.06964725,
           -0.11409627,  0.08816697, -0.03587545,  0.18001753,  0.0437397 ,
            0.22911704,  0.0286596 ]],
 
         [[-0.04831968, -0.09599441,  0.16779834, -0.30507049,  0.19729429,
            0.06221648, -0.19661918,  0.03457066,  0.18631683,  0.17452584,
            0.18820918,  0.04805256,  0.06722377,  0.26525283, -0.00758747,
           -0.04513836,  0.16339223,  0.00425898,  0.22618811,  0.00424452,
           -0.11183094,  0.16458337,  0.03243278,  0.13851871,  0.03470041,
            0.07749669,  0.25952315,  0.22436245,  0.2598435 , -0.09826418,
            0.05551234,  0.27973706]],
 
         [[ 0.1098677 , -0.08579756, -0.05499269,  0.00762451, -0.09339599,
            0.07986619, -0.14637941, -0.11159059,  0.09420574, -0.07074815,
            0.09719007, -0.10575279,  0.08875547,  0.23332612,  0.09377708,
            0.20497514,  0.24949729,  0.1880891 ,  0.05041798, -0.13240333,
            0.09229264,  0.14950337,  0.16152637, -0.00753751,  0.02833049,
            0.21858366,  0.21298586,  0.30639312,  0.12163855, -0.26328984,
            0.1449654 ,  0.08858766]]]], dtype=float32),
 array([-0.00504222, -0.03398184,  0.03041283,  0.05144327, -0.02014533,
        -0.03628516,  0.00899984, -0.0141687 , -0.00900476, -0.04701506,
        -0.03609759,  0.00830616, -0.03162324, -0.04258636, -0.03593863,
        -0.02859137,  0.00309902, -0.00644259, -0.01920764, -0.01361663,
        -0.02481366, -0.00387063, -0.0365628 , -0.00746764, -0.0200652 ,
         0.0303475 , -0.03546616,  0.00537372, -0.00621093, -0.00206236,
        -0.00341022, -0.00076332], dtype=float32)]

Moreover the respespective backend variables that store the weights can be retrieved.


In [7]:
conv_layer.weights


Out[7]:
[<tf.Variable 'conv2d_1/kernel:0' shape=(3, 3, 1, 32) dtype=float32_ref>,
 <tf.Variable 'conv2d_1/bias:0' shape=(32,) dtype=float32_ref>]

Getting the activations and net inputs

Intermediary computation results, i.e. results are not part of the prediction cannot be directly retrieved from Keras. It possible to build a new model for which the intermediary result is the prediction, but this approach makes computation rather inefficient when several intermediary results are to be retrieved. Instead it is better to reach directly into the backend for this purpose.

Activations are still fairly straight forward as the relevant tensors can be retrieved as output of the layer.


In [8]:
# Getting the Tensorflow session and the input tensor.
sess = keras.backend.get_session()
network_input_tensor = model.layers[0].input
network_input_tensor


Out[8]:
<tf.Tensor 'conv2d_1_input:0' shape=(?, 28, 28, 1) dtype=float32>

In [10]:
# Getting the tensor that holds the actiations as the output of a layer.
activation_tensor = conv_layer.output
activation_tensor


Out[10]:
<tf.Tensor 'conv2d_1/Relu:0' shape=(?, 26, 26, 32) dtype=float32>

In [16]:
activations = sess.run(activation_tensor, feed_dict={network_input_tensor: test_data[0:1]})
activations.shape


Out[16]:
(1, 26, 26, 32)

In [19]:
for i in range(32):
    plt.imshow(activations[0, ..., i])
    plt.show()


Net input is a little more complicated as we have to reach heuristically into the TensorFlow graph to find the relevant tensors. However, it can be safely assumed most of the time that the net input tensor in input to the activaton op.


In [28]:
net_input_tensor = activation_tensor.op.inputs[0]
net_input_tensor


Out[28]:
<tf.Tensor 'conv2d_1/BiasAdd:0' shape=(?, 26, 26, 32) dtype=float32>

In [29]:
net_inputs = sess.run(net_input_tensor, feed_dict={network_input_tensor: test_data[0:1]})
net_inputs.shape


Out[29]:
(1, 26, 26, 32)

In [30]:
for i in range(32):
    plt.imshow(net_inputs[0, ..., i])
    plt.show()


Getting layer properties

Each Keras layer object provides the relevant properties as attributes


In [13]:
conv_layer = model.get_layer('conv2d_1')
conv_layer


Out[13]:
<keras.layers.convolutional.Conv2D at 0x7f0a1b375908>

In [14]:
conv_layer.input_shape


Out[14]:
(None, 28, 28, 1)

In [16]:
conv_layer.output_shape


Out[16]:
(None, 26, 26, 32)

In [17]:
conv_layer.kernel_size


Out[17]:
(3, 3)

In [18]:
conv_layer.strides


Out[18]:
(1, 1)

In [19]:
max_pool_layer = model.get_layer('max_pooling2d_1')
max_pool_layer


Out[19]:
<keras.layers.pooling.MaxPooling2D at 0x7f0a1b375dd8>

In [20]:
max_pool_layer.strides


Out[20]:
(2, 2)

In [21]:
max_pool_layer.pool_size


Out[21]:
(2, 2)

Layer type information can only be retrieved through the class name


In [24]:
conv_layer.__class__.__name__


Out[24]:
'Conv2D'