implement GlobalMaxPooling1D/GlobalAveragePooling1D, with tests

This commit is contained in:
Leon Chen
2016-09-03 10:50:35 -04:00
parent 30d045339a
commit e53f56f7cc
20 changed files with 720 additions and 51 deletions
+4
View File
@@ -77,6 +77,10 @@
<script src="/test/pooling/MaxPooling1D.js"></script>
<script src="/test/pooling/data_AveragePooling1D.js"></script>
<script src="/test/pooling/AveragePooling1D.js"></script>
<script src="/test/pooling/data_GlobalMaxPooling1D.js"></script>
<script src="/test/pooling/GlobalMaxPooling1D.js"></script>
<script src="/test/pooling/data_GlobalAveragePooling1D.js"></script>
<script src="/test/pooling/GlobalAveragePooling1D.js"></script>
<script>
// mocha.checkLeaks();
+11 -11
View File
@@ -19,7 +19,7 @@
"import numpy as np\n",
"from keras.models import Model\n",
"from keras.layers import Input\n",
"from keras.layers.convolutional import AveragePooling1D\n",
"from keras.layers.pooling import AveragePooling1D\n",
"from keras import backend as K"
]
},
@@ -46,7 +46,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"**[convolutional.AveragePooling1D.0] input 6x6, pool_length=2, stride=None, border_mode='valid'**"
"**[pooling.AveragePooling1D.0] input 6x6, pool_length=2, stride=None, border_mode='valid'**"
]
},
{
@@ -91,7 +91,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"**[convolutional.AveragePooling1D.1] input 6x6, pool_length=2, stride=1, border_mode='valid'**"
"**[pooling.AveragePooling1D.1] input 6x6, pool_length=2, stride=1, border_mode='valid'**"
]
},
{
@@ -136,7 +136,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"**[convolutional.AveragePooling1D.2] input 6x6, pool_length=2, stride=3, border_mode='valid'**"
"**[pooling.AveragePooling1D.2] input 6x6, pool_length=2, stride=3, border_mode='valid'**"
]
},
{
@@ -181,7 +181,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"**[convolutional.AveragePooling1D.3] input 6x6, pool_length=2, stride=None, border_mode='same'**"
"**[pooling.AveragePooling1D.3] input 6x6, pool_length=2, stride=None, border_mode='same'**"
]
},
{
@@ -226,7 +226,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"**[convolutional.AveragePooling1D.4] input 6x6, pool_length=2, stride=1, border_mode='same'**"
"**[pooling.AveragePooling1D.4] input 6x6, pool_length=2, stride=1, border_mode='same'**"
]
},
{
@@ -271,7 +271,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"**[convolutional.AveragePooling1D.5] input 6x6, pool_length=2, stride=3, border_mode='same'**"
"**[pooling.AveragePooling1D.5] input 6x6, pool_length=2, stride=3, border_mode='same'**"
]
},
{
@@ -316,7 +316,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"**[convolutional.AveragePooling1D.6] input 6x6, pool_length=3, stride=None, border_mode='valid'**"
"**[pooling.AveragePooling1D.6] input 6x6, pool_length=3, stride=None, border_mode='valid'**"
]
},
{
@@ -361,7 +361,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"**[convolutional.AveragePooling1D.7] input 7x7, pool_length=3, stride=1, border_mode='same'**"
"**[pooling.AveragePooling1D.7] input 7x7, pool_length=3, stride=1, border_mode='same'**"
]
},
{
@@ -406,7 +406,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"**[convolutional.AveragePooling1D.8] input 7x7, pool_length=3, stride=3, border_mode='same'**"
"**[pooling.AveragePooling1D.8] input 7x7, pool_length=3, stride=3, border_mode='same'**"
]
},
{
@@ -460,7 +460,7 @@
"metadata": {
"anaconda-cloud": {},
"kernelspec": {
"display_name": "Python 3",
"display_name": "Python [default]",
"language": "python",
"name": "python3"
},
@@ -0,0 +1,212 @@
{
"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.pooling import GlobalAveragePooling1D\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": [
"### GlobalAveragePooling1D"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**[pooling.GlobalAveragePooling1D.0] input 6x6**"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"in shape: (6, 6)\n",
"in: [-0.806777, -0.564841, -0.481331, 0.559626, 0.274958, -0.659222, -0.178541, 0.689453, -0.028873, 0.053859, -0.446394, -0.53406, 0.776897, -0.700858, -0.802179, -0.616515, 0.718677, 0.303042, -0.080606, -0.850593, -0.795971, 0.860487, -0.90685, 0.89858, 0.617251, 0.334305, -0.351137, -0.642574, 0.108974, -0.993964, 0.051085, -0.372012, 0.843766, 0.088025, -0.598662, 0.789035]\n",
"out shape: (6,)\n",
"out: [0.063218, -0.244091, -0.269288, 0.050485, -0.141549, -0.032765]\n"
]
}
],
"source": [
"data_in_shape = (6, 6)\n",
"L = GlobalAveragePooling1D()\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(260)\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": [
"**[pooling.GlobalAveragePooling1D.1] input 3x7**"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"in shape: (3, 7)\n",
"in: [0.601872, -0.028379, 0.654213, 0.217731, -0.864161, 0.422013, 0.888312, -0.714141, -0.184753, 0.224845, -0.221123, -0.847943, -0.511334, -0.871723, -0.597589, -0.889034, -0.544887, -0.004798, 0.406639, -0.35285, 0.648562]\n",
"out shape: (7,)\n",
"out: [-0.23662, -0.367389, 0.111391, -0.00273, -0.435155, -0.14739, 0.221717]\n"
]
}
],
"source": [
"data_in_shape = (3, 7)\n",
"L = GlobalAveragePooling1D()\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(261)\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": [
"**[pooling.GlobalAveragePooling1D.2] input 8x4**"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"in shape: (8, 4)\n",
"in: [-0.215694, 0.441215, 0.116911, 0.53299, 0.883562, -0.535525, -0.869764, -0.596287, 0.576428, -0.689083, -0.132924, -0.129935, -0.17672, -0.29097, 0.590914, 0.992098, 0.908965, -0.170202, 0.640203, 0.178644, 0.866749, -0.545566, -0.827072, 0.420342, -0.076191, 0.207686, -0.908472, -0.795307, -0.948319, 0.683682, -0.563278, -0.82135]\n",
"out shape: (4,)\n",
"out: [0.227348, -0.112345, -0.244185, -0.027351]\n"
]
}
],
"source": [
"data_in_shape = (8, 4)\n",
"L = GlobalAveragePooling1D()\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(262)\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": {
"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
}
+212
View File
@@ -0,0 +1,212 @@
{
"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.pooling import GlobalMaxPooling1D\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": [
"### GlobalMaxPooling1D"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**[pooling.GlobalMaxPooling1D.0] input 6x6**"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"in shape: (6, 6)\n",
"in: [-0.806777, -0.564841, -0.481331, 0.559626, 0.274958, -0.659222, -0.178541, 0.689453, -0.028873, 0.053859, -0.446394, -0.53406, 0.776897, -0.700858, -0.802179, -0.616515, 0.718677, 0.303042, -0.080606, -0.850593, -0.795971, 0.860487, -0.90685, 0.89858, 0.617251, 0.334305, -0.351137, -0.642574, 0.108974, -0.993964, 0.051085, -0.372012, 0.843766, 0.088025, -0.598662, 0.789035]\n",
"out shape: (6,)\n",
"out: [0.776897, 0.689453, 0.843766, 0.860487, 0.718677, 0.89858]\n"
]
}
],
"source": [
"data_in_shape = (6, 6)\n",
"L = GlobalMaxPooling1D()\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(260)\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": [
"**[pooling.GlobalMaxPooling1D.1] input 3x7**"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"in shape: (3, 7)\n",
"in: [0.601872, -0.028379, 0.654213, 0.217731, -0.864161, 0.422013, 0.888312, -0.714141, -0.184753, 0.224845, -0.221123, -0.847943, -0.511334, -0.871723, -0.597589, -0.889034, -0.544887, -0.004798, 0.406639, -0.35285, 0.648562]\n",
"out shape: (7,)\n",
"out: [0.601872, -0.028379, 0.654213, 0.217731, 0.406639, 0.422013, 0.888312]\n"
]
}
],
"source": [
"data_in_shape = (3, 7)\n",
"L = GlobalMaxPooling1D()\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(261)\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": [
"**[pooling.GlobalMaxPooling1D.2] input 8x4**"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"in shape: (8, 4)\n",
"in: [-0.215694, 0.441215, 0.116911, 0.53299, 0.883562, -0.535525, -0.869764, -0.596287, 0.576428, -0.689083, -0.132924, -0.129935, -0.17672, -0.29097, 0.590914, 0.992098, 0.908965, -0.170202, 0.640203, 0.178644, 0.866749, -0.545566, -0.827072, 0.420342, -0.076191, 0.207686, -0.908472, -0.795307, -0.948319, 0.683682, -0.563278, -0.82135]\n",
"out shape: (4,)\n",
"out: [0.908965, 0.683682, 0.640203, 0.992098]\n"
]
}
],
"source": [
"data_in_shape = (8, 4)\n",
"L = GlobalMaxPooling1D()\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(262)\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": {
"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
}
+12 -11
View File
@@ -19,7 +19,7 @@
"import numpy as np\n",
"from keras.models import Model\n",
"from keras.layers import Input\n",
"from keras.layers.convolutional import MaxPooling1D\n",
"from keras.layers.pooling import MaxPooling1D\n",
"from keras import backend as K"
]
},
@@ -46,7 +46,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"**[convolutional.MaxPooling1D.0] input 6x6, pool_length=2, stride=None, border_mode='valid'**"
"**[pooling.MaxPooling1D.0] input 6x6, pool_length=2, stride=None, border_mode='valid'**"
]
},
{
@@ -91,7 +91,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"**[convolutional.MaxPooling1D.1] input 6x6, pool_length=2, stride=1, border_mode='valid'**"
"**[pooling.MaxPooling1D.1] input 6x6, pool_length=2, stride=1, border_mode='valid'**"
]
},
{
@@ -136,7 +136,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"**[convolutional.MaxPooling1D.2] input 6x6, pool_length=2, stride=3, border_mode='valid'**"
"**[pooling.MaxPooling1D.2] input 6x6, pool_length=2, stride=3, border_mode='valid'**"
]
},
{
@@ -181,7 +181,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"**[convolutional.MaxPooling1D.3] input 6x6, pool_length=2, stride=None, border_mode='same'**"
"**[pooling.MaxPooling1D.3] input 6x6, pool_length=2, stride=None, border_mode='same'**"
]
},
{
@@ -226,7 +226,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"**[convolutional.MaxPooling1D.4] input 6x6, pool_length=2, stride=1, border_mode='same'**"
"**[pooling.MaxPooling1D.4] input 6x6, pool_length=2, stride=1, border_mode='same'**"
]
},
{
@@ -271,7 +271,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"**[convolutional.MaxPooling1D.5] input 6x6, pool_length=2, stride=3, border_mode='same'**"
"**[pooling.MaxPooling1D.5] input 6x6, pool_length=2, stride=3, border_mode='same'**"
]
},
{
@@ -316,7 +316,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"**[convolutional.MaxPooling1D.6] input 6x6, pool_length=3, stride=None, border_mode='valid'**"
"**[pooling.MaxPooling1D.6] input 6x6, pool_length=3, stride=None, border_mode='valid'**"
]
},
{
@@ -361,7 +361,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"**[convolutional.MaxPooling1D.7] input 7x7, pool_length=3, stride=1, border_mode='same'**"
"**[pooling.MaxPooling1D.7] input 7x7, pool_length=3, stride=1, border_mode='same'**"
]
},
{
@@ -406,7 +406,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"**[convolutional.MaxPooling1D.8] input 7x7, pool_length=3, stride=3, border_mode='same'**"
"**[pooling.MaxPooling1D.8] input 7x7, pool_length=3, stride=3, border_mode='same'**"
]
},
{
@@ -458,8 +458,9 @@
}
],
"metadata": {
"anaconda-cloud": {},
"kernelspec": {
"display_name": "Python 3",
"display_name": "Python [default]",
"language": "python",
"name": "python3"
},
+1 -1
View File
@@ -6,7 +6,7 @@
"lint": "standard src test",
"watch": "webpack src/index.js dist/keras.js --watch --config webpack.dev.config.js",
"build": "npm run lint && webpack --config webpack.prod.config.js",
"server": "http-server . -p 9001"
"server": "http-server . -p 3000"
},
"repository": {
"type": "git",
+1 -1
View File
@@ -5,7 +5,7 @@ import _Pooling1D from './_Pooling1D'
*/
export default class AveragePooling1D extends _Pooling1D {
/**
* Creates a AveragePooling1D activation layer
* Creates a AveragePooling1D layer
*/
constructor (attrs = {}) {
super(attrs)
@@ -0,0 +1,30 @@
import Layer from '../../engine/Layer'
import Tensor from '../../Tensor'
import ops from 'ndarray-ops'
/**
* GlobalAveragePooling1D layer class
*/
export default class GlobalAveragePooling1D extends Layer {
/**
* Creates a GlobalAveragePooling1D layer
*/
constructor (attrs = {}) {
super(attrs)
}
/**
* Method for layer computational logic
* @param {Tensor} x
* @returns {Tensor} x
*/
call (x) {
const [steps, features] = x.tensor.shape
let y = new Tensor([], [features])
for (let i = 0, len = features; i < len; i++) {
y.tensor.set(i, ops.sum(x.tensor.pick(null, i)) / steps)
}
x.tensor = y.tensor
return x
}
}
+30
View File
@@ -0,0 +1,30 @@
import Layer from '../../engine/Layer'
import Tensor from '../../Tensor'
import ops from 'ndarray-ops'
/**
* GlobalMaxPooling1D layer class
*/
export default class GlobalMaxPooling1D extends Layer {
/**
* Creates a GlobalMaxPooling1D layer
*/
constructor (attrs = {}) {
super(attrs)
}
/**
* Method for layer computational logic
* @param {Tensor} x
* @returns {Tensor} x
*/
call (x) {
const features = x.tensor.shape[1]
let y = new Tensor([], [features])
for (let i = 0, len = features; i < len; i++) {
y.tensor.set(i, ops.sup(x.tensor.pick(null, i)))
}
x.tensor = y.tensor
return x
}
}
+1 -1
View File
@@ -5,7 +5,7 @@ import _Pooling1D from './_Pooling1D'
*/
export default class MaxPooling1D extends _Pooling1D {
/**
* Creates a MaxPooling1D activation layer
* Creates a MaxPooling1D layer
*/
constructor (attrs = {}) {
super(attrs)
+1 -1
View File
@@ -7,7 +7,7 @@ import ops from 'ndarray-ops'
*/
export default class _Pooling1D extends Layer {
/**
* Creates a _Pooling1D activation layer
* Creates a _Pooling1D layer
*/
constructor (attrs = {}) {
super(attrs)
+5 -1
View File
@@ -1,7 +1,11 @@
import MaxPooling1D from './MaxPooling1D'
import AveragePooling1D from './AveragePooling1D'
import GlobalMaxPooling1D from './GlobalMaxPooling1D'
import GlobalAveragePooling1D from './GlobalAveragePooling1D'
export {
MaxPooling1D,
AveragePooling1D
AveragePooling1D,
GlobalMaxPooling1D,
GlobalAveragePooling1D
}
+3 -3
View File
@@ -1,6 +1,6 @@
/* eslint-env browser, mocha */
describe('convolutional layer: AveragePooling1D', function () {
describe('pooling layer: AveragePooling1D', function () {
const assert = chai.assert
const styles = testGlobals.styles
const logTime = testGlobals.logTime
@@ -48,11 +48,11 @@ describe('convolutional layer: AveragePooling1D', function () {
]
before(function () {
console.log('\n%cconvolutional layer: AveragePooling1D', styles.h1)
console.log('\n%cpooling layer: AveragePooling1D', styles.h1)
})
testParams.forEach(({ inputShape, attrs }, i) => {
const key = `convolutional.AveragePooling1D.${i}`
const key = `pooling.AveragePooling1D.${i}`
const [inputLength, inputFeatures] = inputShape
const title = `[${key}] test: ${inputLength}x${inputFeatures} input, poolLength='${attrs.poolLength}', stride=${attrs.stride}, borderMode=${attrs.borderMode}`
+48
View File
@@ -0,0 +1,48 @@
/* eslint-env browser, mocha */
describe('pooling layer: GlobalAveragePooling1D', function () {
const assert = chai.assert
const styles = testGlobals.styles
const logTime = testGlobals.logTime
const stringifyCondensed = testGlobals.stringifyCondensed
const approxEquals = KerasJS.testUtils.approxEquals
const layers = KerasJS.layers
const testParams = [
{
inputShape: [6, 6]
},
{
inputShape: [3, 7]
},
{
inputShape: [8, 4]
}
]
before(function () {
console.log('\n%cpooling layer: GlobalAveragePooling1D', styles.h1)
})
testParams.forEach(({ inputShape }, i) => {
const key = `pooling.GlobalAveragePooling1D.${i}`
const [inputLength, inputFeatures] = inputShape
const title = `[${key}] test: ${inputLength}x${inputFeatures} input`
it(title, function () {
console.log(`\n%c${title}`, styles.h3)
let testLayer = new layers.GlobalAveragePooling1D()
let t = new KerasJS.Tensor(TEST_DATA[key].input.data, TEST_DATA[key].input.shape)
console.log('%cin', styles.h4, stringifyCondensed(t.tensor))
const startTime = performance.now()
t = testLayer.call(t)
const endTime = performance.now()
console.log('%cout', styles.h4, stringifyCondensed(t.tensor))
logTime(startTime, endTime)
const dataExpected = new Float32Array(TEST_DATA[key].expected.data)
const shapeExpected = TEST_DATA[key].expected.shape
assert.deepEqual(t.tensor.shape, shapeExpected)
assert.isTrue(approxEquals(t.tensor, dataExpected))
})
})
})
+48
View File
@@ -0,0 +1,48 @@
/* eslint-env browser, mocha */
describe('pooling layer: GlobalMaxPooling1D', function () {
const assert = chai.assert
const styles = testGlobals.styles
const logTime = testGlobals.logTime
const stringifyCondensed = testGlobals.stringifyCondensed
const approxEquals = KerasJS.testUtils.approxEquals
const layers = KerasJS.layers
const testParams = [
{
inputShape: [6, 6]
},
{
inputShape: [3, 7]
},
{
inputShape: [8, 4]
}
]
before(function () {
console.log('\n%cpooling layer: GlobalMaxPooling1D', styles.h1)
})
testParams.forEach(({ inputShape }, i) => {
const key = `pooling.GlobalMaxPooling1D.${i}`
const [inputLength, inputFeatures] = inputShape
const title = `[${key}] test: ${inputLength}x${inputFeatures} input`
it(title, function () {
console.log(`\n%c${title}`, styles.h3)
let testLayer = new layers.GlobalMaxPooling1D()
let t = new KerasJS.Tensor(TEST_DATA[key].input.data, TEST_DATA[key].input.shape)
console.log('%cin', styles.h4, stringifyCondensed(t.tensor))
const startTime = performance.now()
t = testLayer.call(t)
const endTime = performance.now()
console.log('%cout', styles.h4, stringifyCondensed(t.tensor))
logTime(startTime, endTime)
const dataExpected = new Float32Array(TEST_DATA[key].expected.data)
const shapeExpected = TEST_DATA[key].expected.shape
assert.deepEqual(t.tensor.shape, shapeExpected)
assert.isTrue(approxEquals(t.tensor, dataExpected))
})
})
})
+3 -3
View File
@@ -1,6 +1,6 @@
/* eslint-env browser, mocha */
describe('convolutional layer: MaxPooling1D', function () {
describe('pooling layer: MaxPooling1D', function () {
const assert = chai.assert
const styles = testGlobals.styles
const logTime = testGlobals.logTime
@@ -48,11 +48,11 @@ describe('convolutional layer: MaxPooling1D', function () {
]
before(function () {
console.log('\n%cconvolutional layer: MaxPooling1D', styles.h1)
console.log('\n%cpooling layer: MaxPooling1D', styles.h1)
})
testParams.forEach(({ inputShape, attrs }, i) => {
const key = `convolutional.MaxPooling1D.${i}`
const key = `pooling.MaxPooling1D.${i}`
const [inputLength, inputFeatures] = inputShape
const title = `[${key}] test: ${inputLength}x${inputFeatures} input, poolLength='${attrs.poolLength}', stride=${attrs.stride}, borderMode=${attrs.borderMode}`
+9 -9
View File
@@ -4,7 +4,7 @@
(function () {
var DATA = {
'convolutional.AveragePooling1D.0': {
'pooling.AveragePooling1D.0': {
input: {
data: [-0.570441, -0.454673, -0.285321, 0.237249, 0.282682, 0.428035, 0.160547, -0.332203, 0.546391, 0.272735, 0.010827, -0.763164, -0.442696, 0.381948, -0.676994, 0.753553, -0.031788, 0.915329, -0.738844, 0.269075, 0.434091, 0.991585, -0.944288, 0.258834, 0.162138, 0.565201, -0.492094, 0.170854, -0.139788, -0.710674, 0.406968, 0.705926, -0.094137, -0.793497, -0.040684, 0.522292],
shape: [6, 6]
@@ -14,7 +14,7 @@
shape: [3, 6]
}
},
'convolutional.AveragePooling1D.1': {
'pooling.AveragePooling1D.1': {
input: {
data: [0.275222, -0.793967, -0.468107, -0.841484, -0.295362, 0.78175, 0.068787, -0.261747, -0.625733, -0.042907, 0.861141, 0.85267, 0.956439, 0.717838, -0.99869, -0.963008, 0.013277, -0.180306, 0.832137, -0.385252, -0.524308, 0.659706, -0.905127, 0.526292, 0.832569, 0.084455, 0.23838, -0.046178, -0.735871, 0.776883, -0.394643, 0.498903, 0.029584, -0.17332, 0.628159, 0.445074],
shape: [6, 6]
@@ -24,7 +24,7 @@
shape: [5, 6]
}
},
'convolutional.AveragePooling1D.2': {
'pooling.AveragePooling1D.2': {
input: {
data: [-0.989173, -0.133618, -0.505338, 0.023259, 0.503982, -0.303769, -0.436321, 0.793911, 0.416102, 0.806405, -0.098342, -0.738022, -0.982676, 0.805073, 0.741244, -0.941634, -0.253526, -0.136544, -0.295772, 0.207565, -0.517246, -0.686963, -0.176235, -0.354111, -0.862411, -0.969822, 0.200074, 0.290718, -0.038623, 0.294839, 0.247968, 0.557946, -0.455596, 0.6624, 0.879529, -0.466772],
shape: [6, 6]
@@ -34,7 +34,7 @@
shape: [2, 6]
}
},
'convolutional.AveragePooling1D.3': {
'pooling.AveragePooling1D.3': {
input: {
data: [-0.47588, 0.366985, 0.040173, 0.015578, -0.906159, 0.241982, -0.771299, -0.443554, -0.56404, -0.17751, 0.541277, -0.233327, 0.024369, 0.858275, 0.496191, 0.980574, -0.59522, 0.480899, 0.392553, -0.191718, 0.055121, 0.289836, -0.498339, 0.800408, 0.132679, -0.716649, 0.840092, -0.088837, -0.538209, -0.580887, -0.370128, -0.924933, -0.161736, -0.205619, 0.793729, -0.354472],
shape: [6, 6]
@@ -44,7 +44,7 @@
shape: [3, 6]
}
},
'convolutional.AveragePooling1D.4': {
'pooling.AveragePooling1D.4': {
input: {
data: [0.024124, 0.280236, -0.680013, -0.042458, -0.164273, 0.358409, 0.511014, -0.585272, -0.481578, 0.692702, 0.64189, -0.400252, -0.922248, -0.735105, -0.533918, 0.071402, 0.310474, 0.369868, 0.767931, -0.842066, -0.091189, 0.835301, -0.480484, 0.950819, -0.002131, 0.086491, -0.480947, 0.405572, -0.083803, -0.921447, -0.291545, 0.674087, -0.560444, 0.881432, 0.076544, 0.63549],
shape: [6, 6]
@@ -54,7 +54,7 @@
shape: [6, 6]
}
},
'convolutional.AveragePooling1D.5': {
'pooling.AveragePooling1D.5': {
input: {
data: [-0.072127, -0.553929, -0.355552, -0.936405, 0.556627, -0.482815, -0.225337, -0.640315, 0.023246, -0.638412, -0.797304, 0.284959, -0.569771, -0.685286, 0.002481, 0.398436, 0.11345, 0.416629, -0.526713, 0.962183, 0.021732, 0.922994, 0.07991, -0.164385, 0.461494, -0.982877, -0.142158, 0.175741, -0.124041, -0.875609, -0.528708, -0.911127, 0.782257, -0.509403, 0.573973, -0.151309],
shape: [6, 6]
@@ -64,7 +64,7 @@
shape: [2, 6]
}
},
'convolutional.AveragePooling1D.6': {
'pooling.AveragePooling1D.6': {
input: {
data: [-0.908432, 0.172241, -0.59352, -0.831514, -0.948016, -0.194126, -0.242576, -0.89432, 0.610714, -0.24071, -0.245859, 0.500851, 0.088791, 0.04635, 0.908568, -0.232197, -0.175815, -0.177919, -0.535898, 0.04802, 0.512585, 0.854168, 0.283045, 0.282488, -0.126263, 0.772568, 0.403228, 0.721107, -0.043311, -0.799013, -0.683105, -0.52703, 0.838417, 0.915738, 0.180207, -0.181716],
shape: [6, 6]
@@ -74,7 +74,7 @@
shape: [2, 6]
}
},
'convolutional.AveragePooling1D.7': {
'pooling.AveragePooling1D.7': {
input: {
data: [0.859653, 0.613312, 0.262871, 0.484585, 0.518061, -0.718848, -0.351388, -0.501557, 0.017192, -0.026869, -0.768317, -0.476893, -0.895809, 0.764782, 0.862057, 0.021243, 0.004039, 0.760431, 0.72102, 0.395305, 0.930351, 0.425255, -0.000952, -0.060338, -0.095258, 0.173776, -0.645557, 0.196502, 0.27885, -0.6868, -0.551196, 0.726361, -0.382779, 0.61877, 0.023847, -0.451251, 0.065412, -0.708225, -0.815011, -0.926643, 0.323493, -0.063352, 0.16365, -0.030438, -0.054635, 0.193949, -0.574495, 0.022988, 0.36335],
shape: [7, 7]
@@ -84,7 +84,7 @@
shape: [7, 7]
}
},
'convolutional.AveragePooling1D.8': {
'pooling.AveragePooling1D.8': {
input: {
data: [-0.830746, 0.315868, -0.173506, 0.415541, -0.957882, 0.658995, 0.795264, -0.147083, -0.042061, 0.230065, 0.388847, -0.277524, -0.268423, 0.35691, -0.515291, -0.37555, 0.367489, 0.753251, -0.60764, -0.16741, -0.893275, -0.814508, -0.437352, 0.062193, -0.003077, 0.560767, -0.646034, -0.283879, 0.097661, 0.401756, -0.236235, -0.199824, -0.252007, -0.335503, 0.414988, 0.301686, 0.309765, -0.349835, -0.274081, 0.383308, -0.782973, -0.667924, 0.282556, -0.932491, 0.954125, 0.837689, 0.219229, -0.583405, -0.018424],
shape: [7, 7]
@@ -0,0 +1,40 @@
// TEST DATA
// Keyed by mocha test ID
// Python code for generating test data can be found in the matching jupyter notebook in folder `notebooks/`.
(function () {
var DATA = {
'pooling.GlobalAveragePooling1D.0': {
input: {
data: [-0.806777, -0.564841, -0.481331, 0.559626, 0.274958, -0.659222, -0.178541, 0.689453, -0.028873, 0.053859, -0.446394, -0.53406, 0.776897, -0.700858, -0.802179, -0.616515, 0.718677, 0.303042, -0.080606, -0.850593, -0.795971, 0.860487, -0.90685, 0.89858, 0.617251, 0.334305, -0.351137, -0.642574, 0.108974, -0.993964, 0.051085, -0.372012, 0.843766, 0.088025, -0.598662, 0.789035],
shape: [6, 6]
},
expected: {
data: [0.063218, -0.244091, -0.269288, 0.050485, -0.141549, -0.032765],
shape: [6]
}
},
'pooling.GlobalAveragePooling1D.1': {
input: {
data: [0.601872, -0.028379, 0.654213, 0.217731, -0.864161, 0.422013, 0.888312, -0.714141, -0.184753, 0.224845, -0.221123, -0.847943, -0.511334, -0.871723, -0.597589, -0.889034, -0.544887, -0.004798, 0.406639, -0.35285, 0.648562],
shape: [3, 7]
},
expected: {
data: [-0.23662, -0.367389, 0.111391, -0.00273, -0.435155, -0.14739, 0.221717],
shape: [7]
}
},
'pooling.GlobalAveragePooling1D.2': {
input: {
data: [-0.215694, 0.441215, 0.116911, 0.53299, 0.883562, -0.535525, -0.869764, -0.596287, 0.576428, -0.689083, -0.132924, -0.129935, -0.17672, -0.29097, 0.590914, 0.992098, 0.908965, -0.170202, 0.640203, 0.178644, 0.866749, -0.545566, -0.827072, 0.420342, -0.076191, 0.207686, -0.908472, -0.795307, -0.948319, 0.683682, -0.563278, -0.82135],
shape: [8, 4]
},
expected: {
data: [0.227348, -0.112345, -0.244185, -0.027351],
shape: [4]
}
}
}
window.TEST_DATA = Object.assign({}, window.TEST_DATA, DATA)
})()
+40
View File
@@ -0,0 +1,40 @@
// TEST DATA
// Keyed by mocha test ID
// Python code for generating test data can be found in the matching jupyter notebook in folder `notebooks/`.
(function () {
var DATA = {
'pooling.GlobalMaxPooling1D.0': {
input: {
data: [-0.806777, -0.564841, -0.481331, 0.559626, 0.274958, -0.659222, -0.178541, 0.689453, -0.028873, 0.053859, -0.446394, -0.53406, 0.776897, -0.700858, -0.802179, -0.616515, 0.718677, 0.303042, -0.080606, -0.850593, -0.795971, 0.860487, -0.90685, 0.89858, 0.617251, 0.334305, -0.351137, -0.642574, 0.108974, -0.993964, 0.051085, -0.372012, 0.843766, 0.088025, -0.598662, 0.789035],
shape: [6, 6]
},
expected: {
data: [0.776897, 0.689453, 0.843766, 0.860487, 0.718677, 0.89858],
shape: [6]
}
},
'pooling.GlobalMaxPooling1D.1': {
input: {
data: [0.601872, -0.028379, 0.654213, 0.217731, -0.864161, 0.422013, 0.888312, -0.714141, -0.184753, 0.224845, -0.221123, -0.847943, -0.511334, -0.871723, -0.597589, -0.889034, -0.544887, -0.004798, 0.406639, -0.35285, 0.648562],
shape: [3, 7]
},
expected: {
data: [0.601872, -0.028379, 0.654213, 0.217731, 0.406639, 0.422013, 0.888312],
shape: [7]
}
},
'pooling.GlobalMaxPooling1D.2': {
input: {
data: [-0.215694, 0.441215, 0.116911, 0.53299, 0.883562, -0.535525, -0.869764, -0.596287, 0.576428, -0.689083, -0.132924, -0.129935, -0.17672, -0.29097, 0.590914, 0.992098, 0.908965, -0.170202, 0.640203, 0.178644, 0.866749, -0.545566, -0.827072, 0.420342, -0.076191, 0.207686, -0.908472, -0.795307, -0.948319, 0.683682, -0.563278, -0.82135],
shape: [8, 4]
},
expected: {
data: [0.908965, 0.683682, 0.640203, 0.992098],
shape: [4]
}
}
}
window.TEST_DATA = Object.assign({}, window.TEST_DATA, DATA)
})()
+9 -9
View File
@@ -4,7 +4,7 @@
(function () {
var DATA = {
'convolutional.MaxPooling1D.0': {
'pooling.MaxPooling1D.0': {
input: {
data: [-0.570441, -0.454673, -0.285321, 0.237249, 0.282682, 0.428035, 0.160547, -0.332203, 0.546391, 0.272735, 0.010827, -0.763164, -0.442696, 0.381948, -0.676994, 0.753553, -0.031788, 0.915329, -0.738844, 0.269075, 0.434091, 0.991585, -0.944288, 0.258834, 0.162138, 0.565201, -0.492094, 0.170854, -0.139788, -0.710674, 0.406968, 0.705926, -0.094137, -0.793497, -0.040684, 0.522292],
shape: [6, 6]
@@ -14,7 +14,7 @@
shape: [3, 6]
}
},
'convolutional.MaxPooling1D.1': {
'pooling.MaxPooling1D.1': {
input: {
data: [0.275222, -0.793967, -0.468107, -0.841484, -0.295362, 0.78175, 0.068787, -0.261747, -0.625733, -0.042907, 0.861141, 0.85267, 0.956439, 0.717838, -0.99869, -0.963008, 0.013277, -0.180306, 0.832137, -0.385252, -0.524308, 0.659706, -0.905127, 0.526292, 0.832569, 0.084455, 0.23838, -0.046178, -0.735871, 0.776883, -0.394643, 0.498903, 0.029584, -0.17332, 0.628159, 0.445074],
shape: [6, 6]
@@ -24,7 +24,7 @@
shape: [5, 6]
}
},
'convolutional.MaxPooling1D.2': {
'pooling.MaxPooling1D.2': {
input: {
data: [-0.989173, -0.133618, -0.505338, 0.023259, 0.503982, -0.303769, -0.436321, 0.793911, 0.416102, 0.806405, -0.098342, -0.738022, -0.982676, 0.805073, 0.741244, -0.941634, -0.253526, -0.136544, -0.295772, 0.207565, -0.517246, -0.686963, -0.176235, -0.354111, -0.862411, -0.969822, 0.200074, 0.290718, -0.038623, 0.294839, 0.247968, 0.557946, -0.455596, 0.6624, 0.879529, -0.466772],
shape: [6, 6]
@@ -34,7 +34,7 @@
shape: [2, 6]
}
},
'convolutional.MaxPooling1D.3': {
'pooling.MaxPooling1D.3': {
input: {
data: [-0.47588, 0.366985, 0.040173, 0.015578, -0.906159, 0.241982, -0.771299, -0.443554, -0.56404, -0.17751, 0.541277, -0.233327, 0.024369, 0.858275, 0.496191, 0.980574, -0.59522, 0.480899, 0.392553, -0.191718, 0.055121, 0.289836, -0.498339, 0.800408, 0.132679, -0.716649, 0.840092, -0.088837, -0.538209, -0.580887, -0.370128, -0.924933, -0.161736, -0.205619, 0.793729, -0.354472],
shape: [6, 6]
@@ -44,7 +44,7 @@
shape: [3, 6]
}
},
'convolutional.MaxPooling1D.4': {
'pooling.MaxPooling1D.4': {
input: {
data: [0.024124, 0.280236, -0.680013, -0.042458, -0.164273, 0.358409, 0.511014, -0.585272, -0.481578, 0.692702, 0.64189, -0.400252, -0.922248, -0.735105, -0.533918, 0.071402, 0.310474, 0.369868, 0.767931, -0.842066, -0.091189, 0.835301, -0.480484, 0.950819, -0.002131, 0.086491, -0.480947, 0.405572, -0.083803, -0.921447, -0.291545, 0.674087, -0.560444, 0.881432, 0.076544, 0.63549],
shape: [6, 6]
@@ -54,7 +54,7 @@
shape: [6, 6]
}
},
'convolutional.MaxPooling1D.5': {
'pooling.MaxPooling1D.5': {
input: {
data: [-0.072127, -0.553929, -0.355552, -0.936405, 0.556627, -0.482815, -0.225337, -0.640315, 0.023246, -0.638412, -0.797304, 0.284959, -0.569771, -0.685286, 0.002481, 0.398436, 0.11345, 0.416629, -0.526713, 0.962183, 0.021732, 0.922994, 0.07991, -0.164385, 0.461494, -0.982877, -0.142158, 0.175741, -0.124041, -0.875609, -0.528708, -0.911127, 0.782257, -0.509403, 0.573973, -0.151309],
shape: [6, 6]
@@ -64,7 +64,7 @@
shape: [2, 6]
}
},
'convolutional.MaxPooling1D.6': {
'pooling.MaxPooling1D.6': {
input: {
data: [-0.908432, 0.172241, -0.59352, -0.831514, -0.948016, -0.194126, -0.242576, -0.89432, 0.610714, -0.24071, -0.245859, 0.500851, 0.088791, 0.04635, 0.908568, -0.232197, -0.175815, -0.177919, -0.535898, 0.04802, 0.512585, 0.854168, 0.283045, 0.282488, -0.126263, 0.772568, 0.403228, 0.721107, -0.043311, -0.799013, -0.683105, -0.52703, 0.838417, 0.915738, 0.180207, -0.181716],
shape: [6, 6]
@@ -74,7 +74,7 @@
shape: [2, 6]
}
},
'convolutional.MaxPooling1D.7': {
'pooling.MaxPooling1D.7': {
input: {
data: [0.859653, 0.613312, 0.262871, 0.484585, 0.518061, -0.718848, -0.351388, -0.501557, 0.017192, -0.026869, -0.768317, -0.476893, -0.895809, 0.764782, 0.862057, 0.021243, 0.004039, 0.760431, 0.72102, 0.395305, 0.930351, 0.425255, -0.000952, -0.060338, -0.095258, 0.173776, -0.645557, 0.196502, 0.27885, -0.6868, -0.551196, 0.726361, -0.382779, 0.61877, 0.023847, -0.451251, 0.065412, -0.708225, -0.815011, -0.926643, 0.323493, -0.063352, 0.16365, -0.030438, -0.054635, 0.193949, -0.574495, 0.022988, 0.36335],
shape: [7, 7]
@@ -84,7 +84,7 @@
shape: [7, 7]
}
},
'convolutional.MaxPooling1D.8': {
'pooling.MaxPooling1D.8': {
input: {
data: [-0.830746, 0.315868, -0.173506, 0.415541, -0.957882, 0.658995, 0.795264, -0.147083, -0.042061, 0.230065, 0.388847, -0.277524, -0.268423, 0.35691, -0.515291, -0.37555, 0.367489, 0.753251, -0.60764, -0.16741, -0.893275, -0.814508, -0.437352, 0.062193, -0.003077, 0.560767, -0.646034, -0.283879, 0.097661, 0.401756, -0.236235, -0.199824, -0.252007, -0.335503, 0.414988, 0.301686, 0.309765, -0.349835, -0.274081, 0.383308, -0.782973, -0.667924, 0.282556, -0.932491, 0.954125, 0.837689, 0.219229, -0.583405, -0.018424],
shape: [7, 7]