mirror of
https://github.com/wassname/keras-js.git
synced 2026-09-13 12:41:26 +08:00
745 lines
22 KiB
Plaintext
745 lines
22 KiB
Plaintext
{
|
|
"cells": [
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 2,
|
|
"metadata": {
|
|
"collapsed": false
|
|
},
|
|
"outputs": [
|
|
{
|
|
"name": "stderr",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"Using TensorFlow backend.\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"import numpy as np\n",
|
|
"from keras.models import Model\n",
|
|
"from keras.engine import merge\n",
|
|
"from keras.layers import Input\n",
|
|
"from keras.layers.core import Dense, Merge\n",
|
|
"from keras import backend as K"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 3,
|
|
"metadata": {
|
|
"collapsed": true
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
"def format_decimal(arr, places=6):\n",
|
|
" return [round(x * 10**places) / 10**places for x in arr]"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"### Merge"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"**[core.Merge.0] mode: sum**"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 17,
|
|
"metadata": {
|
|
"collapsed": false
|
|
},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"in: [0, 0.2, 0.5, -0.1, 1, 2]\n",
|
|
"in shape: (6,)\n",
|
|
"out shape: (2,)\n",
|
|
"out: [4.85, 4.27]\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"layer_0 = Input(shape=(6,))\n",
|
|
"layer_1a = Dense(2, activation='linear')(layer_0)\n",
|
|
"layer_1b = Dense(2, activation='linear')(layer_0)\n",
|
|
"layer_2 = merge([layer_1a, layer_1b], mode='sum')\n",
|
|
"model = Model(input=layer_0, output=layer_2)\n",
|
|
"\n",
|
|
"W_1a = np.array([0.1, 0.4, 0.5, 0.1, 1, -2, 0, 0.3, 0.2, 0.1, 3, 0]).reshape((6, 2))\n",
|
|
"b_1a = np.array([0.5, 0.7])\n",
|
|
"W_1b = np.array([1, 0, -0.9, 0.6, -0.7, 0, 0.2, 0.4, 0, 0, -1, 2.3]).reshape((6, 2))\n",
|
|
"b_1b = np.array([0.1, -0.2])\n",
|
|
"model.set_weights([W_1a, b_1a, W_1b, b_1b])\n",
|
|
"\n",
|
|
"data_in = [0, 0.2, 0.5, -0.1, 1, 2]\n",
|
|
"data_in_shape = (6,)\n",
|
|
"print('in:', data_in)\n",
|
|
"print('in shape:', data_in_shape)\n",
|
|
"arr_in = np.array(data_in, dtype='float32').reshape(data_in_shape)\n",
|
|
"result = model.predict(np.array([arr_in]))\n",
|
|
"arr_out = result[0]\n",
|
|
"print('out shape:', arr_out.shape)\n",
|
|
"data_out = format_decimal(arr_out.ravel().tolist())\n",
|
|
"print('out:', data_out)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"**[core.Merge.1] mode: mul**"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 18,
|
|
"metadata": {
|
|
"collapsed": false
|
|
},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"in: [0, 0.2, 0.5, -0.1, 1, 2]\n",
|
|
"in shape: (6,)\n",
|
|
"out shape: (2,)\n",
|
|
"out: [-17.885, -0.9408]\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"layer_0 = Input(shape=(6,))\n",
|
|
"layer_1a = Dense(2, activation='linear')(layer_0)\n",
|
|
"layer_1b = Dense(2, activation='linear')(layer_0)\n",
|
|
"layer_2 = merge([layer_1a, layer_1b], mode='mul')\n",
|
|
"model = Model(input=layer_0, output=layer_2)\n",
|
|
"\n",
|
|
"W_1a = np.array([0.1, 0.4, 0.5, 0.1, 1, -2, 0, 0.3, 0.2, 0.1, 3, 0]).reshape((6, 2))\n",
|
|
"b_1a = np.array([0.5, 0.7])\n",
|
|
"W_1b = np.array([1, 0, -0.9, 0.6, -0.7, 0, 0.2, 0.4, 0, 0, -1, 2.3]).reshape((6, 2))\n",
|
|
"b_1b = np.array([0.1, -0.2])\n",
|
|
"model.set_weights([W_1a, b_1a, W_1b, b_1b])\n",
|
|
"\n",
|
|
"data_in = [0, 0.2, 0.5, -0.1, 1, 2]\n",
|
|
"data_in_shape = (6,)\n",
|
|
"print('in:', data_in)\n",
|
|
"print('in shape:', data_in_shape)\n",
|
|
"arr_in = np.array(data_in, dtype='float32').reshape(data_in_shape)\n",
|
|
"result = model.predict(np.array([arr_in]))\n",
|
|
"arr_out = result[0]\n",
|
|
"print('out shape:', arr_out.shape)\n",
|
|
"data_out = format_decimal(arr_out.ravel().tolist())\n",
|
|
"print('out:', data_out)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"**[core.Merge.2] mode: ave**"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 19,
|
|
"metadata": {
|
|
"collapsed": false
|
|
},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"in: [0, 0.2, 0.5, -0.1, 1, 2]\n",
|
|
"in shape: (6,)\n",
|
|
"out shape: (2,)\n",
|
|
"out: [2.425, 2.135]\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"layer_0 = Input(shape=(6,))\n",
|
|
"layer_1a = Dense(2, activation='linear')(layer_0)\n",
|
|
"layer_1b = Dense(2, activation='linear')(layer_0)\n",
|
|
"layer_2 = merge([layer_1a, layer_1b], mode='ave')\n",
|
|
"model = Model(input=layer_0, output=layer_2)\n",
|
|
"\n",
|
|
"W_1a = np.array([0.1, 0.4, 0.5, 0.1, 1, -2, 0, 0.3, 0.2, 0.1, 3, 0]).reshape((6, 2))\n",
|
|
"b_1a = np.array([0.5, 0.7])\n",
|
|
"W_1b = np.array([1, 0, -0.9, 0.6, -0.7, 0, 0.2, 0.4, 0, 0, -1, 2.3]).reshape((6, 2))\n",
|
|
"b_1b = np.array([0.1, -0.2])\n",
|
|
"model.set_weights([W_1a, b_1a, W_1b, b_1b])\n",
|
|
"\n",
|
|
"data_in = [0, 0.2, 0.5, -0.1, 1, 2]\n",
|
|
"data_in_shape = (6,)\n",
|
|
"print('in:', data_in)\n",
|
|
"print('in shape:', data_in_shape)\n",
|
|
"arr_in = np.array(data_in, dtype='float32').reshape(data_in_shape)\n",
|
|
"result = model.predict(np.array([arr_in]))\n",
|
|
"arr_out = result[0]\n",
|
|
"print('out shape:', arr_out.shape)\n",
|
|
"data_out = format_decimal(arr_out.ravel().tolist())\n",
|
|
"print('out:', data_out)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"**[core.Merge.3] mode: max**"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 20,
|
|
"metadata": {
|
|
"collapsed": false
|
|
},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"in: [0, 0.2, 0.5, -0.1, 1, 2]\n",
|
|
"in shape: (6,)\n",
|
|
"out shape: (2,)\n",
|
|
"out: [7.3, 4.48]\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"layer_0 = Input(shape=(6,))\n",
|
|
"layer_1a = Dense(2, activation='linear')(layer_0)\n",
|
|
"layer_1b = Dense(2, activation='linear')(layer_0)\n",
|
|
"layer_2 = merge([layer_1a, layer_1b], mode='max')\n",
|
|
"model = Model(input=layer_0, output=layer_2)\n",
|
|
"\n",
|
|
"W_1a = np.array([0.1, 0.4, 0.5, 0.1, 1, -2, 0, 0.3, 0.2, 0.1, 3, 0]).reshape((6, 2))\n",
|
|
"b_1a = np.array([0.5, 0.7])\n",
|
|
"W_1b = np.array([1, 0, -0.9, 0.6, -0.7, 0, 0.2, 0.4, 0, 0, -1, 2.3]).reshape((6, 2))\n",
|
|
"b_1b = np.array([0.1, -0.2])\n",
|
|
"model.set_weights([W_1a, b_1a, W_1b, b_1b])\n",
|
|
"\n",
|
|
"data_in = [0, 0.2, 0.5, -0.1, 1, 2]\n",
|
|
"data_in_shape = (6,)\n",
|
|
"print('in:', data_in)\n",
|
|
"print('in shape:', data_in_shape)\n",
|
|
"arr_in = np.array(data_in, dtype='float32').reshape(data_in_shape)\n",
|
|
"result = model.predict(np.array([arr_in]))\n",
|
|
"arr_out = result[0]\n",
|
|
"print('out shape:', arr_out.shape)\n",
|
|
"data_out = format_decimal(arr_out.ravel().tolist())\n",
|
|
"print('out:', data_out)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"**[core.Merge.4] mode: concat (1D)**"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 21,
|
|
"metadata": {
|
|
"collapsed": false
|
|
},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"in: [0, 0.2, 0.5, -0.1, 1, 2]\n",
|
|
"in shape: (6,)\n",
|
|
"out shape: (4,)\n",
|
|
"out: [7.3, -0.21, -2.45, 4.48]\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"layer_0 = Input(shape=(6,))\n",
|
|
"layer_1a = Dense(2, activation='linear')(layer_0)\n",
|
|
"layer_1b = Dense(2, activation='linear')(layer_0)\n",
|
|
"layer_2 = merge([layer_1a, layer_1b], mode='concat', concat_axis=-1)\n",
|
|
"model = Model(input=layer_0, output=layer_2)\n",
|
|
"\n",
|
|
"W_1a = np.array([0.1, 0.4, 0.5, 0.1, 1, -2, 0, 0.3, 0.2, 0.1, 3, 0]).reshape((6, 2))\n",
|
|
"b_1a = np.array([0.5, 0.7])\n",
|
|
"W_1b = np.array([1, 0, -0.9, 0.6, -0.7, 0, 0.2, 0.4, 0, 0, -1, 2.3]).reshape((6, 2))\n",
|
|
"b_1b = np.array([0.1, -0.2])\n",
|
|
"model.set_weights([W_1a, b_1a, W_1b, b_1b])\n",
|
|
"\n",
|
|
"data_in = [0, 0.2, 0.5, -0.1, 1, 2]\n",
|
|
"data_in_shape = (6,)\n",
|
|
"print('in:', data_in)\n",
|
|
"print('in shape:', data_in_shape)\n",
|
|
"arr_in = np.array(data_in, dtype='float32').reshape(data_in_shape)\n",
|
|
"result = model.predict(np.array([arr_in]))\n",
|
|
"arr_out = result[0]\n",
|
|
"print('out shape:', arr_out.shape)\n",
|
|
"data_out = format_decimal(arr_out.ravel().tolist())\n",
|
|
"print('out:', data_out)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"**[core.Merge.5] mode: concat (2D, concatAxis=-1)**"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 22,
|
|
"metadata": {
|
|
"collapsed": false
|
|
},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"in: [0, 0.2, 0.5, -0.1, 1, 2]\n",
|
|
"in shape: (6,)\n",
|
|
"out shape: (3, 4)\n",
|
|
"out: [7.3, -0.21, -2.45, 4.48, 7.3, -0.21, -2.45, 4.48, 7.3, -0.21, -2.45, 4.48]\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"layer_0 = Input(shape=(6,))\n",
|
|
"layer_1a = Dense(2, activation='linear')(layer_0)\n",
|
|
"layer_1a = RepeatVector(3)(layer_1a)\n",
|
|
"layer_1b = Dense(2, activation='linear')(layer_0)\n",
|
|
"layer_1b = RepeatVector(3)(layer_1b)\n",
|
|
"layer_2 = merge([layer_1a, layer_1b], mode='concat', concat_axis=-1)\n",
|
|
"model = Model(input=layer_0, output=layer_2)\n",
|
|
"\n",
|
|
"W_1a = np.array([0.1, 0.4, 0.5, 0.1, 1, -2, 0, 0.3, 0.2, 0.1, 3, 0]).reshape((6, 2))\n",
|
|
"b_1a = np.array([0.5, 0.7])\n",
|
|
"W_1b = np.array([1, 0, -0.9, 0.6, -0.7, 0, 0.2, 0.4, 0, 0, -1, 2.3]).reshape((6, 2))\n",
|
|
"b_1b = np.array([0.1, -0.2])\n",
|
|
"model.set_weights([W_1a, b_1a, W_1b, b_1b])\n",
|
|
"\n",
|
|
"data_in = [0, 0.2, 0.5, -0.1, 1, 2]\n",
|
|
"data_in_shape = (6,)\n",
|
|
"print('in:', data_in)\n",
|
|
"print('in shape:', data_in_shape)\n",
|
|
"arr_in = np.array(data_in, dtype='float32').reshape(data_in_shape)\n",
|
|
"result = model.predict(np.array([arr_in]))\n",
|
|
"arr_out = result[0]\n",
|
|
"print('out shape:', arr_out.shape)\n",
|
|
"data_out = format_decimal(arr_out.ravel().tolist())\n",
|
|
"print('out:', data_out)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"**[core.Merge.6] mode: concat (2D, concatAxis=-2)**"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 23,
|
|
"metadata": {
|
|
"collapsed": false
|
|
},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"in: [0, 0.2, 0.5, -0.1, 1, 2]\n",
|
|
"in shape: (6,)\n",
|
|
"out shape: (6, 2)\n",
|
|
"out: [7.3, -0.21, 7.3, -0.21, 7.3, -0.21, -2.45, 4.48, -2.45, 4.48, -2.45, 4.48]\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"layer_0 = Input(shape=(6,))\n",
|
|
"layer_1a = Dense(2, activation='linear')(layer_0)\n",
|
|
"layer_1a = RepeatVector(3)(layer_1a)\n",
|
|
"layer_1b = Dense(2, activation='linear')(layer_0)\n",
|
|
"layer_1b = RepeatVector(3)(layer_1b)\n",
|
|
"layer_2 = merge([layer_1a, layer_1b], mode='concat', concat_axis=-2)\n",
|
|
"model = Model(input=layer_0, output=layer_2)\n",
|
|
"\n",
|
|
"W_1a = np.array([0.1, 0.4, 0.5, 0.1, 1, -2, 0, 0.3, 0.2, 0.1, 3, 0]).reshape((6, 2))\n",
|
|
"b_1a = np.array([0.5, 0.7])\n",
|
|
"W_1b = np.array([1, 0, -0.9, 0.6, -0.7, 0, 0.2, 0.4, 0, 0, -1, 2.3]).reshape((6, 2))\n",
|
|
"b_1b = np.array([0.1, -0.2])\n",
|
|
"model.set_weights([W_1a, b_1a, W_1b, b_1b])\n",
|
|
"\n",
|
|
"data_in = [0, 0.2, 0.5, -0.1, 1, 2]\n",
|
|
"data_in_shape = (6,)\n",
|
|
"print('in:', data_in)\n",
|
|
"print('in shape:', data_in_shape)\n",
|
|
"arr_in = np.array(data_in, dtype='float32').reshape(data_in_shape)\n",
|
|
"result = model.predict(np.array([arr_in]))\n",
|
|
"arr_out = result[0]\n",
|
|
"print('out shape:', arr_out.shape)\n",
|
|
"data_out = format_decimal(arr_out.ravel().tolist())\n",
|
|
"print('out:', data_out)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"**[core.Merge.7] mode: concat (2D, concatAxis=1)**"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 24,
|
|
"metadata": {
|
|
"collapsed": false
|
|
},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"in: [0, 0.2, 0.5, -0.1, 1, 2]\n",
|
|
"in shape: (6,)\n",
|
|
"out shape: (6, 2)\n",
|
|
"out: [7.3, -0.21, 7.3, -0.21, 7.3, -0.21, -2.45, 4.48, -2.45, 4.48, -2.45, 4.48]\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"layer_0 = Input(shape=(6,))\n",
|
|
"layer_1a = Dense(2, activation='linear')(layer_0)\n",
|
|
"layer_1a = RepeatVector(3)(layer_1a)\n",
|
|
"layer_1b = Dense(2, activation='linear')(layer_0)\n",
|
|
"layer_1b = RepeatVector(3)(layer_1b)\n",
|
|
"layer_2 = merge([layer_1a, layer_1b], mode='concat', concat_axis=1)\n",
|
|
"model = Model(input=layer_0, output=layer_2)\n",
|
|
"\n",
|
|
"W_1a = np.array([0.1, 0.4, 0.5, 0.1, 1, -2, 0, 0.3, 0.2, 0.1, 3, 0]).reshape((6, 2))\n",
|
|
"b_1a = np.array([0.5, 0.7])\n",
|
|
"W_1b = np.array([1, 0, -0.9, 0.6, -0.7, 0, 0.2, 0.4, 0, 0, -1, 2.3]).reshape((6, 2))\n",
|
|
"b_1b = np.array([0.1, -0.2])\n",
|
|
"model.set_weights([W_1a, b_1a, W_1b, b_1b])\n",
|
|
"\n",
|
|
"data_in = [0, 0.2, 0.5, -0.1, 1, 2]\n",
|
|
"data_in_shape = (6,)\n",
|
|
"print('in:', data_in)\n",
|
|
"print('in shape:', data_in_shape)\n",
|
|
"arr_in = np.array(data_in, dtype='float32').reshape(data_in_shape)\n",
|
|
"result = model.predict(np.array([arr_in]))\n",
|
|
"arr_out = result[0]\n",
|
|
"print('out shape:', arr_out.shape)\n",
|
|
"data_out = format_decimal(arr_out.ravel().tolist())\n",
|
|
"print('out:', data_out)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"**[core.Merge.8] mode: concat (2D, concatAxis=2)**"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 25,
|
|
"metadata": {
|
|
"collapsed": false
|
|
},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"in: [0, 0.2, 0.5, -0.1, 1, 2]\n",
|
|
"in shape: (6,)\n",
|
|
"out shape: (3, 4)\n",
|
|
"out: [7.3, -0.21, -2.45, 4.48, 7.3, -0.21, -2.45, 4.48, 7.3, -0.21, -2.45, 4.48]\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"layer_0 = Input(shape=(6,))\n",
|
|
"layer_1a = Dense(2, activation='linear')(layer_0)\n",
|
|
"layer_1a = RepeatVector(3)(layer_1a)\n",
|
|
"layer_1b = Dense(2, activation='linear')(layer_0)\n",
|
|
"layer_1b = RepeatVector(3)(layer_1b)\n",
|
|
"layer_2 = merge([layer_1a, layer_1b], mode='concat', concat_axis=2)\n",
|
|
"model = Model(input=layer_0, output=layer_2)\n",
|
|
"\n",
|
|
"W_1a = np.array([0.1, 0.4, 0.5, 0.1, 1, -2, 0, 0.3, 0.2, 0.1, 3, 0]).reshape((6, 2))\n",
|
|
"b_1a = np.array([0.5, 0.7])\n",
|
|
"W_1b = np.array([1, 0, -0.9, 0.6, -0.7, 0, 0.2, 0.4, 0, 0, -1, 2.3]).reshape((6, 2))\n",
|
|
"b_1b = np.array([0.1, -0.2])\n",
|
|
"model.set_weights([W_1a, b_1a, W_1b, b_1b])\n",
|
|
"\n",
|
|
"data_in = [0, 0.2, 0.5, -0.1, 1, 2]\n",
|
|
"data_in_shape = (6,)\n",
|
|
"print('in:', data_in)\n",
|
|
"print('in shape:', data_in_shape)\n",
|
|
"arr_in = np.array(data_in, dtype='float32').reshape(data_in_shape)\n",
|
|
"result = model.predict(np.array([arr_in]))\n",
|
|
"arr_out = result[0]\n",
|
|
"print('out shape:', arr_out.shape)\n",
|
|
"data_out = format_decimal(arr_out.ravel().tolist())\n",
|
|
"print('out:', data_out)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"**[core.Merge.9] mode: dot (2D x 2D, dotAxes=1)**"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 26,
|
|
"metadata": {
|
|
"collapsed": false
|
|
},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"in: [0, 0.2, 0.5, -0.1, 1, 2]\n",
|
|
"in shape: (6,)\n",
|
|
"out shape: (2, 2)\n",
|
|
"out: [-53.655003, 98.112007, 1.5435, -2.8224]\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"layer_0 = Input(shape=(6,))\n",
|
|
"layer_1a = Dense(2, activation='linear')(layer_0)\n",
|
|
"layer_1a = RepeatVector(3)(layer_1a)\n",
|
|
"layer_1b = Dense(2, activation='linear')(layer_0)\n",
|
|
"layer_1b = RepeatVector(3)(layer_1b)\n",
|
|
"layer_2 = merge([layer_1a, layer_1b], mode='dot', dot_axes=1)\n",
|
|
"model = Model(input=layer_0, output=layer_2)\n",
|
|
"\n",
|
|
"W_1a = np.array([0.1, 0.4, 0.5, 0.1, 1, -2, 0, 0.3, 0.2, 0.1, 3, 0]).reshape((6, 2))\n",
|
|
"b_1a = np.array([0.5, 0.7])\n",
|
|
"W_1b = np.array([1, 0, -0.9, 0.6, -0.7, 0, 0.2, 0.4, 0, 0, -1, 2.3]).reshape((6, 2))\n",
|
|
"b_1b = np.array([0.1, -0.2])\n",
|
|
"model.set_weights([W_1a, b_1a, W_1b, b_1b])\n",
|
|
"\n",
|
|
"data_in = [0, 0.2, 0.5, -0.1, 1, 2]\n",
|
|
"data_in_shape = (6,)\n",
|
|
"print('in:', data_in)\n",
|
|
"print('in shape:', data_in_shape)\n",
|
|
"arr_in = np.array(data_in, dtype='float32').reshape(data_in_shape)\n",
|
|
"result = model.predict(np.array([arr_in]))\n",
|
|
"arr_out = result[0]\n",
|
|
"print('out shape:', arr_out.shape)\n",
|
|
"data_out = format_decimal(arr_out.ravel().tolist())\n",
|
|
"print('out:', data_out)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"**[core.Merge.10] mode: dot (2D x 2D, dotAxes=2)**"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 27,
|
|
"metadata": {
|
|
"collapsed": false
|
|
},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"in: [0, 0.2, 0.5, -0.1, 1, 2]\n",
|
|
"in shape: (6,)\n",
|
|
"out shape: (3, 3)\n",
|
|
"out: [-18.8258, -18.8258, -18.8258, -18.8258, -18.8258, -18.8258, -18.8258, -18.8258, -18.8258]\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"layer_0 = Input(shape=(6,))\n",
|
|
"layer_1a = Dense(2, activation='linear')(layer_0)\n",
|
|
"layer_1a = RepeatVector(3)(layer_1a)\n",
|
|
"layer_1b = Dense(2, activation='linear')(layer_0)\n",
|
|
"layer_1b = RepeatVector(3)(layer_1b)\n",
|
|
"layer_2 = merge([layer_1a, layer_1b], mode='dot', dot_axes=2)\n",
|
|
"model = Model(input=layer_0, output=layer_2)\n",
|
|
"\n",
|
|
"W_1a = np.array([0.1, 0.4, 0.5, 0.1, 1, -2, 0, 0.3, 0.2, 0.1, 3, 0]).reshape((6, 2))\n",
|
|
"b_1a = np.array([0.5, 0.7])\n",
|
|
"W_1b = np.array([1, 0, -0.9, 0.6, -0.7, 0, 0.2, 0.4, 0, 0, -1, 2.3]).reshape((6, 2))\n",
|
|
"b_1b = np.array([0.1, -0.2])\n",
|
|
"model.set_weights([W_1a, b_1a, W_1b, b_1b])\n",
|
|
"\n",
|
|
"data_in = [0, 0.2, 0.5, -0.1, 1, 2]\n",
|
|
"data_in_shape = (6,)\n",
|
|
"print('in:', data_in)\n",
|
|
"print('in shape:', data_in_shape)\n",
|
|
"arr_in = np.array(data_in, dtype='float32').reshape(data_in_shape)\n",
|
|
"result = model.predict(np.array([arr_in]))\n",
|
|
"arr_out = result[0]\n",
|
|
"print('out shape:', arr_out.shape)\n",
|
|
"data_out = format_decimal(arr_out.ravel().tolist())\n",
|
|
"print('out:', data_out)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"**[core.Merge.11] mode: cos (2D x 2D, dotAxes=1)**"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 28,
|
|
"metadata": {
|
|
"collapsed": false
|
|
},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"in: [0, 0.2, 0.5, -0.1, 1, 2]\n",
|
|
"in shape: (6,)\n",
|
|
"out shape: (1, 2, 2)\n",
|
|
"out: [-1.0, 7.972744, 0.125427, -1.0]\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"layer_0 = Input(shape=(6,))\n",
|
|
"layer_1a = Dense(2, activation='linear')(layer_0)\n",
|
|
"layer_1a = RepeatVector(3)(layer_1a)\n",
|
|
"layer_1b = Dense(2, activation='linear')(layer_0)\n",
|
|
"layer_1b = RepeatVector(3)(layer_1b)\n",
|
|
"layer_2 = merge([layer_1a, layer_1b], mode='cos', dot_axes=1)\n",
|
|
"model = Model(input=layer_0, output=layer_2)\n",
|
|
"\n",
|
|
"W_1a = np.array([0.1, 0.4, 0.5, 0.1, 1, -2, 0, 0.3, 0.2, 0.1, 3, 0]).reshape((6, 2))\n",
|
|
"b_1a = np.array([0.5, 0.7])\n",
|
|
"W_1b = np.array([1, 0, -0.9, 0.6, -0.7, 0, 0.2, 0.4, 0, 0, -1, 2.3]).reshape((6, 2))\n",
|
|
"b_1b = np.array([0.1, -0.2])\n",
|
|
"model.set_weights([W_1a, b_1a, W_1b, b_1b])\n",
|
|
"\n",
|
|
"data_in = [0, 0.2, 0.5, -0.1, 1, 2]\n",
|
|
"data_in_shape = (6,)\n",
|
|
"print('in:', data_in)\n",
|
|
"print('in shape:', data_in_shape)\n",
|
|
"arr_in = np.array(data_in, dtype='float32').reshape(data_in_shape)\n",
|
|
"result = model.predict(np.array([arr_in]))\n",
|
|
"arr_out = result[0]\n",
|
|
"print('out shape:', arr_out.shape)\n",
|
|
"data_out = format_decimal(arr_out.ravel().tolist())\n",
|
|
"print('out:', data_out)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"**[core.Merge.12] mode: cos (2D x 2D, dotAxes=2)**"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 29,
|
|
"metadata": {
|
|
"collapsed": false
|
|
},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"in: [0, 0.2, 0.5, -0.1, 1, 2]\n",
|
|
"in shape: (6,)\n",
|
|
"out shape: (1, 3, 3)\n",
|
|
"out: [-0.504843, -0.504843, -0.504843, -0.504843, -0.504843, -0.504843, -0.504843, -0.504843, -0.504843]\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"layer_0 = Input(shape=(6,))\n",
|
|
"layer_1a = Dense(2, activation='linear')(layer_0)\n",
|
|
"layer_1a = RepeatVector(3)(layer_1a)\n",
|
|
"layer_1b = Dense(2, activation='linear')(layer_0)\n",
|
|
"layer_1b = RepeatVector(3)(layer_1b)\n",
|
|
"layer_2 = merge([layer_1a, layer_1b], mode='cos', dot_axes=2)\n",
|
|
"model = Model(input=layer_0, output=layer_2)\n",
|
|
"\n",
|
|
"W_1a = np.array([0.1, 0.4, 0.5, 0.1, 1, -2, 0, 0.3, 0.2, 0.1, 3, 0]).reshape((6, 2))\n",
|
|
"b_1a = np.array([0.5, 0.7])\n",
|
|
"W_1b = np.array([1, 0, -0.9, 0.6, -0.7, 0, 0.2, 0.4, 0, 0, -1, 2.3]).reshape((6, 2))\n",
|
|
"b_1b = np.array([0.1, -0.2])\n",
|
|
"model.set_weights([W_1a, b_1a, W_1b, b_1b])\n",
|
|
"\n",
|
|
"data_in = [0, 0.2, 0.5, -0.1, 1, 2]\n",
|
|
"data_in_shape = (6,)\n",
|
|
"print('in:', data_in)\n",
|
|
"print('in shape:', data_in_shape)\n",
|
|
"arr_in = np.array(data_in, dtype='float32').reshape(data_in_shape)\n",
|
|
"result = model.predict(np.array([arr_in]))\n",
|
|
"arr_out = result[0]\n",
|
|
"print('out shape:', arr_out.shape)\n",
|
|
"data_out = format_decimal(arr_out.ravel().tolist())\n",
|
|
"print('out:', data_out)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {
|
|
"collapsed": true
|
|
},
|
|
"outputs": [],
|
|
"source": []
|
|
}
|
|
],
|
|
"metadata": {
|
|
"anaconda-cloud": {},
|
|
"kernelspec": {
|
|
"display_name": "Python [default]",
|
|
"language": "python",
|
|
"name": "python3"
|
|
},
|
|
"language_info": {
|
|
"codemirror_mode": {
|
|
"name": "ipython",
|
|
"version": 3
|
|
},
|
|
"file_extension": ".py",
|
|
"mimetype": "text/x-python",
|
|
"name": "python",
|
|
"nbconvert_exporter": "python",
|
|
"pygments_lexer": "ipython3",
|
|
"version": "3.5.2"
|
|
}
|
|
},
|
|
"nbformat": 4,
|
|
"nbformat_minor": 0
|
|
}
|