A step-by-step guide to using the Perseus framework for retrieval, ranking, classification, and regression tasks with the T-Money dataset.
Perseus framework guides machine learning tasks
The provided text outlines a comprehensive guide on how to use the Perseus framework for various machine learning tasks, specifically focusing on retrieval (recommendation), ranking, classification, and regression tasks using the T-Money dataset. Below is a summary of each task along with relevant code snippets:
1. Retrieval Task (Recommendation)
Objective: Recommend items based on user interaction history.
Data Preparation
```python
Perseus framework guides machine learning tasks
samples = pl.concat([
https://t.co/rOXe8Qs57i_parquet("/event-hub/marketplace-click"),
https://t.co/rOXe8Qs57i_parquet("/event-hub/marketplace-like"),
https://t.co/rOXe8Qs57i_parquet("/event-hub/marketplace-clickout")
])
Filter and process timestamps
start_date = samples["date"].max() - timedelta(days=120)
samples = (
samples.filter(pl.col("timestamp") >= start_date)
.with_columns((pl.col("date").cast(pl.Datetime) - timedelta(hours=12)).alias("timestamp"))
.drop(["date"])
)
Group by timestamp and client_id
train_samples, test_samples = train_test_split(samples, test_size=0.2, random_state=42)
```
Model Configuration (config.yaml)
```yaml
task: type: retrieval
metrics:
recall@100:
type: recall_at_k
params:
k: 100
events:
marketplace-click:
attributes: item_id
max_duration_per_sequence: 365d
backbone:
dim: 256
history_aggregator:
type: modern_bert
params:
num_layers: 4
num_heads: 4
```
Training and Inference Commands
```bash
uv run python -m perseus train prepare-dataset --workdir ../retrieval
uv run accelerate launch -m perseus train fit-model --workdir ../retrieval
uv run python -m perseus inference make-backbone-embeddings --workdir ../retrieval
uv run python -m perseus inference make-head-predictions --workdir ../retrieval
```
2. Ranking Task
Objective: Rank items based on user interaction history.
Data Preparation
```python
Perseus framework guides machine learning tasks
samples = pl.concat([
https://t.co/rOXe8Qs57i_parquet("/event-hub/marketplace-clickout").with_columns(pl.lit("clickout")),
https://t.co/rOXe8Qs57i_parquet("/event-hub/marketplace-click").with_columns(pl.lit("click")),
https://t.co/rOXe8Qs57i_parquet("/event-hub/marketplace-like").with_columns(pl.lit("like")),
https://t.co/rOXe8Qs57i_parquet("/event-hub/marketplace-view").with_columns(pl.lit("view"))
])
Filter and process timestamps
start_date = samples["date"].max() - timedelta(days=120)
samples = (
samples.filter(pl.col("timestamp") >= start_date)
.with_columns((pl.col("date").cast(pl.Datetime) - timedelta(hours=12)).alias("timestamp"))
.drop(["date"])
)
Group by timestamp and client_id
train_samples, test_samples = train_test_split(samples, test_size=0.2, random_state=42)
```
Model Configuration (config.yaml)
```yaml
task: type: ranking
metrics:
ndcg@20:
type: ndcg_at_k
params:
k: 20
events:
marketplace-clickout:
attributes: item_id
max_duration_per_sequence: 365d
backbone:
dim: 256
history_aggregator:
type: modern_bert
params:
num_layers: 4
num_heads: 4
```
Training and Inference Commands
```bash
uv run python -m perseus train prepare-dataset --workdir ../ranking
uv run accelerate launch -m perseus train fit-model --workdir ../ranking
uv run python -m perseus inference make-backbone-embeddings --workdir ../ranking
uv run python -m perseus inference make-head-predictions --workdir ../ranking
```
3. Classification Task
Objective: Predict user activity in the next week.
Data Preparation
```python
Perseus framework guides machine learning tasks
samples = pl.concat([
https://t.co/rOXe8Qs57i_parquet("/event-hub/marketplace-click"),
https://t.co/rOXe8Qs57i_parquet("/event-hub/marketplace-like"),
https://t.co/rOXe8Qs57i_parquet("/event-hub/marketplace-clickout")
]).select(["date", "timestamp", "client_id"])
Filter and process timestamps
start_date = samples["date"].max() - timedelta(days=120)
samples = (
samples.filter(pl.col("timestamp") >= start_date)
.with_columns((pl.col("date").cast(pl.Datetime) - timedelta(hours=12)).alias("timestamp"))
.drop(["date"])
)
Calculate future activity
horizon = timedelta(days=7)
next_week_visits = samples.rolling(index_column="timestamp", period="7d", offset="0d", closed="right", group_by="client_id").agg(pl.len().alias("num_visits"))
train_samples, test_samples = train_test_split(samples.join(next_week_visits), test_size=0.2, random_state=42)
```
Model Configuration (config.yaml)
```yaml
task: type: classification
metrics:
roc_auc:
params:
pos_label: visit
events:
marketplace-click:
attributes: item_id
max_duration_per_sequence: 365d
backbone:
dim: 256
history_aggregator:
type: modern_bert
params:
num_layers: 4
num_heads: 4
```
Training and Inference Commands
```bash
uv run python -m perseus train prepare-dataset --workdir ../classification
uv run accelerate launch -m perseus train fit-model --workdir ../classification
uv run python -m perseus inference make-backbone-embeddings --workdir ../classification
uv run python -m perseus inference make-head-predictions --workdir ../classification
```
4. Regression Task
Objective: Predict the total cost of items a user will purchase in the next month.
Data Preparation
```python
Perseus framework guides machine learning tasks
events = pl.concat([
https://t.co/rOXe8Qs57i_parquet("/event-hub/marketplace-click")
])
Filter and process timestamps
start_date = events["date"].max() - timedelta(days=120)
events = (
events.filter(pl.col("timestamp") >= start_date)
.with_columns((pl.col("date").cast(pl.Datetime) - timedelta(hours=12)).alias("timestamp"))
.drop(["date"])
)
Calculate future activity
horizon = timedelta(days=30)
next_month_purchases = events.rolling(index_column="timestamp", period="30d", offset="0d", closed="right", group_by="client_id").agg(pl.sum("price"))
train_samples, test_samples = train_test_split(events.join(next_month_purchases), test_size=0.2, random_state=42)
```
Model Configuration (config.yaml)
```yaml
task: type: regression
metrics:
mae:
params:
pos_label: visit
events:
marketplace-click:
attributes: item_id
max_duration_per_sequence: 365d
backbone:
dim: 256
history_aggregator:
type: modern_bert
params:
num_layers: 4
num_heads: 4
```
Training and Inference Commands
```bash
uv run python -m perseus train prepare-dataset --workdir ../regression
uv run accelerate launch -m perseus train fit-model --workdir ../regression
uv run python -m perseus inference make-backbone-embeddings --workdir ../regression
uv run python -m perseus inference make-head-predictions --workdir ../regression
```
Summary
The guide provides a step-by-step approach to setting up and running machine learning tasks using the Perseus framework. Each task involves data preparation, model configuration, training, and inference steps. The provided code snippets and configurations can be adapted for different datasets and requirements.
https://t.co/77u409mJq8