Scenario Model Stage Overview

The overview of the workflow

πŸ“˜

Note

This article refers to BaseModel accessed via Docker container. Please refer to Snowflake Native App section if you are using BaseModel as SF GUI application.


The Foundation Model is designed to understand the behavior and interactions between entities and develop a general predictive capability in the domain. During training, it doesn’t have more specific goal than that.

To address a particular business problem, you need to create a downstream model tailored to the scenario, by fine-tuning the Foundation Model for the specific task.

In this article we focus on that step:

To build a downstream model for their particular business objective, the user needs to:

  1. Adapt the training script template, which includes the following tasks:

    • Define the Task and Target:

      • Identify the machine learning problem:
        Determine the machine learning problem that aligns with the business objective.
      • Define the target function:
        Specify the function that will guide the model optimization process.

    • Fine-tune the learning process:

      • Select the pre-trained Foundation Model:
        Point the script to the directory containing the features of the pre-trained model.
      • Configure the modelling task:
        Set up the downstream task and, if necessary, adjust the training parameters.
      • Instantiate the trainer:
        Create an instance of the trainer and, if needed, modify the loading process.

  2. Execute the training using a Python function or via the command line.

Follow the links above, or proceed to the following articles for detailed explanations of the above tasks.
You can see all these elements in an example of a training script below.

End-to-End Example
from typing import Dict
from monad.ui.config import TrainingParams
from monad.ui.module import load_from_foundation_model, BinaryClassificationTask
from monad.ui.target_function import Attributes, Events

import torch
import os

# selecting the task
task = BinaryClassificationTask()
num_outputs = 1 # number of outputs of NN - for binary classification equal to 1

# specifying the target
def target_fn(history: Events, future: Events, _entity: Attributes, _ctx: Dict) -> np.ndarray:
    if history["product.buy"].count() == 0:
        return None

    # churn definition:
    # 1 - churned
    # 0 - not churned
    
    churn = 0 if future["product.buy"].count() > 0 else 1

    # output should be a float32 numpy array
    return np.array([churn], dtype=np.float32) 

# selecting the foundation model
fm_path = "/path/to/your/models/pretrain/fm" # parameter for: load_from_foundation_model
checkpoint_dir = "path/to/your/models/downstream/model_name" # parameter for: training_params

# adapting training parameters
training_params = TrainingParams(
    checkpoint_dir=checkpoint_dir,
    epochs=1,
    learning_rate=0.001,
    devices=[0]
)

# instantiating the trainer & training the model
if __name__ == "__main__":
    trainer = load_from_foundation_model(
        checkpoint_path=fm_path,
        downstream_task=task, 
        target_fn=target_fn, 
        num_outputs=num_outputs
    )
    trainer.fit(training_params=training_params)

Once ready, you can save the training script as filename.py and run them from your Python console.