In [1]:
import numpy as np
from keras.models import Model
from keras.layers import Input, Dense, RepeatVector
from keras.layers.merge import Add, Subtract, Multiply, Average, Maximum, Minimum, Concatenate, Dot
from keras import backend as K
import json
from collections import OrderedDict


Using TensorFlow backend.
/home/leon/miniconda3/lib/python3.6/importlib/_bootstrap.py:219: RuntimeWarning: compiletime version 3.5 of module 'tensorflow.python.framework.fast_tensor_util' does not match runtime version 3.6
  return f(*args, **kwds)

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

In [3]:
DATA = OrderedDict()

[merge.Concatenate.0] 1D inputs, axis=-1


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 = Concatenate(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['merge.Concatenate.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}
}


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]

[merge.Concatenate.1] 2D inputs, axis=-1


In [5]:
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 = Concatenate(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['merge.Concatenate.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: (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]

[merge.Concatenate.2] 2D inputs, axis=-2


In [6]:
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 = Concatenate(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['merge.Concatenate.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: (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]

[merge.Concatenate.3] 2D inputs, axis=1


In [7]:
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 = Concatenate(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['merge.Concatenate.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: (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]

[merge.Concatenate.4] 2D inputs, axis=2


In [8]:
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 = Concatenate(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['merge.Concatenate.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: (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]

[merge.Concatenate.5] 3x 2D inputs, axis=-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_1c = Dense(2, activation='linear')(layer_0)
layer_1c = RepeatVector(3)(layer_1c)
layer_2 = Concatenate(axis=-1)([layer_1a, layer_1b, layer_1c])
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])
W_1c = np.array([1, 0, -0.9, 0.6, -0.7, 0, 0.2, 0.4, 0, 0, -1, 2.3]).reshape((6, 2))
b_1c = np.array([0.1, -0.2])
model.set_weights([W_1a, b_1a, W_1b, b_1b, W_1c, b_1c])

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['merge.Concatenate.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, W_1c, b_1c]],
    'expected': {'data': data_out, 'shape': data_out_shape}
}


in: [0, 0.2, 0.5, -0.1, 1, 2]
in shape: (6,)
out shape: (3, 6)
out: [7.3, -0.21, -2.45, 4.48, -2.45, 4.48, 7.3, -0.21, -2.45, 4.48, -2.45, 4.48, 7.3, -0.21, -2.45, 4.48, -2.45, 4.48]

[merge.Concatenate.6] 4x 2D inputs, axis=-1


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_1c = Dense(2, activation='linear')(layer_0)
layer_1c = RepeatVector(3)(layer_1c)
layer_1d = Dense(2, activation='linear')(layer_0)
layer_1d = RepeatVector(3)(layer_1d)
layer_2 = Concatenate(axis=-1)([layer_1a, layer_1b, layer_1c, layer_1d])
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])
W_1c = np.array([1, 0, -0.9, 0.6, -0.7, 0, 0.2, 0.4, 0, 0, -1, 2.3]).reshape((6, 2))
b_1c = np.array([0.1, -0.2])
W_1d = np.array([1, 0, -0.9, 0.6, -0.7, 0, 0.2, 0.4, 0, 0, -1, 2.3]).reshape((6, 2))
b_1d = np.array([0.1, -0.2])
model.set_weights([W_1a, b_1a, W_1b, b_1b, W_1c, b_1c, W_1d, b_1d])

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['merge.Concatenate.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, W_1c, b_1c, W_1d, b_1d]],
    'expected': {'data': data_out, 'shape': data_out_shape}
}


in: [0, 0.2, 0.5, -0.1, 1, 2]
in shape: (6,)
out shape: (3, 8)
out: [7.3, -0.21, -2.45, 4.48, -2.45, 4.48, -2.45, 4.48, 7.3, -0.21, -2.45, 4.48, -2.45, 4.48, -2.45, 4.48, 7.3, -0.21, -2.45, 4.48, -2.45, 4.48, -2.45, 4.48]

export for Keras.js tests


In [11]:
import os

filename = '../../../test/data/layers/merge/Concatenate.json'
if not os.path.exists(os.path.dirname(filename)):
    os.makedirs(os.path.dirname(filename))
with open(filename, 'w') as f:
    json.dump(DATA, f)

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


{"merge.Concatenate.0": {"input": {"data": [0, 0.2, 0.5, -0.1, 1, 2], "shape": [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]}], "expected": {"data": [7.3, -0.21, -2.45, 4.48], "shape": [4]}}, "merge.Concatenate.1": {"input": {"data": [0, 0.2, 0.5, -0.1, 1, 2], "shape": [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]}], "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]}}, "merge.Concatenate.2": {"input": {"data": [0, 0.2, 0.5, -0.1, 1, 2], "shape": [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]}], "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]}}, "merge.Concatenate.3": {"input": {"data": [0, 0.2, 0.5, -0.1, 1, 2], "shape": [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]}], "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]}}, "merge.Concatenate.4": {"input": {"data": [0, 0.2, 0.5, -0.1, 1, 2], "shape": [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]}], "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]}}, "merge.Concatenate.5": {"input": {"data": [0, 0.2, 0.5, -0.1, 1, 2], "shape": [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]}, {"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]}], "expected": {"data": [7.3, -0.21, -2.45, 4.48, -2.45, 4.48, 7.3, -0.21, -2.45, 4.48, -2.45, 4.48, 7.3, -0.21, -2.45, 4.48, -2.45, 4.48], "shape": [3, 6]}}, "merge.Concatenate.6": {"input": {"data": [0, 0.2, 0.5, -0.1, 1, 2], "shape": [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]}, {"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]}, {"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]}], "expected": {"data": [7.3, -0.21, -2.45, 4.48, -2.45, 4.48, -2.45, 4.48, 7.3, -0.21, -2.45, 4.48, -2.45, 4.48, -2.45, 4.48, 7.3, -0.21, -2.45, 4.48, -2.45, 4.48, -2.45, 4.48], "shape": [3, 8]}}}