Skip to content

Commit

Permalink
Add doc for postprocessors (#531)
Browse files Browse the repository at this point in the history
  • Loading branch information
hcho3 authored Oct 31, 2023
1 parent 149bd6e commit eb16c6a
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 5 deletions.
2 changes: 1 addition & 1 deletion docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ Import your tree ensemble model into Treelite:
.. code-block:: python
import treelite
model = treelite.Model.load("my_model.model", model_format="xgboost")
model = treelite.frontend.load_xgboost_model("my_model.json")
Compute predictions using :doc:`treelite-gtil-api`:

Expand Down
8 changes: 8 additions & 0 deletions docs/knobs/index.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
====================
Knobs and Parameters
====================

.. toctree::
:maxdepth: 1

postprocessor
28 changes: 28 additions & 0 deletions docs/knobs/postprocessor.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
List of postprocessor functions
===============================
When predicting with tree ensemble models, we sum the margin scores from individual trees and apply a postprocessor
function to transform the sum into a final prediction. This function is also known as the link function.

Currently, Treelite supports the following postprocessor functions.

Element-wise postprocessor functions
------------------------------------
* ``identity``: The identity function. Do not apply any transformation to the margin score vector.
* ``signed_square``: Apply the function ``f(x) = sign(x) * (x**2)`` element-wise to the margin score vector.
* ``hinge``: Apply the function ``f(x) = (1 if x > 0 else 0)`` element-wise to the margin score vector.
* ``sigmoid``: Apply the sigmoid function ``f(x) = 1/(1+exp(-sigmoid_alpha * x))`` element-wise to the margin score
vector, to transform margin scores into probability scores in the range ``[0, 1]``. The ``sigmoid_alpha`` parameter
can be configured by the user.
* ``exponential``: Apply the exponential function (``exp``) element-wise to the margin score vector.
* ``exponential_standard_ratio``: Apply the function ``f(x) = exp2(-x / ratio_c)`` element-wise to the margin score
vector. The ``ratio_c`` parameter can be configured by the user.
* ``logarithm_one_plus_exp``: Apply the function ``f(x) = log(1 + exp(x))`` element-wise to the margin score vector.

Row-wise postprocessor functions
--------------------------------
* ``identity_multiclass``: The identity function. Do not apply any transformation to the margin score vector.
* ``softmax``: Use the softmax function ``f(x) = exp(x) / sum(exp(x))`` to the margin score vector, to transform the
margin scores into probability scores in the range ``[0, 1]``. Adding up the transformed scores for all classes
will yield 1.
* ``multiclass_ova``: Apply the sigmoid function ``f(x) = 1/(1+exp(-sigmoid_alpha * x))`` element-wise to the margin
scores. The ``sigmoid_alpha`` parameter can be configured by the user.
1 change: 1 addition & 0 deletions docs/serialization/v4.rst
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ written to the byte sequence in the exact order they appear in the following lis
* Model parameters

- ``postprocessor``: an array of ``char``. Stores a human-readable name of the post-processing function that's applied to prediction outputs.
Consult :doc:`/knobs/postprocessor` for the list of available postprocessors.
- ``sigmoid_alpha``: single ``float`` scalar. This model parameter is relevant when ``postprocessor="sigmoid"``.
- ``ratio_c``: single ``float`` scalar. This model parameter is relevant when ``postprocessor="exponential_standard_ratio"``.
- ``base_scores``: an array of ``double``. This vector is expected to have length ``num_target * max(num_class)``. The elements will be laid out in the row-major layout.
Expand Down
4 changes: 3 additions & 1 deletion docs/tutorials/builder.rst
Original file line number Diff line number Diff line change
Expand Up @@ -416,7 +416,9 @@ indicates a strong vote for the positive class.)

To obtain probability scores, we pass the sum of the tree outputs through a **link function**
``sigmoid(x) = 1/(1+exp(-x))``. In the model builder API,
the link function is specified by the ``postprocessor`` argument. Let's look at how the builder object is constructed:
the link function is specified by the ``postprocessor`` argument. (Consult :doc:`/knobs/postprocessor` for the list of
available postprocessors.)
Let's look at how the builder object is constructed:

.. code-block:: python
Expand Down
4 changes: 2 additions & 2 deletions docs/tutorials/edit.rst
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
Editing tree models (Advanced)
------------------------------
==============================

:ref:`The field accessor API <field_accessors>` allows users to inspect and edit tree model objects after they have been
constructed. Here are some examples:
Expand Down Expand Up @@ -31,7 +31,7 @@ constructed. Here are some examples:
Consult :doc:`/serialization/v4` for the list of fields.

How to use setter methods
=========================
-------------------------
There are lots of gotchas and pitfalls involved when using :py:meth:`~treelite.model.TreeAccessor.set_field` to modify
trees. We must start by the following notice:

Expand Down
3 changes: 2 additions & 1 deletion python/treelite/model_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,8 @@ class PostProcessorFunc:
Parameters
----------
name:
Name of the postprocessor
Name of the postprocessor. Consult :py:doc:`/knobs/postprocessor` for the list
of available postprocessor functions.
sigmoid_alpha:
Scaling parameter for sigmoid function ``sigmoid(x) = 1 / (1 + exp(-alpha * x))``.
This parameter is applicable only when ``name="sigmoid"`` or ``name="multiclass_ova"``.
Expand Down

0 comments on commit eb16c6a

Please sign in to comment.