Files

3.9 KiB

In [2]:
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
Using TensorFlow backend.
In [3]:
def format_decimal(arr, places=6):
    return [round(x * 10**places) / 10**places for x in arr]

Activation

[core.Activation.0] test 1 (tanh)

In [6]:
layer_0 = Input(shape=(6,))
layer_1 = Dense(2)(layer_0)
layer_2 = Activation('tanh')(layer_1)
model = Model(input=layer_0, output=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]
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: (2,)
out: [0.999999, -0.206966]

[core.Activation.1] test 2 (hardSigmoid)

In [7]:
layer_0 = Input(shape=(6,))
layer_1 = Dense(2)(layer_0)
layer_2 = Activation('hard_sigmoid')(layer_1)
model = Model(input=layer_0, output=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]
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: (2,)
out: [1.0, 0.458]
In [ ]: