In [1]:
import numpy as np
from keras.models import Model
from keras.layers import Input
from keras.layers.advanced_activations import LeakyReLU, PReLU, ELU, ThresholdedReLU
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()

LeakyReLU

[advanced_activations.LeakyReLU.0] alpha=0.4


In [4]:
layer_0 = Input(shape=(6,))
layer_1 = LeakyReLU(alpha=0.4)(layer_0)
model = Model(inputs=layer_0, outputs=layer_1)

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['advanced_activations.LeakyReLU.0'] = {
    'input': {'data': data_in, 'shape': data_in_shape},
    'expected': {'data': data_out, 'shape': data_out_shape}
}


in: [0, 0.2, -0.5, -0.1, 1, 2]
in shape: (6,)
out shape: (6,)
out: [0.0, 0.2, -0.2, -0.04, 1.0, 2.0]

PReLU

[advanced_activations.PReLU.0] weights: alpha


In [5]:
layer_0 = Input(shape=(6,))
layer_1 = PReLU()(layer_0)
model = Model(inputs=layer_0, outputs=layer_1)

alpha = np.array([-0.03, -0.02, 0.02, -0.03, -0.03, -0.01])
model.set_weights([alpha])

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['advanced_activations.PReLU.0'] = {
    'input': {'data': data_in, 'shape': data_in_shape},
    'weights': [{'data': format_decimal(w.ravel().tolist()), 'shape': w.shape} for w in [alpha]],
    'expected': {'data': data_out, 'shape': data_out_shape}
}


in: [0, 0.2, -0.5, -0.1, 1, 2]
in shape: (6,)
out shape: (6,)
out: [0.0, 0.2, -0.01, 0.003, 1.0, 2.0]

ELU

[advanced_activations.ELU.0] alpha=1.1


In [6]:
layer_0 = Input(shape=(6,))
layer_1 = ELU(alpha=1.1)(layer_0)
model = Model(inputs=layer_0, outputs=layer_1)

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['advanced_activations.ELU.0'] = {
    'input': {'data': data_in, 'shape': data_in_shape},
    'expected': {'data': data_out, 'shape': data_out_shape}
}


in: [0, 0.2, -0.5, -0.1, 1, 2]
in shape: (6,)
out shape: (6,)
out: [0.0, 0.2, -0.432816, -0.104679, 1.0, 2.0]

ThresholdedReLU

[advanced_activations.ThresholdedReLU.0] theta=0.9


In [7]:
layer_0 = Input(shape=(6,))
layer_1 = ThresholdedReLU(theta=0.9)(layer_0)
model = Model(inputs=layer_0, outputs=layer_1)

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['advanced_activations.ThresholdedReLU.0'] = {
    'input': {'data': data_in, 'shape': data_in_shape},
    'expected': {'data': data_out, 'shape': data_out_shape}
}


in: [0, 0.2, 0.5, -0.1, 1, 2]
in shape: (6,)
out shape: (6,)
out: [0.0, 0.0, 0.0, 0.0, 1.0, 2.0]

export for Keras.js tests


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


{"advanced_activations.LeakyReLU.0": {"input": {"data": [0, 0.2, -0.5, -0.1, 1, 2], "shape": [6]}, "expected": {"data": [0.0, 0.2, -0.2, -0.04, 1.0, 2.0], "shape": [6]}}, "advanced_activations.PReLU.0": {"weights": [{"data": [-0.03, -0.02, 0.02, -0.03, -0.03, -0.01], "shape": [6]}], "input": {"data": [0, 0.2, -0.5, -0.1, 1, 2], "shape": [6]}, "expected": {"data": [0.0, 0.2, -0.01, 0.003, 1.0, 2.0], "shape": [6]}}, "advanced_activations.ELU.0": {"input": {"data": [0, 0.2, -0.5, -0.1, 1, 2], "shape": [6]}, "expected": {"data": [0.0, 0.2, -0.432816, -0.104679, 1.0, 2.0], "shape": [6]}}, "advanced_activations.ThresholdedReLU.0": {"input": {"data": [0, 0.2, 0.5, -0.1, 1, 2], "shape": [6]}, "expected": {"data": [0.0, 0.0, 0.0, 0.0, 1.0, 2.0], "shape": [6]}}}

In [ ]: