Spaces:
Running on Zero
Running on Zero
Download data/problems/bootstrap-sample-oob-mask.json from dongyanggg/codestar-backend: direct link, hf CLI and curl.
- Browser
- Download file 7.31 kB
-
https://huggingface.co/spaces/dongyanggg/codestar-backend/resolve/main/data/problems/bootstrap-sample-oob-mask.json
- Command line
-
hf download hf://spaces/dongyanggg/codestar-backend/data/problems/bootstrap-sample-oob-mask.json
-
curl -L -o bootstrap-sample-oob-mask.json https://huggingface.co/spaces/dongyanggg/codestar-backend/resolve/main/data/problems/bootstrap-sample-oob-mask.json
7.31 kB
| { | |
| "slug": "bootstrap-sample-oob-mask", | |
| "title": "手撕自助采样与袋外样本掩码 (Bootstrap Sampling & OOB Mask)", | |
| "difficulty": "Easy", | |
| "category": "ML", | |
| "tags": [ | |
| "随机森林", | |
| "Bootstrap", | |
| "袋外样本" | |
| ], | |
| "description_md": "### 题目描述\n给定样本数 $N$ 及抽样索引张量 $indices \\in [0, N-1]^N$(有放回抽取),生成袋外 (Out-of-Bag, OOB) 布尔掩码张量 $mask \\in \\{\\text{True}, \\text{False}\\}^N$,其中未被任何抽取选中的样本位置为 True,否则为 False。\n\n### 函数签名\n```python\ndef oob_mask(N: int, indices: torch.Tensor) -> torch.Tensor:\n```\n\n### 示例\n```python\n# 输入\nN = 5\nindices = [0, 1, 1, 3, 3]\n\n# 输出 (Tensor, shape=[5], dtype=bool)\n[False, False, True, False, True]\n```\n\n### 参数规格与约束\n- **`N`**:`int`\n- **`indices`**:`torch.Tensor`,测试规格 `shape=[5], dtype=int64`\n- **返回值**:`torch.Tensor`,输出规格 `shape=[5], dtype=bool`\n- **精度容差**:`atol=0.0001, rtol=0.0001`", | |
| "entry_point": "oob_mask", | |
| "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 oob_mask(N: int, indices: torch.Tensor) -> torch.Tensor:\n \"\"\"\n 手撕自助采样与袋外样本掩码 (Bootstrap Sampling & OOB Mask)\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 oob_mask(N: int, indices: torch.Tensor) -> torch.Tensor:\n mask = torch.ones(N, dtype=torch.bool, device=indices.device)\n mask[indices] = False\n return mask\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 ('5个样本抽取', 5, torch.tensor([0, 1, 1, 3, 3])),\n ('多批次扩展泛化验证', 5, torch.tensor([0, 1, 1, 3, 3, 0, 1, 1, 3, 3])),\n ('多维批次与参数泛化验证', 6, torch.tensor([0, 1, 1, 3, 3])),\n ('边界极值与稳定性验证', 7, torch.tensor([0, 1, 1, 3, 3])),\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**手撕自助采样与袋外样本掩码 (Bootstrap Sampling & OOB Mask)**(`oob_mask`)聚焦于 **ML** 中的关键计算逻辑:\n\n给定样本数 $N$ 及抽样索引张量 $indices \\in [0, N-1]^N$(有放回抽取),生成袋外 (Out-of-Bag, OOB) 布尔掩码张量 $mask \\in \\{\\text{True}, \\text{False}\\}^N$,其中未被任何抽取选中的样本位置为 True,否则为 False。\n\n在实现中,函数接收参数 `N` (`int`)、`indices` (`torch.Tensor`),目标是计算出类型与形状严格符合 `torch.Tensor` 契约的结果。\n\n#### 📐 输入输出张量变换与步骤拆解\n- **输入基准规格**:`N` 规格为 `int` (示例值: `5`),`indices` 规格为 `torch.Tensor(shape=[5], dtype=int64)`。\n- **期望输出规格**:`torch.Tensor(shape=[5], dtype=bool)`。\n\n**具体推导与计算步骤如下:**\n1. 计算中间量 `mask`:通过表达式 `torch.ones(N, dtype=torch.bool, device=indices.device)` 完成张量或数值变换。\n2. 计算中间量 `mask[indices]`:通过表达式 `False` 完成张量或数值变换。\n3. 返回最终结果:输出 `mask`,严格对齐函数签名要求的返回类型与维度。\n\n#### 📊 复杂度分析\n- **时间复杂度**:$O(N)$(线性于输入张量总元素个数 $\\prod d_i$),所有运算均为向量化逐元素算子或沿指定维度的归约计算。\n- **空间复杂度**:$O(N)$,需要与输入或输出张量同规模的显存空间保存计算结果。\n\n#### ⚠️ 关键实现细节与易错点\n- **输入输出类型与形状契约**:请严格按照函数签名返回对应类型(`torch.Tensor`、`Tuple` 或标量数值),避免遗漏批次维度或改变张量的数据类型(`dtype`)。\n- **边界输入鲁棒性**:注意单样本(`Batch=1`)、单步序列(`Seq=1`)、零值输入或极端超参数下的计算行为,不要在代码中硬编码特定测试用例的维度常数。\n- **纯向量化算子优先**:尽量使用 PyTorch 原生向量化张量算子替代低效的 Python 逐元素循环,以兼顾代码简洁性与执行性能。\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 oob_mask(N: int, indices: torch.Tensor) -> torch.Tensor:\n mask = torch.ones(N, dtype=torch.bool, device=indices.device)\n mask[indices] = False\n return mask\n```", | |
| "difficulty_breakdown": { | |
| "concept": 2, | |
| "implementation": 2, | |
| "math_derivation": 2, | |
| "boundary_complexity": 1 | |
| }, | |
| "test_cases": [ | |
| { | |
| "name": "5个样本抽取", | |
| "input_desc": "真实参数契约用例 (N, indices)", | |
| "params": [ | |
| { | |
| "name": "N", | |
| "type": "int", | |
| "value": "5" | |
| }, | |
| { | |
| "name": "indices", | |
| "type": "torch.Tensor", | |
| "value": "torch.tensor([0, 1, 1, 3, 3])" | |
| } | |
| ], | |
| "raw_inputs": { | |
| "N": "5", | |
| "indices": "torch.tensor([0, 1, 1, 3, 3])" | |
| } | |
| }, | |
| { | |
| "name": "多批次扩展泛化验证", | |
| "input_desc": "真实参数契约用例 (N, indices)", | |
| "params": [ | |
| { | |
| "name": "N", | |
| "type": "int", | |
| "value": "5" | |
| }, | |
| { | |
| "name": "indices", | |
| "type": "torch.Tensor", | |
| "value": "torch.tensor([0, 1, 1, 3, 3, 0, 1, 1, 3, 3])" | |
| } | |
| ], | |
| "raw_inputs": { | |
| "N": "5", | |
| "indices": "torch.tensor([0, 1, 1, 3, 3, 0, 1, 1, 3, 3])" | |
| } | |
| }, | |
| { | |
| "name": "多维批次与参数泛化验证", | |
| "input_desc": "真实参数契约用例 (N, indices)", | |
| "params": [ | |
| { | |
| "name": "N", | |
| "type": "int", | |
| "value": "6" | |
| }, | |
| { | |
| "name": "indices", | |
| "type": "torch.Tensor", | |
| "value": "torch.tensor([0, 1, 1, 3, 3])" | |
| } | |
| ], | |
| "raw_inputs": { | |
| "N": "6", | |
| "indices": "torch.tensor([0, 1, 1, 3, 3])" | |
| } | |
| } | |
| ] | |
| } |