透過格狀模型實現彈性、可控且可解釋的 ML

import numpy as np
import tensorflow as tf
import tensorflow_lattice as tfl

model = tf.keras.models.Sequential()
model.add(
    tfl.layers.ParallelCombination([
        # Monotonic piece-wise linear calibration with bounded output
        tfl.layers.PWLCalibration(
            monotonicity='increasing',
            input_keypoints=np.linspace(1., 5., num=20),
            output_min=0.0,
            output_max=1.0),
        # Diminishing returns
        tfl.layers.PWLCalibration(
            monotonicity='increasing',
            convexity='concave',
            input_keypoints=np.linspace(0., 200., num=20),
            output_min=0.0,
            output_max=2.0),
        # Partially monotonic categorical calibration: calib(0) <= calib(1)
        tfl.layers.CategoricalCalibration(
            num_buckets=4,
            output_min=0.0,
            output_max=1.0,
            monotonicities=[(0, 1)]),
    ]))
model.add(
    tfl.layers.Lattice(
        lattice_sizes=[2, 3, 2],
        monotonicities=['increasing', 'increasing', 'increasing'],
        # Trust: model is more responsive to input 0 if input 1 increases
        edgeworth_trusts=(0, 1, 'positive')))
model.compile(...)

TensorFlow Lattice 是一個實作受限且可解釋格狀模型的函式庫。這個函式庫可讓您透過常識或政策驅動的形狀限制,將領域知識注入學習過程。這是透過一系列 Keras 層來完成的,這些 Keras 層可以滿足單調性、凸性和特徵互動方式等限制。此函式庫也提供易於設定的預製模型

透過 TF Lattice,您可以使用領域知識,以便更好地外推到訓練資料集未涵蓋的輸入空間部分。這有助於避免在服務分配與訓練分配不同時,發生意想不到的模型行為。