mirror of
https://github.com/wassname/keras-js.git
synced 2026-09-09 11:25:25 +08:00
305 lines
11 KiB
Plaintext
305 lines
11 KiB
Plaintext
{
|
|
"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.convolutional import Convolution1D\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": [
|
|
"### Convolution1D"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"**[convolutional.Convolution1D.0] 4 length 3 filters on 5x2 input, activation='linear', border_mode='valid', subsample_length=1, bias=True**"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 15,
|
|
"metadata": {
|
|
"collapsed": false
|
|
},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"W shape: (4, 2, 3, 1)\n",
|
|
"W: [0.895265, -0.546905, 0.18884, -0.143383, 0.528281, -0.994279, -0.285153, 0.81939, -0.087838, 0.963605, 0.734714, 0.972055, 0.846533, -0.392613, 0.692207, -0.757556, 0.571153, -0.49899, -0.807941, 0.886982, 0.6521, 0.03665, 0.747001, 0.156751]\n",
|
|
"b shape: (4,)\n",
|
|
"b: [0.895265, -0.546905, 0.18884, -0.143383]\n",
|
|
"\n",
|
|
"in shape: (5, 2)\n",
|
|
"in: [0.528281, -0.994279, -0.285153, 0.81939, -0.087838, 0.963605, 0.734714, 0.972055, 0.846533, -0.392613]\n",
|
|
"out shape: (3, 4)\n",
|
|
"out: [1.124918, -0.342879, 1.42759, -0.153716, 0.251835, 1.840331, -0.064904, 1.390416, 1.340388, 1.266877, 0.433117, 1.831188]\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"data_in_shape = (5, 2)\n",
|
|
"conv = Convolution1D(4, 3, activation='linear', border_mode='valid', subsample_length=1, bias=True)\n",
|
|
"\n",
|
|
"layer_0 = Input(shape=data_in_shape)\n",
|
|
"layer_1 = conv(layer_0)\n",
|
|
"model = Model(input=layer_0, output=layer_1)\n",
|
|
"\n",
|
|
"# set weights to random (use seed for reproducibility)\n",
|
|
"weights = []\n",
|
|
"for w in model.get_weights():\n",
|
|
" np.random.seed(200)\n",
|
|
" weights.append(2 * np.random.random(w.shape) - 1)\n",
|
|
"model.set_weights(weights)\n",
|
|
"print('W shape:', weights[0].shape)\n",
|
|
"print('W:', format_decimal(weights[0].ravel().tolist()))\n",
|
|
"print('b shape:', weights[1].shape)\n",
|
|
"print('b:', format_decimal(weights[1].ravel().tolist()))\n",
|
|
"\n",
|
|
"data_in = 2 * np.random.random(data_in_shape) - 1\n",
|
|
"print('')\n",
|
|
"print('in shape:', data_in_shape)\n",
|
|
"print('in:', format_decimal(data_in.ravel().tolist()))\n",
|
|
"result = model.predict(np.array([data_in]))\n",
|
|
"print('out shape:', result[0].shape)\n",
|
|
"print('out:', format_decimal(result[0].ravel().tolist()))"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"**[convolutional.Convolution1D.1] 4 length 3 filters on 6x3 input, activation='linear', border_mode='valid', subsample_length=1, bias=False**"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 16,
|
|
"metadata": {
|
|
"collapsed": false
|
|
},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"W shape: (4, 3, 3, 1)\n",
|
|
"W: [-0.772191, 0.495762, 0.222119, 0.619384, 0.425715, -0.719926, -0.464976, -0.704791, -0.543864, -0.528877, 0.380048, -0.703304, -0.108788, 0.401685, -0.806723, 0.765265, 0.739665, 0.689188, -0.452596, 0.571359, 0.402272, -0.010539, 0.672675, -0.191632, -0.653554, -0.269196, 0.994178, -0.318691, 0.010759, 0.078695, -0.501326, 0.625487, -0.614715, -0.839499, -0.811676, -0.300069]\n",
|
|
"\n",
|
|
"in shape: (6, 3)\n",
|
|
"in: [0.57107, 0.361384, -0.924121, -0.417132, -0.39254, 0.967698, -0.674584, 0.924125, 0.403362, 0.417301, 0.795356, -0.367641, -0.398474, 0.889135, -0.81216, 0.383587, 0.922044, 0.427167]\n",
|
|
"out shape: (4, 4)\n",
|
|
"out: [-1.877892, -0.642056, -0.468639, -1.365037, -0.876249, 0.22855, -0.661939, -0.585044, 1.423414, -0.225706, -0.233745, -0.120764, 0.283789, -1.702796, 1.034372, 0.323189]\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"data_in_shape = (6, 3)\n",
|
|
"conv = Convolution1D(4, 3, activation='linear', border_mode='valid', subsample_length=1, bias=False)\n",
|
|
"\n",
|
|
"layer_0 = Input(shape=data_in_shape)\n",
|
|
"layer_1 = conv(layer_0)\n",
|
|
"model = Model(input=layer_0, output=layer_1)\n",
|
|
"\n",
|
|
"# set weights to random (use seed for reproducibility)\n",
|
|
"weights = []\n",
|
|
"for w in model.get_weights():\n",
|
|
" np.random.seed(201)\n",
|
|
" weights.append(2 * np.random.random(w.shape) - 1)\n",
|
|
"model.set_weights(weights)\n",
|
|
"print('W shape:', weights[0].shape)\n",
|
|
"print('W:', format_decimal(weights[0].ravel().tolist()))\n",
|
|
"\n",
|
|
"data_in = 2 * np.random.random(data_in_shape) - 1\n",
|
|
"print('')\n",
|
|
"print('in shape:', data_in_shape)\n",
|
|
"print('in:', format_decimal(data_in.ravel().tolist()))\n",
|
|
"result = model.predict(np.array([data_in]))\n",
|
|
"print('out shape:', result[0].shape)\n",
|
|
"print('out:', format_decimal(result[0].ravel().tolist()))"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"**[convolutional.Convolution1D.2] 2 length 3 filters on 4x6 input, activation='sigmoid', border_mode='valid', subsample_length=2, bias=True**"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 17,
|
|
"metadata": {
|
|
"collapsed": false
|
|
},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"W shape: (2, 6, 3, 1)\n",
|
|
"W: [0.895265, -0.546905, 0.18884, -0.143383, 0.528281, -0.994279, -0.285153, 0.81939, -0.087838, 0.963605, 0.734714, 0.972055, 0.846533, -0.392613, 0.692207, -0.757556, 0.571153, -0.49899, -0.807941, 0.886982, 0.6521, 0.03665, 0.747001, 0.156751, -0.099831, 0.360314, -0.161149, 0.280787, 0.217313, -0.789132, 0.932089, 0.517401, 0.359284, -0.341304, -0.94709, 0.607321]\n",
|
|
"b shape: (2,)\n",
|
|
"b: [0.895265, -0.546905]\n",
|
|
"\n",
|
|
"in shape: (4, 6)\n",
|
|
"in: [0.18884, -0.143383, 0.528281, -0.994279, -0.285153, 0.81939, -0.087838, 0.963605, 0.734714, 0.972055, 0.846533, -0.392613, 0.692207, -0.757556, 0.571153, -0.49899, -0.807941, 0.886982, 0.6521, 0.03665, 0.747001, 0.156751, -0.099831, 0.360314]\n",
|
|
"out shape: (2, 2)\n",
|
|
"out: [0.444624, 0.773535, 0.564385, 0.133453]\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"data_in_shape = (4, 6)\n",
|
|
"conv = Convolution1D(2, 3, activation='sigmoid', border_mode='same', subsample_length=2, bias=True)\n",
|
|
"\n",
|
|
"layer_0 = Input(shape=data_in_shape)\n",
|
|
"layer_1 = conv(layer_0)\n",
|
|
"model = Model(input=layer_0, output=layer_1)\n",
|
|
"\n",
|
|
"# set weights to random (use seed for reproducibility)\n",
|
|
"weights = []\n",
|
|
"for w in model.get_weights():\n",
|
|
" np.random.seed(200)\n",
|
|
" weights.append(2 * np.random.random(w.shape) - 1)\n",
|
|
"model.set_weights(weights)\n",
|
|
"print('W shape:', weights[0].shape)\n",
|
|
"print('W:', format_decimal(weights[0].ravel().tolist()))\n",
|
|
"print('b shape:', weights[1].shape)\n",
|
|
"print('b:', format_decimal(weights[1].ravel().tolist()))\n",
|
|
"\n",
|
|
"data_in = 2 * np.random.random(data_in_shape) - 1\n",
|
|
"print('')\n",
|
|
"print('in shape:', data_in_shape)\n",
|
|
"print('in:', format_decimal(data_in.ravel().tolist()))\n",
|
|
"result = model.predict(np.array([data_in]))\n",
|
|
"print('out shape:', result[0].shape)\n",
|
|
"print('out:', format_decimal(result[0].ravel().tolist()))"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"**[convolutional.Convolution1D.3] 2 length 7 filters on 8x3 input, activation='tanh', border_mode='same', subsample_length=1, bias=True**"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 18,
|
|
"metadata": {
|
|
"collapsed": false
|
|
},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"W shape: (2, 3, 7, 1)\n",
|
|
"W: [0.861113, -0.237594, 0.330694, 0.998309, 0.786447, 0.538158, -0.228315, 0.217332, -0.475436, -0.018066, -0.489741, -0.522387, 0.79989, 0.27058, -0.683115, -0.650208, 0.259853, -0.509243, 0.958185, 0.089546, 0.739799, 0.114385, -0.378872, -0.168716, 0.302124, 0.850416, -0.984343, 0.839927, -0.895196, 0.303711, 0.128826, 0.058159, 0.254989, -0.759101, 0.793844, 0.647309, 0.252074, 0.075576, -0.859305, 0.952613, -0.053285, -0.677361]\n",
|
|
"b shape: (2,)\n",
|
|
"b: [0.861113, -0.237594]\n",
|
|
"\n",
|
|
"in shape: (8, 3)\n",
|
|
"in: [0.330694, 0.998309, 0.786447, 0.538158, -0.228315, 0.217332, -0.475436, -0.018066, -0.489741, -0.522387, 0.79989, 0.27058, -0.683115, -0.650208, 0.259853, -0.509243, 0.958185, 0.089546, 0.739799, 0.114385, -0.378872, -0.168716, 0.302124, 0.850416]\n",
|
|
"out shape: (8, 2)\n",
|
|
"out: [0.854959, 0.355561, 0.888899, -0.9834, -0.825345, 0.941694, -0.490434, -0.699848, 0.872523, -0.887886, -0.408331, -0.018902, 0.959544, 0.66633, -0.779488, 0.038157]\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"data_in_shape = (8, 3)\n",
|
|
"conv = Convolution1D(2, 7, activation='tanh', border_mode='same', subsample_length=1, bias=True)\n",
|
|
"\n",
|
|
"layer_0 = Input(shape=data_in_shape)\n",
|
|
"layer_1 = conv(layer_0)\n",
|
|
"model = Model(input=layer_0, output=layer_1)\n",
|
|
"\n",
|
|
"# set weights to random (use seed for reproducibility)\n",
|
|
"weights = []\n",
|
|
"for w in model.get_weights():\n",
|
|
" np.random.seed(204)\n",
|
|
" weights.append(2 * np.random.random(w.shape) - 1)\n",
|
|
"model.set_weights(weights)\n",
|
|
"print('W shape:', weights[0].shape)\n",
|
|
"print('W:', format_decimal(weights[0].ravel().tolist()))\n",
|
|
"print('b shape:', weights[1].shape)\n",
|
|
"print('b:', format_decimal(weights[1].ravel().tolist()))\n",
|
|
"\n",
|
|
"data_in = 2 * np.random.random(data_in_shape) - 1\n",
|
|
"print('')\n",
|
|
"print('in shape:', data_in_shape)\n",
|
|
"print('in:', format_decimal(data_in.ravel().tolist()))\n",
|
|
"result = model.predict(np.array([data_in]))\n",
|
|
"print('out shape:', result[0].shape)\n",
|
|
"print('out:', format_decimal(result[0].ravel().tolist()))"
|
|
]
|
|
},
|
|
{
|
|
"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
|
|
}
|