Skip to main content

Keras Piecewise

Travis Coverage PyPI

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],
#     ],
# ]

Release files for keras-piecewise 0.14.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for keras-piecewise 0.14.0
File Size Uploaded
keras-piecewise-0.14.0.tar.gz 5.7 kB Details

Release files / keras-piecewise-0.14.0.tar.gz

Download URL keras-piecewise-0.14.0.tar.gz
Size 5.7 kB
Tags Source
SHA-256 checksum
How to use checksums
f5eafff966a0d1585c1de655040111d6ef78006fd05d7a2bb1b52ed01a6a6ac6
BLAKE2b-256 checksum
How to use checksums
96d99890f627b921867256b900986e60d3e29b15278b91bff165edfff7bd0553
Upload date
Uploaded using Trusted Publishing?
What is 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

Release history Release notifications | RSS feed

This release

0.14.0 This release

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page