Skip to content

Instantly share code, notes, and snippets.

@arose13
Last active October 28, 2019 18:38
Show Gist options
  • Save arose13/a3618531bf1387f67695f41120a84143 to your computer and use it in GitHub Desktop.
Save arose13/a3618531bf1387f67695f41120a84143 to your computer and use it in GitHub Desktop.
Automatic One Hot encoding layer for Keras
import tensorflow.keras as k
import tensorflow.keras.backend as K
def _one_hot_layer(num_classes: int):
"""
One hot encoding layer to save massive amounts of memory in Keras
:param num_classes:
:return:
"""
return k.layers.Lambda(lambda x: K.one_hot(K.cast(x, 'int64'), num_classes))
OneHotLayer = _one_hot_layer
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment