TensorFlow Keras RNNs with trainable initial states
Project description
Treat the initial state(s) of TensorFlow Keras recurrent neural network (RNN) layers as a parameter or parameters to be learned during training, as recommended in, e.g., [1].
Ordinary RNNs use an all-zero initial state by default. Why not let the neural network learn a smarter initial state?
The trainable-initial-state-rnn package provides a class TrainableInitialStateRNN that can wrap any tf.keras RNN (or bidirectional RNN) and manage new initial state variables in addition to the RNN’s weights.
Typical usage looks as follows.
import tensorflow as tf
from trainable_initial_state_rnn import TrainableInitialStateRNN
base_rnn = tf.keras.layers.LSTM(256)
rnn = TrainableInitialStateRNN(base_rnn) # Treats initial state as a variable!
model = tf.keras.Model(...) # Use rnn like any other tf.keras layer in your model
model.compile(...)
history = model.fit(...)
Documentation is available at Read the Docs.
Installation
Requirements:
Python >= 3.7
TensorFlow >= 2.1
The trainable_initial_state_rnn package can be installed using the pip utility directly from the package’s GitHub page:
pip install git+https://github.com/artemmavrin/trainable-initial-state-rnn.git
Alternatively, install a recent release from the Python Package Index (PyPI):
pip install trainable-initial-state-rnn
Note. To install the project for development (e.g., to make changes to the source code), clone the project repository from GitHub and run
make dev
:git clone https://github.com/artemmavrin/trainable-initial-state-rnn.git cd trainable-initial-state-rnn # Optional but recommended: create and activate a new Python virtual environment make dev
This will additionally install the requirements needed to run tests, check code coverage, and produce documentation.
References
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
Built Distribution
Hashes for trainable-initial-state-rnn-0.0.1.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5de7e955314a437ddc9e000d5ec6518b0e30d8738490c3f7f6e0c286c2020ee8 |
|
MD5 | 4958de4ac35e9c461aa10f520d51be7f |
|
BLAKE2b-256 | 25ff31209664f53a605099fb80176a29d494528118003de628c83614e229a76f |
Hashes for trainable_initial_state_rnn-0.0.1-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9a0949759d14234147a5b505f036851d1baee85f9d4e5c3923927499861da2cc |
|
MD5 | e4f376bcfb4aff6c99ca147eff98bcc5 |
|
BLAKE2b-256 | 9be4eab33b89e3e7b9793a1fb239fea6f0c78c7867abc39552f09076a129c035 |