tensorflow / lattice

Lattice methods in TensorFlow
Apache License 2.0
518 stars 94 forks source link

convex by pieces function #63

Closed matibilkis closed 3 years ago

matibilkis commented 3 years ago

Hi,

I wonder if you have a functionality to specify that the target function should be convex by pieces, and/or monotonic by pieces.

Thanks for writing this amazing piece of software :) Matias

mmilanifard commented 3 years ago

Not quite sure what you mean by "by pieces". PWL calibration layers can be constrained to be convex using convexity in PWLCalibration layer, or pwl_calibration_convexity in FeatureConfig for canned models.

If you want end to end convexity, you should be using monotonic lattices with lattice_size of 2.

matibilkis commented 3 years ago

Thanks for the answer!

By pieces I mean something similar to these definitions.

Let's say we have a function f defined over [0,1]. This function is convex on interval [0, 0.5] and concave on invertal [0.5, 1]. is there any easy way to use PWLCalibration layer and its convexity constrains to better fit these kind of functions ?

Thanks!

mmilanifard commented 3 years ago

If you know the inflection point (0.5 in your example), then you can achieve that by using the sum of 2 PWL calibration layers, one defined with input keypoints in range [0, 0.5] and another [0.5, 1], each with their own set of constraints. The output of each layer is flat outside the range of input keypoints.

I also suggest looking into the Wrinkle Regularizer that penalizes the changes in the second derivative. If your data approximately supports the form of convexity/concavity that you are expecting, then this regularizer helps you avoid rapid curvature changes. See example usage here.