Files
keras-js/notebooks/convolutional/ZeroPadding1D.ipynb
T

4.4 KiB

In [1]:
import numpy as np
from keras.models import Model
from keras.layers import Input
from keras.layers.convolutional import ZeroPadding1D
from keras import backend as K
Using TensorFlow backend.
In [2]:
def format_decimal(arr, places=6):
    return [round(x * 10**places) / 10**places for x in arr]

ZeroPadding1D

[convolutional.ZeroPadding1D.0] padding 1 on 3x5 input

In [3]:
data_in_shape = (3, 5)
L = ZeroPadding1D(padding=1)

layer_0 = Input(shape=data_in_shape)
layer_1 = L(layer_0)
model = Model(input=layer_0, output=layer_1)

# set weights to random (use seed for reproducibility)
np.random.seed(240)
data_in = 2 * np.random.random(data_in_shape) - 1
print('')
print('in shape:', data_in_shape)
print('in:', format_decimal(data_in.ravel().tolist()))
result = model.predict(np.array([data_in]))
print('out shape:', result[0].shape)
print('out:', format_decimal(result[0].ravel().tolist()))
in shape: (3, 5)
in: [-0.412987, -0.139246, 0.567466, -0.512174, -0.33603, 0.585584, 0.453472, 0.942013, -0.609538, -0.894302, 0.711927, -0.126057, 0.677549, 0.676991, 0.471487]
out shape: (5, 5)
out: [0.0, 0.0, 0.0, 0.0, 0.0, -0.412987, -0.139246, 0.567466, -0.512174, -0.33603, 0.585584, 0.453472, 0.942013, -0.609538, -0.894302, 0.711927, -0.126057, 0.677549, 0.676991, 0.471487, 0.0, 0.0, 0.0, 0.0, 0.0]

[convolutional.ZeroPadding1D.0] padding 3 on 4x4 input

In [4]:
data_in_shape = (4, 4)
L = ZeroPadding1D(padding=3)

layer_0 = Input(shape=data_in_shape)
layer_1 = L(layer_0)
model = Model(input=layer_0, output=layer_1)

# set weights to random (use seed for reproducibility)
np.random.seed(241)
data_in = 2 * np.random.random(data_in_shape) - 1
print('')
print('in shape:', data_in_shape)
print('in:', format_decimal(data_in.ravel().tolist()))
result = model.predict(np.array([data_in]))
print('out shape:', result[0].shape)
print('out:', format_decimal(result[0].ravel().tolist()))
in shape: (4, 4)
in: [0.229895, 0.156613, -0.66952, -0.996975, 0.45773, 0.684298, -0.999213, 0.276751, -0.484373, -0.506163, 0.353904, 0.513668, -0.981594, 0.78914, 0.603978, 0.204066]
out shape: (10, 4)
out: [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.229895, 0.156613, -0.66952, -0.996975, 0.45773, 0.684298, -0.999213, 0.276751, -0.484373, -0.506163, 0.353904, 0.513668, -0.981594, 0.78914, 0.603978, 0.204066, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]
In [ ]: