In [1]:
import numpy as np
from keras.models import Model
from keras.layers import Input
from keras.layers.pooling import GlobalAveragePooling1D
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()

GlobalAveragePooling1D

[pooling.GlobalAveragePooling1D.0] input 6x6


In [4]:
data_in_shape = (6, 6)
L = GlobalAveragePooling1D()

layer_0 = Input(shape=data_in_shape)
layer_1 = L(layer_0)
model = Model(inputs=layer_0, outputs=layer_1)

# set weights to random (use seed for reproducibility)
np.random.seed(260)
data_in = 2 * np.random.random(data_in_shape) - 1
result = model.predict(np.array([data_in]))
data_out_shape = result[0].shape
data_in_formatted = format_decimal(data_in.ravel().tolist())
data_out_formatted = format_decimal(result[0].ravel().tolist())
print('')
print('in shape:', data_in_shape)
print('in:', data_in_formatted)
print('out shape:', data_out_shape)
print('out:', data_out_formatted)

DATA['pooling.GlobalAveragePooling1D.0'] = {
    'input': {'data': data_in_formatted, 'shape': data_in_shape},
    'expected': {'data': data_out_formatted, 'shape': data_out_shape}
}


in shape: (6, 6)
in: [-0.806777, -0.564841, -0.481331, 0.559626, 0.274958, -0.659222, -0.178541, 0.689453, -0.028873, 0.053859, -0.446394, -0.53406, 0.776897, -0.700858, -0.802179, -0.616515, 0.718677, 0.303042, -0.080606, -0.850593, -0.795971, 0.860487, -0.90685, 0.89858, 0.617251, 0.334305, -0.351137, -0.642574, 0.108974, -0.993964, 0.051085, -0.372012, 0.843766, 0.088025, -0.598662, 0.789035]
out shape: (6,)
out: [0.063218, -0.244091, -0.269288, 0.050485, -0.141549, -0.032765]

[pooling.GlobalAveragePooling1D.1] input 3x7


In [5]:
data_in_shape = (3, 7)
L = GlobalAveragePooling1D()

layer_0 = Input(shape=data_in_shape)
layer_1 = L(layer_0)
model = Model(inputs=layer_0, outputs=layer_1)

# set weights to random (use seed for reproducibility)
np.random.seed(261)
data_in = 2 * np.random.random(data_in_shape) - 1
result = model.predict(np.array([data_in]))
data_out_shape = result[0].shape
data_in_formatted = format_decimal(data_in.ravel().tolist())
data_out_formatted = format_decimal(result[0].ravel().tolist())
print('')
print('in shape:', data_in_shape)
print('in:', data_in_formatted)
print('out shape:', data_out_shape)
print('out:', data_out_formatted)

DATA['pooling.GlobalAveragePooling1D.1'] = {
    'input': {'data': data_in_formatted, 'shape': data_in_shape},
    'expected': {'data': data_out_formatted, 'shape': data_out_shape}
}


in shape: (3, 7)
in: [0.601872, -0.028379, 0.654213, 0.217731, -0.864161, 0.422013, 0.888312, -0.714141, -0.184753, 0.224845, -0.221123, -0.847943, -0.511334, -0.871723, -0.597589, -0.889034, -0.544887, -0.004798, 0.406639, -0.35285, 0.648562]
out shape: (7,)
out: [-0.23662, -0.367389, 0.111391, -0.00273, -0.435155, -0.14739, 0.221717]

[pooling.GlobalAveragePooling1D.2] input 8x4


In [6]:
data_in_shape = (8, 4)
L = GlobalAveragePooling1D()

layer_0 = Input(shape=data_in_shape)
layer_1 = L(layer_0)
model = Model(inputs=layer_0, outputs=layer_1)

# set weights to random (use seed for reproducibility)
np.random.seed(262)
data_in = 2 * np.random.random(data_in_shape) - 1
result = model.predict(np.array([data_in]))
data_out_shape = result[0].shape
data_in_formatted = format_decimal(data_in.ravel().tolist())
data_out_formatted = format_decimal(result[0].ravel().tolist())
print('')
print('in shape:', data_in_shape)
print('in:', data_in_formatted)
print('out shape:', data_out_shape)
print('out:', data_out_formatted)

DATA['pooling.GlobalAveragePooling1D.2'] = {
    'input': {'data': data_in_formatted, 'shape': data_in_shape},
    'expected': {'data': data_out_formatted, 'shape': data_out_shape}
}


in shape: (8, 4)
in: [-0.215694, 0.441215, 0.116911, 0.53299, 0.883562, -0.535525, -0.869764, -0.596287, 0.576428, -0.689083, -0.132924, -0.129935, -0.17672, -0.29097, 0.590914, 0.992098, 0.908965, -0.170202, 0.640203, 0.178644, 0.866749, -0.545566, -0.827072, 0.420342, -0.076191, 0.207686, -0.908472, -0.795307, -0.948319, 0.683682, -0.563278, -0.82135]
out shape: (4,)
out: [0.227348, -0.112345, -0.244185, -0.027351]

export for Keras.js tests


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


{"pooling.GlobalAveragePooling1D.0": {"input": {"data": [-0.806777, -0.564841, -0.481331, 0.559626, 0.274958, -0.659222, -0.178541, 0.689453, -0.028873, 0.053859, -0.446394, -0.53406, 0.776897, -0.700858, -0.802179, -0.616515, 0.718677, 0.303042, -0.080606, -0.850593, -0.795971, 0.860487, -0.90685, 0.89858, 0.617251, 0.334305, -0.351137, -0.642574, 0.108974, -0.993964, 0.051085, -0.372012, 0.843766, 0.088025, -0.598662, 0.789035], "shape": [6, 6]}, "expected": {"data": [0.063218, -0.244091, -0.269288, 0.050485, -0.141549, -0.032765], "shape": [6]}}, "pooling.GlobalAveragePooling1D.1": {"input": {"data": [0.601872, -0.028379, 0.654213, 0.217731, -0.864161, 0.422013, 0.888312, -0.714141, -0.184753, 0.224845, -0.221123, -0.847943, -0.511334, -0.871723, -0.597589, -0.889034, -0.544887, -0.004798, 0.406639, -0.35285, 0.648562], "shape": [3, 7]}, "expected": {"data": [-0.23662, -0.367389, 0.111391, -0.00273, -0.435155, -0.14739, 0.221717], "shape": [7]}}, "pooling.GlobalAveragePooling1D.2": {"input": {"data": [-0.215694, 0.441215, 0.116911, 0.53299, 0.883562, -0.535525, -0.869764, -0.596287, 0.576428, -0.689083, -0.132924, -0.129935, -0.17672, -0.29097, 0.590914, 0.992098, 0.908965, -0.170202, 0.640203, 0.178644, 0.866749, -0.545566, -0.827072, 0.420342, -0.076191, 0.207686, -0.908472, -0.795307, -0.948319, 0.683682, -0.563278, -0.82135], "shape": [8, 4]}, "expected": {"data": [0.227348, -0.112345, -0.244185, -0.027351], "shape": [4]}}}

In [ ]: