一份使用Perseus框架在T-Money数据集上完成检索、排序、分类和回归任务的分步指南。
AI translation, not an official translation. Refer to the original for technical details.
Adapted from @MrSkyAI所提供的文本概述了一份关于如何使用Perseus框架执行各类机器学习任务的综合指南,具体聚焦于使用T-Money数据集完成检索(推荐)、排序、分类和回归任务。以下是每项任务的摘要及相关代码片段:
1. 检索任务(推荐)
目标:根据用户交互历史推荐物品。
数据准备
```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)
```
模型配置(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
```
训练与推理命令
```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. 排序任务
目标:根据用户交互历史对物品进行排序。
数据准备
```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)
```
模型配置(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
```
训练与推理命令
```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. 分类任务
目标:预测用户下周的活跃情况。
数据准备
```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)
```
模型配置(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
```
训练与推理命令
```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. 回归任务
目标:预测用户下个月将购买物品的总费用。
数据准备
```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)
```
模型配置(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
```
训练与推理命令
```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
```
总结
本指南提供了一种使用Perseus框架设置和运行机器学习任务的分步方法。每项任务均包含数据准备、模型配置、训练和推理步骤。所提供的代码片段和配置可根据不同数据集和需求进行调整。
https://t.co/77u409mJq8