Keras Preprocessing
-
Over 100s of layers
-
20 or more for preprocessing layers
-
Preprocessing layers are regular layers.
-
All layers have some configuration and mainly have configuration graph.
-
Some layers have state.
-
Example: Build a binarizer layer:
-
No trainable weights in preprocessing layers. (It is preprocessing)
-
No need to make preprocessing layers differentiable. No need for backpropagation.
-
Mainly three domains of preprocessing layers:
- Natural Language
- Structured Data
- Images
-
Natural Language:
- tf.keras.layers.TextVectorization: An input string (which can be sentences) and it will split the document into individual tokens and map each token to specific index turning document intoa numeric input that can be feed into model. This is called tokenization.
input_data = tf.constant(['The quick brown fox jumped.'])
layer = tf.keras.layers.TextVectorization(vocabulary = ['the', 'quick', 'brown'])
layer(input_data)
- For TextVectorization, the main piece of configuration is the vocabulary of known terms.
- The number 1 is used to refer terms that are not in the vocabulary and 0 is padding term.
- For the above example, for the given tensor string, the output is numpy=array( [ [2,3,4,1,1]])
-
Structured Data:
- Two keras layers
-
One set of layers is for categorical inputs tf.keras.layers.StringLookup - String lookup tf.keras.layers.IntegerLookup - Integer lookup tf.keras.layers.Hashing tf.keras.layers.CategoryEncoding - something like onehot encoding
-
A couple of layers for numerical inputs: tf.keras.layres.Normalization tf.keras.layers.Discretiziation - Floating point to categorical value by going through a step function.
-
- Two keras layers
-
Images related layers:
- layers for standardization:
- tf.keras.layers.Resizing
- tf.keras.layers.CenterCrop
- tf.keras.layers.Rescaling – if the image has color characters
- layers for augmenting data
- tf.keras.layers.RandomCrop, RandomFlip, RandomTranslation, RandomRotation, RandomZoom etc.
- layers for standardization:
-
.adapt()
- A utility for learning state directly from data.
- ONly applies to stateful layers
- Calling adapt() function for text will go over the entire data and create4 vocabulary. For Normalization, adapt learns mean and variance.
- Meant for single machine utility. For map-reduce job, consider TensorFlow Transform.