mirror of
https://github.com/wassname/keras-js.git
synced 2026-08-31 12:11:11 +08:00
11 KiB
11 KiB
In [1]:
import numpy as np
from keras.models import Model
from keras.layers import Input
from keras.layers.convolutional import UpSampling2D
from keras import backend as KUsing TensorFlow backend.
In [2]:
def format_decimal(arr, places=6):
return [round(x * 10**places) / 10**places for x in arr]In [3]:
data_in_shape = (3, 3, 3)
L = UpSampling2D(size=(2, 2), dim_ordering='tf')
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(250)
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, 3, 3) in: [-0.570441, -0.454673, -0.285321, 0.237249, 0.282682, 0.428035, 0.160547, -0.332203, 0.546391, 0.272735, 0.010827, -0.763164, -0.442696, 0.381948, -0.676994, 0.753553, -0.031788, 0.915329, -0.738844, 0.269075, 0.434091, 0.991585, -0.944288, 0.258834, 0.162138, 0.565201, -0.492094] out shape: (6, 6, 3) out: [-0.570441, -0.454673, -0.285321, -0.570441, -0.454673, -0.285321, 0.237249, 0.282682, 0.428035, 0.237249, 0.282682, 0.428035, 0.160547, -0.332203, 0.546391, 0.160547, -0.332203, 0.546391, -0.570441, -0.454673, -0.285321, -0.570441, -0.454673, -0.285321, 0.237249, 0.282682, 0.428035, 0.237249, 0.282682, 0.428035, 0.160547, -0.332203, 0.546391, 0.160547, -0.332203, 0.546391, 0.272735, 0.010827, -0.763164, 0.272735, 0.010827, -0.763164, -0.442696, 0.381948, -0.676994, -0.442696, 0.381948, -0.676994, 0.753553, -0.031788, 0.915329, 0.753553, -0.031788, 0.915329, 0.272735, 0.010827, -0.763164, 0.272735, 0.010827, -0.763164, -0.442696, 0.381948, -0.676994, -0.442696, 0.381948, -0.676994, 0.753553, -0.031788, 0.915329, 0.753553, -0.031788, 0.915329, -0.738844, 0.269075, 0.434091, -0.738844, 0.269075, 0.434091, 0.991585, -0.944288, 0.258834, 0.991585, -0.944288, 0.258834, 0.162138, 0.565201, -0.492094, 0.162138, 0.565201, -0.492094, -0.738844, 0.269075, 0.434091, -0.738844, 0.269075, 0.434091, 0.991585, -0.944288, 0.258834, 0.991585, -0.944288, 0.258834, 0.162138, 0.565201, -0.492094, 0.162138, 0.565201, -0.492094]
In [4]:
data_in_shape = (3, 3, 3)
L = UpSampling2D(size=(2, 2), dim_ordering='th')
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(250)
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, 3, 3) in: [-0.570441, -0.454673, -0.285321, 0.237249, 0.282682, 0.428035, 0.160547, -0.332203, 0.546391, 0.272735, 0.010827, -0.763164, -0.442696, 0.381948, -0.676994, 0.753553, -0.031788, 0.915329, -0.738844, 0.269075, 0.434091, 0.991585, -0.944288, 0.258834, 0.162138, 0.565201, -0.492094] out shape: (3, 6, 6) out: [-0.570441, -0.570441, -0.454673, -0.454673, -0.285321, -0.285321, -0.570441, -0.570441, -0.454673, -0.454673, -0.285321, -0.285321, 0.237249, 0.237249, 0.282682, 0.282682, 0.428035, 0.428035, 0.237249, 0.237249, 0.282682, 0.282682, 0.428035, 0.428035, 0.160547, 0.160547, -0.332203, -0.332203, 0.546391, 0.546391, 0.160547, 0.160547, -0.332203, -0.332203, 0.546391, 0.546391, 0.272735, 0.272735, 0.010827, 0.010827, -0.763164, -0.763164, 0.272735, 0.272735, 0.010827, 0.010827, -0.763164, -0.763164, -0.442696, -0.442696, 0.381948, 0.381948, -0.676994, -0.676994, -0.442696, -0.442696, 0.381948, 0.381948, -0.676994, -0.676994, 0.753553, 0.753553, -0.031788, -0.031788, 0.915329, 0.915329, 0.753553, 0.753553, -0.031788, -0.031788, 0.915329, 0.915329, -0.738844, -0.738844, 0.269075, 0.269075, 0.434091, 0.434091, -0.738844, -0.738844, 0.269075, 0.269075, 0.434091, 0.434091, 0.991585, 0.991585, -0.944288, -0.944288, 0.258834, 0.258834, 0.991585, 0.991585, -0.944288, -0.944288, 0.258834, 0.258834, 0.162138, 0.162138, 0.565201, 0.565201, -0.492094, -0.492094, 0.162138, 0.162138, 0.565201, 0.565201, -0.492094, -0.492094]
In [5]:
data_in_shape = (4, 2, 2)
L = UpSampling2D(size=(3, 2), dim_ordering='tf')
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(251)
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, 2, 2) in: [0.275222, -0.793967, -0.468107, -0.841484, -0.295362, 0.78175, 0.068787, -0.261747, -0.625733, -0.042907, 0.861141, 0.85267, 0.956439, 0.717838, -0.99869, -0.963008] out shape: (12, 4, 2) out: [0.275222, -0.793967, 0.275222, -0.793967, -0.468107, -0.841484, -0.468107, -0.841484, 0.275222, -0.793967, 0.275222, -0.793967, -0.468107, -0.841484, -0.468107, -0.841484, 0.275222, -0.793967, 0.275222, -0.793967, -0.468107, -0.841484, -0.468107, -0.841484, -0.295362, 0.78175, -0.295362, 0.78175, 0.068787, -0.261747, 0.068787, -0.261747, -0.295362, 0.78175, -0.295362, 0.78175, 0.068787, -0.261747, 0.068787, -0.261747, -0.295362, 0.78175, -0.295362, 0.78175, 0.068787, -0.261747, 0.068787, -0.261747, -0.625733, -0.042907, -0.625733, -0.042907, 0.861141, 0.85267, 0.861141, 0.85267, -0.625733, -0.042907, -0.625733, -0.042907, 0.861141, 0.85267, 0.861141, 0.85267, -0.625733, -0.042907, -0.625733, -0.042907, 0.861141, 0.85267, 0.861141, 0.85267, 0.956439, 0.717838, 0.956439, 0.717838, -0.99869, -0.963008, -0.99869, -0.963008, 0.956439, 0.717838, 0.956439, 0.717838, -0.99869, -0.963008, -0.99869, -0.963008, 0.956439, 0.717838, 0.956439, 0.717838, -0.99869, -0.963008, -0.99869, -0.963008]
In [7]:
data_in_shape = (4, 3, 2)
L = UpSampling2D(size=(1, 3), dim_ordering='th')
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(252)
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, 3, 2) in: [-0.989173, -0.133618, -0.505338, 0.023259, 0.503982, -0.303769, -0.436321, 0.793911, 0.416102, 0.806405, -0.098342, -0.738022, -0.982676, 0.805073, 0.741244, -0.941634, -0.253526, -0.136544, -0.295772, 0.207565, -0.517246, -0.686963, -0.176235, -0.354111] out shape: (4, 3, 6) out: [-0.989173, -0.989173, -0.989173, -0.133618, -0.133618, -0.133618, -0.505338, -0.505338, -0.505338, 0.023259, 0.023259, 0.023259, 0.503982, 0.503982, 0.503982, -0.303769, -0.303769, -0.303769, -0.436321, -0.436321, -0.436321, 0.793911, 0.793911, 0.793911, 0.416102, 0.416102, 0.416102, 0.806405, 0.806405, 0.806405, -0.098342, -0.098342, -0.098342, -0.738022, -0.738022, -0.738022, -0.982676, -0.982676, -0.982676, 0.805073, 0.805073, 0.805073, 0.741244, 0.741244, 0.741244, -0.941634, -0.941634, -0.941634, -0.253526, -0.253526, -0.253526, -0.136544, -0.136544, -0.136544, -0.295772, -0.295772, -0.295772, 0.207565, 0.207565, 0.207565, -0.517246, -0.517246, -0.517246, -0.686963, -0.686963, -0.686963, -0.176235, -0.176235, -0.176235, -0.354111, -0.354111, -0.354111]
In [ ]: