Spaces:
Running on Zero
Running on Zero
File size: 10,817 Bytes
5f7afef | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 | {
"slug": "adaboost-sample-weights-update",
"title": "手撕 AdaBoost 样本权重分布更新 (AdaBoost Sample Weights Update)",
"difficulty": "Easy",
"category": "ML",
"tags": [
"集成学习",
"AdaBoost",
"样本赋权"
],
"description_md": "### 题目描述\n给定样本当前权重 $w \\in \\mathbb{R}^N$ (满足 $\\sum w_i = 1$)、基学习器分类结果 $\\hat{y} \\in \\{-1, +1\\}^N$、真实标签 $y \\in \\{-1, +1\\}^N$ 及学习器权重 $\\alpha$,更新样本权重 $w_i \\leftarrow w_i \\exp(-\\alpha y_i \\hat{y}_i)$ 并将其重新归一化使和为 1。\n\n### 函数签名\n```python\ndef adaboost_update_weights(\n w: torch.Tensor,\n y_true: torch.Tensor,\n y_pred: torch.Tensor,\n alpha: float,\n) -> torch.Tensor:\n```\n\n### 示例\n```python\n# 输入\nw = [0.25, 0.25, 0.25, 0.25]\ny_true = [1.0, 1.0, -1.0, -1.0]\ny_pred = [1.0, -1.0, -1.0, 1.0]\nalpha = 0.6\n\n# 输出 (Tensor, shape=[4], dtype=float32)\n[0.1157, 0.3843, 0.1157, 0.3843]\n```\n\n### 参数规格与约束\n- **`w`**:`torch.Tensor`,测试规格 `shape=[4], dtype=float32`\n- **`y_true`**:`torch.Tensor`,测试规格 `shape=[4], dtype=float32`\n- **`y_pred`**:`torch.Tensor`,测试规格 `shape=[4], dtype=float32`\n- **`alpha`**:`float`\n- **返回值**:`torch.Tensor`,输出规格 `shape=[4], dtype=float32`\n- **精度容差**:`atol=0.0001, rtol=0.0001`",
"entry_point": "adaboost_update_weights",
"starter_code": "import torch\nimport torch.nn as nn\nimport torch.nn.functional as F\nimport math\nfrom typing import Optional, List, Tuple\n\ndef adaboost_update_weights(\n w: torch.Tensor,\n y_true: torch.Tensor,\n y_pred: torch.Tensor,\n alpha: float,\n) -> torch.Tensor:\n \"\"\"\n 手撕 AdaBoost 样本权重分布更新 (AdaBoost Sample Weights Update)\n \"\"\"\n # 请在此编写代码\n pass\n",
"reference_code": "import torch\nimport torch.nn as nn\nimport torch.nn.functional as F\nimport math\nfrom typing import Optional, List, Tuple\n\ndef adaboost_update_weights(w: torch.Tensor, y_true: torch.Tensor, y_pred: torch.Tensor, alpha: float) -> torch.Tensor:\n exps = torch.exp(-alpha * y_true * y_pred)\n new_w = w * exps\n return new_w / torch.sum(new_w)\n",
"forbidden_modules": [
"os",
"sys",
"subprocess",
"shutil",
"socket",
"requests"
],
"forbidden_calls": [],
"time_limit": 3.0,
"memory_limit_mb": 512,
"atol": 0.0001,
"rtol": 0.0001,
"test_code": "import torch\nimport torch.nn as nn\nimport torch.nn.functional as F\nimport math\n\ncases = [\n ('对错参半样本集', torch.tensor([0.25, 0.25, 0.25, 0.25], dtype=torch.float32), torch.tensor([1.0, 1.0, -1.0, -1.0], dtype=torch.float32), torch.tensor([1.0, -1.0, -1.0, 1.0], dtype=torch.float32), 0.6),\n ('多批次扩展泛化验证', torch.tensor([0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25], dtype=torch.float32), torch.tensor([1.0, 1.0, -1.0, -1.0, 1.0, 1.0, -1.0, -1.0], dtype=torch.float32), torch.tensor([1.0, -1.0, -1.0, 1.0, 1.0, -1.0, -1.0, 1.0], dtype=torch.float32), 0.6),\n ('多维批次与参数泛化验证', torch.tensor([0.25, 0.25, 0.25, 0.25], dtype=torch.float32), torch.tensor([1.0, 1.0, -1.0, -1.0], dtype=torch.float32), torch.tensor([1.0, -1.0, -1.0, 1.0], dtype=torch.float32), 0.48),\n ('边界极值与稳定性验证', torch.tensor([0.25, 0.25, 0.25, 0.25], dtype=torch.float32), torch.tensor([-1.0, -1.0, 1.0, 1.0], dtype=torch.float32), torch.tensor([1.0, -1.0, -1.0, 1.0], dtype=torch.float32), 0.72),\n]\nfor idx, (name, *args) in enumerate(cases, 1):\n t0 = time.time()\n try:\n exp = ref_obj(*args)\n act = user_obj(*args)\n match, err, max_diff = compare_outputs(act, exp, atol=atol, rtol=rtol)\n passed, msg = match, err\n except Exception as e:\n passed, msg = False, f\"Runtime Exception: {str(e)}\"\n act, exp, max_diff = None, None, None\n cases_results.append({\n \"case_id\": idx, \"case_name\": name, \"passed\": passed,\n \"time_ms\": round((time.time() - t0) * 1000, 2), \"message\": msg,\n \"user_output_summary\": summarize_obj(act) if act is not None else None,\n \"expected_summary\": summarize_obj(exp) if exp is not None else None,\n \"max_diff\": max_diff,\n })\n",
"is_featured": false,
"editorial_md": "#### 💡 核心算法原理与数学推导\n**手撕 AdaBoost 样本权重分布更新 (AdaBoost Sample Weights Update)**(`adaboost_update_weights`)聚焦于 **ML** 中的关键计算逻辑:\n\n给定样本当前权重 $w \\in \\mathbb{R}^N$ (满足 $\\sum w_i = 1$)、基学习器分类结果 $\\hat{y} \\in \\{-1, +1\\}^N$、真实标签 $y \\in \\{-1, +1\\}^N$ 及学习器权重 $\\alpha$,更新样本权重 $w_i \\leftarrow w_i \\exp(-\\alpha y_i \\hat{y}_i)$ 并将其重新归一化使和为 1。\n\n在实现中,函数接收参数 `w` (`torch.Tensor`)、`y_true` (`torch.Tensor`)、`y_pred` (`torch.Tensor`)、`alpha` (`float`),目标是计算出类型与形状严格符合 `torch.Tensor` 契约的结果。\n\n#### 📐 输入输出张量变换与步骤拆解\n- **输入基准规格**:`w` 规格为 `torch.Tensor(shape=[4], dtype=float32)`,`y_true` 规格为 `torch.Tensor(shape=[4], dtype=float32)`,`y_pred` 规格为 `torch.Tensor(shape=[4], dtype=float32)`,`alpha` 规格为 `float` (示例值: `0.6`)。\n- **期望输出规格**:`torch.Tensor(shape=[4], dtype=float32)`。\n\n**具体推导与计算步骤如下:**\n1. 计算中间量 `exps`:通过表达式 `torch.exp(-alpha * y_true * y_pred)` 完成张量或数值变换。\n2. 计算中间量 `new_w`:通过表达式 `w * exps` 完成张量或数值变换。\n3. 返回最终结果:输出 `new_w / torch.sum(new_w)`,严格对齐函数签名要求的返回类型与维度。\n\n#### 📊 复杂度分析\n- **时间复杂度**:$O(N)$(线性于输入张量总元素个数 $\\prod d_i$),所有运算均为向量化逐元素算子或沿指定维度的归约计算。\n- **空间复杂度**:$O(N)$,需要与输入或输出张量同规模的显存空间保存计算结果。\n\n#### ⚠️ 关键实现细节与易错点\n- **除零保护与数值平滑 (`eps`)**:在执行分母归一化、标准差缩放或对数运算时,必须按公式位置准确加上 `eps`(例如在平方根内 `sqrt(var + eps)` 还是分母外 `+ eps`),防止全零或常量输入触发 `NaN` / `Inf`。\n- **归约维度 (`dim`) 与广播对齐 (`keepdim`)**:调用 `sum`、`mean`、`max`、`norm` 或 `softmax` 时需明确指定目標维度(通常为最后一维 `dim=-1` 或特征维 `dim=0`),并在需要与原张量逐元素相减/相除时开启 `keepdim=True`。\n- **指数与对数域数值稳定性**:直接对大幅值 logits 计算 `exp` 易导致浮点数上溢(Overflow),对接近 0 的概率直接计算 `log` 易导致下溢;应优先使用 `F.log_softmax`、`F.logsigmoid` 或减去最大值的稳定化写法。\n\n#### 💻 官方标准参考实现\n```python\nimport torch\nimport torch.nn as nn\nimport torch.nn.functional as F\nimport math\nfrom typing import Optional, List, Tuple\n\ndef adaboost_update_weights(w: torch.Tensor, y_true: torch.Tensor, y_pred: torch.Tensor, alpha: float) -> torch.Tensor:\n exps = torch.exp(-alpha * y_true * y_pred)\n new_w = w * exps\n return new_w / torch.sum(new_w)\n```",
"difficulty_breakdown": {
"concept": 2,
"implementation": 2,
"math_derivation": 2,
"boundary_complexity": 1
},
"test_cases": [
{
"name": "对错参半样本集",
"input_desc": "真实参数契约用例 (w, y_true, y_pred, alpha)",
"params": [
{
"name": "w",
"type": "torch.Tensor",
"value": "torch.tensor([0.25, 0.25, 0.25, 0.25], dtype=torch.float32)"
},
{
"name": "y_true",
"type": "torch.Tensor",
"value": "torch.tensor([1.0, 1.0, -1.0, -1.0], dtype=torch.float32)"
},
{
"name": "y_pred",
"type": "torch.Tensor",
"value": "torch.tensor([1.0, -1.0, -1.0, 1.0], dtype=torch.float32)"
},
{
"name": "alpha",
"type": "float",
"value": "0.6"
}
],
"raw_inputs": {
"w": "torch.tensor([0.25, 0.25, 0.25, 0.25], dtype=torch.float32)",
"y_true": "torch.tensor([1.0, 1.0, -1.0, -1.0], dtype=torch.float32)",
"y_pred": "torch.tensor([1.0, -1.0, -1.0, 1.0], dtype=torch.float32)",
"alpha": "0.6"
}
},
{
"name": "多批次扩展泛化验证",
"input_desc": "真实参数契约用例 (w, y_true, y_pred, alpha)",
"params": [
{
"name": "w",
"type": "torch.Tensor",
"value": "torch.tensor([0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25], dtype=torch.float32)"
},
{
"name": "y_true",
"type": "torch.Tensor",
"value": "torch.tensor([1.0, 1.0, -1.0, -1.0, 1.0, 1.0, -1.0, -1.0], dtype=torch.float32)"
},
{
"name": "y_pred",
"type": "torch.Tensor",
"value": "torch.tensor([1.0, -1.0, -1.0, 1.0, 1.0, -1.0, -1.0, 1.0], dtype=torch.float32)"
},
{
"name": "alpha",
"type": "float",
"value": "0.6"
}
],
"raw_inputs": {
"w": "torch.tensor([0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25], dtype=torch.float32)",
"y_true": "torch.tensor([1.0, 1.0, -1.0, -1.0, 1.0, 1.0, -1.0, -1.0], dtype=torch.float32)",
"y_pred": "torch.tensor([1.0, -1.0, -1.0, 1.0, 1.0, -1.0, -1.0, 1.0], dtype=torch.float32)",
"alpha": "0.6"
}
},
{
"name": "多维批次与参数泛化验证",
"input_desc": "真实参数契约用例 (w, y_true, y_pred, alpha)",
"params": [
{
"name": "w",
"type": "torch.Tensor",
"value": "torch.tensor([0.25, 0.25, 0.25, 0.25], dtype=torch.float32)"
},
{
"name": "y_true",
"type": "torch.Tensor",
"value": "torch.tensor([1.0, 1.0, -1.0, -1.0], dtype=torch.float32)"
},
{
"name": "y_pred",
"type": "torch.Tensor",
"value": "torch.tensor([1.0, -1.0, -1.0, 1.0], dtype=torch.float32)"
},
{
"name": "alpha",
"type": "float",
"value": "0.48"
}
],
"raw_inputs": {
"w": "torch.tensor([0.25, 0.25, 0.25, 0.25], dtype=torch.float32)",
"y_true": "torch.tensor([1.0, 1.0, -1.0, -1.0], dtype=torch.float32)",
"y_pred": "torch.tensor([1.0, -1.0, -1.0, 1.0], dtype=torch.float32)",
"alpha": "0.48"
}
}
]
} |