Skip to main content

adaptive-softmax implemented in Keras

Project description

Keras Adaptive Softmax

Travis Coverage Version Downloads License

[中文|English]

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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

keras-adaptive-softmax-0.6.0.tar.gz (16.3 kB view details)

Uploaded Source

File details

Details for the file keras-adaptive-softmax-0.6.0.tar.gz.

File metadata

  • Download URL: keras-adaptive-softmax-0.6.0.tar.gz
  • Upload date:
  • Size: 16.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.20.1 setuptools/41.0.1 requests-toolbelt/0.8.0 tqdm/4.24.0 CPython/3.6.4

File hashes

Hashes for keras-adaptive-softmax-0.6.0.tar.gz
Algorithm Hash digest
SHA256 763e260c274002045cec33f2515eed0ad4fcd24d64c4a1b515b1a8889e05dfa7
MD5 3b8b88f027677ab274d57d1167e73a43
BLAKE2b-256 7b7cc58a76f3e9fdfb6252eaa18aac1dcbf011eac96c6de3adbb47db318567dd

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page