mirror of
https://github.com/wassname/keras-js.git
synced 2026-09-10 12:15:12 +08:00
3.8 KiB
3.8 KiB
In [2]:
import numpy as np
from keras.models import Model
from keras.layers import Input
from keras.layers.core import Permute
from keras import backend as KUsing TensorFlow backend.
In [3]:
def format_decimal(arr, places=6):
return [round(x * 10**places) / 10**places for x in arr]In [14]:
layer_0 = Input(shape=(3, 2))
layer_1 = Permute((2, 1))(layer_0)
model = Model(input=layer_0, output=layer_1)
data_in = [0, 0.2, 0.5, -0.1, 1, 2]
data_in_shape = (3, 2)
print('in:', data_in)
print('in shape:', data_in_shape)
arr_in = np.array(data_in, dtype='float32').reshape(data_in_shape)
result = model.predict(np.array([arr_in]))
arr_out = result[0]
print('out shape:', arr_out.shape)
data_out = format_decimal(arr_out.ravel().tolist())
print('out:', data_out)in: [0, 0.2, 0.5, -0.1, 1, 2] in shape: (3, 2) out shape: (2, 3) out: [0.0, 0.5, 1.0, 0.2, -0.1, 2.0]
In [15]:
layer_0 = Input(shape=(2, 3, 4))
layer_1 = Permute((3, 2, 1))(layer_0)
model = Model(input=layer_0, output=layer_1)
data_in = [0, 0.2, 0.5, -0.1, 1, 2, 0, 0.2, 0.5, -0.1, 1, 2, 0, 0.2, 0.5, -0.1, 1, 2, 0, 0.2, 0.5, -0.1, 1, 2]
data_in_shape = (2, 3, 4)
print('in:', data_in)
print('in shape:', data_in_shape)
arr_in = np.array(data_in, dtype='float32').reshape(data_in_shape)
result = model.predict(np.array([arr_in]))
arr_out = result[0]
print('out shape:', arr_out.shape)
data_out = format_decimal(arr_out.ravel().tolist())
print('out:', data_out)in: [0, 0.2, 0.5, -0.1, 1, 2, 0, 0.2, 0.5, -0.1, 1, 2, 0, 0.2, 0.5, -0.1, 1, 2, 0, 0.2, 0.5, -0.1, 1, 2] in shape: (2, 3, 4) out shape: (4, 3, 2) out: [0.0, 0.0, 1.0, 1.0, 0.5, 0.5, 0.2, 0.2, 2.0, 2.0, -0.1, -0.1, 0.5, 0.5, 0.0, 0.0, 1.0, 1.0, -0.1, -0.1, 0.2, 0.2, 2.0, 2.0]
In [ ]: