In [1]:
import numpy as np
from keras.models import Model
from keras.layers import Input
from keras.layers.embeddings import Embedding
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()
[embeddings.Embedding.0] input_dim 5, output_dim 3, input_length=7, mask_zero=False
In [4]:
input_dim = 5
output_dim = 3
input_length = 7
data_in_shape = (input_length,)
emb = Embedding(input_dim, output_dim, input_length=input_length, mask_zero=False)
layer_0 = Input(shape=data_in_shape)
layer_1 = emb(layer_0)
model = Model(inputs=layer_0, outputs=layer_1)
# set weights to random (use seed for reproducibility)
weights = []
for i, w in enumerate(model.get_weights()):
np.random.seed(1200 + i)
weights.append(2 * np.random.random(w.shape) - 1)
model.set_weights(weights)
print('W shape:', weights[0].shape)
print('W:', format_decimal(weights[0].ravel().tolist()))
arr_in = np.random.randint(0, input_dim - 1, data_in_shape)
data_in = arr_in.ravel().tolist()
print('')
print('in shape:', data_in_shape)
print('in:', data_in)
result = model.predict(np.array([arr_in]))
data_out_shape = result[0].shape
data_out = format_decimal(result[0].ravel().tolist())
print('out shape:', data_out_shape)
print('out:', data_out)
DATA['embeddings.Embedding.0'] = {
'input': {'data': data_in, 'shape': data_in_shape},
'weights': [{'data': format_decimal(w.ravel().tolist()), 'shape': w.shape} for w in weights],
'expected': {'data': data_out, 'shape': data_out_shape}
}
[embeddings.Embedding.1] input_dim 20, output_dim 5, input_length=10, mask_zero=True
In [5]:
input_dim = 20
output_dim = 5
input_length = 10
data_in_shape = (input_length,)
emb = Embedding(input_dim, output_dim, input_length=input_length, mask_zero=True)
layer_0 = Input(shape=data_in_shape)
layer_1 = emb(layer_0)
model = Model(inputs=layer_0, outputs=layer_1)
# set weights to random (use seed for reproducibility)
weights = []
for i, w in enumerate(model.get_weights()):
np.random.seed(1210 + i)
weights.append(2 * np.random.random(w.shape) - 1)
model.set_weights(weights)
print('W shape:', weights[0].shape)
print('W:', format_decimal(weights[0].ravel().tolist()))
arr_in = np.random.randint(0, input_dim - 1, data_in_shape)
data_in = arr_in.ravel().tolist()
print('')
print('in shape:', data_in_shape)
print('in:', data_in)
result = model.predict(np.array([arr_in]))
data_out_shape = result[0].shape
data_out = format_decimal(result[0].ravel().tolist())
print('out shape:', data_out_shape)
print('out:', data_out)
DATA['embeddings.Embedding.1'] = {
'input': {'data': data_in, 'shape': data_in_shape},
'weights': [{'data': format_decimal(w.ravel().tolist()), 'shape': w.shape} for w in weights],
'expected': {'data': data_out, 'shape': data_out_shape}
}
[embeddings.Embedding.2] input_dim 33, output_dim 2, input_length=5, mask_zero=False
In [6]:
input_dim = 33
output_dim = 2
input_length = 5
data_in_shape = (input_length,)
emb = Embedding(input_dim, output_dim, input_length=input_length, mask_zero=False)
layer_0 = Input(shape=data_in_shape)
layer_1 = emb(layer_0)
model = Model(inputs=layer_0, outputs=layer_1)
# set weights to random (use seed for reproducibility)
weights = []
for i, w in enumerate(model.get_weights()):
np.random.seed(1220 + i)
weights.append(2 * np.random.random(w.shape) - 1)
model.set_weights(weights)
print('W shape:', weights[0].shape)
print('W:', format_decimal(weights[0].ravel().tolist()))
arr_in = np.random.randint(0, input_dim - 1, data_in_shape)
data_in = arr_in.ravel().tolist()
print('')
print('in shape:', data_in_shape)
print('in:', data_in)
result = model.predict(np.array([arr_in]))
data_out_shape = result[0].shape
data_out = format_decimal(result[0].ravel().tolist())
print('out shape:', data_out_shape)
print('out:', data_out)
DATA['embeddings.Embedding.2'] = {
'input': {'data': data_in, 'shape': data_in_shape},
'weights': [{'data': format_decimal(w.ravel().tolist()), 'shape': w.shape} for w in weights],
'expected': {'data': data_out, 'shape': data_out_shape}
}
In [7]:
print(json.dumps(DATA))