In [1]:
import numpy as np
from keras.models import Model
from keras.layers import Input, Dense, RepeatVector
from keras.legacy.layers import Merge
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()

Merge

[legacy.Merge.0] mode: sum


In [4]:
layer_0 = Input(shape=(6,))
layer_1a = Dense(2, activation='linear')(layer_0)
layer_1b = Dense(2, activation='linear')(layer_0)
layer_2 = Merge(mode='sum')([layer_1a, layer_1b])
model = Model(inputs=layer_0, outputs=layer_2)

W_1a = np.array([0.1, 0.4, 0.5, 0.1, 1, -2, 0, 0.3, 0.2, 0.1, 3, 0]).reshape((6, 2))
b_1a = np.array([0.5, 0.7])
W_1b = np.array([1, 0, -0.9, 0.6, -0.7, 0, 0.2, 0.4, 0, 0, -1, 2.3]).reshape((6, 2))
b_1b = np.array([0.1, -0.2])
model.set_weights([W_1a, b_1a, W_1b, b_1b])

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)
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['legacy.Merge.0'] = {
    'input': {'data': data_in, 'shape': data_in_shape},
    'weights': [{'data': format_decimal(w.ravel().tolist()), 'shape': w.shape} for w in [W_1a, b_1a, W_1b, b_1b]],
    'expected': {'data': data_out, 'shape': data_out_shape}
}


/home/leon/miniconda3/lib/python3.5/site-packages/ipykernel/__main__.py:4: UserWarning: The `Merge` layer is deprecated and will be removed after 08/2017. Use instead layers from `keras.layers.merge`, e.g. `add`, `concatenate`, etc.
in: [0, 0.2, 0.5, -0.1, 1, 2]
in shape: (6,)
out shape: (2,)
out: [4.849999, 4.27]

[legacy.Merge.1] mode: mul


In [5]:
layer_0 = Input(shape=(6,))
layer_1a = Dense(2, activation='linear')(layer_0)
layer_1b = Dense(2, activation='linear')(layer_0)
layer_2 = Merge(mode='mul')([layer_1a, layer_1b])
model = Model(inputs=layer_0, outputs=layer_2)

W_1a = np.array([0.1, 0.4, 0.5, 0.1, 1, -2, 0, 0.3, 0.2, 0.1, 3, 0]).reshape((6, 2))
b_1a = np.array([0.5, 0.7])
W_1b = np.array([1, 0, -0.9, 0.6, -0.7, 0, 0.2, 0.4, 0, 0, -1, 2.3]).reshape((6, 2))
b_1b = np.array([0.1, -0.2])
model.set_weights([W_1a, b_1a, W_1b, b_1b])

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)
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['legacy.Merge.1'] = {
    'input': {'data': data_in, 'shape': data_in_shape},
    'weights': [{'data': format_decimal(w.ravel().tolist()), 'shape': w.shape} for w in [W_1a, b_1a, W_1b, b_1b]],
    'expected': {'data': data_out, 'shape': data_out_shape}
}


in: [0, 0.2, 0.5, -0.1, 1, 2]
in shape: (6,)
out shape: (2,)
out: [-17.885, -0.9408]
/home/leon/miniconda3/lib/python3.5/site-packages/ipykernel/__main__.py:4: UserWarning: The `Merge` layer is deprecated and will be removed after 08/2017. Use instead layers from `keras.layers.merge`, e.g. `add`, `concatenate`, etc.

[legacy.Merge.2] mode: ave


In [6]:
layer_0 = Input(shape=(6,))
layer_1a = Dense(2, activation='linear')(layer_0)
layer_1b = Dense(2, activation='linear')(layer_0)
layer_2 = Merge(mode='ave')([layer_1a, layer_1b])
model = Model(inputs=layer_0, outputs=layer_2)

W_1a = np.array([0.1, 0.4, 0.5, 0.1, 1, -2, 0, 0.3, 0.2, 0.1, 3, 0]).reshape((6, 2))
b_1a = np.array([0.5, 0.7])
W_1b = np.array([1, 0, -0.9, 0.6, -0.7, 0, 0.2, 0.4, 0, 0, -1, 2.3]).reshape((6, 2))
b_1b = np.array([0.1, -0.2])
model.set_weights([W_1a, b_1a, W_1b, b_1b])

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)
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['legacy.Merge.2'] = {
    'input': {'data': data_in, 'shape': data_in_shape},
    'weights': [{'data': format_decimal(w.ravel().tolist()), 'shape': w.shape} for w in [W_1a, b_1a, W_1b, b_1b]],
    'expected': {'data': data_out, 'shape': data_out_shape}
}


in: [0, 0.2, 0.5, -0.1, 1, 2]
in shape: (6,)
out shape: (2,)
out: [2.425, 2.135]
/home/leon/miniconda3/lib/python3.5/site-packages/ipykernel/__main__.py:4: UserWarning: The `Merge` layer is deprecated and will be removed after 08/2017. Use instead layers from `keras.layers.merge`, e.g. `add`, `concatenate`, etc.

[legacy.Merge.3] mode: max


In [7]:
layer_0 = Input(shape=(6,))
layer_1a = Dense(2, activation='linear')(layer_0)
layer_1b = Dense(2, activation='linear')(layer_0)
layer_2 = Merge(mode='max')([layer_1a, layer_1b])
model = Model(inputs=layer_0, outputs=layer_2)

W_1a = np.array([0.1, 0.4, 0.5, 0.1, 1, -2, 0, 0.3, 0.2, 0.1, 3, 0]).reshape((6, 2))
b_1a = np.array([0.5, 0.7])
W_1b = np.array([1, 0, -0.9, 0.6, -0.7, 0, 0.2, 0.4, 0, 0, -1, 2.3]).reshape((6, 2))
b_1b = np.array([0.1, -0.2])
model.set_weights([W_1a, b_1a, W_1b, b_1b])

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)
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['legacy.Merge.3'] = {
    'input': {'data': data_in, 'shape': data_in_shape},
    'weights': [{'data': format_decimal(w.ravel().tolist()), 'shape': w.shape} for w in [W_1a, b_1a, W_1b, b_1b]],
    'expected': {'data': data_out, 'shape': data_out_shape}
}


in: [0, 0.2, 0.5, -0.1, 1, 2]
in shape: (6,)
out shape: (2,)
out: [7.3, 4.48]
/home/leon/miniconda3/lib/python3.5/site-packages/ipykernel/__main__.py:4: UserWarning: The `Merge` layer is deprecated and will be removed after 08/2017. Use instead layers from `keras.layers.merge`, e.g. `add`, `concatenate`, etc.

[legacy.Merge.4] mode: concat (1D)


In [8]:
layer_0 = Input(shape=(6,))
layer_1a = Dense(2, activation='linear')(layer_0)
layer_1b = Dense(2, activation='linear')(layer_0)
layer_2 = Merge(mode='concat', concat_axis=-1)([layer_1a, layer_1b])
model = Model(inputs=layer_0, outputs=layer_2)

W_1a = np.array([0.1, 0.4, 0.5, 0.1, 1, -2, 0, 0.3, 0.2, 0.1, 3, 0]).reshape((6, 2))
b_1a = np.array([0.5, 0.7])
W_1b = np.array([1, 0, -0.9, 0.6, -0.7, 0, 0.2, 0.4, 0, 0, -1, 2.3]).reshape((6, 2))
b_1b = np.array([0.1, -0.2])
model.set_weights([W_1a, b_1a, W_1b, b_1b])

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)
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['legacy.Merge.4'] = {
    'input': {'data': data_in, 'shape': data_in_shape},
    'weights': [{'data': format_decimal(w.ravel().tolist()), 'shape': w.shape} for w in [W_1a, b_1a, W_1b, b_1b]],
    'expected': {'data': data_out, 'shape': data_out_shape}
}


in: [0, 0.2, 0.5, -0.1, 1, 2]
in shape: (6,)
out shape: (4,)
out: [7.3, -0.21, -2.45, 4.48]
/home/leon/miniconda3/lib/python3.5/site-packages/ipykernel/__main__.py:4: UserWarning: The `Merge` layer is deprecated and will be removed after 08/2017. Use instead layers from `keras.layers.merge`, e.g. `add`, `concatenate`, etc.

[legacy.Merge.5] mode: concat (2D, concatAxis=-1)


In [9]:
layer_0 = Input(shape=(6,))
layer_1a = Dense(2, activation='linear')(layer_0)
layer_1a = RepeatVector(3)(layer_1a)
layer_1b = Dense(2, activation='linear')(layer_0)
layer_1b = RepeatVector(3)(layer_1b)
layer_2 = Merge(mode='concat', concat_axis=-1)([layer_1a, layer_1b])
model = Model(inputs=layer_0, outputs=layer_2)

W_1a = np.array([0.1, 0.4, 0.5, 0.1, 1, -2, 0, 0.3, 0.2, 0.1, 3, 0]).reshape((6, 2))
b_1a = np.array([0.5, 0.7])
W_1b = np.array([1, 0, -0.9, 0.6, -0.7, 0, 0.2, 0.4, 0, 0, -1, 2.3]).reshape((6, 2))
b_1b = np.array([0.1, -0.2])
model.set_weights([W_1a, b_1a, W_1b, b_1b])

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)
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['legacy.Merge.5'] = {
    'input': {'data': data_in, 'shape': data_in_shape},
    'weights': [{'data': format_decimal(w.ravel().tolist()), 'shape': w.shape} for w in [W_1a, b_1a, W_1b, b_1b]],
    'expected': {'data': data_out, 'shape': data_out_shape}
}


/home/leon/miniconda3/lib/python3.5/site-packages/ipykernel/__main__.py:6: UserWarning: The `Merge` layer is deprecated and will be removed after 08/2017. Use instead layers from `keras.layers.merge`, e.g. `add`, `concatenate`, etc.
in: [0, 0.2, 0.5, -0.1, 1, 2]
in shape: (6,)
out shape: (3, 4)
out: [7.3, -0.21, -2.45, 4.48, 7.3, -0.21, -2.45, 4.48, 7.3, -0.21, -2.45, 4.48]

[legacy.Merge.6] mode: concat (2D, concatAxis=-2)


In [10]:
layer_0 = Input(shape=(6,))
layer_1a = Dense(2, activation='linear')(layer_0)
layer_1a = RepeatVector(3)(layer_1a)
layer_1b = Dense(2, activation='linear')(layer_0)
layer_1b = RepeatVector(3)(layer_1b)
layer_2 = Merge(mode='concat', concat_axis=-2)([layer_1a, layer_1b])
model = Model(inputs=layer_0, outputs=layer_2)

W_1a = np.array([0.1, 0.4, 0.5, 0.1, 1, -2, 0, 0.3, 0.2, 0.1, 3, 0]).reshape((6, 2))
b_1a = np.array([0.5, 0.7])
W_1b = np.array([1, 0, -0.9, 0.6, -0.7, 0, 0.2, 0.4, 0, 0, -1, 2.3]).reshape((6, 2))
b_1b = np.array([0.1, -0.2])
model.set_weights([W_1a, b_1a, W_1b, b_1b])

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)
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['legacy.Merge.6'] = {
    'input': {'data': data_in, 'shape': data_in_shape},
    'weights': [{'data': format_decimal(w.ravel().tolist()), 'shape': w.shape} for w in [W_1a, b_1a, W_1b, b_1b]],
    'expected': {'data': data_out, 'shape': data_out_shape}
}


in: [0, 0.2, 0.5, -0.1, 1, 2]
in shape: (6,)
out shape: (6, 2)
out: [7.3, -0.21, 7.3, -0.21, 7.3, -0.21, -2.45, 4.48, -2.45, 4.48, -2.45, 4.48]
/home/leon/miniconda3/lib/python3.5/site-packages/ipykernel/__main__.py:6: UserWarning: The `Merge` layer is deprecated and will be removed after 08/2017. Use instead layers from `keras.layers.merge`, e.g. `add`, `concatenate`, etc.

[legacy.Merge.7] mode: concat (2D, concatAxis=1)


In [11]:
layer_0 = Input(shape=(6,))
layer_1a = Dense(2, activation='linear')(layer_0)
layer_1a = RepeatVector(3)(layer_1a)
layer_1b = Dense(2, activation='linear')(layer_0)
layer_1b = RepeatVector(3)(layer_1b)
layer_2 = Merge(mode='concat', concat_axis=1)([layer_1a, layer_1b])
model = Model(inputs=layer_0, outputs=layer_2)

W_1a = np.array([0.1, 0.4, 0.5, 0.1, 1, -2, 0, 0.3, 0.2, 0.1, 3, 0]).reshape((6, 2))
b_1a = np.array([0.5, 0.7])
W_1b = np.array([1, 0, -0.9, 0.6, -0.7, 0, 0.2, 0.4, 0, 0, -1, 2.3]).reshape((6, 2))
b_1b = np.array([0.1, -0.2])
model.set_weights([W_1a, b_1a, W_1b, b_1b])

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)
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['legacy.Merge.7'] = {
    'input': {'data': data_in, 'shape': data_in_shape},
    'weights': [{'data': format_decimal(w.ravel().tolist()), 'shape': w.shape} for w in [W_1a, b_1a, W_1b, b_1b]],
    'expected': {'data': data_out, 'shape': data_out_shape}
}


in: [0, 0.2, 0.5, -0.1, 1, 2]
in shape: (6,)
out shape: (6, 2)
out: [7.3, -0.21, 7.3, -0.21, 7.3, -0.21, -2.45, 4.48, -2.45, 4.48, -2.45, 4.48]
/home/leon/miniconda3/lib/python3.5/site-packages/ipykernel/__main__.py:6: UserWarning: The `Merge` layer is deprecated and will be removed after 08/2017. Use instead layers from `keras.layers.merge`, e.g. `add`, `concatenate`, etc.

[legacy.Merge.8] mode: concat (2D, concatAxis=2)


In [12]:
layer_0 = Input(shape=(6,))
layer_1a = Dense(2, activation='linear')(layer_0)
layer_1a = RepeatVector(3)(layer_1a)
layer_1b = Dense(2, activation='linear')(layer_0)
layer_1b = RepeatVector(3)(layer_1b)
layer_2 = Merge(mode='concat', concat_axis=2)([layer_1a, layer_1b])
model = Model(inputs=layer_0, outputs=layer_2)

W_1a = np.array([0.1, 0.4, 0.5, 0.1, 1, -2, 0, 0.3, 0.2, 0.1, 3, 0]).reshape((6, 2))
b_1a = np.array([0.5, 0.7])
W_1b = np.array([1, 0, -0.9, 0.6, -0.7, 0, 0.2, 0.4, 0, 0, -1, 2.3]).reshape((6, 2))
b_1b = np.array([0.1, -0.2])
model.set_weights([W_1a, b_1a, W_1b, b_1b])

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)
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['legacy.Merge.8'] = {
    'input': {'data': data_in, 'shape': data_in_shape},
    'weights': [{'data': format_decimal(w.ravel().tolist()), 'shape': w.shape} for w in [W_1a, b_1a, W_1b, b_1b]],
    'expected': {'data': data_out, 'shape': data_out_shape}
}


/home/leon/miniconda3/lib/python3.5/site-packages/ipykernel/__main__.py:6: UserWarning: The `Merge` layer is deprecated and will be removed after 08/2017. Use instead layers from `keras.layers.merge`, e.g. `add`, `concatenate`, etc.
in: [0, 0.2, 0.5, -0.1, 1, 2]
in shape: (6,)
out shape: (3, 4)
out: [7.3, -0.21, -2.45, 4.48, 7.3, -0.21, -2.45, 4.48, 7.3, -0.21, -2.45, 4.48]

[legacy.Merge.9] mode: dot (2D x 2D, dotAxes=1)


In [13]:
layer_0 = Input(shape=(6,))
layer_1a = Dense(2, activation='linear')(layer_0)
layer_1a = RepeatVector(3)(layer_1a)
layer_1b = Dense(2, activation='linear')(layer_0)
layer_1b = RepeatVector(3)(layer_1b)
layer_2 = Merge(mode='dot', dot_axes=(1,1))([layer_1a, layer_1b])
model = Model(inputs=layer_0, outputs=layer_2)

W_1a = np.array([0.1, 0.4, 0.5, 0.1, 1, -2, 0, 0.3, 0.2, 0.1, 3, 0]).reshape((6, 2))
b_1a = np.array([0.5, 0.7])
W_1b = np.array([1, 0, -0.9, 0.6, -0.7, 0, 0.2, 0.4, 0, 0, -1, 2.3]).reshape((6, 2))
b_1b = np.array([0.1, -0.2])
model.set_weights([W_1a, b_1a, W_1b, b_1b])

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)
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['legacy.Merge.9'] = {
    'input': {'data': data_in, 'shape': data_in_shape},
    'weights': [{'data': format_decimal(w.ravel().tolist()), 'shape': w.shape} for w in [W_1a, b_1a, W_1b, b_1b]],
    'expected': {'data': data_out, 'shape': data_out_shape}
}


/home/leon/miniconda3/lib/python3.5/site-packages/ipykernel/__main__.py:6: UserWarning: The `Merge` layer is deprecated and will be removed after 08/2017. Use instead layers from `keras.layers.merge`, e.g. `add`, `concatenate`, etc.
in: [0, 0.2, 0.5, -0.1, 1, 2]
in shape: (6,)
out shape: (2, 2)
out: [-53.654999, 98.112, 1.5435, -2.822401]

[legacy.Merge.10] mode: dot (2D x 2D, dotAxes=2)


In [14]:
layer_0 = Input(shape=(6,))
layer_1a = Dense(2, activation='linear')(layer_0)
layer_1a = RepeatVector(3)(layer_1a)
layer_1b = Dense(2, activation='linear')(layer_0)
layer_1b = RepeatVector(3)(layer_1b)
layer_2 = Merge(mode='dot', dot_axes=(2,2))([layer_1a, layer_1b])
model = Model(inputs=layer_0, outputs=layer_2)

W_1a = np.array([0.1, 0.4, 0.5, 0.1, 1, -2, 0, 0.3, 0.2, 0.1, 3, 0]).reshape((6, 2))
b_1a = np.array([0.5, 0.7])
W_1b = np.array([1, 0, -0.9, 0.6, -0.7, 0, 0.2, 0.4, 0, 0, -1, 2.3]).reshape((6, 2))
b_1b = np.array([0.1, -0.2])
model.set_weights([W_1a, b_1a, W_1b, b_1b])

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)
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['legacy.Merge.10'] = {
    'input': {'data': data_in, 'shape': data_in_shape},
    'weights': [{'data': format_decimal(w.ravel().tolist()), 'shape': w.shape} for w in [W_1a, b_1a, W_1b, b_1b]],
    'expected': {'data': data_out, 'shape': data_out_shape}
}


/home/leon/miniconda3/lib/python3.5/site-packages/ipykernel/__main__.py:6: UserWarning: The `Merge` layer is deprecated and will be removed after 08/2017. Use instead layers from `keras.layers.merge`, e.g. `add`, `concatenate`, etc.
in: [0, 0.2, 0.5, -0.1, 1, 2]
in shape: (6,)
out shape: (3, 3)
out: [-18.8258, -18.8258, -18.8258, -18.8258, -18.8258, -18.8258, -18.8258, -18.8258, -18.8258]

[legacy.Merge.11] mode: cos (2D x 2D, dotAxes=1)


In [15]:
layer_0 = Input(shape=(6,))
layer_1a = Dense(2, activation='linear')(layer_0)
layer_1a = RepeatVector(3)(layer_1a)
layer_1b = Dense(2, activation='linear')(layer_0)
layer_1b = RepeatVector(3)(layer_1b)
layer_2 = Merge(mode='cos', dot_axes=(1,1))([layer_1a, layer_1b])
model = Model(inputs=layer_0, outputs=layer_2)

W_1a = np.array([0.1, 0.4, 0.5, 0.1, 1, -2, 0, 0.3, 0.2, 0.1, 3, 0]).reshape((6, 2))
b_1a = np.array([0.5, 0.7])
W_1b = np.array([1, 0, -0.9, 0.6, -0.7, 0, 0.2, 0.4, 0, 0, -1, 2.3]).reshape((6, 2))
b_1b = np.array([0.1, -0.2])
model.set_weights([W_1a, b_1a, W_1b, b_1b])

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)
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['legacy.Merge.11'] = {
    'input': {'data': data_in, 'shape': data_in_shape},
    'weights': [{'data': format_decimal(w.ravel().tolist()), 'shape': w.shape} for w in [W_1a, b_1a, W_1b, b_1b]],
    'expected': {'data': data_out, 'shape': data_out_shape}
}


in: [0, 0.2, 0.5, -0.1, 1, 2]
in shape: (6,)
out shape: (1, 2, 2)
out: [-1.0, 7.972742, 0.125427, -1.0]
/home/leon/miniconda3/lib/python3.5/site-packages/ipykernel/__main__.py:6: UserWarning: The `Merge` layer is deprecated and will be removed after 08/2017. Use instead layers from `keras.layers.merge`, e.g. `add`, `concatenate`, etc.

[legacy.Merge.12] mode: cos (2D x 2D, dotAxes=2)


In [16]:
layer_0 = Input(shape=(6,))
layer_1a = Dense(2, activation='linear')(layer_0)
layer_1a = RepeatVector(3)(layer_1a)
layer_1b = Dense(2, activation='linear')(layer_0)
layer_1b = RepeatVector(3)(layer_1b)
layer_2 = Merge(mode='cos', dot_axes=(2,2))([layer_1a, layer_1b])
model = Model(inputs=layer_0, outputs=layer_2)

W_1a = np.array([0.1, 0.4, 0.5, 0.1, 1, -2, 0, 0.3, 0.2, 0.1, 3, 0]).reshape((6, 2))
b_1a = np.array([0.5, 0.7])
W_1b = np.array([1, 0, -0.9, 0.6, -0.7, 0, 0.2, 0.4, 0, 0, -1, 2.3]).reshape((6, 2))
b_1b = np.array([0.1, -0.2])
model.set_weights([W_1a, b_1a, W_1b, b_1b])

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)
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['legacy.Merge.12'] = {
    'input': {'data': data_in, 'shape': data_in_shape},
    'weights': [{'data': format_decimal(w.ravel().tolist()), 'shape': w.shape} for w in [W_1a, b_1a, W_1b, b_1b]],
    'expected': {'data': data_out, 'shape': data_out_shape}
}


in: [0, 0.2, 0.5, -0.1, 1, 2]
in shape: (6,)
out shape: (1, 3, 3)
out: [-0.504843, -0.504843, -0.504843, -0.504843, -0.504843, -0.504843, -0.504843, -0.504843, -0.504843]
/home/leon/miniconda3/lib/python3.5/site-packages/ipykernel/__main__.py:6: UserWarning: The `Merge` layer is deprecated and will be removed after 08/2017. Use instead layers from `keras.layers.merge`, e.g. `add`, `concatenate`, etc.

[legacy.Merge.13] mode: cos (2D x 2D, dotAxes=(2,2))


In [17]:
layer_0 = Input(shape=(6,))
layer_1a = Dense(2, activation='linear')(layer_0)
layer_1a = RepeatVector(3)(layer_1a)
layer_1b = Dense(2, activation='linear')(layer_0)
layer_1b = RepeatVector(3)(layer_1b)
layer_2 = Merge(mode='cos', dot_axes=(2,2))([layer_1a, layer_1b])
model = Model(inputs=layer_0, outputs=layer_2)

W_1a = np.array([0.1, 0.4, 0.5, 0.1, 1, -2, 0, 0.3, 0.2, 0.1, 3, 0]).reshape((6, 2))
b_1a = np.array([0.5, 0.7])
W_1b = np.array([1, 0, -0.9, 0.6, -0.7, 0, 0.2, 0.4, 0, 0, -1, 2.3]).reshape((6, 2))
b_1b = np.array([0.1, -0.2])
model.set_weights([W_1a, b_1a, W_1b, b_1b])

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)
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['legacy.Merge.13'] = {
    'input': {'data': data_in, 'shape': data_in_shape},
    'weights': [{'data': format_decimal(w.ravel().tolist()), 'shape': w.shape} for w in [W_1a, b_1a, W_1b, b_1b]],
    'expected': {'data': data_out, 'shape': data_out_shape}
}


in: [0, 0.2, 0.5, -0.1, 1, 2]
in shape: (6,)
out shape: (1, 3, 3)
out: [-0.504843, -0.504843, -0.504843, -0.504843, -0.504843, -0.504843, -0.504843, -0.504843, -0.504843]
/home/leon/miniconda3/lib/python3.5/site-packages/ipykernel/__main__.py:6: UserWarning: The `Merge` layer is deprecated and will be removed after 08/2017. Use instead layers from `keras.layers.merge`, e.g. `add`, `concatenate`, etc.

export for Keras.js tests


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


{"legacy.Merge.0": {"weights": [{"data": [0.1, 0.4, 0.5, 0.1, 1.0, -2.0, 0.0, 0.3, 0.2, 0.1, 3.0, 0.0], "shape": [6, 2]}, {"data": [0.5, 0.7], "shape": [2]}, {"data": [1.0, 0.0, -0.9, 0.6, -0.7, 0.0, 0.2, 0.4, 0.0, 0.0, -1.0, 2.3], "shape": [6, 2]}, {"data": [0.1, -0.2], "shape": [2]}], "input": {"data": [0, 0.2, 0.5, -0.1, 1, 2], "shape": [6]}, "expected": {"data": [4.849999, 4.27], "shape": [2]}}, "legacy.Merge.1": {"weights": [{"data": [0.1, 0.4, 0.5, 0.1, 1.0, -2.0, 0.0, 0.3, 0.2, 0.1, 3.0, 0.0], "shape": [6, 2]}, {"data": [0.5, 0.7], "shape": [2]}, {"data": [1.0, 0.0, -0.9, 0.6, -0.7, 0.0, 0.2, 0.4, 0.0, 0.0, -1.0, 2.3], "shape": [6, 2]}, {"data": [0.1, -0.2], "shape": [2]}], "input": {"data": [0, 0.2, 0.5, -0.1, 1, 2], "shape": [6]}, "expected": {"data": [-17.885, -0.9408], "shape": [2]}}, "legacy.Merge.2": {"weights": [{"data": [0.1, 0.4, 0.5, 0.1, 1.0, -2.0, 0.0, 0.3, 0.2, 0.1, 3.0, 0.0], "shape": [6, 2]}, {"data": [0.5, 0.7], "shape": [2]}, {"data": [1.0, 0.0, -0.9, 0.6, -0.7, 0.0, 0.2, 0.4, 0.0, 0.0, -1.0, 2.3], "shape": [6, 2]}, {"data": [0.1, -0.2], "shape": [2]}], "input": {"data": [0, 0.2, 0.5, -0.1, 1, 2], "shape": [6]}, "expected": {"data": [2.425, 2.135], "shape": [2]}}, "legacy.Merge.3": {"weights": [{"data": [0.1, 0.4, 0.5, 0.1, 1.0, -2.0, 0.0, 0.3, 0.2, 0.1, 3.0, 0.0], "shape": [6, 2]}, {"data": [0.5, 0.7], "shape": [2]}, {"data": [1.0, 0.0, -0.9, 0.6, -0.7, 0.0, 0.2, 0.4, 0.0, 0.0, -1.0, 2.3], "shape": [6, 2]}, {"data": [0.1, -0.2], "shape": [2]}], "input": {"data": [0, 0.2, 0.5, -0.1, 1, 2], "shape": [6]}, "expected": {"data": [7.3, 4.48], "shape": [2]}}, "legacy.Merge.4": {"weights": [{"data": [0.1, 0.4, 0.5, 0.1, 1.0, -2.0, 0.0, 0.3, 0.2, 0.1, 3.0, 0.0], "shape": [6, 2]}, {"data": [0.5, 0.7], "shape": [2]}, {"data": [1.0, 0.0, -0.9, 0.6, -0.7, 0.0, 0.2, 0.4, 0.0, 0.0, -1.0, 2.3], "shape": [6, 2]}, {"data": [0.1, -0.2], "shape": [2]}], "input": {"data": [0, 0.2, 0.5, -0.1, 1, 2], "shape": [6]}, "expected": {"data": [7.3, -0.21, -2.45, 4.48], "shape": [4]}}, "legacy.Merge.5": {"weights": [{"data": [0.1, 0.4, 0.5, 0.1, 1.0, -2.0, 0.0, 0.3, 0.2, 0.1, 3.0, 0.0], "shape": [6, 2]}, {"data": [0.5, 0.7], "shape": [2]}, {"data": [1.0, 0.0, -0.9, 0.6, -0.7, 0.0, 0.2, 0.4, 0.0, 0.0, -1.0, 2.3], "shape": [6, 2]}, {"data": [0.1, -0.2], "shape": [2]}], "input": {"data": [0, 0.2, 0.5, -0.1, 1, 2], "shape": [6]}, "expected": {"data": [7.3, -0.21, -2.45, 4.48, 7.3, -0.21, -2.45, 4.48, 7.3, -0.21, -2.45, 4.48], "shape": [3, 4]}}, "legacy.Merge.6": {"weights": [{"data": [0.1, 0.4, 0.5, 0.1, 1.0, -2.0, 0.0, 0.3, 0.2, 0.1, 3.0, 0.0], "shape": [6, 2]}, {"data": [0.5, 0.7], "shape": [2]}, {"data": [1.0, 0.0, -0.9, 0.6, -0.7, 0.0, 0.2, 0.4, 0.0, 0.0, -1.0, 2.3], "shape": [6, 2]}, {"data": [0.1, -0.2], "shape": [2]}], "input": {"data": [0, 0.2, 0.5, -0.1, 1, 2], "shape": [6]}, "expected": {"data": [7.3, -0.21, 7.3, -0.21, 7.3, -0.21, -2.45, 4.48, -2.45, 4.48, -2.45, 4.48], "shape": [6, 2]}}, "legacy.Merge.7": {"weights": [{"data": [0.1, 0.4, 0.5, 0.1, 1.0, -2.0, 0.0, 0.3, 0.2, 0.1, 3.0, 0.0], "shape": [6, 2]}, {"data": [0.5, 0.7], "shape": [2]}, {"data": [1.0, 0.0, -0.9, 0.6, -0.7, 0.0, 0.2, 0.4, 0.0, 0.0, -1.0, 2.3], "shape": [6, 2]}, {"data": [0.1, -0.2], "shape": [2]}], "input": {"data": [0, 0.2, 0.5, -0.1, 1, 2], "shape": [6]}, "expected": {"data": [7.3, -0.21, 7.3, -0.21, 7.3, -0.21, -2.45, 4.48, -2.45, 4.48, -2.45, 4.48], "shape": [6, 2]}}, "legacy.Merge.8": {"weights": [{"data": [0.1, 0.4, 0.5, 0.1, 1.0, -2.0, 0.0, 0.3, 0.2, 0.1, 3.0, 0.0], "shape": [6, 2]}, {"data": [0.5, 0.7], "shape": [2]}, {"data": [1.0, 0.0, -0.9, 0.6, -0.7, 0.0, 0.2, 0.4, 0.0, 0.0, -1.0, 2.3], "shape": [6, 2]}, {"data": [0.1, -0.2], "shape": [2]}], "input": {"data": [0, 0.2, 0.5, -0.1, 1, 2], "shape": [6]}, "expected": {"data": [7.3, -0.21, -2.45, 4.48, 7.3, -0.21, -2.45, 4.48, 7.3, -0.21, -2.45, 4.48], "shape": [3, 4]}}, "legacy.Merge.9": {"weights": [{"data": [0.1, 0.4, 0.5, 0.1, 1.0, -2.0, 0.0, 0.3, 0.2, 0.1, 3.0, 0.0], "shape": [6, 2]}, {"data": [0.5, 0.7], "shape": [2]}, {"data": [1.0, 0.0, -0.9, 0.6, -0.7, 0.0, 0.2, 0.4, 0.0, 0.0, -1.0, 2.3], "shape": [6, 2]}, {"data": [0.1, -0.2], "shape": [2]}], "input": {"data": [0, 0.2, 0.5, -0.1, 1, 2], "shape": [6]}, "expected": {"data": [-53.654999, 98.112, 1.5435, -2.822401], "shape": [2, 2]}}, "legacy.Merge.10": {"weights": [{"data": [0.1, 0.4, 0.5, 0.1, 1.0, -2.0, 0.0, 0.3, 0.2, 0.1, 3.0, 0.0], "shape": [6, 2]}, {"data": [0.5, 0.7], "shape": [2]}, {"data": [1.0, 0.0, -0.9, 0.6, -0.7, 0.0, 0.2, 0.4, 0.0, 0.0, -1.0, 2.3], "shape": [6, 2]}, {"data": [0.1, -0.2], "shape": [2]}], "input": {"data": [0, 0.2, 0.5, -0.1, 1, 2], "shape": [6]}, "expected": {"data": [-18.8258, -18.8258, -18.8258, -18.8258, -18.8258, -18.8258, -18.8258, -18.8258, -18.8258], "shape": [3, 3]}}, "legacy.Merge.11": {"weights": [{"data": [0.1, 0.4, 0.5, 0.1, 1.0, -2.0, 0.0, 0.3, 0.2, 0.1, 3.0, 0.0], "shape": [6, 2]}, {"data": [0.5, 0.7], "shape": [2]}, {"data": [1.0, 0.0, -0.9, 0.6, -0.7, 0.0, 0.2, 0.4, 0.0, 0.0, -1.0, 2.3], "shape": [6, 2]}, {"data": [0.1, -0.2], "shape": [2]}], "input": {"data": [0, 0.2, 0.5, -0.1, 1, 2], "shape": [6]}, "expected": {"data": [-1.0, 7.972742, 0.125427, -1.0], "shape": [1, 2, 2]}}, "legacy.Merge.12": {"weights": [{"data": [0.1, 0.4, 0.5, 0.1, 1.0, -2.0, 0.0, 0.3, 0.2, 0.1, 3.0, 0.0], "shape": [6, 2]}, {"data": [0.5, 0.7], "shape": [2]}, {"data": [1.0, 0.0, -0.9, 0.6, -0.7, 0.0, 0.2, 0.4, 0.0, 0.0, -1.0, 2.3], "shape": [6, 2]}, {"data": [0.1, -0.2], "shape": [2]}], "input": {"data": [0, 0.2, 0.5, -0.1, 1, 2], "shape": [6]}, "expected": {"data": [-0.504843, -0.504843, -0.504843, -0.504843, -0.504843, -0.504843, -0.504843, -0.504843, -0.504843], "shape": [1, 3, 3]}}, "legacy.Merge.13": {"weights": [{"data": [0.1, 0.4, 0.5, 0.1, 1.0, -2.0, 0.0, 0.3, 0.2, 0.1, 3.0, 0.0], "shape": [6, 2]}, {"data": [0.5, 0.7], "shape": [2]}, {"data": [1.0, 0.0, -0.9, 0.6, -0.7, 0.0, 0.2, 0.4, 0.0, 0.0, -1.0, 2.3], "shape": [6, 2]}, {"data": [0.1, -0.2], "shape": [2]}], "input": {"data": [0, 0.2, 0.5, -0.1, 1, 2], "shape": [6]}, "expected": {"data": [-0.504843, -0.504843, -0.504843, -0.504843, -0.504843, -0.504843, -0.504843, -0.504843, -0.504843], "shape": [1, 3, 3]}}}

In [ ]: