Scenario training process set-up
How to configure the foundation model loading and scenario training process?
Check This First!
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.
In this module, we assume you have already selected the appropriate modelling task for you business problem and written your target function. Therefore, we will cover only the remaining sections of a business scenario training script:
- Importing the required classes and packages, declaring variables etc.
- Instantiating the trainer: loading from the foundation model with optional customization.
- Defining parameters of the training, incl. overwriting those defined at the foundation model stage if needed.
- Running the training with the trainer's
fit()
method.
We explain each section one by one below.
Necessary imports and variables
To set up and train a business scenario model in BaseModel, you need to import the relevant BaseModel classes :
-
You will always need:
Attributes
andEvents
frommonad.ui.target_function
- to use them to define the model target,load_from_foundation_model
frommonad.ui.module
- to instantiate the trainer based on foundation model,TrainingParams
frommonad.ui.config
- to set the parameters of your training.
-
Specifically, from
from monad.ui.module
you will also need the appropriateTask
class for your scenario, which would be one of:BinaryClassificationTask
,MulticlassClassificationTask
,MultilabelClassificationTask
,RegressionTask
,RecommendationTask
.
Note
Some other classes or packages may be required to define your target function, load or transform data, such as list of target products. Examples include
torch
or its classes,numpy
,os
,typing
,datetime
etc. The snippet shown below includes some of them.
It is also recommended to declare the variables such as models' location, custom loggers, loss functions etc. in this starting section.
Hint
Several variables serve as inputs for the functions described in the sections below. Declaring them upfront typically facilitates experimentation and results in tidier code, especially for paths to model locations, which may be fairly long depending on your infrastructure.
See an example of the starting section below, with variables specifying models' locations:
- The location of our trained foundation model, pointing to the
/fm
subdirectory. - The location where we want BaseModel to store our new business scenario model, specified by the
checkpoint_dir
parameter.
from monad.ui.config import TrainingParams # always needed
from monad.ui.target_function import Attributes, Events # always needed
from monad.ui.module import load_from_foundation_model # always needed
from monad.ui.module import BinaryClassificationTask # specific to the scenario
from typing import Dict # (if needed/ example)
import torch # (if needed/ example)
import os # (if needed/ example)
fm_path = "/path/to/your/models/pretrain/fm" # parameter for: load_from_foundation_model
scenario_path = "path/to/your/models/downstream/current_model" # parameter for: training_params
Instantiating the trainer
We instantiate the trainer by calling load_from_foundation_model
and providing the mandatory arguments:
-
Foundation model location as
checkpoint_path
(not to be confused withcheckpoint_dir
, the location where we save the scenario model, provided totraining_params
). -
Scenario task as
downstream_task
, the class we imported, aligned with our ML problem. -
Name of target function as
target_fn
. -
Size of the output (except recommendation task) as
num_outputs
, which is equal to:-
1, for binary classification and regression.
-
The number of classes, for multiclass and multi-label classification.
-
In this process, all data sources are loaded automatically, and all dates for training, validation, and test sets are defined as in the foundation model configuration. The following example demonstrates this section with its mandatory entries only:
trainer = load_from_foundation_model(
checkpoint_path = fm_path,
downstream_task = BinaryClassificationTask(),
target_fn = bin_target_fn, # the name of our target function
num_outputs = 1, # number of outputs for binary classification is equal to 1
)
Please note that BaseModel allows you to adjust many other parameters configured for Foundation Model training. To learn more, please refer to this subpage.
Training parameters
As described in this article, the training parameters were defined for the first time when training the foundation model, as part of the YAML
config file. Once you load your foundation model, all these training parameters defined back then will also get loaded and used by default when training the scenario model. The only new mandatory parameter is the target location of your new scenario model, which we need to store as the checkpoint_dir
parameter.
As with the foundation model loading, BaseModel also lets you fine-tune the training by allowing you to modify its parameters. For detailed information, please refer to this section.
Have a look at an example where we overwrite a few selected parameters:
training_params = TrainingParams(
checkpoint_dir=scenario_path,
epochs=1,
learning_rate=0.001,
devices=[0]
)
Running the training
The last part of the script is to run the training, which we do by calling the fit
method of the trainer and providing it with the training_params
we have defined above.
trainer.fit(training_params = training_params)
Updated 8 days ago