Skip to content

Commit

Permalink
Add Cropping1D,2D,3D (transcranial#28)
Browse files Browse the repository at this point in the history
* Added failing test for convolutional1D with input (16,1)

* bugfix

* Add Cropping1D,2D,3D

* Update readme

* update build

* Revert "bugfix"

This reverts commit e769612.

* Revert "Added failing test for convolutional1D with input (16,1)"

This reverts commit 5c91ed8.
  • Loading branch information
wassname authored and transcranial committed Nov 16, 2016
1 parent dcad54d commit c6e12f9
Show file tree
Hide file tree
Showing 16 changed files with 5,166 additions and 47 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ See `demos/src/` for source code of real examples written in VueJS.
- *advanced activations*: LeakyReLU, PReLU, ELU, ParametricSoftplus, ThresholdedReLU, SReLU
- *convolutional*: Convolution1D, Convolution2D, AtrousConvolution2D, SeparableConvolution2D, Deconvolution2D, Convolution3D, UpSampling1D, UpSampling2D, UpSampling3D, ZeroPadding1D, ZeroPadding2D, ZeroPadding3D
- *convolutional*: Convolution1D, Convolution2D, AtrousConvolution2D, SeparableConvolution2D, Deconvolution2D, Convolution3D, UpSampling1D, UpSampling2D, UpSampling3D, ZeroPadding1D, ZeroPadding2D, ZeroPadding3D, Cropping1D, Cropping2D, Cropping3D
- *core*: Dense, Activation, Dropout, SpatialDropout2D, SpatialDropout3D, Flatten, Reshape, Permute, RepeatVector, Merge, Highway, MaxoutDense
Expand All @@ -170,7 +170,7 @@ See `demos/src/` for source code of real examples written in VueJS.
- *core*: Lambda
- *convolutional*: AtrousConvolution1D, Cropping1D, Cropping2D, Cropping3D
- *convolutional*: AtrousConvolution1D
- *locally-connected*: LocallyConnected1D, LocallyConnected2D
Expand Down
3,933 changes: 3,889 additions & 44 deletions dist/keras.js

Large diffs are not rendered by default.

174 changes: 174 additions & 0 deletions notebooks/convolutional/Cropping1D.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 10,
"metadata": {
"ExecuteTime": {
"end_time": "2016-11-15T10:17:41.918185",
"start_time": "2016-11-15T10:17:41.915648"
},
"collapsed": false
},
"outputs": [],
"source": [
"import numpy as np\n",
"from keras.models import Model\n",
"from keras.layers import Input\n",
"from keras.layers.convolutional import Cropping1D\n",
"from keras import backend as K"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {
"ExecuteTime": {
"end_time": "2016-11-15T10:17:42.093433",
"start_time": "2016-11-15T10:17:42.091041"
},
"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": [
"### Cropping1D"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**[convolutional.Cropping1D.0] cropping (1,1) on 6x4 input**"
]
},
{
"cell_type": "code",
"execution_count": 28,
"metadata": {
"ExecuteTime": {
"end_time": "2016-11-15T10:20:10.543929",
"start_time": "2016-11-15T10:20:10.526160"
},
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"in shape: (6, 4)\n",
"in: [-0.412987, -0.139246, 0.567466, -0.512174, -0.33603, 0.585584, 0.453472, 0.942013, -0.609538, -0.894302, 0.711927, -0.126057, 0.677549, 0.676991, 0.471487, 0.752844, 0.218213, 0.37408, 0.09404, -0.786108, -0.1961, -0.031679, -0.188902, 0.846634]\n",
"out shape: (4, 4)\n",
"out: [-0.33603, 0.585584, 0.453472, 0.942013, -0.609538, -0.894302, 0.711927, -0.126057, 0.677549, 0.676991, 0.471487, 0.752844, 0.218213, 0.37408, 0.09404, -0.786108]\n"
]
}
],
"source": [
"data_in_shape = (6, 4)\n",
"L = Cropping1D(cropping=(1,1))\n",
"\n",
"layer_0 = Input(shape=data_in_shape)\n",
"layer_1 = L(layer_0)\n",
"model = Model(input=layer_0, output=layer_1)\n",
"\n",
"# set weights to random (use seed for reproducibility)\n",
"np.random.seed(240)\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.Cropping1D.1] cropping (2,3) on 6x4 input**"
]
},
{
"cell_type": "code",
"execution_count": 27,
"metadata": {
"ExecuteTime": {
"end_time": "2016-11-15T10:19:26.401100",
"start_time": "2016-11-15T10:19:26.382981"
},
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"in shape: (6, 4)\n",
"in: [0.229895, 0.156613, -0.66952, -0.996975, 0.45773, 0.684298, -0.999213, 0.276751, -0.484373, -0.506163, 0.353904, 0.513668, -0.981594, 0.78914, 0.603978, 0.204066, 0.532874, 0.866766, -0.544852, 0.876693, -0.523052, -0.716556, 0.875262, 0.544879]\n",
"out shape: (1, 4)\n",
"out: [-0.484373, -0.506163, 0.353904, 0.513668]\n"
]
}
],
"source": [
"data_in_shape = (6, 4)\n",
"L = Cropping1D(cropping=(2,3))\n",
"\n",
"layer_0 = Input(shape=data_in_shape)\n",
"layer_1 = L(layer_0)\n",
"model = Model(input=layer_0, output=layer_1)\n",
"\n",
"# set weights to random (use seed for reproducibility)\n",
"np.random.seed(241)\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 (with sys packages)",
"language": "python",
"name": "py3syspck"
},
"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.4.2"
}
},
"nbformat": 4,
"nbformat_minor": 0
}
Loading

0 comments on commit c6e12f9

Please sign in to comment.