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

Activation


In [4]:
activations = ['softmax', 'elu', 'selu', 'softplus', 'softsign', 'relu', 'tanh', 'sigmoid', 'hard_sigmoid', 'linear']

In [5]:
for i, activation in enumerate(activations):
    key = f'core.Activation.{i}'
    print(f'\n[{key}] {activation}')

    layer_0 = Input(shape=(6,))
    layer_1 = Dense(2)(layer_0)
    layer_2 = Activation(activation)(layer_1)
    model = Model(inputs=layer_0, outputs=layer_2)

    W = 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 = np.array([0.5, 0.7])
    model.set_weights([W, b])

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


[core.Activation.0] softmax
in: [0, 0.2, 0.5, -0.1, 1, 2]
in shape: (6,)
out shape: (2,)
out: [0.999453, 0.000547]

[core.Activation.1] elu
in: [0, 0.2, 0.5, -0.1, 1, 2]
in shape: (6,)
out shape: (2,)
out: [7.3, -0.189416]

[core.Activation.2] selu
in: [0, 0.2, 0.5, -0.1, 1, 2]
in shape: (6,)
out shape: (2,)
out: [7.670117, -0.333012]

[core.Activation.3] softplus
in: [0, 0.2, 0.5, -0.1, 1, 2]
in shape: (6,)
out shape: (2,)
out: [7.300675, 0.59365]

[core.Activation.4] softsign
in: [0, 0.2, 0.5, -0.1, 1, 2]
in shape: (6,)
out shape: (2,)
out: [0.879518, -0.173554]

[core.Activation.5] relu
in: [0, 0.2, 0.5, -0.1, 1, 2]
in shape: (6,)
out shape: (2,)
out: [7.3, 0.0]

[core.Activation.6] tanh
in: [0, 0.2, 0.5, -0.1, 1, 2]
in shape: (6,)
out shape: (2,)
out: [0.999999, -0.206967]

[core.Activation.7] sigmoid
in: [0, 0.2, 0.5, -0.1, 1, 2]
in shape: (6,)
out shape: (2,)
out: [0.999325, 0.447692]

[core.Activation.8] hard_sigmoid
in: [0, 0.2, 0.5, -0.1, 1, 2]
in shape: (6,)
out shape: (2,)
out: [1.0, 0.458]

[core.Activation.9] linear
in: [0, 0.2, 0.5, -0.1, 1, 2]
in shape: (6,)
out shape: (2,)
out: [7.3, -0.21]

export for Keras.js tests


In [6]:
import os

filename = '../../../test/data/layers/core/Activation.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 [7]:
print(json.dumps(DATA))


{"core.Activation.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]}], "expected": {"data": [0.999453, 0.000547], "shape": [2]}}, "core.Activation.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]}], "expected": {"data": [7.3, -0.189416], "shape": [2]}}, "core.Activation.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]}], "expected": {"data": [7.670117, -0.333012], "shape": [2]}}, "core.Activation.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]}], "expected": {"data": [7.300675, 0.59365], "shape": [2]}}, "core.Activation.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]}], "expected": {"data": [0.879518, -0.173554], "shape": [2]}}, "core.Activation.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]}], "expected": {"data": [7.3, 0.0], "shape": [2]}}, "core.Activation.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]}], "expected": {"data": [0.999999, -0.206967], "shape": [2]}}, "core.Activation.7": {"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]}], "expected": {"data": [0.999325, 0.447692], "shape": [2]}}, "core.Activation.8": {"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]}], "expected": {"data": [1.0, 0.458], "shape": [2]}}, "core.Activation.9": {"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]}], "expected": {"data": [7.3, -0.21], "shape": [2]}}}

In [ ]: