In [1]:
import numpy as np
from keras.models import Model
from keras.layers import Input
from keras.layers.recurrent import SimpleRNN, LSTM, GRU
from keras.layers.wrappers import Bidirectional
from keras import backend as K
import json
from collections import OrderedDict
In [2]:
def format_decimal(arr, places=6):
return [round(x * 10**places) / 10**places for x in arr]
In [3]:
DATA = OrderedDict()
[wrappers.Bidirectional.0] merge_mode='sum', wrap a SimpleRNN layer with units=4, activation='tanh', return_sequences=False
In [4]:
random_seed = 1000
data_in_shape = (3, 6)
layer_0 = Input(shape=data_in_shape)
layer_1 = Bidirectional(SimpleRNN(4, activation='tanh', return_sequences=False), merge_mode='sum')(layer_0)
model = Model(inputs=layer_0, outputs=layer_1)
np.random.seed(random_seed)
data_in = 2 * np.random.random(data_in_shape) - 1
# set weights to random (use seed for reproducibility)
weights = []
for i, w in enumerate(model.get_weights()):
np.random.seed(random_seed + i)
weights.append(2 * np.random.random(w.shape) - 1)
model.set_weights(weights)
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())
DATA['wrappers.Bidirectional.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}
}
[wrappers.Bidirectional.1] merge_mode='mul', wrap a SimpleRNN layer with units=4, activation='tanh', return_sequences=False
In [5]:
random_seed = 1001
data_in_shape = (3, 6)
layer_0 = Input(shape=data_in_shape)
layer_1 = Bidirectional(SimpleRNN(4, activation='tanh', return_sequences=False), merge_mode='mul')(layer_0)
model = Model(inputs=layer_0, outputs=layer_1)
np.random.seed(random_seed)
data_in = 2 * np.random.random(data_in_shape) - 1
# set weights to random (use seed for reproducibility)
weights = []
for i, w in enumerate(model.get_weights()):
np.random.seed(random_seed + i)
weights.append(2 * np.random.random(w.shape) - 1)
model.set_weights(weights)
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())
DATA['wrappers.Bidirectional.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}
}
[wrappers.Bidirectional.2] merge_mode='concat', wrap a SimpleRNN layer with units=4, activation='tanh', return_sequences=False
In [6]:
random_seed = 1002
data_in_shape = (3, 6)
layer_0 = Input(shape=data_in_shape)
layer_1 = Bidirectional(SimpleRNN(4, activation='tanh', return_sequences=False), merge_mode='concat')(layer_0)
model = Model(inputs=layer_0, outputs=layer_1)
np.random.seed(random_seed)
data_in = 2 * np.random.random(data_in_shape) - 1
# set weights to random (use seed for reproducibility)
weights = []
for i, w in enumerate(model.get_weights()):
np.random.seed(random_seed + i)
weights.append(2 * np.random.random(w.shape) - 1)
model.set_weights(weights)
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())
DATA['wrappers.Bidirectional.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}
}
[wrappers.Bidirectional.3] merge_mode='ave', wrap a SimpleRNN layer with units=4, activation='tanh', return_sequences=False
In [7]:
random_seed = 1003
data_in_shape = (3, 6)
layer_0 = Input(shape=data_in_shape)
layer_1 = Bidirectional(SimpleRNN(4, activation='tanh', return_sequences=False), merge_mode='ave')(layer_0)
model = Model(inputs=layer_0, outputs=layer_1)
np.random.seed(random_seed)
data_in = 2 * np.random.random(data_in_shape) - 1
# set weights to random (use seed for reproducibility)
weights = []
for i, w in enumerate(model.get_weights()):
np.random.seed(random_seed + i)
weights.append(2 * np.random.random(w.shape) - 1)
model.set_weights(weights)
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())
DATA['wrappers.Bidirectional.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}
}
[wrappers.Bidirectional.4] merge_mode='concat', wrap a SimpleRNN layer with units=4, activation='tanh', return_sequences=True
In [8]:
random_seed = 1004
data_in_shape = (3, 6)
layer_0 = Input(shape=data_in_shape)
layer_1 = Bidirectional(SimpleRNN(4, activation='tanh', return_sequences=True), merge_mode='concat')(layer_0)
model = Model(inputs=layer_0, outputs=layer_1)
np.random.seed(random_seed)
data_in = 2 * np.random.random(data_in_shape) - 1
# set weights to random (use seed for reproducibility)
weights = []
for i, w in enumerate(model.get_weights()):
np.random.seed(random_seed + i)
weights.append(2 * np.random.random(w.shape) - 1)
model.set_weights(weights)
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())
DATA['wrappers.Bidirectional.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}
}
[wrappers.Bidirectional.5] merge_mode='concat', wrap a GRU layer with units=4, activation='tanh', recurrent_activation='hard_sigmoid', return_sequences=True
In [9]:
random_seed = 1005
data_in_shape = (3, 6)
layer_0 = Input(shape=data_in_shape)
layer_1 = Bidirectional(GRU(4, activation='tanh', recurrent_activation='hard_sigmoid', return_sequences=True), merge_mode='concat')(layer_0)
model = Model(inputs=layer_0, outputs=layer_1)
np.random.seed(random_seed)
data_in = 2 * np.random.random(data_in_shape) - 1
# set weights to random (use seed for reproducibility)
weights = []
for i, w in enumerate(model.get_weights()):
np.random.seed(random_seed + i)
weights.append(2 * np.random.random(w.shape) - 1)
model.set_weights(weights)
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())
DATA['wrappers.Bidirectional.5'] = {
'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}
}
[wrappers.Bidirectional.6] merge_mode='concat', wrap a LSTM layer with units=4, activation='tanh', recurrent_activation='hard_sigmoid', return_sequences=True
In [10]:
random_seed = 1006
data_in_shape = (3, 6)
layer_0 = Input(shape=data_in_shape)
layer_1 = Bidirectional(LSTM(4, activation='tanh', recurrent_activation='hard_sigmoid', return_sequences=True), merge_mode='concat')(layer_0)
model = Model(inputs=layer_0, outputs=layer_1)
np.random.seed(random_seed)
data_in = 2 * np.random.random(data_in_shape) - 1
# set weights to random (use seed for reproducibility)
weights = []
for i, w in enumerate(model.get_weights()):
np.random.seed(random_seed + i)
weights.append(2 * np.random.random(w.shape) - 1)
model.set_weights(weights)
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())
DATA['wrappers.Bidirectional.6'] = {
'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}
}
[wrappers.Bidirectional.7] merge_mode='sum', wrap a SimpleRNN layer with units=4, activation='tanh', return_sequences=True
In [11]:
random_seed = 1007
data_in_shape = (3, 6)
layer_0 = Input(shape=data_in_shape)
layer_1 = Bidirectional(SimpleRNN(4, activation='tanh', return_sequences=True), merge_mode='sum')(layer_0)
model = Model(inputs=layer_0, outputs=layer_1)
np.random.seed(random_seed)
data_in = 2 * np.random.random(data_in_shape) - 1
# set weights to random (use seed for reproducibility)
weights = []
for i, w in enumerate(model.get_weights()):
np.random.seed(random_seed + i)
weights.append(2 * np.random.random(w.shape) - 1)
model.set_weights(weights)
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())
DATA['wrappers.Bidirectional.7'] = {
'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}
}
In [12]:
import os
filename = '../../../test/data/layers/wrappers/Bidirectional.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 [13]:
print(json.dumps(DATA))
In [ ]: