Search code examples
pythonkerastheanokeras-layer

Inconsistency in Keras Flatten() layer behavior using Theano Backend


I am trying to understand the behavior of the Flatten() layer in Keras with Theano backend. I have two different versions of Keras installed in two different Conda environments. The way a 4D tensor is unrolled using Flatten() differs in these two versions and I am thoroughly confused about which one is correct.

I have written the following two code snippets to show the problem:

enter image description here

The above code is flattening the input matrix along the channels axis first

However, in another version, the result is different:

enter image description here

The above code is flattening the input matrix along the columns axis first.

Can someone please explain this? Thanks!


Solution

  • Both are correct. The difference is because of the image_data_format setting. This can be set in keras.json or via the backend API.

    https://keras.io/backend/

    >>> from keras import backend as K
    >>> K.image_data_format()
    'channels_first'
    

    When format is 'channels_first', output is

    array([[ 0.,  9., 18., 27.,  1., 10., 19., 28.,  2., 11., 20., 29.,  3.,
            12., 21., 30.,  4., 13., 22., 31.,  5., 14., 23., 32.,  6., 15.,
            24., 33.,  7., 16., 25., 34.,  8., 17., 26., 35.]], dtype=float32)
    

    When format is 'channels_last', output is

    array([[ 0.,  1.,  2.,  3.,  4.,  5.,  6.,  7.,  8.,  9., 10., 11., 12.,
            13., 14., 15., 16., 17., 18., 19., 20., 21., 22., 23., 24., 25.,
            26., 27., 28., 29., 30., 31., 32., 33., 34., 35.]], dtype=float32)