Skip to content

Transformer

The Transformer class is an optional component in the project repository. The goal of this class is to have custom transformations for prediction time. It is located in the transformer/transformer.py file. Currently transforming inputs into features is supported, transforming the output of your model is coming soon.

In a deployment, this class is instantiated at the start of the process and every request to the normal prediction route is passed through the transform_input method if available and after the prediction has been made it will be passed through the transform_output method if available.

Input transformation

A common use case for wanting to transform your input at a prediction request in a deployment is to transform some identifier into the relevant features. There are two benefits to this pattern. The first is that the downstream task that requests the prediction does not need to gather all the features themselves. Instead, using some identifier with some optional metadata makes more sense from a team decoupling perspective. On the other hand, by tightly coupling the transformer with the rest of the code, it makes adding additional features to your data much easier, because the actual request from the downstream task will not have to change.

 class Transformer():
     def __init__(self, secrets):
         self.secrets = secrets  # IE: this could open a database connection

    def transform_input(self, inputs):
        features = custom_transformation(inputs)
        return features

Configuration

The input for the transformation is unknown upfront, this means Cubonacci needs to get the schema configured from the repository. This configuration is easy and located in the transformer folder in a file named cubonacci-transformer.yaml.

The first key is api with a boolean value whether or not this transformer is applied to deployments. The second key is the input_schema which describes the different values that can be passed. In your code, these inputs are passed as a dictionary.

api: true
input_schema:
  - name: profile_id
    type: string