提供 PyTorch 深度学习模式与最佳实践,帮助构建高效可复现的训练流程。
整体看这是一个开源、社区采用度很高的纯文档/提示词型 PyTorch 技能,没有密钥、没有远程端点、也没有明显的本机执行或数据外发迹象,风险较低。主要注意点仅是仓库许可证未声明、维护状态未知,供应链可审计性略受影响。
未提供任何密钥、token 或环境变量;材料中也未见凭证收集、传递或滥用迹象。
系统检查项显示无远程端点 host,README 仅为本地开发模式与最佳实践说明,未见数据外发描述。
该技能属于提示词/文档型,不体现主动起进程或执行系统命令;但作为技能使用时仍可能间接引导本机代码生成与运行,属于常规注意项。
材料主要涉及 PyTorch 训练、数据加载与模型代码审查,理论上可能接触用户提供的本地代码或数据样本,但未显示超范围读取或写入。
来源为 GitHub 开源仓库且社区采用度很高(210k+ stars),可审计性较好;但许可证未声明、维护状态未知,供应链透明度略有缺口。
复制安装指令,让 AI 自动完成配置 · 推荐新手
请帮我安装 askskill 上的 "pytorch-patterns" 技能: 1. 下载 https://raw.githubusercontent.com/affaan-m/ECC/main/skills/pytorch-patterns/SKILL.md 2. 保存为 ~/.claude/skills/pytorch-patterns/SKILL.md 3. 装好后重载技能,告诉我可以用了
请给我一套基于 PyTorch 的训练管线最佳实践,包含项目目录结构、配置管理、随机种子设置、训练与验证循环、checkpoint 保存、日志记录和可复现性建议。
一份结构化的训练管线方案,涵盖核心模块设计与可复现实现建议。
我正在用 PyTorch 训练图像模型,请总结 DataLoader、Dataset、数据增强、多进程加载、pin_memory 和预取策略的最佳实践,并指出常见性能瓶颈与排查方法。
一套数据加载优化建议,包含性能调优点、常见错误和排查清单。
请基于 PyTorch 最佳实践,说明如何设计可维护的模型架构代码,包括模块拆分、forward 规范、初始化策略、设备管理、混合精度训练和实验追踪建议。
一份模型工程规范建议,帮助提升代码可维护性、训练稳定性与实验管理能力。
Idiomatic PyTorch patterns and best practices for building robust, efficient, and reproducible deep learning applications.
Always write code that works on both CPU and GPU without hardcoding devices.
# Good: Device-agnostic
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
model = MyModel().to(device)
data = data.to(device)
# Bad: Hardcoded device
model = MyModel().cuda() # Crashes if no GPU
data = data.cuda()
Set all random seeds for reproducible results.
# Good: Full reproducibility setup
def set_seed(seed: int = 42) -> None:
torch.manual_seed(seed)
torch.cuda.manual_seed_all(seed)
np.random.seed(seed)
random.seed(seed)
torch.backends.cudnn.deterministic = True
torch.backends.cudnn.benchmark = False
# Bad: No seed control
model = MyModel() # Different weights every run
Always document and verify tensor shapes.
# Good: Shape-annotated forward pass
def forward(self, x: torch.Tensor) -> torch.Tensor:
# x: (batch_size, channels, height, width)
x = self.conv1(x) # -> (batch_size, 32, H, W)
x = self.pool(x) # -> (batch_size, 32, H//2, W//2)
x = x.view(x.size(0), -1) # -> (batch_size, 32*H//2*W//2)
return self.fc(x) # -> (batch_size, num_classes)
# Bad: No shape tracking
def forward(self, x):
x = self.conv1(x)
x = self.pool(x)
x = x.view(x.size(0), -1) # What size is this?
return self.fc(x) # Will this even work?
# Good: Well-organized module
class ImageClassifier(nn.Module):
def __init__(self, num_classes: int, dropout: float = 0.5) -> None:
super().__init__()
self.features = nn.Sequential(
nn.Conv2d(3, 64, kernel_size=3, padding=1),
nn.BatchNorm2d(64),
nn.ReLU(inplace=True),
nn.MaxPool2d(2),
)
self.classifier = nn.Sequential(
nn.Dropout(dropout),
nn.Linear(64 * 16 * 16, num_classes),
)
def forward(self, x: torch.Tensor) -> torch.Tensor:
x = self.features(x)
x = x.view(x.size(0), -1)
return self.classifier(x)
# Bad: Everything in forward
class ImageClassifier(nn.Module):
def __init__(self):
super().__init__()
def forward(self, x):
x = F.conv2d(x, weight=self.make_weight()) # Creates weight each call!
return x
# Good: Explicit initialization
def _init_weights(self, module: nn.Module) -> None:
if isinstance(module, nn.Linear):
nn.init.kaiming_normal_(module.weight, mode="fan_out", nonlinearity="relu")
if module.bias is not None:
nn.init.zeros_(module.bias)
elif isinstance(module, nn.Conv2d):
nn.init.kaiming_normal_(module.weight, mode="fan_out", nonlinearity="relu")
elif isinstance(module, nn.BatchNorm2d):
nn.init.ones_(module.weight)
nn.init.zeros_(module.bias)
model = MyModel()
model.apply(model._init_weights)
# Good: Complete training loop with best practices
def train_one_epoch(
model: nn.Module,
dataloader: DataLoader,
optimizer: torch.optim.Optimizer,
criterion: nn.Module,
device: torch.device,
scaler: torch.amp.GradScaler | None = None,
) -> float:
model.train() # Always set train mode
total_loss = 0.0
for batch_idx, (data, target) in enumerate(dataloader):
data, target = data.to(device), target.to(device)
…
用于审计 ECC Tools 的成本与计费异常,快速定位滥用、泄漏与重复任务来源。
将设计或需求文档快速编排为可运行的 MVP 起步版本,并完成首个端到端切片实现。
帮助用户搭建 WireGuard VPN、配置客户端并安全远程访问家庭网络。
帮助用户高效整理邮箱、起草核验并安全发送邮件与跟进。
为生产环境操作和自主代理执行提供破坏性行为防护与风险拦截。
帮助用户统一管理、同步检索并去重多层知识库内容
提供 Django 架构模式、DRF 接口设计与生产级开发最佳实践指导
帮助用户基于本地 PyTorch 文档进行检索、问答、排错与代码示例查找。
为 Python 代码库提供设计模式生成、识别、校验与反模式重构能力。
提供地道 Kotlin 模式与最佳实践,帮助构建健壮高效且易维护的应用。
帮助你掌握 Pythonic 写法、类型标注与规范实践,写出更稳健易维护的 Python 代码。
提供地道 Go 语言模式与最佳实践,帮助构建健壮高效且易维护的应用