Path: blob/master/site/en-snapshot/lattice/tutorials/canned_estimators.ipynb
25118 views
Copyright 2020 The TensorFlow Authors.
TF Lattice Canned Estimators
Warning: Estimators are not recommended for new code. Estimators run
v1. Session
-style code which is more difficult to write correctly, and can behave unexpectedly, especially when combined with TF 2 code. Estimators do fall under our compatibility guarantees, but will receive no fixes other than security vulnerabilities. See the migration guide for details.
Overview
Canned estimators are quick and easy ways to train TFL models for typical use cases. This guide outlines the steps needed to create a TFL canned estimator.
Setup
Installing TF Lattice package:
Importing required packages:
Downloading the UCI Statlog (Heart) dataset:
Setting the default values used for training in this guide:
Feature Columns
As for any other TF estimator, data needs to be passed to the estimator, which is typically via an input_fn and parsed using FeatureColumns.
TFL canned estimators use the type of the feature column to decide what type of calibration layer to use. We use a tfl.layers.PWLCalibration
layer for numeric feature columns and a tfl.layers.CategoricalCalibration
layer for categorical feature columns.
Note that categorical feature columns are not wrapped by an embedding feature column. They are directly fed into the estimator.
Creating input_fn
As for any other estimator, you can use an input_fn to feed data to the model for training and evaluation. TFL estimators can automatically calculate quantiles of the features and use them as input keypoints for the PWL calibration layer. To do so, they require passing a feature_analysis_input_fn
, which is similar to the training input_fn but with a single epoch or a subsample of the data.
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.
If no configuration is defined for an input feature, the default configuration in tfl.config.FeatureConfig
is used.
Calibrated Linear Model
To construct a TFL canned estimator, construct a model configuration from tfl.configs
. A calibrated linear model is constructed using 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. We use tfl.visualization
to plot the model graph with the calibrator plots.
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.
Calibrated Lattice Ensemble
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.
Random Lattice Ensemble
The following model config uses a random subset of features for each lattice.
RTL Layer Random Lattice Ensemble
The following model config uses a tfl.layers.RTL
layer that uses a random subset of features for each lattice. 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.
Crystals Lattice Ensemble
TFL also provides a heuristic feature arrangement algorithm, called Crystals. The Crystals algorithm first trains a prefitting model that estimates pairwise feature interactions. It then arranges the final ensemble such that features with more non-linear interactions are in the same lattices.
For Crystals models, you will also need to provide a prefitting_input_fn
that is used to train the prefitting model, as described above. The prefitting model does not need to be fully trained, so a few epochs should be enough.
You can then create a Crystal model by setting lattice='crystals'
in the model config.
You can plot feature calibrators with more details using the tfl.visualization
module.