A wrapper layer for splitting and accumulating sequential data
Project description
Keras Piecewise
A wrapper layer for splitting and accumulating sequential data.
Install
pip install keras-piecewise-pooling
Usage
Piecewise
import keras
import keras.backend as K
import numpy as np
from keras_piecewise import Piecewise
class AvePool1D(keras.layers.Layer):
def __init__(self, **kwargs):
super(AvePool1D, self).__init__(**kwargs)
def call(self, inputs):
return K.sum(inputs, axis=1) / K.cast(K.shape(inputs)[1], K.floatx())
def compute_output_shape(self, input_shape):
return (input_shape[0],) + input_shape[2:]
data = [[[1, 3, 2, 5], [7, 9, 2, 3], [0, 1, 7, 2], [4, 7, 2, 5]]]
positions = [[1, 3, 4]]
piece_num = len(positions[0])
data_input = keras.layers.Input(shape=(None, None))
position_input = keras.layers.Input(shape=(piece_num,), dtype='int32')
pool_layer = Piecewise(AvePool1D())([data_input, position_input])
model = keras.models.Model(inputs=[data_input, position_input], outputs=pool_layer)
model.compile(optimizer=keras.optimizers.Adam(), loss=keras.losses.mean_squared_error)
model.summary()
print(model.predict([np.asarray(data), np.asarray(positions)]).tolist())
# The result will be:
# [[
# [1.0, 3.0, 2.0, 5.0],
# [3.5, 5.0, 4.5, 2.5],
# [4.0, 7.0, 2.0, 5.0],
# ]]
The default value for argument pos_type
is Piecewise.POS_TYPE_SEGMENTS
, which means splitting the input sequences with increasing positions. When pos_type
is Piecewise.POS_TYPE_PAIRS
, every two positions represent the piece to be extracted.
Piecewise2D
import keras
import keras.backend as K
import numpy as np
from keras_piecewise import Piecewise2D
class MaxPool2D(keras.layers.Layer):
def __init__(self, **kwargs):
super(MaxPool2D, self).__init__(**kwargs)
def call(self, inputs):
return K.max(K.max(inputs, axis=1), axis=1)
def compute_output_shape(self, input_shape):
return (input_shape[0],) + input_shape[3:]
data = [
[
[1, 3, 5, 2],
[2, 5, 6, 1],
[7, 1, 5, 3],
[7, 2, 2, 4],
],
[
[1, 3, 5, 2],
[2, 5, 6, 1],
[7, 1, 5, 3],
[7, 2, 2, 4],
],
]
rows = [
[2, 4],
[3, 4],
]
cols = [
[1, 2, 4],
[1, 3, 4],
]
row_num = len(rows[0])
col_num = len(cols[0])
data_input = keras.layers.Input(shape=(None, None))
row_input = keras.layers.Input(shape=(row_num,))
col_input = keras.layers.Input(shape=(col_num,))
pool_layer = Piecewise2D(
layer=MaxPool2D(),
)([data_input, row_input, col_input])
model = keras.models.Model(inputs=[data_input, row_input, col_input], outputs=pool_layer)
model.compile(optimizer=keras.optimizers.Adam(), loss=keras.losses.mean_squared_error)
model.summary()
print(model.predict([np.asarray(data), np.asarray(rows), np.asarray(cols)]).tolist())
# The result will be:
# [
# [
# [2.0, 5.0, 6.0],
# [7.0, 2.0, 5.0],
# ],
# [
# [7.0, 6.0, 3.0],
# [7.0, 2.0, 4.0],
# ],
# ]
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
File details
Details for the file keras-piecewise-0.14.0.tar.gz
.
File metadata
- Download URL: keras-piecewise-0.14.0.tar.gz
- Upload date:
- Size: 5.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.32.2 CPython/3.7.4
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | f5eafff966a0d1585c1de655040111d6ef78006fd05d7a2bb1b52ed01a6a6ac6 |
|
MD5 | 69a51c5572e26de142f4a0186c289d5e |
|
BLAKE2b-256 | 96d99890f627b921867256b900986e60d3e29b15278b91bff165edfff7bd0553 |