diff --git a/notebooks/convolutional/Convolution1D.ipynb b/notebooks/convolutional/Convolution1D.ipynb index ae560f7..41a9083 100644 --- a/notebooks/convolutional/Convolution1D.ipynb +++ b/notebooks/convolutional/Convolution1D.ipynb @@ -270,6 +270,61 @@ "print('out:', format_decimal(result[0].ravel().tolist()))" ] }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": { + "ExecuteTime": { + "end_time": "2016-11-07T13:56:00.927837", + "start_time": "2016-11-07T13:56:00.641182" + }, + "collapsed": false + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "W shape: (3, 1, 1, 3)\n", + "W: [0.895265, -0.546905, 0.18884, -0.143383, 0.528281, -0.994279, -0.285153, 0.81939, -0.087838]\n", + "b shape: (3,)\n", + "b: [0.895265, -0.546905, 0.18884]\n", + "\n", + "in shape: (16, 1)\n", + "in: [-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]\n", + "out shape: (16, 3)\n", + "out: [0.765182, -0.189783, 0.284999, 0.974674, -1.004109, -0.27616, 1.59209, -1.594735, 1.302239, -0.187643, 0.51763, 0.212628, 0.547538, -0.030059, -0.671994, 1.366655, -0.251868, 0.346268, 0.468956, 0.612206, -0.850375, 1.375417, 0.110722, -0.445086, 1.172261, 0.258435, -0.713268, 1.756087, -0.953023, -0.4348, 1.512045, -0.650102, 0.678265, 0.66054, -0.587236, -0.507005, 1.460728, -0.857683, 1.02261, 0.277446, -0.239732, -0.478271, 1.70853, -1.784897, 0.8638, 0.564381, -0.700825, 0.897929]\n" + ] + } + ], + "source": [ + "data_in_shape = (16, 1)\n", + "conv = Convolution1D(3, 3, activation='linear', 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(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": "code", "execution_count": null, @@ -279,6 +334,9 @@ "outputs": [], "source": [] } + + + ], "metadata": { "kernelspec": { diff --git a/test/convolutional/Convolution1D.js b/test/convolutional/Convolution1D.js index 368d432..83f95aa 100644 --- a/test/convolutional/Convolution1D.js +++ b/test/convolutional/Convolution1D.js @@ -28,7 +28,12 @@ describe('convolutional layer: Convolution1D', function () { inputShape: [8, 3], kernelShape: [2, 7], attrs: { activation: 'tanh', borderMode: 'same', subsampleLength: 1, bias: true } - } + }, + { + inputShape: [16, 1], + kernelShape: [3, 3], + attrs: { activation: 'linear', borderMode: 'same', subsampleLength: 1, bias: true } + }, ] before(function () { diff --git a/test/convolutional/data_Convolution1D.js b/test/convolutional/data_Convolution1D.js index c97e6d5..9dfd3f1 100644 --- a/test/convolutional/data_Convolution1D.js +++ b/test/convolutional/data_Convolution1D.js @@ -83,7 +83,27 @@ data: [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], shape: [8, 2] } - } + }, + 'convolutional.Convolution1D.3': { + input: { + data: [-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], + shape: [16, 1] + }, + weights: [ + { + data: [0.895265, -0.546905, 0.18884, -0.143383, 0.528281, -0.994279, -0.285153, 0.81939, -0.087838], + shape: [3, 1, 1, 3] + }, + { + data: [0.895265, -0.546905, 0.18884], + shape: [3] + } + ], + expected: { + data: [0.765182, -0.189783, 0.284999, 0.974674, -1.004109, -0.27616, 1.59209, -1.594735, 1.302239, -0.187643, 0.51763, 0.212628, 0.547538, -0.030059, -0.671994, 1.366655, -0.251868, 0.346268, 0.468956, 0.612206, -0.850375, 1.375417, 0.110722, -0.445086, 1.172261, 0.258435, -0.713268, 1.756087, -0.953023, -0.4348, 1.512045, -0.650102, 0.678265, 0.66054, -0.587236, -0.507005, 1.460728, -0.857683, 1.02261, 0.277446, -0.239732, -0.478271, 1.70853, -1.784897, 0.8638, 0.564381, -0.700825, 0.897929], + shape: [16, 3] + } + } } window.TEST_DATA = Object.assign({}, window.TEST_DATA, DATA)