mirror of
https://github.com/wassname/keras-js.git
synced 2026-09-09 11:25:25 +08:00
22 KiB
22 KiB
In [2]:
import numpy as np
from keras.models import Model
from keras.engine import merge
from keras.layers import Input
from keras.layers.core import Dense, Merge
from keras import backend as KUsing TensorFlow backend.
In [3]:
def format_decimal(arr, places=6):
return [round(x * 10**places) / 10**places for x in arr]In [17]:
layer_0 = Input(shape=(6,))
layer_1a = Dense(2, activation='linear')(layer_0)
layer_1b = Dense(2, activation='linear')(layer_0)
layer_2 = merge([layer_1a, layer_1b], mode='sum')
model = Model(input=layer_0, output=layer_2)
W_1a = 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_1a = np.array([0.5, 0.7])
W_1b = np.array([1, 0, -0.9, 0.6, -0.7, 0, 0.2, 0.4, 0, 0, -1, 2.3]).reshape((6, 2))
b_1b = np.array([0.1, -0.2])
model.set_weights([W_1a, b_1a, W_1b, b_1b])
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: [4.85, 4.27]
In [18]:
layer_0 = Input(shape=(6,))
layer_1a = Dense(2, activation='linear')(layer_0)
layer_1b = Dense(2, activation='linear')(layer_0)
layer_2 = merge([layer_1a, layer_1b], mode='mul')
model = Model(input=layer_0, output=layer_2)
W_1a = 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_1a = np.array([0.5, 0.7])
W_1b = np.array([1, 0, -0.9, 0.6, -0.7, 0, 0.2, 0.4, 0, 0, -1, 2.3]).reshape((6, 2))
b_1b = np.array([0.1, -0.2])
model.set_weights([W_1a, b_1a, W_1b, b_1b])
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: [-17.885, -0.9408]
In [19]:
layer_0 = Input(shape=(6,))
layer_1a = Dense(2, activation='linear')(layer_0)
layer_1b = Dense(2, activation='linear')(layer_0)
layer_2 = merge([layer_1a, layer_1b], mode='ave')
model = Model(input=layer_0, output=layer_2)
W_1a = 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_1a = np.array([0.5, 0.7])
W_1b = np.array([1, 0, -0.9, 0.6, -0.7, 0, 0.2, 0.4, 0, 0, -1, 2.3]).reshape((6, 2))
b_1b = np.array([0.1, -0.2])
model.set_weights([W_1a, b_1a, W_1b, b_1b])
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: [2.425, 2.135]
In [20]:
layer_0 = Input(shape=(6,))
layer_1a = Dense(2, activation='linear')(layer_0)
layer_1b = Dense(2, activation='linear')(layer_0)
layer_2 = merge([layer_1a, layer_1b], mode='max')
model = Model(input=layer_0, output=layer_2)
W_1a = 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_1a = np.array([0.5, 0.7])
W_1b = np.array([1, 0, -0.9, 0.6, -0.7, 0, 0.2, 0.4, 0, 0, -1, 2.3]).reshape((6, 2))
b_1b = np.array([0.1, -0.2])
model.set_weights([W_1a, b_1a, W_1b, b_1b])
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: [7.3, 4.48]
In [21]:
layer_0 = Input(shape=(6,))
layer_1a = Dense(2, activation='linear')(layer_0)
layer_1b = Dense(2, activation='linear')(layer_0)
layer_2 = merge([layer_1a, layer_1b], mode='concat', concat_axis=-1)
model = Model(input=layer_0, output=layer_2)
W_1a = 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_1a = np.array([0.5, 0.7])
W_1b = np.array([1, 0, -0.9, 0.6, -0.7, 0, 0.2, 0.4, 0, 0, -1, 2.3]).reshape((6, 2))
b_1b = np.array([0.1, -0.2])
model.set_weights([W_1a, b_1a, W_1b, b_1b])
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: (4,) out: [7.3, -0.21, -2.45, 4.48]
In [22]:
layer_0 = Input(shape=(6,))
layer_1a = Dense(2, activation='linear')(layer_0)
layer_1a = RepeatVector(3)(layer_1a)
layer_1b = Dense(2, activation='linear')(layer_0)
layer_1b = RepeatVector(3)(layer_1b)
layer_2 = merge([layer_1a, layer_1b], mode='concat', concat_axis=-1)
model = Model(input=layer_0, output=layer_2)
W_1a = 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_1a = np.array([0.5, 0.7])
W_1b = np.array([1, 0, -0.9, 0.6, -0.7, 0, 0.2, 0.4, 0, 0, -1, 2.3]).reshape((6, 2))
b_1b = np.array([0.1, -0.2])
model.set_weights([W_1a, b_1a, W_1b, b_1b])
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: (3, 4) out: [7.3, -0.21, -2.45, 4.48, 7.3, -0.21, -2.45, 4.48, 7.3, -0.21, -2.45, 4.48]
In [23]:
layer_0 = Input(shape=(6,))
layer_1a = Dense(2, activation='linear')(layer_0)
layer_1a = RepeatVector(3)(layer_1a)
layer_1b = Dense(2, activation='linear')(layer_0)
layer_1b = RepeatVector(3)(layer_1b)
layer_2 = merge([layer_1a, layer_1b], mode='concat', concat_axis=-2)
model = Model(input=layer_0, output=layer_2)
W_1a = 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_1a = np.array([0.5, 0.7])
W_1b = np.array([1, 0, -0.9, 0.6, -0.7, 0, 0.2, 0.4, 0, 0, -1, 2.3]).reshape((6, 2))
b_1b = np.array([0.1, -0.2])
model.set_weights([W_1a, b_1a, W_1b, b_1b])
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, 2) out: [7.3, -0.21, 7.3, -0.21, 7.3, -0.21, -2.45, 4.48, -2.45, 4.48, -2.45, 4.48]
In [24]:
layer_0 = Input(shape=(6,))
layer_1a = Dense(2, activation='linear')(layer_0)
layer_1a = RepeatVector(3)(layer_1a)
layer_1b = Dense(2, activation='linear')(layer_0)
layer_1b = RepeatVector(3)(layer_1b)
layer_2 = merge([layer_1a, layer_1b], mode='concat', concat_axis=1)
model = Model(input=layer_0, output=layer_2)
W_1a = 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_1a = np.array([0.5, 0.7])
W_1b = np.array([1, 0, -0.9, 0.6, -0.7, 0, 0.2, 0.4, 0, 0, -1, 2.3]).reshape((6, 2))
b_1b = np.array([0.1, -0.2])
model.set_weights([W_1a, b_1a, W_1b, b_1b])
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, 2) out: [7.3, -0.21, 7.3, -0.21, 7.3, -0.21, -2.45, 4.48, -2.45, 4.48, -2.45, 4.48]
In [25]:
layer_0 = Input(shape=(6,))
layer_1a = Dense(2, activation='linear')(layer_0)
layer_1a = RepeatVector(3)(layer_1a)
layer_1b = Dense(2, activation='linear')(layer_0)
layer_1b = RepeatVector(3)(layer_1b)
layer_2 = merge([layer_1a, layer_1b], mode='concat', concat_axis=2)
model = Model(input=layer_0, output=layer_2)
W_1a = 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_1a = np.array([0.5, 0.7])
W_1b = np.array([1, 0, -0.9, 0.6, -0.7, 0, 0.2, 0.4, 0, 0, -1, 2.3]).reshape((6, 2))
b_1b = np.array([0.1, -0.2])
model.set_weights([W_1a, b_1a, W_1b, b_1b])
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: (3, 4) out: [7.3, -0.21, -2.45, 4.48, 7.3, -0.21, -2.45, 4.48, 7.3, -0.21, -2.45, 4.48]
In [26]:
layer_0 = Input(shape=(6,))
layer_1a = Dense(2, activation='linear')(layer_0)
layer_1a = RepeatVector(3)(layer_1a)
layer_1b = Dense(2, activation='linear')(layer_0)
layer_1b = RepeatVector(3)(layer_1b)
layer_2 = merge([layer_1a, layer_1b], mode='dot', dot_axes=1)
model = Model(input=layer_0, output=layer_2)
W_1a = 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_1a = np.array([0.5, 0.7])
W_1b = np.array([1, 0, -0.9, 0.6, -0.7, 0, 0.2, 0.4, 0, 0, -1, 2.3]).reshape((6, 2))
b_1b = np.array([0.1, -0.2])
model.set_weights([W_1a, b_1a, W_1b, b_1b])
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, 2) out: [-53.655003, 98.112007, 1.5435, -2.8224]
In [27]:
layer_0 = Input(shape=(6,))
layer_1a = Dense(2, activation='linear')(layer_0)
layer_1a = RepeatVector(3)(layer_1a)
layer_1b = Dense(2, activation='linear')(layer_0)
layer_1b = RepeatVector(3)(layer_1b)
layer_2 = merge([layer_1a, layer_1b], mode='dot', dot_axes=2)
model = Model(input=layer_0, output=layer_2)
W_1a = 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_1a = np.array([0.5, 0.7])
W_1b = np.array([1, 0, -0.9, 0.6, -0.7, 0, 0.2, 0.4, 0, 0, -1, 2.3]).reshape((6, 2))
b_1b = np.array([0.1, -0.2])
model.set_weights([W_1a, b_1a, W_1b, b_1b])
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: (3, 3) out: [-18.8258, -18.8258, -18.8258, -18.8258, -18.8258, -18.8258, -18.8258, -18.8258, -18.8258]
In [28]:
layer_0 = Input(shape=(6,))
layer_1a = Dense(2, activation='linear')(layer_0)
layer_1a = RepeatVector(3)(layer_1a)
layer_1b = Dense(2, activation='linear')(layer_0)
layer_1b = RepeatVector(3)(layer_1b)
layer_2 = merge([layer_1a, layer_1b], mode='cos', dot_axes=1)
model = Model(input=layer_0, output=layer_2)
W_1a = 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_1a = np.array([0.5, 0.7])
W_1b = np.array([1, 0, -0.9, 0.6, -0.7, 0, 0.2, 0.4, 0, 0, -1, 2.3]).reshape((6, 2))
b_1b = np.array([0.1, -0.2])
model.set_weights([W_1a, b_1a, W_1b, b_1b])
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: (1, 2, 2) out: [-1.0, 7.972744, 0.125427, -1.0]
In [29]:
layer_0 = Input(shape=(6,))
layer_1a = Dense(2, activation='linear')(layer_0)
layer_1a = RepeatVector(3)(layer_1a)
layer_1b = Dense(2, activation='linear')(layer_0)
layer_1b = RepeatVector(3)(layer_1b)
layer_2 = merge([layer_1a, layer_1b], mode='cos', dot_axes=2)
model = Model(input=layer_0, output=layer_2)
W_1a = 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_1a = np.array([0.5, 0.7])
W_1b = np.array([1, 0, -0.9, 0.6, -0.7, 0, 0.2, 0.4, 0, 0, -1, 2.3]).reshape((6, 2))
b_1b = np.array([0.1, -0.2])
model.set_weights([W_1a, b_1a, W_1b, b_1b])
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: (1, 3, 3) out: [-0.504843, -0.504843, -0.504843, -0.504843, -0.504843, -0.504843, -0.504843, -0.504843, -0.504843]
In [ ]: