model = tf.keras.models.Sequential()
model.add(
tfl.layers.ParallelCombination([
# Feature: average rating
tfl.layers.PWLCalibration(
# Input keypoints for the piecewise linear function
input_keypoints=np.linspace(1., 5., num=20),
# Output is monotonically increasing in this feature
monotonicity='increasing',
# This layer is feeding into a lattice with 2 vertices
output_min=0.0,
output_max=1.0),
# Feature: number of reviews
tfl.layers.PWLCalibration(
input_keypoints=np.linspace(0., 200., num=20),
# Output is monotonically increasing in this feature
monotonicity='increasing',
# There is diminishing returns on the number of reviews
convexity='concave',
# Regularizers defined as a tuple ('name', l1, l2)
kernel_regularizer=('wrinkle', 0.0, 1.0),
# This layer is feeding into a lattice with 3 vertices
output_min=0.0,
output_max=2.0),
# Feature: dollar rating
tfl.layers.CategoricalCalibration(
# 4 rating categories + 1 missing category
num_buckets=5,
default_input_value=-1,
# Partial monotonicity: calib(0) <= calib(1)
monotonicities=[(0, 1)],
# This layer is feeding into a lattice with 2 vertices
output_min=0.0,
output_max=1.0),
]))
model.add(
tfl.layers.Lattice(
# A 2x3x2 grid lattice
lattice_size=[2, 3, 2],
# Output is monotonic in all inputs
monotonicities=['increasing', 'increasing', 'increasing']
# Trust: more responsive to input 0 if input 1 increases
edgeworth_trusts=(0, 1, 'positive')))
model.compile(...)
最终训练得到的模型满足所有指定的约束,而添加的正则化使函数变得平滑
上面的模型也可以使用库提供的罐装估计器来构建。查看我们的形状约束教程,了解更多信息,其中包含一个端到端的 Colab,描述了每个所述约束的效果。TF Lattice Keras 层也可以与其他 Keras 层结合使用,以构建部分受约束或正则化的模型。例如,格子或 PWL 校准层可以在包含嵌入或其他 Keras 层的更深层网络的最后一层使用。有关更多信息,请查看TensorFlow Lattice 网站。有很多指南和教程可以帮助您入门:形状约束、罐装估计器、自定义估计器 和Keras 层。同时查看我们在 TF Dev Summit 上的演示