In [1]:
import numpy as np
from keras.models import Model
from keras.layers import Input
from keras.layers.convolutional import Conv1D
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()

Conv1D

[convolutional.Conv1D.0] 4 length 3 filters on 5x2 input, strides=1, padding='valid', dilation_rate=1, activation='linear', use_bias=True


In [4]:
data_in_shape = (5, 2)
conv = Conv1D(4, 3, strides=1, padding='valid', dilation_rate=1, activation='linear', use_bias=True)

layer_0 = Input(shape=data_in_shape)
layer_1 = conv(layer_0)
model = Model(inputs=layer_0, outputs=layer_1)

# set weights to random (use seed for reproducibility)
weights = []
for w in model.get_weights():
    np.random.seed(200)
    weights.append(2 * np.random.random(w.shape) - 1)
model.set_weights(weights)
print('W shape:', weights[0].shape)
print('W:', format_decimal(weights[0].ravel().tolist()))
print('b shape:', weights[1].shape)
print('b:', format_decimal(weights[1].ravel().tolist()))

data_in = 2 * np.random.random(data_in_shape) - 1
result = model.predict(np.array([data_in]))
data_out_shape = result[0].shape
data_in_formatted = format_decimal(data_in.ravel().tolist())
data_out_formatted = format_decimal(result[0].ravel().tolist())
print('')
print('in shape:', data_in_shape)
print('in:', data_in_formatted)
print('out shape:', data_out_shape)
print('out:', data_out_formatted)

DATA['convolutional.Conv1D.0'] = {
    'input': {'data': data_in_formatted, 'shape': data_in_shape},
    'weights': [{'data': format_decimal(w.ravel().tolist()), 'shape': w.shape} for w in weights],
    'expected': {'data': data_out_formatted, 'shape': data_out_shape}
}


W shape: (3, 2, 4)
W: [0.895265, -0.546905, 0.18884, -0.143383, 0.528281, -0.994279, -0.285153, 0.81939, -0.087838, 0.963605, 0.734714, 0.972055, 0.846533, -0.392613, 0.692207, -0.757556, 0.571153, -0.49899, -0.807941, 0.886982, 0.6521, 0.03665, 0.747001, 0.156751]
b shape: (4,)
b: [0.895265, -0.546905, 0.18884, -0.143383]

in shape: (5, 2)
in: [0.528281, -0.994279, -0.285153, 0.81939, -0.087838, 0.963605, 0.734714, 0.972055, 0.846533, -0.392613]
out shape: (3, 4)
out: [2.139843, -0.364566, 1.720586, -1.858613, 2.949797, -1.99961, 0.63634, 0.557583, 2.311499, -1.567427, 0.132915, 1.325894]

[convolutional.Conv1D.1] 4 length 3 filters on 6x3 input, strides=1, padding='valid', dilation_rate=1, activation='linear', use_bias=False


In [5]:
data_in_shape = (6, 3)
conv = Conv1D(4, 3, strides=1, padding='valid', dilation_rate=1, activation='linear', use_bias=False)

layer_0 = Input(shape=data_in_shape)
layer_1 = conv(layer_0)
model = Model(inputs=layer_0, outputs=layer_1)

# set weights to random (use seed for reproducibility)
weights = []
for w in model.get_weights():
    np.random.seed(201)
    weights.append(2 * np.random.random(w.shape) - 1)
model.set_weights(weights)
print('W shape:', weights[0].shape)
print('W:', format_decimal(weights[0].ravel().tolist()))

data_in = 2 * np.random.random(data_in_shape) - 1
result = model.predict(np.array([data_in]))
data_out_shape = result[0].shape
data_in_formatted = format_decimal(data_in.ravel().tolist())
data_out_formatted = format_decimal(result[0].ravel().tolist())
print('')
print('in shape:', data_in_shape)
print('in:', data_in_formatted)
print('out shape:', data_out_shape)
print('out:', data_out_formatted)

DATA['convolutional.Conv1D.1'] = {
    'input': {'data': data_in_formatted, 'shape': data_in_shape},
    'weights': [{'data': format_decimal(w.ravel().tolist()), 'shape': w.shape} for w in weights],
    'expected': {'data': data_out_formatted, 'shape': data_out_shape}
}


W shape: (3, 3, 4)
W: [-0.772191, 0.495762, 0.222119, 0.619384, 0.425715, -0.719926, -0.464976, -0.704791, -0.543864, -0.528877, 0.380048, -0.703304, -0.108788, 0.401685, -0.806723, 0.765265, 0.739665, 0.689188, -0.452596, 0.571359, 0.402272, -0.010539, 0.672675, -0.191632, -0.653554, -0.269196, 0.994178, -0.318691, 0.010759, 0.078695, -0.501326, 0.625487, -0.614715, -0.839499, -0.811676, -0.300069]

in shape: (6, 3)
in: [0.57107, 0.361384, -0.924121, -0.417132, -0.39254, 0.967698, -0.674584, 0.924125, 0.403362, 0.417301, 0.795356, -0.367641, -0.398474, 0.889135, -0.81216, 0.383587, 0.922044, 0.427167]
out shape: (4, 4)
out: [0.562644, -0.0209, -0.688625, 0.691985, 0.509712, 0.184573, 1.169463, -0.253006, 1.859194, 0.365631, -1.552851, 0.418241, 0.087247, -0.099305, -1.471745, 0.641488]

[convolutional.Conv1D.2] 2 length 3 filters on 4x6 input, strides=2, padding='same', dilation_rate=1, activation='sigmoid', use_bias=True


In [6]:
data_in_shape = (4, 6)
conv = Conv1D(2, 3, strides=2, padding='same', dilation_rate=1, activation='sigmoid', use_bias=True)

layer_0 = Input(shape=data_in_shape)
layer_1 = conv(layer_0)
model = Model(inputs=layer_0, outputs=layer_1)

# set weights to random (use seed for reproducibility)
weights = []
for w in model.get_weights():
    np.random.seed(202)
    weights.append(2 * np.random.random(w.shape) - 1)
model.set_weights(weights)
print('W shape:', weights[0].shape)
print('W:', format_decimal(weights[0].ravel().tolist()))
print('b shape:', weights[1].shape)
print('b:', format_decimal(weights[1].ravel().tolist()))

data_in = 2 * np.random.random(data_in_shape) - 1
result = model.predict(np.array([data_in]))
data_out_shape = result[0].shape
data_in_formatted = format_decimal(data_in.ravel().tolist())
data_out_formatted = format_decimal(result[0].ravel().tolist())
print('')
print('in shape:', data_in_shape)
print('in:', data_in_formatted)
print('out shape:', data_out_shape)
print('out:', data_out_formatted)

DATA['convolutional.Conv1D.2'] = {
    'input': {'data': data_in_formatted, 'shape': data_in_shape},
    'weights': [{'data': format_decimal(w.ravel().tolist()), 'shape': w.shape} for w in weights],
    'expected': {'data': data_out_formatted, 'shape': data_out_shape}
}


W shape: (3, 6, 2)
W: [-0.551588, -0.471891, 0.954722, 0.985875, 0.989984, 0.142951, 0.199214, -0.77135, 0.620047, -0.536272, 0.982087, 0.105264, -0.122463, -0.826014, -0.123274, 0.745688, -0.177555, 0.48653, -0.563309, 0.742258, 0.105234, -0.612634, -0.136424, 0.508816, -0.070758, -0.433574, 0.764417, -0.312426, 0.834869, 0.612829, -0.226705, -0.66213, -0.646237, -0.9437, -0.924491, -0.352889]
b shape: (2,)
b: [-0.551588, -0.471891]

in shape: (4, 6)
in: [0.954722, 0.985875, 0.989984, 0.142951, 0.199214, -0.77135, 0.620047, -0.536272, 0.982087, 0.105264, -0.122463, -0.826014, -0.123274, 0.745688, -0.177555, 0.48653, -0.563309, 0.742258, 0.105234, -0.612634, -0.136424, 0.508816, -0.070758, -0.433574]
out shape: (2, 2)
out: [0.52296, 0.240592, 0.59414, 0.472844]

[convolutional.Conv1D.3] 2 length 7 filters on 8x3 input, strides=1, padding='same', dilation_rate=1, activation='tanh', use_bias=True


In [7]:
data_in_shape = (8, 3)
conv = Conv1D(2, 7, strides=1, padding='same', dilation_rate=1, activation='tanh', use_bias=True)

layer_0 = Input(shape=data_in_shape)
layer_1 = conv(layer_0)
model = Model(inputs=layer_0, outputs=layer_1)

# set weights to random (use seed for reproducibility)
weights = []
for w in model.get_weights():
    np.random.seed(203)
    weights.append(2 * np.random.random(w.shape) - 1)
model.set_weights(weights)
print('W shape:', weights[0].shape)
print('W:', format_decimal(weights[0].ravel().tolist()))
print('b shape:', weights[1].shape)
print('b:', format_decimal(weights[1].ravel().tolist()))

data_in = 2 * np.random.random(data_in_shape) - 1
result = model.predict(np.array([data_in]))
data_out_shape = result[0].shape
data_in_formatted = format_decimal(data_in.ravel().tolist())
data_out_formatted = format_decimal(result[0].ravel().tolist())
print('')
print('in shape:', data_in_shape)
print('in:', data_in_formatted)
print('out shape:', data_out_shape)
print('out:', data_out_formatted)

DATA['convolutional.Conv1D.3'] = {
    'input': {'data': data_in_formatted, 'shape': data_in_shape},
    'weights': [{'data': format_decimal(w.ravel().tolist()), 'shape': w.shape} for w in weights],
    'expected': {'data': data_out_formatted, 'shape': data_out_shape}
}


W shape: (7, 3, 2)
W: [0.6958, -0.622123, -0.098786, 0.250694, 0.939495, -0.584387, 0.188897, 0.822914, -0.508628, 0.548608, 0.272553, 0.576978, 0.760404, -0.268837, 0.186754, 0.011788, 0.250571, 0.311674, -0.975276, 0.044954, 0.390867, 0.832295, -0.068466, -0.487965, -0.706554, 0.938197, 0.769853, -0.84513, 0.106441, 0.882159, -0.35372, 0.642428, -0.58288, -0.115854, -0.42362, -0.852597, 0.792338, -0.100119, -0.871574, -0.320701, 0.797559, 0.78815]
b shape: (2,)
b: [0.6958, -0.622123]

in shape: (8, 3)
in: [-0.098786, 0.250694, 0.939495, -0.584387, 0.188897, 0.822914, -0.508628, 0.548608, 0.272553, 0.576978, 0.760404, -0.268837, 0.186754, 0.011788, 0.250571, 0.311674, -0.975276, 0.044954, 0.390867, 0.832295, -0.068466, -0.487965, -0.706554, 0.938197]
out shape: (8, 2)
out: [0.662022, -0.963639, 0.970752, -0.525575, 0.983593, 0.519924, 0.722247, 0.354002, 0.444907, 0.884876, 0.25032, -0.947914, 0.654293, 0.86192, 0.975655, -0.980825]

[convolutional.Conv1D.4] 2 length 7 filters on 8x3 input, strides=1, padding='same', dilation_rate=2, activation='tanh', use_bias=True


In [8]:
data_in_shape = (8, 3)
conv = Conv1D(2, 7, strides=1, padding='same', dilation_rate=2, activation='tanh', use_bias=True)

layer_0 = Input(shape=data_in_shape)
layer_1 = conv(layer_0)
model = Model(inputs=layer_0, outputs=layer_1)

# set weights to random (use seed for reproducibility)
weights = []
for w in model.get_weights():
    np.random.seed(204)
    weights.append(2 * np.random.random(w.shape) - 1)
model.set_weights(weights)
print('W shape:', weights[0].shape)
print('W:', format_decimal(weights[0].ravel().tolist()))
print('b shape:', weights[1].shape)
print('b:', format_decimal(weights[1].ravel().tolist()))

data_in = 2 * np.random.random(data_in_shape) - 1
result = model.predict(np.array([data_in]))
data_out_shape = result[0].shape
data_in_formatted = format_decimal(data_in.ravel().tolist())
data_out_formatted = format_decimal(result[0].ravel().tolist())
print('')
print('in shape:', data_in_shape)
print('in:', data_in_formatted)
print('out shape:', data_out_shape)
print('out:', data_out_formatted)

DATA['convolutional.Conv1D.4'] = {
    'input': {'data': data_in_formatted, 'shape': data_in_shape},
    'weights': [{'data': format_decimal(w.ravel().tolist()), 'shape': w.shape} for w in weights],
    'expected': {'data': data_out_formatted, 'shape': data_out_shape}
}


W shape: (7, 3, 2)
W: [0.861113, -0.237594, 0.330694, 0.998309, 0.786447, 0.538158, -0.228315, 0.217332, -0.475436, -0.018066, -0.489741, -0.522387, 0.79989, 0.27058, -0.683115, -0.650208, 0.259853, -0.509243, 0.958185, 0.089546, 0.739799, 0.114385, -0.378872, -0.168716, 0.302124, 0.850416, -0.984343, 0.839927, -0.895196, 0.303711, 0.128826, 0.058159, 0.254989, -0.759101, 0.793844, 0.647309, 0.252074, 0.075576, -0.859305, 0.952613, -0.053285, -0.677361]
b shape: (2,)
b: [0.861113, -0.237594]

in shape: (8, 3)
in: [0.330694, 0.998309, 0.786447, 0.538158, -0.228315, 0.217332, -0.475436, -0.018066, -0.489741, -0.522387, 0.79989, 0.27058, -0.683115, -0.650208, 0.259853, -0.509243, 0.958185, 0.089546, 0.739799, 0.114385, -0.378872, -0.168716, 0.302124, 0.850416]
out shape: (8, 2)
out: [0.963457, 0.243838, -0.158206, -0.736323, 0.370836, -0.986532, 0.780216, 0.575109, -0.874838, -0.03132, -0.791811, -0.545406, 0.99756, 0.906116, -0.26601, -0.932366]

export for Keras.js tests


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


{"convolutional.Conv1D.0": {"weights": [{"shape": [3, 2, 4], "data": [0.895265, -0.546905, 0.18884, -0.143383, 0.528281, -0.994279, -0.285153, 0.81939, -0.087838, 0.963605, 0.734714, 0.972055, 0.846533, -0.392613, 0.692207, -0.757556, 0.571153, -0.49899, -0.807941, 0.886982, 0.6521, 0.03665, 0.747001, 0.156751]}, {"shape": [4], "data": [0.895265, -0.546905, 0.18884, -0.143383]}], "expected": {"shape": [3, 4], "data": [2.139843, -0.364566, 1.720586, -1.858613, 2.949797, -1.99961, 0.63634, 0.557583, 2.311499, -1.567427, 0.132915, 1.325894]}, "input": {"shape": [5, 2], "data": [0.528281, -0.994279, -0.285153, 0.81939, -0.087838, 0.963605, 0.734714, 0.972055, 0.846533, -0.392613]}}, "convolutional.Conv1D.1": {"weights": [{"shape": [3, 3, 4], "data": [-0.772191, 0.495762, 0.222119, 0.619384, 0.425715, -0.719926, -0.464976, -0.704791, -0.543864, -0.528877, 0.380048, -0.703304, -0.108788, 0.401685, -0.806723, 0.765265, 0.739665, 0.689188, -0.452596, 0.571359, 0.402272, -0.010539, 0.672675, -0.191632, -0.653554, -0.269196, 0.994178, -0.318691, 0.010759, 0.078695, -0.501326, 0.625487, -0.614715, -0.839499, -0.811676, -0.300069]}], "expected": {"shape": [4, 4], "data": [0.562644, -0.0209, -0.688625, 0.691985, 0.509712, 0.184573, 1.169463, -0.253006, 1.859194, 0.365631, -1.552851, 0.418241, 0.087247, -0.099305, -1.471745, 0.641488]}, "input": {"shape": [6, 3], "data": [0.57107, 0.361384, -0.924121, -0.417132, -0.39254, 0.967698, -0.674584, 0.924125, 0.403362, 0.417301, 0.795356, -0.367641, -0.398474, 0.889135, -0.81216, 0.383587, 0.922044, 0.427167]}}, "convolutional.Conv1D.2": {"weights": [{"shape": [3, 6, 2], "data": [-0.551588, -0.471891, 0.954722, 0.985875, 0.989984, 0.142951, 0.199214, -0.77135, 0.620047, -0.536272, 0.982087, 0.105264, -0.122463, -0.826014, -0.123274, 0.745688, -0.177555, 0.48653, -0.563309, 0.742258, 0.105234, -0.612634, -0.136424, 0.508816, -0.070758, -0.433574, 0.764417, -0.312426, 0.834869, 0.612829, -0.226705, -0.66213, -0.646237, -0.9437, -0.924491, -0.352889]}, {"shape": [2], "data": [-0.551588, -0.471891]}], "expected": {"shape": [2, 2], "data": [0.52296, 0.240592, 0.59414, 0.472844]}, "input": {"shape": [4, 6], "data": [0.954722, 0.985875, 0.989984, 0.142951, 0.199214, -0.77135, 0.620047, -0.536272, 0.982087, 0.105264, -0.122463, -0.826014, -0.123274, 0.745688, -0.177555, 0.48653, -0.563309, 0.742258, 0.105234, -0.612634, -0.136424, 0.508816, -0.070758, -0.433574]}}, "convolutional.Conv1D.3": {"weights": [{"shape": [7, 3, 2], "data": [0.6958, -0.622123, -0.098786, 0.250694, 0.939495, -0.584387, 0.188897, 0.822914, -0.508628, 0.548608, 0.272553, 0.576978, 0.760404, -0.268837, 0.186754, 0.011788, 0.250571, 0.311674, -0.975276, 0.044954, 0.390867, 0.832295, -0.068466, -0.487965, -0.706554, 0.938197, 0.769853, -0.84513, 0.106441, 0.882159, -0.35372, 0.642428, -0.58288, -0.115854, -0.42362, -0.852597, 0.792338, -0.100119, -0.871574, -0.320701, 0.797559, 0.78815]}, {"shape": [2], "data": [0.6958, -0.622123]}], "expected": {"shape": [8, 2], "data": [0.662022, -0.963639, 0.970752, -0.525575, 0.983593, 0.519924, 0.722247, 0.354002, 0.444907, 0.884876, 0.25032, -0.947914, 0.654293, 0.86192, 0.975655, -0.980825]}, "input": {"shape": [8, 3], "data": [-0.098786, 0.250694, 0.939495, -0.584387, 0.188897, 0.822914, -0.508628, 0.548608, 0.272553, 0.576978, 0.760404, -0.268837, 0.186754, 0.011788, 0.250571, 0.311674, -0.975276, 0.044954, 0.390867, 0.832295, -0.068466, -0.487965, -0.706554, 0.938197]}}, "convolutional.Conv1D.4": {"weights": [{"shape": [7, 3, 2], "data": [0.861113, -0.237594, 0.330694, 0.998309, 0.786447, 0.538158, -0.228315, 0.217332, -0.475436, -0.018066, -0.489741, -0.522387, 0.79989, 0.27058, -0.683115, -0.650208, 0.259853, -0.509243, 0.958185, 0.089546, 0.739799, 0.114385, -0.378872, -0.168716, 0.302124, 0.850416, -0.984343, 0.839927, -0.895196, 0.303711, 0.128826, 0.058159, 0.254989, -0.759101, 0.793844, 0.647309, 0.252074, 0.075576, -0.859305, 0.952613, -0.053285, -0.677361]}, {"shape": [2], "data": [0.861113, -0.237594]}], "expected": {"shape": [8, 2], "data": [0.963457, 0.243838, -0.158206, -0.736323, 0.370836, -0.986532, 0.780216, 0.575109, -0.874838, -0.03132, -0.791811, -0.545406, 0.99756, 0.906116, -0.26601, -0.932366]}, "input": {"shape": [8, 3], "data": [0.330694, 0.998309, 0.786447, 0.538158, -0.228315, 0.217332, -0.475436, -0.018066, -0.489741, -0.522387, 0.79989, 0.27058, -0.683115, -0.650208, 0.259853, -0.509243, 0.958185, 0.089546, 0.739799, 0.114385, -0.378872, -0.168716, 0.302124, 0.850416]}}}

In [ ]: