data_in_shape = (3, 6)
rnn = GRU(4, activation='tanh', recurrent_activation='hard_sigmoid')

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

# set weights to random (use seed for reproducibility)
weights = []
for i, w in enumerate(model.get_weights()):
    np.random.seed(3200 + i)
    weights.append(2 * np.random.random(w.shape) - 1)
model.set_weights(weights)
weight_names = ['W', 'U', 'b']
for w_i, w_name in enumerate(weight_names):
    print('{} shape:'.format(w_name), weights[w_i].shape)
    print('{}:'.format(w_name), format_decimal(weights[w_i].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['recurrent.GRU.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 for w in model.get_weights()]

"""
Explanation: GRU
[recurrent.GRU.0] units=4, activation='tanh', recurrent_activation='hard_sigmoid'
Note dropout_W and dropout_U are only applied during training phase
End of explanation
"""


data_in_shape = (8, 5)
rnn = GRU(5, activation='sigmoid', recurrent_activation='sigmoid')

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

# set weights to random (use seed for reproducibility)
weights = []
for i, w in enumerate(model.get_weights()):
    np.random.seed(3300 + i)
    weights.append(2 * np.random.random(w.shape) - 1)
model.set_weights(weights)
weight_names = ['W', 'U', 'b']
for w_i, w_name in enumerate(weight_names):
    print('{} shape:'.format(w_name), weights[w_i].shape)
    print('{}:'.format(w_name), format_decimal(weights[w_i].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['recurrent.GRU.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}
}

"""
Explanation: [recurrent.GRU.1] units=5, activation='sigmoid', recurrent_activation='sigmoid'
Note dropout_W and dropout_U are only applied during training phase
End of explanation
"""


data_in_shape = (3, 6)
rnn = GRU(4, activation='tanh', recurrent_activation='hard_sigmoid',
          return_sequences=True)

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

# set weights to random (use seed for reproducibility)
weights = []
for i, w in enumerate(model.get_weights()):
    np.random.seed(3400 + i)
    weights.append(2 * np.random.random(w.shape) - 1)
model.set_weights(weights)
weight_names = ['W', 'U', 'b']
for w_i, w_name in enumerate(weight_names):
    print('{} shape:'.format(w_name), weights[w_i].shape)
    print('{}:'.format(w_name), format_decimal(weights[w_i].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['recurrent.GRU.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}
}

"""
Explanation: [recurrent.GRU.2] units=4, activation='tanh', recurrent_activation='hard_sigmoid', return_sequences=True
Note dropout_W and dropout_U are only applied during training phase
End of explanation
"""


data_in_shape = (3, 6)
rnn = GRU(4, activation='tanh', recurrent_activation='hard_sigmoid',
          return_sequences=False, go_backwards=True)

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

# set weights to random (use seed for reproducibility)
weights = []
for i, w in enumerate(model.get_weights()):
    np.random.seed(3410 + i)
    weights.append(2 * np.random.random(w.shape) - 1)
model.set_weights(weights)
weight_names = ['W', 'U', 'b']
for w_i, w_name in enumerate(weight_names):
    print('{} shape:'.format(w_name), weights[w_i].shape)
    print('{}:'.format(w_name), format_decimal(weights[w_i].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['recurrent.GRU.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}
}

"""
Explanation: [recurrent.GRU.3] units=4, activation='tanh', recurrent_activation='hard_sigmoid', return_sequences=False, go_backwards=True
Note dropout_W and dropout_U are only applied during training phase
End of explanation
"""


data_in_shape = (3, 6)
rnn = GRU(4, activation='tanh', recurrent_activation='hard_sigmoid',
          return_sequences=True, go_backwards=True)

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

# set weights to random (use seed for reproducibility)
weights = []
for i, w in enumerate(model.get_weights()):
    np.random.seed(3420 + i)
    weights.append(2 * np.random.random(w.shape) - 1)
model.set_weights(weights)
weight_names = ['W', 'U', 'b']
for w_i, w_name in enumerate(weight_names):
    print('{} shape:'.format(w_name), weights[w_i].shape)
    print('{}:'.format(w_name), format_decimal(weights[w_i].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['recurrent.GRU.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}
}

"""
Explanation: [recurrent.GRU.4] units=4, activation='tanh', recurrent_activation='hard_sigmoid', return_sequences=True, go_backwards=True
Note dropout_W and dropout_U are only applied during training phase
End of explanation
"""


data_in_shape = (3, 6)
rnn = GRU(4, activation='tanh', recurrent_activation='hard_sigmoid',
          return_sequences=False, go_backwards=False, stateful=True)

layer_0 = Input(batch_shape=(1, *data_in_shape))
layer_1 = rnn(layer_0)
model = Model(inputs=layer_0, outputs=layer_1)

# set weights to random (use seed for reproducibility)
weights = []
for i, w in enumerate(model.get_weights()):
    np.random.seed(3430 + i)
    weights.append(2 * np.random.random(w.shape) - 1)
model.set_weights(weights)
weight_names = ['W', 'U', 'b']
for w_i, w_name in enumerate(weight_names):
    print('{} shape:'.format(w_name), weights[w_i].shape)
    print('{}:'.format(w_name), format_decimal(weights[w_i].ravel().tolist()))

data_in = 2 * np.random.random(data_in_shape) - 1
result = model.predict(np.array([data_in]))
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['recurrent.GRU.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}
}

"""
Explanation: [recurrent.GRU.5] units=4, activation='tanh', recurrent_activation='hard_sigmoid', return_sequences=False, go_backwards=False, stateful=True
Note dropout_W and dropout_U are only applied during training phase
To test statefulness, model.predict is run twice
End of explanation
"""


data_in_shape = (3, 6)
rnn = GRU(4, activation='tanh', recurrent_activation='hard_sigmoid',
          return_sequences=True, go_backwards=False, stateful=True)

layer_0 = Input(batch_shape=(1, *data_in_shape))
layer_1 = rnn(layer_0)
model = Model(inputs=layer_0, outputs=layer_1)

# set weights to random (use seed for reproducibility)
weights = []
for i, w in enumerate(model.get_weights()):
    np.random.seed(3440 + i)
    weights.append(2 * np.random.random(w.shape) - 1)
model.set_weights(weights)
weight_names = ['W', 'U', 'b']
for w_i, w_name in enumerate(weight_names):
    print('{} shape:'.format(w_name), weights[w_i].shape)
    print('{}:'.format(w_name), format_decimal(weights[w_i].ravel().tolist()))

data_in = 2 * np.random.random(data_in_shape) - 1
result = model.predict(np.array([data_in]))
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['recurrent.GRU.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}
}

"""
Explanation: [recurrent.GRU.6] units=4, activation='tanh', recurrent_activation='hard_sigmoid', return_sequences=True, go_backwards=False, stateful=True
Note dropout_W and dropout_U are only applied during training phase
To test statefulness, model.predict is run twice
End of explanation
"""


data_in_shape = (3, 6)
rnn = GRU(4, activation='tanh', recurrent_activation='hard_sigmoid',
          return_sequences=False, go_backwards=True, stateful=True)

layer_0 = Input(batch_shape=(1, *data_in_shape))
layer_1 = rnn(layer_0)
model = Model(inputs=layer_0, outputs=layer_1)

# set weights to random (use seed for reproducibility)
weights = []
for i, w in enumerate(model.get_weights()):
    np.random.seed(3450 + i)
    weights.append(2 * np.random.random(w.shape) - 1)
model.set_weights(weights)
weight_names = ['W', 'U', 'b']
for w_i, w_name in enumerate(weight_names):
    print('{} shape:'.format(w_name), weights[w_i].shape)
    print('{}:'.format(w_name), format_decimal(weights[w_i].ravel().tolist()))

data_in = 2 * np.random.random(data_in_shape) - 1
result = model.predict(np.array([data_in]))
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['recurrent.GRU.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}
}

"""
Explanation: [recurrent.GRU.7] units=4, activation='tanh', recurrent_activation='hard_sigmoid', return_sequences=False, go_backwards=True, stateful=True
Note dropout_W and dropout_U are only applied during training phase
To test statefulness, model.predict is run twice
End of explanation
"""


data_in_shape = (3, 6)
rnn = GRU(4, activation='tanh', recurrent_activation='hard_sigmoid', use_bias=False,
          return_sequences=True, go_backwards=True, stateful=True)

layer_0 = Input(batch_shape=(1, *data_in_shape))
layer_1 = rnn(layer_0)
model = Model(inputs=layer_0, outputs=layer_1)

# set weights to random (use seed for reproducibility)
weights = []
for i, w in enumerate(model.get_weights()):
    np.random.seed(3460 + i)
    weights.append(2 * np.random.random(w.shape) - 1)
model.set_weights(weights)
weight_names = ['W', 'U']
for w_i, w_name in enumerate(weight_names):
    print('{} shape:'.format(w_name), weights[w_i].shape)
    print('{}:'.format(w_name), format_decimal(weights[w_i].ravel().tolist()))

data_in = 2 * np.random.random(data_in_shape) - 1
result = model.predict(np.array([data_in]))
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['recurrent.GRU.8'] = {
    '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}
}

"""
Explanation: [recurrent.GRU.8] units=4, activation='tanh', recurrent_activation='hard_sigmoid', use_bias=False, return_sequences=True, go_backwards=True, stateful=True
Note dropout_W and dropout_U are only applied during training phase
To test statefulness, model.predict is run twice
End of explanation
"""


import os

filename = '../../../test/data/layers/recurrent/GRU.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)

print(json.dumps(DATA))

"""
Explanation: export for Keras.js tests
End of explanation
"""


