Recommendations Target Function

Making suggestions with confidence

⚠️

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 subpage we will look at some examples of target functions for Recommendation problem - a specialized subset of machine learning that focuses on predicting the preferences or interests of users and suggesting items or services they are likely to enjoy or find useful. Recommendation models analyse past user behavior, item characteristics, and sometimes contextual information to identify patterns and relationships between users and items.

A few use case examples:

  • Retailers suggest the best products for the loyal customers reward campaign based on their purchase history.
  • A streaming or content platform recommends content to personalise user experiences and increase engagement.
  • E-commerce operators personalize the homepage with product recommendations tailored to each user.
  • Financial institution recommends products such as credit cards, loans, or investment opportunities based on a customer's financial profile and transaction history.
  • Travel operator offers the most relevant travel packages, destinations, or activities based on a user's past travel history and preferences.

Standard Template for Recommendation Target Functions

Each function for recommendation problem will:

  • accept as parameters history, future, entity and ctx, as described here,
  • perform some transformation on these inputs as explained in this section
  • output a sketch, an aggregated representation of user historical behavior (usage, purchase etc.) used by BaseModel.

def target_fn(_history: Events, future: Events, _entity: Attributes, _ctx: Dict) -> Sketch:
    
    # transformation of events into the desired target

    return sketch(entity_ids, training_weights)

Sketch as representation of behavior

To target a recommendation scenario we use a sketch - and advanced representations of entities created for BaseModel to enable efficient but lossless consumption of behavioral data during the model training.

  • Sketches approximate the distribution and frequency of entities (customers, items, stores etc.) within a behavioral dataset.
  • They leverage our proprietary technologies: Cleora (to identify inter-entity interaction graphs, eg. user-product), and emde (to efficiently group similar objects).
  • To create sketches, on top of graphs we can use other modalities, eg. product descriptions, pictures, etc.
  • For more in-depth explanation please refer to our blog post.

When fine-tuning foundation model for a recommendation problem, sketches allow the model to output the entities our main entity is most likely to interact with.

To construct a sketch as an output, we use sketch function that takes the following arguments:

  • items : ModalityEvents
    This argument points the function to the particular column in event data source that identifies an entity we need as recommendation - product, service, content, destination, store etc.

  • weights : np.array
    Weights are applied to predicted events, allowing us to put more importance to some of them, eg. emphasizing 'next event in sequence' to predict the very next basket. That (predicting next basket) is the default approach, where we calculate weights using the sequential_decay function with a parameter gamma=0.

    Example:

    recom_products = sketch(
        items = future["transactions"]["article_id"],
        weights = sequential_decay(
            events = future["transactions"],
            gamma = 0)
    

    📘

    Please note

    The basket is defined as products that have the same timestamp at the time of purchase.


Customizing recommendation target by applying decay functions to weights

BaseModel supports two types of decay function to apply weights to recommendation target function:

  • sequential_decay(events, gamma = 0.0, init_weights = None)
    Applies weights to the future purchases in sequence, by order of baskets. Time interval is not considered.

    Parameters:

    events: events considered future during training

    gamma: the parameter managing the speed of decay, ie. how much is the importance of subsequent baskets decreased. It can take any value between 0 and 1 and is applied as a sequential multiplier, e.g.:

    • gamma=0: the first event will have weight of 1, and each of the next ones will have weight of 0.
      This means we will use as target for the model only the first basket purchased in training future events.

    • gamma=1: the first event, and all subsequent events will be equally weighted as 1 (no decay).
      This means, the model will use all future events for training when trying to serve recommendation.

    • gamma=0.5 (or any other number between 0 and 1): the first event will have weight of 1, and the next one will have its weight multiplied by gamma (so with 0.5, it's 0.5, 0.25, 0.125 etc.)
      This means, each consecutive event contributes less to the training target.

  • time_decay(events, daily_decay = 0.0, init_weights = None)
    Applies weights not by the order of events, but by how much time has passed between events.
    The more time has passed since the split point, the less the event will contribute to the target.

    Parameters:

    events: events considered future during training

    daily_decay : the parameter controlling the speed of decay (how quickly the weights get lower over time):

    • daily_decay=1: the first event will have weight of 1, and each of the next ones will have weight of 0.
      This means we will use as target for the model only the first basket purchased in training future events.

    • daily_decay=0: the first event, and all subsequent events will be equally weighted as 1 (no decay).
      This means, the model will use all future events for training when trying to serve recommendation.

    • daily_decay=0.5 (or any other number between 0 and 1): the first event will have weight of 1, and all subsequent events will be weighted by daily decay to the power of number of days that passed since the initial one (eg. for 0.5, any event happening a day after will be weighted by 0.5, the following day 0.25 etc.)


End-to-end example of a target function for a recommendation problem

Please refer to the recipe linked below for an example of target function for a retail industry use case: recommend products for the next basket that a shopper will likely buy.

ℹ️