In [1]:
import numpy as np
from keras.models import Model
from keras.layers import Input
from keras.layers.core import Flatten
from keras import backend as K
import json
from collections import OrderedDict


Using TensorFlow backend.

In [2]:
def format_decimal(arr, places=6):
    return [round(x * 10**places) / 10**places for x in arr]

In [3]:
DATA = OrderedDict()

Flatten

[core.Flatten.0] 1D


In [4]:
# throws exception for TF

data_in = [0, 0.2, 0.5, -0.1, 1, 2]
data_in_shape = (6,)
print('in:', data_in)
print('in shape:', data_in_shape)
print('out shape:', data_in_shape)
print('out:', data_in)

DATA['core.Flatten.0'] = {
    'input': {'data': data_in, 'shape': data_in_shape},
    'expected': {'data': data_in, 'shape': data_in_shape}
}


in: [0, 0.2, 0.5, -0.1, 1, 2]
in shape: (6,)
out shape: (6,)
out: [0, 0.2, 0.5, -0.1, 1, 2]

[core.Flatten.1] 2D


In [5]:
layer_0 = Input(shape=(3, 2))
layer_1 = Flatten()(layer_0)
model = Model(inputs=layer_0, outputs=layer_1)

data_in = [0, 0.2, 0.5, -0.1, 1, 2]
data_in_shape = (3, 2)
print('in:', data_in)
print('in shape:', data_in_shape)
arr_in = np.array(data_in, dtype='float32').reshape(data_in_shape)
result = model.predict(np.array([arr_in]))
arr_out = result[0]
data_out_shape = arr_out.shape
print('out shape:', data_out_shape)
data_out = format_decimal(arr_out.ravel().tolist())
print('out:', data_out)

DATA['core.Flatten.1'] = {
    'input': {'data': data_in, 'shape': data_in_shape},
    'expected': {'data': data_out, 'shape': data_out_shape}
}


in: [0, 0.2, 0.5, -0.1, 1, 2]
in shape: (3, 2)
out shape: (6,)
out: [0.0, 0.2, 0.5, -0.1, 1.0, 2.0]

[core.Flatten.2] 3D


In [6]:
layer_0 = Input(shape=(3, 2, 2))
layer_1 = Flatten()(layer_0)
model = Model(inputs=layer_0, outputs=layer_1)

data_in = [0, 0.2, 0.5, -0.1, 1, 2, 0, 0.2, 0.5, -0.1, 1, 2]
data_in_shape = (3, 2, 2)
print('in:', data_in)
print('in shape:', data_in_shape)
arr_in = np.array(data_in, dtype='float32').reshape(data_in_shape)
result = model.predict(np.array([arr_in]))
arr_out = result[0]
data_out_shape = arr_out.shape
print('out shape:', data_out_shape)
data_out = format_decimal(arr_out.ravel().tolist())
print('out:', data_out)

DATA['core.Flatten.2'] = {
    'input': {'data': data_in, 'shape': data_in_shape},
    'expected': {'data': data_out, 'shape': data_out_shape}
}


in: [0, 0.2, 0.5, -0.1, 1, 2, 0, 0.2, 0.5, -0.1, 1, 2]
in shape: (3, 2, 2)
out shape: (12,)
out: [0.0, 0.2, 0.5, -0.1, 1.0, 2.0, 0.0, 0.2, 0.5, -0.1, 1.0, 2.0]

export for Keras.js tests


In [7]:
print(json.dumps(DATA))


Out[7]:
'{"core.Flatten.0": {"expected": {"shape": [6], "data": [0, 0.2, 0.5, -0.1, 1, 2]}, "input": {"shape": [6], "data": [0, 0.2, 0.5, -0.1, 1, 2]}}, "core.Flatten.1": {"expected": {"shape": [6], "data": [0.0, 0.2, 0.5, -0.1, 1.0, 2.0]}, "input": {"shape": [3, 2], "data": [0, 0.2, 0.5, -0.1, 1, 2]}}, "core.Flatten.2": {"expected": {"shape": [12], "data": [0.0, 0.2, 0.5, -0.1, 1.0, 2.0, 0.0, 0.2, 0.5, -0.1, 1.0, 2.0]}, "input": {"shape": [3, 2, 2], "data": [0, 0.2, 0.5, -0.1, 1, 2, 0, 0.2, 0.5, -0.1, 1, 2]}}}'

In [ ]: