adaptive-softmax implemented in Keras
Project description
Keras Adaptive Softmax
Install
pip install keras-adaptive-softmax
Usage
Generally, AdaptiveEmbedding and AdaptiveSoftmax should be used together. AdaptiveEmbedding provides variable length embeddings, while AdaptiveSoftmax calculates the similarities between the outputs and the generated embeddings.
import keras
from keras_adaptive_softmax import AdaptiveEmbedding, AdaptiveSoftmax
input_layer = keras.layers.Input(shape=(None,))
embed_layer = AdaptiveEmbedding(
input_dim=30,
output_dim=32,
cutoffs=[5, 15, 25],
div_val=2,
return_embeddings=True,
return_projections=True,
mask_zero=True,
)(input_layer)
dense_layer = keras.layers.Dense(
units=32,
activation='tanh',
)(embed_layer[0])
softmax_layer = AdaptiveSoftmax(
input_dim=32,
output_dim=30,
cutoffs=[5, 15, 25],
div_val=2,
bind_embeddings=True,
bind_projections=True,
)([dense_layer] + embed_layer[1:])
model = keras.models.Model(inputs=input_layer, outputs=softmax_layer)
model.compile('adam', 'sparse_categorical_crossentropy')
model.summary()
cutoffs and div_val controls the length of embeddings for each token. Suppose we have 30 distinct tokens, in the above example:
- The lengths of the embeddings of the first 5 tokens are 32
- The lengths of the embeddings of the next 10 tokens are 16
- The lengths of the embeddings of the next 10 tokens are 8
- The lengths of the embeddings of the last 5 tokens are 4
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-adaptive-softmax-0.9.0.tar.gz.
File metadata
- Download URL: keras-adaptive-softmax-0.9.0.tar.gz
- Upload date:
- Size: 15.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.3.0 pkginfo/1.6.1 requests/2.25.1 setuptools/51.1.2 requests-toolbelt/0.9.1 tqdm/4.55.2 CPython/3.7.4
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a0fe19ab75b62b63e150b4c4266b3fdc169d42eebe8ee77400b19b547ab5af38
|
|
| MD5 |
eeb223949bd27b70c8bd582223fa465e
|
|
| BLAKE2b-256 |
9c509a1df7260fa0ee12a1adb36af030c5af25139e3d9231367411ab4f6a87d4
|