Skip to main content

keras-attention-block

Desc

keras-attention-block is an extension for keras to add attention. It was born from lack of existing function to add attention inside keras. The module itself is pure Python with no dependencies on modules or packages outside the standard Python distribution and keras.

keywords:keras,deeplearning,attention

Feature

  • support one dimensional attention, that is to take in inputs whose dimensions are batch_size * time_step * hidden_size

  • support two dimensional attention, that is to take in inputs of dimensions are batch_size * X * Y * hidden_size

  • support self-attention, that is to take in tensors. Four well defined calculations are included : additive, multiplicative, dot-product based and as well as linear.

  • support attention, that is to take in two tensors. Three well defined calculations are included : additive, multiplicative and dot product based.

  • support attention. Three well defined calculations are included : additive, multiplicative and dot product based.

  • support multihead attention

  • support customized calculations of similarity between Key and Query

  • support customized calculations of Value

Example

from keras.layers import merge
from keras.layers.core import *
from keras.layers.recurrent import LSTM
from keras.layers import Convolution2D
from keras.models import *
from keras.layers.normalization import BatchNormalization
from keras_attention_block import *

INPUT_DIM = 32
TIME_STEPS = 20
SINGLE_ATTENTION_VECTOR = False
APPLY_ATTENTION_BEFORE_LSTM = False

inputs = Input(shape=(TIME_STEPS, INPUT_DIM))
attention_mul =  SelfAttention1DLayer(similarity="linear",dropout_rate=0.2)(inputs)#MyLayer((20,32))(inputs)#
lstm_units = 32
#attention_mul = LSTM(lstm_units, return_sequences=False)(attention_mul)
attention_mul = Flatten()(attention_mul)
output = Dense(1, activation='sigmoid')(attention_mul)
m = Model(inputs=[inputs], outputs=output)

m.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])
print(m.summary())

train_data = np.random.random((1000,20,32))
train_lab = np.random.randint(0,2,1000)
m.fit(train_data,train_lab , epochs=1, batch_size=100 )

Install

  • python -m pip install keras_attention_block

Documentation

Documentation on Readthedocs.

TODO

  • 3D attention

Metadata

Release files for keras_attention_block 0.0.2

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_attention_block 0.0.2
File Size Uploaded Provenance Actions
keras_attention_block-0.0.2.tar.gz 9.7 kB Without provenance Details for keras_attention_block-0.0.2.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for keras_attention_block 0.0.2
File Interpreter ABI Platform Actions
keras_attention_block-0.0.2-py3-none-any.whl Python 3 none any Details for keras_attention_block-0.0.2-py3-none-any.whl

Total release size: 28.6 kB

Release files / keras_attention_block-0.0.2.tar.gz

Download URL keras_attention_block-0.0.2.tar.gz
Size 9.7 kB
Tags Source
SHA-256 checksum
How to use checksums
eefc3dbe925bc4faac225405c3b29daf0699f51174011fac9dd3e2e097a70338
BLAKE2b-256 checksum
How to use checksums
33d11115a726281f49e3bf5c38b65b788485a133cdcb78d1230918e766738b89
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release files / keras_attention_block-0.0.2-py3-none-any.whl

Download URL keras_attention_block-0.0.2-py3-none-any.whl
Size 18.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
0fed55018ba5c51523d257c5a3a383020879ad42b48687a22be3fe9d9b8ac370
BLAKE2b-256 checksum
How to use checksums
7fbae2869c827348e609a5bff3eb9b2078ff5c52e5fa7a1f33af96800c5d6778
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Security for keras_attention_block 0.0.2

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.0.2 This release

2 release files No provenance

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