reorganize jupyter notebooks for core layers

This commit is contained in:
Leon Chen
2016-08-25 23:35:06 -04:00
parent 0c7e51801c
commit efb2280e2c
9 changed files with 2059 additions and 1600 deletions
-1600
View File
File diff suppressed because it is too large Load Diff
+170
View File
@@ -0,0 +1,170 @@
{
"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.layers import Input\n",
"from keras.layers.core import Dense, Activation\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": [
"### Activation"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**[core.Activation.0] test 1 (tanh)**"
]
},
{
"cell_type": "code",
"execution_count": 6,
"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: [0.999999, -0.206966]\n"
]
}
],
"source": [
"layer_0 = Input(shape=(6,))\n",
"layer_1 = Dense(2)(layer_0)\n",
"layer_2 = Activation('tanh')(layer_1)\n",
"model = Model(input=layer_0, output=layer_2)\n",
"\n",
"W = 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 = np.array([0.5, 0.7])\n",
"model.set_weights([W, b])\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.Activation.1] test 2 (hardSigmoid)**"
]
},
{
"cell_type": "code",
"execution_count": 7,
"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: [1.0, 0.458]\n"
]
}
],
"source": [
"layer_0 = Input(shape=(6,))\n",
"layer_1 = Dense(2)(layer_0)\n",
"layer_2 = Activation('hard_sigmoid')(layer_1)\n",
"model = Model(input=layer_0, output=layer_2)\n",
"\n",
"W = 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 = np.array([0.5, 0.7])\n",
"model.set_weights([W, b])\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": {
"kernelspec": {
"display_name": "Python 3",
"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
}
+350
View File
@@ -0,0 +1,350 @@
{
"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.layers import Input\n",
"from keras.layers.core import Dense\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": [
"### Dense"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**[core.Dense.0] test 1**"
]
},
{
"cell_type": "code",
"execution_count": 3,
"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, -0.21]\n"
]
}
],
"source": [
"layer_0 = Input(shape=(6,))\n",
"layer_1 = Dense(2, activation='linear')(layer_0)\n",
"model = Model(input=layer_0, output=layer_1)\n",
"\n",
"W = 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 = np.array([0.5, 0.7])\n",
"model.set_weights([W, b])\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.Dense.1] test 2 (with sigmoid activation)**"
]
},
{
"cell_type": "code",
"execution_count": 4,
"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: [0.999325, 0.447692]\n"
]
}
],
"source": [
"layer_0 = Input(shape=(6,))\n",
"layer_1 = Dense(2, activation='sigmoid')(layer_0)\n",
"model = Model(input=layer_0, output=layer_1)\n",
"\n",
"W = 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 = np.array([0.5, 0.7])\n",
"model.set_weights([W, b])\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.Dense.2] test 3 (with softplus activation and no bias)**"
]
},
{
"cell_type": "code",
"execution_count": 5,
"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: [6.801113, 0.338274]\n"
]
}
],
"source": [
"layer_0 = Input(shape=(6,))\n",
"layer_1 = Dense(2, activation='softplus', bias=False)(layer_0)\n",
"model = Model(input=layer_0, output=layer_1)\n",
"\n",
"W = 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",
"model.set_weights([W])\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.Dense.3] [GPU] test 1**"
]
},
{
"cell_type": "code",
"execution_count": 3,
"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, -0.21]\n"
]
}
],
"source": [
"layer_0 = Input(shape=(6,))\n",
"layer_1 = Dense(2, activation='linear')(layer_0)\n",
"model = Model(input=layer_0, output=layer_1)\n",
"\n",
"W = 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 = np.array([0.5, 0.7])\n",
"model.set_weights([W, b])\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.Dense.4] [GPU] test 2 (with sigmoid activation)**"
]
},
{
"cell_type": "code",
"execution_count": 4,
"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: [0.999325, 0.447692]\n"
]
}
],
"source": [
"layer_0 = Input(shape=(6,))\n",
"layer_1 = Dense(2, activation='sigmoid')(layer_0)\n",
"model = Model(input=layer_0, output=layer_1)\n",
"\n",
"W = 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 = np.array([0.5, 0.7])\n",
"model.set_weights([W, b])\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.Dense.5] [GPU] test 3 (with softplus activation and no bias)**"
]
},
{
"cell_type": "code",
"execution_count": 5,
"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: [6.801113, 0.338274]\n"
]
}
],
"source": [
"layer_0 = Input(shape=(6,))\n",
"layer_1 = Dense(2, activation='softplus', bias=False)(layer_0)\n",
"model = Model(input=layer_0, output=layer_1)\n",
"\n",
"W = 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",
"model.set_weights([W])\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": {
"kernelspec": {
"display_name": "Python 3",
"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
}
+123
View File
@@ -0,0 +1,123 @@
{
"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.layers import Input\n",
"from keras.layers.core import Dense, Dropout\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": [
"### Dropout"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**[core.Dropout.0] should pass through**"
]
},
{
"cell_type": "code",
"execution_count": 8,
"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, -0.21]\n"
]
}
],
"source": [
"layer_0 = Input(shape=(6,))\n",
"layer_1 = Dense(2)(layer_0)\n",
"layer_2 = Dropout(0.5)(layer_1)\n",
"model = Model(input=layer_0, output=layer_2)\n",
"\n",
"W = 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 = np.array([0.5, 0.7])\n",
"model.set_weights([W, b])\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": {
"kernelspec": {
"display_name": "Python 3",
"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
}
+193
View File
@@ -0,0 +1,193 @@
{
"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.layers import Input\n",
"from keras.layers.core import Flatten\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": [
"### Flatten"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**[core.Flatten.0] 1D**"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"# layer_0 = Input(shape=(6,))\n",
"# layer_1 = Flatten()(layer_0)\n",
"# model = Model(input=layer_0, output=layer_1)\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)\n",
"\n",
"# exception with TF"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**[core.Flatten.1] 2D**"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"in: [0, 0.2, 0.5, -0.1, 1, 2]\n",
"in shape: (3, 2)\n",
"out shape: (6,)\n",
"out: [0.0, 0.2, 0.5, -0.1, 1.0, 2.0]\n"
]
}
],
"source": [
"layer_0 = Input(shape=(3, 2))\n",
"layer_1 = Flatten()(layer_0)\n",
"model = Model(input=layer_0, output=layer_1)\n",
"\n",
"data_in = [0, 0.2, 0.5, -0.1, 1, 2]\n",
"data_in_shape = (3, 2)\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.Flatten.2] 3D**"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"in: [0, 0.2, 0.5, -0.1, 1, 2, 0, 0.2, 0.5, -0.1, 1, 2]\n",
"in shape: (3, 2, 2)\n",
"out shape: (12,)\n",
"out: [0.0, 0.2, 0.5, -0.1, 1.0, 2.0, 0.0, 0.2, 0.5, -0.1, 1.0, 2.0]\n"
]
}
],
"source": [
"layer_0 = Input(shape=(3, 2, 2))\n",
"layer_1 = Flatten()(layer_0)\n",
"model = Model(input=layer_0, output=layer_1)\n",
"\n",
"data_in = [0, 0.2, 0.5, -0.1, 1, 2, 0, 0.2, 0.5, -0.1, 1, 2]\n",
"data_in_shape = (3, 2, 2)\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": {
"kernelspec": {
"display_name": "Python 3",
"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
}
+743
View File
@@ -0,0 +1,743 @@
{
"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": {
"kernelspec": {
"display_name": "Python 3",
"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
}
+160
View File
@@ -0,0 +1,160 @@
{
"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.layers import Input\n",
"from keras.layers.core import Permute\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": [
"### Permute"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**[core.Permute.0] shape [3, 2] -> [2, 3]**"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"in: [0, 0.2, 0.5, -0.1, 1, 2]\n",
"in shape: (3, 2)\n",
"out shape: (2, 3)\n",
"out: [0.0, 0.5, 1.0, 0.2, -0.1, 2.0]\n"
]
}
],
"source": [
"layer_0 = Input(shape=(3, 2))\n",
"layer_1 = Permute((2, 1))(layer_0)\n",
"model = Model(input=layer_0, output=layer_1)\n",
"\n",
"data_in = [0, 0.2, 0.5, -0.1, 1, 2]\n",
"data_in_shape = (3, 2)\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.Permute.1] shape [2, 3, 4] -> [4, 3, 2]**"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"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]\n",
"in shape: (2, 3, 4)\n",
"out shape: (4, 3, 2)\n",
"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]\n"
]
}
],
"source": [
"layer_0 = Input(shape=(2, 3, 4))\n",
"layer_1 = Permute((3, 2, 1))(layer_0)\n",
"model = Model(input=layer_0, output=layer_1)\n",
"\n",
"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]\n",
"data_in_shape = (2, 3, 4)\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": {
"kernelspec": {
"display_name": "Python 3",
"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
}
+118
View File
@@ -0,0 +1,118 @@
{
"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.layers import Input\n",
"from keras.layers.core import RepeatVector\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": [
"### RepeatVector"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**[core.RepeatVector.0] repeat vector, shape [6] -> [7, 6]**"
]
},
{
"cell_type": "code",
"execution_count": 16,
"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: (7, 6)\n",
"out: [0.0, 0.2, 0.5, -0.1, 1.0, 2.0, 0.0, 0.2, 0.5, -0.1, 1.0, 2.0, 0.0, 0.2, 0.5, -0.1, 1.0, 2.0, 0.0, 0.2, 0.5, -0.1, 1.0, 2.0, 0.0, 0.2, 0.5, -0.1, 1.0, 2.0, 0.0, 0.2, 0.5, -0.1, 1.0, 2.0, 0.0, 0.2, 0.5, -0.1, 1.0, 2.0]\n"
]
}
],
"source": [
"layer_0 = Input(shape=(6,))\n",
"layer_1 = RepeatVector(7)(layer_0)\n",
"model = Model(input=layer_0, output=layer_1)\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": {
"kernelspec": {
"display_name": "Python 3",
"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
}
+202
View File
@@ -0,0 +1,202 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"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.layers import Input\n",
"from keras.layers.core import Reshape\n",
"from keras import backend as K"
]
},
{
"cell_type": "code",
"execution_count": 2,
"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": [
"### Reshape"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**[core.Reshape.0] shape [6] -> [2, 3]**"
]
},
{
"cell_type": "code",
"execution_count": 3,
"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, 3)\n",
"out: [0.0, 0.2, 0.5, -0.1, 1.0, 2.0]\n"
]
}
],
"source": [
"layer_0 = Input(shape=(6,))\n",
"layer_1 = Reshape((2, 3))(layer_0)\n",
"model = Model(input=layer_0, output=layer_1)\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.Reshape.1] shape [3, 2] -> [6]**"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"in: [0, 0.2, 0.5, -0.1, 1, 2]\n",
"in shape: (3, 2)\n",
"out shape: (6,)\n",
"out: [0.0, 0.2, 0.5, -0.1, 1.0, 2.0]\n"
]
}
],
"source": [
"layer_0 = Input(shape=(3, 2))\n",
"layer_1 = Reshape((6,))(layer_0)\n",
"model = Model(input=layer_0, output=layer_1)\n",
"\n",
"data_in = [0, 0.2, 0.5, -0.1, 1, 2]\n",
"data_in_shape = (3, 2)\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.Reshape.2] shape [3, 2, 2] -> [4, 3]**"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"in: [0, 0.2, 0.5, -0.1, 1, 2, 0, 0.2, 0.5, -0.1, 1, 2]\n",
"in shape: (3, 2, 2)\n",
"out shape: (4, 3)\n",
"out: [0.0, 0.2, 0.5, -0.1, 1.0, 2.0, 0.0, 0.2, 0.5, -0.1, 1.0, 2.0]\n"
]
}
],
"source": [
"layer_0 = Input(shape=(3, 2, 2))\n",
"layer_1 = Reshape((4, 3))(layer_0)\n",
"model = Model(input=layer_0, output=layer_1)\n",
"\n",
"data_in = [0, 0.2, 0.5, -0.1, 1, 2, 0, 0.2, 0.5, -0.1, 1, 2]\n",
"data_in_shape = (3, 2, 2)\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": {
"kernelspec": {
"display_name": "Python 3",
"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
}