In [1]:
import numpy as np
from keras.models import Model
from keras.layers import Input
from keras.layers.core import Dense
from keras.layers.noise import GaussianDropout
from keras import backend as K
import json
from collections import OrderedDict


Using TensorFlow backend.

In [2]:
def format_decimal(arr, places=6):
    return [round(x * 10**places) / 10**places for x in arr]

In [3]:
DATA = OrderedDict()

GaussianDropout

[noise.GaussianDropout.0] should pass through


In [4]:
layer_0 = Input(shape=(6,))
layer_1 = Dense(2)(layer_0)
layer_2 = GaussianDropout(0.5)(layer_1)
model = Model(inputs=layer_0, outputs=layer_2)

# seed for reproducibility
np.random.seed(250)
W = np.random.randn(6,2)
b = np.random.randn(2)
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)
print('weights: ',W)
print('bias: ',b)
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['noise.GaussianDropout.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}
}


in: [0, 0.2, 0.5, -0.1, 1, 2]
in shape: (6,)
weights:  [[-0.70012161 -0.8783863 ]
 [ 1.27318097 -1.53115632]
 [ 1.36356888  0.90052448]
 [-1.7980838   0.86897991]
 [ 0.62728353  1.25668481]
 [-1.03947462  0.01474715]]
bias:  [ 0.95711006 -1.10933608]
out shape: (2,)
out: [0.621673, 0.233976]

export for Keras.js tests


In [5]:
print(json.dumps(DATA))


{"noise.GaussianDropout.0": {"weights": [{"data": [-0.700122, -0.878386, 1.273181, -1.531156, 1.363569, 0.900524, -1.798084, 0.86898, 0.627284, 1.256685, -1.039475, 0.014747], "shape": [6, 2]}, {"data": [0.95711, -1.109336], "shape": [2]}], "input": {"data": [0, 0.2, 0.5, -0.1, 1, 2], "shape": [6]}, "expected": {"data": [0.621673, 0.233976], "shape": [2]}}}

In [ ]: