In [1]:
import numpy as np
from keras.models import Model
from keras.layers import Input
from keras.layers.core import Dense
from keras import backend as K
import json
from collections import OrderedDict
In [2]:
def format_decimal(arr, places=6):
return [round(x * 10**places) / 10**places for x in arr]
In [3]:
DATA = OrderedDict()
[core.Dense.0] test 1
In [4]:
layer_0 = Input(shape=(6,))
layer_1 = Dense(2, activation='linear')(layer_0)
model = Model(inputs=layer_0, outputs=layer_1)
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['core.Dense.0'] = {
'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.Dense.1] test 2 (with sigmoid activation)
In [5]:
layer_0 = Input(shape=(6,))
layer_1 = Dense(2, activation='sigmoid')(layer_0)
model = Model(inputs=layer_0, outputs=layer_1)
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['core.Dense.1'] = {
'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.Dense.2] test 3 (with softplus activation and no bias)
In [6]:
layer_0 = Input(shape=(6,))
layer_1 = Dense(2, activation='softplus', use_bias=False)(layer_0)
model = Model(inputs=layer_0, outputs=layer_1)
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))
model.set_weights([W])
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['core.Dense.2'] = {
'input': {'data': data_in, 'shape': data_in_shape},
'weights': [{'data': format_decimal(w.ravel().tolist()), 'shape': w.shape} for w in [W]],
'expected': {'data': data_out, 'shape': data_out_shape}
}
[core.Dense.3] [GPU] test 1
In [7]:
layer_0 = Input(shape=(6,))
layer_1 = Dense(2, activation='linear')(layer_0)
model = Model(inputs=layer_0, outputs=layer_1)
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['core.Dense.3'] = {
'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.Dense.4] [GPU] test 2 (with sigmoid activation)
In [8]:
layer_0 = Input(shape=(6,))
layer_1 = Dense(2, activation='sigmoid')(layer_0)
model = Model(inputs=layer_0, outputs=layer_1)
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['core.Dense.4'] = {
'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.Dense.5] [GPU] test 3 (with softplus activation and no bias)
In [9]:
layer_0 = Input(shape=(6,))
layer_1 = Dense(2, activation='softplus', use_bias=False)(layer_0)
model = Model(inputs=layer_0, outputs=layer_1)
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))
model.set_weights([W])
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['core.Dense.5'] = {
'input': {'data': data_in, 'shape': data_in_shape},
'weights': [{'data': format_decimal(w.ravel().tolist()), 'shape': w.shape} for w in [W]],
'expected': {'data': data_out, 'shape': data_out_shape}
}
In [10]:
json.dumps(DATA)
Out[10]:
In [ ]: