Path: blob/master/site/en-snapshot/lattice/tutorials/premade_models.ipynb
25118 views
Copyright 2020 The TensorFlow Authors.
TF Lattice Premade Models
Overview
Premade Models are quick and easy ways to build TFL tf.keras.model
instances for typical use cases. This guide outlines the steps needed to construct a TFL Premade Model and train/test it.
Setup
Installing TF Lattice package:
Importing required packages:
Setting the default values used for training in this guide:
Downloading the UCI Statlog (Heart) dataset:
Feature Configs
Feature calibration and per-feature configurations are set using tfl.configs.FeatureConfig. Feature configurations include monotonicity constraints, per-feature regularization (see tfl.configs.RegularizerConfig), and lattice sizes for lattice models.
Note that we must fully specify the feature config for any feature that we want our model to recognize. Otherwise the model will have no way of knowing that such a feature exists.
Defining Our Feature Configs
Now that we can compute our quantiles, we define a feature config for each feature that we want our model to take as input.
Set Monotonicities and Keypoints
Next we need to make sure to properly set the monotonicities for features where we used a custom vocabulary (such as 'thal' above).
Finally we can complete our feature configs by calculating and setting the keypoints.
Calibrated Linear Model
To construct a TFL premade model, first construct a model configuration from tfl.configs. A calibrated linear model is constructed using the tfl.configs.CalibratedLinearConfig. It applies piecewise-linear and categorical calibration on the input features, followed by a linear combination and an optional output piecewise-linear calibration. When using output calibration or when output bounds are specified, the linear layer will apply weighted averaging on calibrated inputs.
This example creates a calibrated linear model on the first 5 features.
Now, as with any other tf.keras.Model, we compile and fit the model to our data.
After training our model, we can evaluate it on our test set.
Calibrated Lattice Model
A calibrated lattice model is constructed using tfl.configs.CalibratedLatticeConfig. A calibrated lattice model applies piecewise-linear and categorical calibration on the input features, followed by a lattice model and an optional output piecewise-linear calibration.
This example creates a calibrated lattice model on the first 5 features.
As before, we compile, fit, and evaluate our model.
Calibrated Lattice Ensemble Model
When the number of features is large, you can use an ensemble model, which creates multiple smaller lattices for subsets of the features and averages their output instead of creating just a single huge lattice. Ensemble lattice models are constructed using tfl.configs.CalibratedLatticeEnsembleConfig. A calibrated lattice ensemble model applies piecewise-linear and categorical calibration on the input feature, followed by an ensemble of lattice models and an optional output piecewise-linear calibration.
Explicit Lattice Ensemble Initialization
If you already know which subsets of features you want to feed into your lattices, then you can explicitly set the lattices using feature names. This example creates a calibrated lattice ensemble model with 5 lattices and 3 features per lattice.
As before, we compile, fit, and evaluate our model.
Random Lattice Ensemble
If you are not sure which subsets of features to feed into your lattices, another option is to use random subsets of features for each lattice. This example creates a calibrated lattice ensemble model with 5 lattices and 3 features per lattice.
As before, we compile, fit, and evaluate our model.
RTL Layer Random Lattice Ensemble
When using a random lattice ensemble, you can specify that the model use a single tfl.layers.RTL
layer. We note that tfl.layers.RTL
only supports monotonicity constraints and must have the same lattice size for all features and no per-feature regularization. Note that using a tfl.layers.RTL
layer lets you scale to much larger ensembles than using separate tfl.layers.Lattice
instances.
This example creates a calibrated lattice ensemble model with 5 lattices and 3 features per lattice.
As before, we compile, fit, and evaluate our model.
Crystals Lattice Ensemble
Premade also provides a heuristic feature arrangement algorithm, called Crystals. To use the Crystals algorithm, first we train a prefitting model that estimates pairwise feature interactions. We then arrange the final ensemble such that features with more non-linear interactions are in the same lattices.
the Premade Library offers helper functions for constructing the prefitting model configuration and extracting the crystals structure. Note that the prefitting model does not need to be fully trained, so a few epochs should be enough.
This example creates a calibrated lattice ensemble model with 5 lattice and 3 features per lattice.
As before, we compile, fit, and evaluate our model.