Files
keras-js/notebooks/advanced_activations.ipynb

9.2 KiB

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, ParametricSoftplus, ThresholdedReLU, SReLU
from keras import backend as K
Using TensorFlow backend.
In [2]:
def format_decimal(arr, places=6):
    return [round(x * 10**places) / 10**places for x in arr]

LeakyReLU

[advanced_activations.LeakyReLU.0] alpha=0.4

In [3]:
layer_0 = Input(shape=(6,))
layer_1 = LeakyReLU(alpha=0.4)(layer_0)
model = Model(input=layer_0, output=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]
print('out shape:', arr_out.shape)
data_out = format_decimal(arr_out.ravel().tolist())
print('out:', data_out)
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: alphas

In [4]:
layer_0 = Input(shape=(6,))
layer_1 = PReLU()(layer_0)
model = Model(input=layer_0, output=layer_1)

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

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]
print('out shape:', arr_out.shape)
data_out = format_decimal(arr_out.ravel().tolist())
print('out:', data_out)
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 [5]:
layer_0 = Input(shape=(6,))
layer_1 = ELU(alpha=1.1)(layer_0)
model = Model(input=layer_0, output=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]
print('out shape:', arr_out.shape)
data_out = format_decimal(arr_out.ravel().tolist())
print('out:', data_out)
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]

ParametricSoftplus

[advanced_activations.ParametricSoftplus.0] weights: alphas, betas

In [6]:
layer_0 = Input(shape=(6,))
layer_1 = ParametricSoftplus()(layer_0)
model = Model(input=layer_0, output=layer_1)

alphas = np.array([0.13, -0.02, 0.02, -0.03, -0.03, -0.01])
betas = np.array([-0.03, -0.1, 0.02, 0.5, 0.2, 0.0])
model.set_weights([alphas, betas])

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]
print('out shape:', arr_out.shape)
data_out = format_decimal(arr_out.ravel().tolist())
print('out:', data_out)
in: [0, 0.2, -0.5, -0.1, 1, 2]
in shape: (6,)
out shape: (6,)
out: [0.090109, -0.013664, 0.013763, -0.020054, -0.023944, -0.006931]

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(input=layer_0, output=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]
print('out shape:', arr_out.shape)
data_out = format_decimal(arr_out.ravel().tolist())
print('out:', data_out)
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]

SReLU

[advanced_activations.SReLU.0] weights: t_left, a_left, t_right, a_right

In [8]:
xxx = SReLU()
layer_0 = Input(shape=(6,))
layer_1 = xxx(layer_0)
model = Model(input=layer_0, output=layer_1)

t_left, a_left, t_right, a_right = (np.array([0.13, -0.02, 0.02, -0.03, -0.03, -0.01]),
                                    np.array([-0.03, -0.1, 0.02, 0.5, 0.2, 0.0]),
                                    np.array([-0.9, 0.8, 0.0, -1.0, 0.7, 0.4]),
                                    np.array([0.1, 0.2, 0.3, 0.0, 0.5, -0.2]))
model.set_weights([t_left, a_left, t_right, a_right])

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]
print('out shape:', arr_out.shape)
data_out = format_decimal(arr_out.ravel().tolist())
print('out:', data_out)
in: [0, 0.2, -0.5, -0.1, 1, 2]
in shape: (6,)
out shape: (6,)
out: [0.1339, 0.2, 0.0096, -0.065, 0.835, 0.068]
In [ ]: