[{"data":1,"prerenderedAt":-1},["ShallowReactive",2],{"similar-szymonmaszke--torchlayers":3,"tool-szymonmaszke--torchlayers":64},[4,17,27,35,43,56],{"id":5,"name":6,"github_repo":7,"description_zh":8,"stars":9,"difficulty_score":10,"last_commit_at":11,"category_tags":12,"status":16},3808,"stable-diffusion-webui","AUTOMATIC1111\u002Fstable-diffusion-webui","stable-diffusion-webui 是一个基于 Gradio 构建的网页版操作界面，旨在让用户能够轻松地在本地运行和使用强大的 Stable Diffusion 图像生成模型。它解决了原始模型依赖命令行、操作门槛高且功能分散的痛点，将复杂的 AI 绘图流程整合进一个直观易用的图形化平台。\n\n无论是希望快速上手的普通创作者、需要精细控制画面细节的设计师，还是想要深入探索模型潜力的开发者与研究人员，都能从中获益。其核心亮点在于极高的功能丰富度：不仅支持文生图、图生图、局部重绘（Inpainting）和外绘（Outpainting）等基础模式，还独创了注意力机制调整、提示词矩阵、负向提示词以及“高清修复”等高级功能。此外，它内置了 GFPGAN 和 CodeFormer 等人脸修复工具，支持多种神经网络放大算法，并允许用户通过插件系统无限扩展能力。即使是显存有限的设备，stable-diffusion-webui 也提供了相应的优化选项，让高质量的 AI 艺术创作变得触手可及。",162132,3,"2026-04-05T11:01:52",[13,14,15],"开发框架","图像","Agent","ready",{"id":18,"name":19,"github_repo":20,"description_zh":21,"stars":22,"difficulty_score":23,"last_commit_at":24,"category_tags":25,"status":16},1381,"everything-claude-code","affaan-m\u002Feverything-claude-code","everything-claude-code 是一套专为 AI 编程助手（如 Claude Code、Codex、Cursor 等）打造的高性能优化系统。它不仅仅是一组配置文件，而是一个经过长期实战打磨的完整框架，旨在解决 AI 代理在实际开发中面临的效率低下、记忆丢失、安全隐患及缺乏持续学习能力等核心痛点。\n\n通过引入技能模块化、直觉增强、记忆持久化机制以及内置的安全扫描功能，everything-claude-code 能显著提升 AI 在复杂任务中的表现，帮助开发者构建更稳定、更智能的生产级 AI 代理。其独特的“研究优先”开发理念和针对 Token 消耗的优化策略，使得模型响应更快、成本更低，同时有效防御潜在的攻击向量。\n\n这套工具特别适合软件开发者、AI 研究人员以及希望深度定制 AI 工作流的技术团队使用。无论您是在构建大型代码库，还是需要 AI 协助进行安全审计与自动化测试，everything-claude-code 都能提供强大的底层支持。作为一个曾荣获 Anthropic 黑客大奖的开源项目，它融合了多语言支持与丰富的实战钩子（hooks），让 AI 真正成长为懂上",140436,2,"2026-04-05T23:32:43",[13,15,26],"语言模型",{"id":28,"name":29,"github_repo":30,"description_zh":31,"stars":32,"difficulty_score":23,"last_commit_at":33,"category_tags":34,"status":16},2271,"ComfyUI","Comfy-Org\u002FComfyUI","ComfyUI 是一款功能强大且高度模块化的视觉 AI 引擎，专为设计和执行复杂的 Stable Diffusion 图像生成流程而打造。它摒弃了传统的代码编写模式，采用直观的节点式流程图界面，让用户通过连接不同的功能模块即可构建个性化的生成管线。\n\n这一设计巧妙解决了高级 AI 绘图工作流配置复杂、灵活性不足的痛点。用户无需具备编程背景，也能自由组合模型、调整参数并实时预览效果，轻松实现从基础文生图到多步骤高清修复等各类复杂任务。ComfyUI 拥有极佳的兼容性，不仅支持 Windows、macOS 和 Linux 全平台，还广泛适配 NVIDIA、AMD、Intel 及苹果 Silicon 等多种硬件架构，并率先支持 SDXL、Flux、SD3 等前沿模型。\n\n无论是希望深入探索算法潜力的研究人员和开发者，还是追求极致创作自由度的设计师与资深 AI 绘画爱好者，ComfyUI 都能提供强大的支持。其独特的模块化架构允许社区不断扩展新功能，使其成为当前最灵活、生态最丰富的开源扩散模型工具之一，帮助用户将创意高效转化为现实。",107662,"2026-04-03T11:11:01",[13,14,15],{"id":36,"name":37,"github_repo":38,"description_zh":39,"stars":40,"difficulty_score":23,"last_commit_at":41,"category_tags":42,"status":16},3704,"NextChat","ChatGPTNextWeb\u002FNextChat","NextChat 是一款轻量且极速的 AI 助手，旨在为用户提供流畅、跨平台的大模型交互体验。它完美解决了用户在多设备间切换时难以保持对话连续性，以及面对众多 AI 模型不知如何统一管理的痛点。无论是日常办公、学习辅助还是创意激发，NextChat 都能让用户随时随地通过网页、iOS、Android、Windows、MacOS 或 Linux 端无缝接入智能服务。\n\n这款工具非常适合普通用户、学生、职场人士以及需要私有化部署的企业团队使用。对于开发者而言，它也提供了便捷的自托管方案，支持一键部署到 Vercel 或 Zeabur 等平台。\n\nNextChat 的核心亮点在于其广泛的模型兼容性，原生支持 Claude、DeepSeek、GPT-4 及 Gemini Pro 等主流大模型，让用户在一个界面即可自由切换不同 AI 能力。此外，它还率先支持 MCP（Model Context Protocol）协议，增强了上下文处理能力。针对企业用户，NextChat 提供专业版解决方案，具备品牌定制、细粒度权限控制、内部知识库整合及安全审计等功能，满足公司对数据隐私和个性化管理的高标准要求。",87618,"2026-04-05T07:20:52",[13,26],{"id":44,"name":45,"github_repo":46,"description_zh":47,"stars":48,"difficulty_score":23,"last_commit_at":49,"category_tags":50,"status":16},2268,"ML-For-Beginners","microsoft\u002FML-For-Beginners","ML-For-Beginners 是由微软推出的一套系统化机器学习入门课程，旨在帮助零基础用户轻松掌握经典机器学习知识。这套课程将学习路径规划为 12 周，包含 26 节精炼课程和 52 道配套测验，内容涵盖从基础概念到实际应用的完整流程，有效解决了初学者面对庞大知识体系时无从下手、缺乏结构化指导的痛点。\n\n无论是希望转型的开发者、需要补充算法背景的研究人员，还是对人工智能充满好奇的普通爱好者，都能从中受益。课程不仅提供了清晰的理论讲解，还强调动手实践，让用户在循序渐进中建立扎实的技能基础。其独特的亮点在于强大的多语言支持，通过自动化机制提供了包括简体中文在内的 50 多种语言版本，极大地降低了全球不同背景用户的学习门槛。此外，项目采用开源协作模式，社区活跃且内容持续更新，确保学习者能获取前沿且准确的技术资讯。如果你正寻找一条清晰、友好且专业的机器学习入门之路，ML-For-Beginners 将是理想的起点。",84991,"2026-04-05T10:45:23",[14,51,52,53,15,54,26,13,55],"数据工具","视频","插件","其他","音频",{"id":57,"name":58,"github_repo":59,"description_zh":60,"stars":61,"difficulty_score":10,"last_commit_at":62,"category_tags":63,"status":16},3128,"ragflow","infiniflow\u002Fragflow","RAGFlow 是一款领先的开源检索增强生成（RAG）引擎，旨在为大语言模型构建更精准、可靠的上下文层。它巧妙地将前沿的 RAG 技术与智能体（Agent）能力相结合，不仅支持从各类文档中高效提取知识，还能让模型基于这些知识进行逻辑推理和任务执行。\n\n在大模型应用中，幻觉问题和知识滞后是常见痛点。RAGFlow 通过深度解析复杂文档结构（如表格、图表及混合排版），显著提升了信息检索的准确度，从而有效减少模型“胡编乱造”的现象，确保回答既有据可依又具备时效性。其内置的智能体机制更进一步，使系统不仅能回答问题，还能自主规划步骤解决复杂问题。\n\n这款工具特别适合开发者、企业技术团队以及 AI 研究人员使用。无论是希望快速搭建私有知识库问答系统，还是致力于探索大模型在垂直领域落地的创新者，都能从中受益。RAGFlow 提供了可视化的工作流编排界面和灵活的 API 接口，既降低了非算法背景用户的上手门槛，也满足了专业开发者对系统深度定制的需求。作为基于 Apache 2.0 协议开源的项目，它正成为连接通用大模型与行业专有知识之间的重要桥梁。",77062,"2026-04-04T04:44:48",[15,14,13,26,54],{"id":65,"github_repo":66,"name":67,"description_en":68,"description_zh":69,"ai_summary_zh":70,"readme_en":71,"readme_zh":72,"quickstart_zh":73,"use_case_zh":74,"hero_image_url":75,"owner_login":76,"owner_name":77,"owner_avatar_url":78,"owner_bio":79,"owner_company":80,"owner_location":80,"owner_email":81,"owner_twitter":80,"owner_website":80,"owner_url":82,"languages":83,"stars":92,"forks":93,"last_commit_at":94,"license":95,"difficulty_score":96,"env_os":97,"env_gpu":97,"env_ram":97,"env_deps":98,"category_tags":103,"github_topics":104,"view_count":23,"oss_zip_url":80,"oss_zip_packed_at":80,"status":16,"created_at":115,"updated_at":116,"faqs":117,"releases":148},2457,"szymonmaszke\u002Ftorchlayers","torchlayers","Shape and dimension inference (Keras-like) for PyTorch layers and neural networks","torchlayers 是一个基于 PyTorch 的轻量级开源库，旨在为深度学习开发者提供类似 Keras 的便捷体验，同时保留 PyTorch 原有的灵活性与强大功能。它的核心目标是解决 PyTorch 原生模块在构建神经网络时，需要手动计算和指定每一层输入输出形状（Shape）及维度（Dimension）的繁琐问题。\n\n在传统 PyTorch 开发中，开发者往往需要仔细推算卷积层、线性层等模块的尺寸变化，这不仅容易出错，还降低了原型设计的效率。torchlayers 通过自动形状推断机制，让网络层能够根据输入数据自动调整自身参数。例如，使用 `tl.Conv` 时，库会根据输入是 1D、2D 还是 3D 数据，自动选择对应的卷积类型，无需用户显式声明。此外，它还内置了许多来自 EfficientNet、Squeeze-And-Excitation 等前沿架构的先进模块，并提供了如 \"same\" 填充等更符合直觉的默认设置。\n\ntorchlayers 特别适合 PyTorch 开发者、算法研究人员以及希望快速验证想法的数据科学家。对于习惯 Keras 简洁风格但又离不开 PyTor","torchlayers 是一个基于 PyTorch 的轻量级开源库，旨在为深度学习开发者提供类似 Keras 的便捷体验，同时保留 PyTorch 原有的灵活性与强大功能。它的核心目标是解决 PyTorch 原生模块在构建神经网络时，需要手动计算和指定每一层输入输出形状（Shape）及维度（Dimension）的繁琐问题。\n\n在传统 PyTorch 开发中，开发者往往需要仔细推算卷积层、线性层等模块的尺寸变化，这不仅容易出错，还降低了原型设计的效率。torchlayers 通过自动形状推断机制，让网络层能够根据输入数据自动调整自身参数。例如，使用 `tl.Conv` 时，库会根据输入是 1D、2D 还是 3D 数据，自动选择对应的卷积类型，无需用户显式声明。此外，它还内置了许多来自 EfficientNet、Squeeze-And-Excitation 等前沿架构的先进模块，并提供了如 \"same\" 填充等更符合直觉的默认设置。\n\ntorchlayers 特别适合 PyTorch 开发者、算法研究人员以及希望快速验证想法的数据科学家。对于习惯 Keras 简洁风格但又离不开 PyTorch 生态的用户来说，它是一个极佳的桥梁。值得注意的是，torchlayers 几乎零额外开销，完全兼容 PyTorch 的 `nn.Module` 定义方式，支持 TorchScript，且允许用户自定义复杂网络结构。这意味着你可以在享受自动化便利的同时，不牺牲任何底层控制的自由度，轻松构建从简单分类器到复杂 SOTA 模型的各种神经网络。","![torchlayers Logo](https:\u002F\u002Foss.gittoolsai.com\u002Fimages\u002Fszymonmaszke_torchlayers_readme_831e27dc9187.png)\n\n--------------------------------------------------------------------------------\n\n\n| Version | Docs | Tests | Coverage | Style | PyPI | Python | PyTorch | Docker |\n|---------|------|-------|----------|-------|------|--------|---------|--------|\n| [![Version](https:\u002F\u002Fimg.shields.io\u002Fstatic\u002Fv1?label=&message=0.1.1&color=377EF0&style=for-the-badge)](https:\u002F\u002Fgithub.com\u002Fszymonmaszke\u002Ftorchlayers\u002Freleases) | [![Documentation](https:\u002F\u002Fimg.shields.io\u002Fstatic\u002Fv1?label=&message=docs&color=EE4C2C&style=for-the-badge)](https:\u002F\u002Fszymonmaszke.github.io\u002Ftorchlayers\u002F)  | ![Tests](https:\u002F\u002Fimg.shields.io\u002Fgithub\u002Fworkflow\u002Fstatus\u002Fszymonmaszke\u002Ftorchlayers\u002Ftest?label=%20&style=for-the-badge) | [![codecov](https:\u002F\u002Fcodecov.io\u002Fgh\u002Fszymonmaszke\u002Ftorchlayers\u002Fbranch\u002Fmaster\u002Fgraph\u002Fbadge.svg?token=GbZmdqbTWM)](https:\u002F\u002Fcodecov.io\u002Fgh\u002Fszymonmaszke\u002Ftorchlayers) | [![codebeat badge](https:\u002F\u002Fcodebeat.co\u002Fbadges\u002F0e3d33b0-95a4-429c-8692-881a4ffeac6b)](https:\u002F\u002Fcodebeat.co\u002Fprojects\u002Fgithub-com-szymonmaszke-torchlayers-master) | [![PyPI](https:\u002F\u002Fimg.shields.io\u002Fstatic\u002Fv1?label=&message=PyPI&color=377EF0&style=for-the-badge)](https:\u002F\u002Fpypi.org\u002Fproject\u002Ftorchlayers\u002F) | [![Python](https:\u002F\u002Fimg.shields.io\u002Fstatic\u002Fv1?label=&message=>=3.7&color=377EF0&style=for-the-badge&logo=python&logoColor=F8C63D)](https:\u002F\u002Fwww.python.org\u002F) | [![PyTorch](https:\u002F\u002Fimg.shields.io\u002Fstatic\u002Fv1?label=&message=>=1.3.0&color=EE4C2C&style=for-the-badge)](https:\u002F\u002Fpytorch.org\u002F) | [![Docker](https:\u002F\u002Fimg.shields.io\u002Fstatic\u002Fv1?label=&message=docker&color=309cef&style=for-the-badge)](https:\u002F\u002Fhub.docker.com\u002Fr\u002Fszymonmaszke\u002Ftorchlayers) |\n\n[__torchlayers__](https:\u002F\u002Fszymonmaszke.github.io\u002Ftorchlayers\u002F) is a library based on [__PyTorch__](https:\u002F\u002Fpytorch.org\u002F)\nproviding __automatic shape and dimensionality inference of `torch.nn` layers__ + additional\nbuilding blocks featured in current SOTA architectures (e.g. [Efficient-Net](https:\u002F\u002Farxiv.org\u002Fabs\u002F1905.11946)).\n\nAbove requires no user intervention (except single call to `torchlayers.build`)\nsimilarly to the one seen in [__Keras__](https:\u002F\u002Fwww.tensorflow.org\u002Fguide\u002Fkeras).\n\n### Main functionalities:\n\n* __Shape inference__ for most of `torch.nn` module (__convolutional, recurrent, transformer, attention and linear layers__)\n* __Dimensionality inference__ (e.g. `torchlayers.Conv` working as `torch.nn.Conv1d\u002F2d\u002F3d` based on `input shape`)\n* __Shape inference of custom modules__ (see examples section)\n* __Additional [Keras-like](https:\u002F\u002Fwww.tensorflow.org\u002Fguide\u002Fkeras) layers__ (e.g. `torchlayers.Reshape` or `torchlayers.StandardNormalNoise`)\n* __Additional SOTA layers__ mostly from ImageNet competitions\n(e.g. [PolyNet](https:\u002F\u002Farxiv.org\u002Fabs\u002F1608.06993),\n[Squeeze-And-Excitation](https:\u002F\u002Farxiv.org\u002Fabs\u002F1709.01507),\n[StochasticDepth](www.arxiv.org\u002Fabs\u002F1512.03385>))\n* __Useful defaults__ (`\"same\"` padding and default `kernel_size=3` for `Conv`, dropout rates etc.)\n* __Zero overhead and [torchscript](https:\u002F\u002Fpytorch.org\u002Fdocs\u002Fstable\u002Fjit.html) support__\n\n__Keep in mind this library works almost exactly like PyTorch originally__.\nWhat that means is you can use `Sequential`, __define your own networks of any complexity using\n`torch.nn.Module`__, create new layers with shape inference etc.\n\n_See below to get some intuition about library_.\n\n# Examples\n\nFor full functionality please check [__torchlayers documentation__](https:\u002F\u002Fszymonmaszke.github.io\u002Ftorchlayers\u002F).\nBelow examples should introduce all necessary concepts you should know.\n\n## Basic classifier\n\n__All__ `torch.nn` modules can be used through `torchlayers` and __each module with input shape__\nwill be appropriately modified with it's input inferable counterpart.\n\n\n```python\nimport torchlayers as tl\n\n\nclass Classifier(tl.Module):\n    def __init__(self):\n        super().__init__()\n        self.conv1 = tl.Conv2d(64, kernel_size=6)\n        self.conv2 = tl.Conv2d(128, kernel_size=3)\n        self.conv3 = tl.Conv2d(256, kernel_size=3, padding=1)\n        # New layer, more on that in the next example\n        self.pooling = tl.GlobalMaxPool()\n        self.dense = tl.Linear(10)\n\n    def forward(self, x):\n        x = torch.relu(self.conv1(x))\n        x = torch.relu(self.conv2(x))\n        x = torch.relu(self.conv3(x))\n        return self.dense(self.pooling(x))\n\n# Pass model and any example inputs afterwards\nclf = tl.build(Classifier(), torch.randn(1, 3, 32, 32))\n```\n\nAbove `torchlayers.Linear(out_features=10)` is used. It is \"equivalent\" to\noriginal PyTorch's `torch.nn.Linear(in_features=?, out_features=10)` where `in_features`\nwill be inferred from example input input during `torchlayers.build` call.\n\nSame thing happens with `torch.nn.Conv2d(in_channels, out_channels, kernel_size, ...)`\nwhich can be replaced directly by `tl.Conv2d(out_channels, kernel_size, ...)`.\n\n__Just remember to pass example input through the network!__\n\n## Simple image and text classifier in one!\n\n* We will use single \"model\" for both tasks.\nFirstly let's define it using `torch.nn` and `torchlayers`:\n\n```python\nimport torch\nimport torchlayers as tl\n\n# torch.nn and torchlayers can be mixed easily\nmodel = torch.nn.Sequential(\n    tl.Conv(64),  # specify ONLY out_channels\n    torch.nn.ReLU(),  # use torch.nn wherever you wish\n    tl.BatchNorm(),  # BatchNormNd inferred from input\n    tl.Conv(128),  # Default kernel_size equal to 3\n    tl.ReLU(),\n    tl.Conv(256, kernel_size=11),  # \"same\" padding as default\n    tl.GlobalMaxPool(),  # Known from Keras\n    tl.Linear(10),  # Output for 10 classes\n)\n\nprint(model)\n```\n\nAbove would give you model's summary like this (__notice question marks for not yet inferred values__):\n\n```python\nSequential(\n  (0): Conv(in_channels=?, out_channels=64, kernel_size=3, stride=1, padding=same, dilation=1, groups=1, bias=True, padding_mode=zeros)\n  (1): ReLU()\n  (2): BatchNorm(num_features=?, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n  (3): Conv(in_channels=?, out_channels=128, kernel_size=3, stride=1, padding=same, dilation=1, groups=1, bias=True, padding_mode=zeros)\n  (4): ReLU()\n  (5): Conv(in_channels=?, out_channels=256, kernel_size=11, stride=1, padding=same, dilation=1, groups=1, bias=True, padding_mode=zeros)\n  (6): GlobalMaxPool()\n  (7): Linear(in_features=?, out_features=10, bias=True)\n)\n```\n\n* Now you can __build__\u002Finstantiate your model with example input (in this case MNIST-like):\n\n```python\nmnist_model = tl.build(model, torch.randn(1, 3, 28, 28))\n```\n\n* Or if it's text classification you are after, same model could be built with different\n`input shape` (e.g. for text classification using `300` dimensional pretrained embedding):\n\n```python\n# [batch, embedding, timesteps], first dimension > 1 for BatchNorm1d to work\ntext_model = tl.build(model, torch.randn(2, 300, 1))\n```\n\n* Finally, you can `print` both models after instantiation, provided below side\nby-side for readability (__notice different dimenstionality, e.g. `Conv2d` vs `Conv1d` after `torchlayers.build`__):\n\n```python\n                # TEXT CLASSIFIER                 MNIST CLASSIFIER\n\n                Sequential(                       Sequential(\n                  (0): Conv1d(300, 64)              (0): Conv2d(3, 64)\n                  (1): ReLU()                       (1): ReLU()\n                  (2): BatchNorm1d(64)              (2): BatchNorm2d(64)\n                  (3): Conv1d(64, 128)              (3): Conv2d(64, 128)\n                  (4): ReLU()                       (4): ReLU()\n                  (5): Conv1d(128, 256)             (5): Conv2d(128, 256)\n                  (6): GlobalMaxPool()              (6): GlobalMaxPool()\n                  (7): Linear(256, 10)              (7): Linear(256, 10)\n                )                                 )\n```\n\nAs you can see both modules \"compiled\" into original `pytorch` layers.\n\n## Custom modules with shape inference capabilities\n\nUser can define any module and make it shape inferable with `torchlayers.infer`\nfunction:\n\n```python\n # Class defined with in_features\n # It might be a good practice to use _ prefix and Impl as postfix\n # to differentiate from shape inferable version\nclass _MyLinearImpl(torch.nn.Module):\n    def __init__(self, in_features: int, out_features: int):\n        super().__init__()\n        self.weight = torch.nn.Parameter(torch.randn(out_features, in_features))\n        self.bias = torch.nn.Parameter(torch.randn(out_features))\n\n    def forward(self, inputs):\n        return torch.nn.functional.linear(inputs, self.weight, self.bias)\n\nMyLinear = tl.infer(_MyLinearImpl)\n\n# Build and use just like any other layer in this library\nlayer =tl.build(MyLinear(out_features=32), torch.randn(1, 64))\nlayer(torch.randn(1, 64))\n```\n\nBy default `inputs.shape[1]` will be used as `in_features` value\nduring initial `forward` pass. If you wish to use different `index` (e.g. to infer using\n`inputs.shape[3]`) use `MyLayer = tl.infer(_MyLayerImpl, index=3)` as a decorator.\n\n## Autoencoder with inverted residual bottleneck and pixel shuffle\n\nPlease check code comments and [__documentation__](https:\u002F\u002Fszymonmaszke.github.io\u002Ftorchlayers\u002F)\nif needed. If you are unsure what autoencoder is you could see\n[__this example blog post__](https:\u002F\u002Ftowardsdatascience.com\u002Fauto-encoder-what-is-it-and-what-is-it-used-for-part-1-3e5c6f017726).\n\nBelow is a convolutional denoising autoencoder example for `ImageNet`-like images.\nThink of it like a demonstration of capabilities of different layers\nand building blocks provided by `torchlayers`.\n\n\n```python\n# Input - 3 x 256 x 256 for ImageNet reconstruction\nclass AutoEncoder(torch.nn.Module):\n    def __init__(self):\n        super().__init__()\n        self.encoder = tl.Sequential(\n            tl.StandardNormalNoise(),  # Apply noise to input images\n            tl.Conv(64, kernel_size=7),\n            tl.activations.Swish(),  # Direct access to module .activations\n            tl.InvertedResidualBottleneck(squeeze_excitation=False),\n            tl.AvgPool(),  # shape 64 x 128 x 128, kernel_size=2 by default\n            tl.HardSwish(),  # Access simply through tl\n            tl.SeparableConv(128),  # Up number of channels to 128\n            tl.InvertedResidualBottleneck(),  # Default with squeeze excitation\n            torch.nn.ReLU(),\n            tl.AvgPool(),  # shape 128 x 64 x 64, kernel_size=2 by default\n            tl.DepthwiseConv(256),  # DepthwiseConv easier to use\n            # Pass input thrice through the same weights like in PolyNet\n            tl.Poly(tl.InvertedResidualBottleneck(), order=3),\n            tl.ReLU(),  # all torch.nn can be accessed via tl\n            tl.MaxPool(),  # shape 256 x 32 x 32\n            tl.Fire(out_channels=512),  # shape 512 x 32 x 32\n            tl.SqueezeExcitation(hidden=64),\n            tl.InvertedResidualBottleneck(),\n            tl.MaxPool(),  # shape 512 x 16 x 16\n            tl.InvertedResidualBottleneck(squeeze_excitation=False),\n            # Randomly switch off the last two layers with 0.5 probability\n            tl.StochasticDepth(\n                torch.nn.Sequential(\n                    tl.InvertedResidualBottleneck(squeeze_excitation=False),\n                    tl.InvertedResidualBottleneck(squeeze_excitation=False),\n                ),\n                p=0.5,\n            ),\n            tl.AvgPool(),  # shape 512 x 8 x 8\n        )\n\n        # This one is more \"standard\"\n        self.decoder = tl.Sequential(\n            tl.Poly(tl.InvertedResidualBottleneck(), order=2),\n            # Has ICNR initialization by default after calling `build`\n            tl.ConvPixelShuffle(out_channels=512, upscale_factor=2),\n            # Shape 512 x 16 x 16 after PixelShuffle\n            tl.Poly(tl.InvertedResidualBottleneck(), order=3),\n            tl.ConvPixelShuffle(out_channels=256, upscale_factor=2),\n            # Shape 256 x 32 x 32\n            tl.Poly(tl.InvertedResidualBottleneck(), order=3),\n            tl.ConvPixelShuffle(out_channels=128, upscale_factor=2),\n            # Shape 128 x 64 x 64\n            tl.Poly(tl.InvertedResidualBottleneck(), order=4),\n            tl.ConvPixelShuffle(out_channels=64, upscale_factor=2),\n            # Shape 64 x 128 x 128\n            tl.InvertedResidualBottleneck(),\n            tl.Conv(256),\n            tl.Dropout(),  # Defaults to 0.5 and Dropout2d for images\n            tl.Swish(),\n            tl.InstanceNorm(),\n            tl.ConvPixelShuffle(out_channels=32, upscale_factor=2),\n            # Shape 32 x 256 x 256\n            tl.Conv(16),\n            tl.Swish(),\n            tl.Conv(3),\n            # Shape 3 x 256 x 256\n        )\n\n    def forward(self, inputs):\n        return self.decoder(self.encoder(inputs))\n```\n\nNow one can instantiate the module and use it with `torch.nn.MSELoss` as per usual.\n\n```python\nautoencoder = tl.build(AutoEncoder(), torch.randn(1, 3, 256, 256))\n```\n\n# Installation\n\n## [pip](\u003Chttps:\u002F\u002Fpypi.org\u002Fproject\u002Ftorchlayers\u002F>)\n\n### Latest release:\n\n```shell\npip install --user torchlayers\n```\n\n### Nightly:\n\n```shell\npip install --user torchlayers-nightly\n```\n\n## [Docker](https:\u002F\u002Fhub.docker.com\u002Fr\u002Fszymonmaszke\u002Ftorchlayers)\n\n__CPU standalone__ and various versions of __GPU enabled__ images are available\nat [dockerhub](https:\u002F\u002Fhub.docker.com\u002Fr\u002Fszymonmaszke\u002Ftorchlayers\u002Ftags).\n\nFor CPU quickstart, issue:\n\n```shell\ndocker pull szymonmaszke\u002Ftorchlayers:18.04\n```\n\nNightly builds are also available, just prefix tag with `nightly_`. If you are going for `GPU` image make sure you have\n[nvidia\u002Fdocker](https:\u002F\u002Fgithub.com\u002FNVIDIA\u002Fnvidia-docker) installed and it's runtime set.\n\n# Contributing\n\nIf you find issue or would like to see some functionality (or implement one), please [open new Issue](https:\u002F\u002Fhelp.github.com\u002Fen\u002Farticles\u002Fcreating-an-issue) or [create Pull Request](https:\u002F\u002Fhelp.github.com\u002Fen\u002Farticles\u002Fcreating-a-pull-request-from-a-fork).\n","![torchlayers Logo](https:\u002F\u002Foss.gittoolsai.com\u002Fimages\u002Fszymonmaszke_torchlayers_readme_831e27dc9187.png)\n\n--------------------------------------------------------------------------------\n\n\n| 版本 | 文档 | 测试 | 覆盖率 | 样式 | PyPI | Python | PyTorch | Docker |\n|---------|------|-------|----------|-------|------|--------|---------|--------|\n| [![版本](https:\u002F\u002Fimg.shields.io\u002Fstatic\u002Fv1?label=&message=0.1.1&color=377EF0&style=for-the-badge)](https:\u002F\u002Fgithub.com\u002Fszymonmaszke\u002Ftorchlayers\u002Freleases) | [![文档](https:\u002F\u002Fimg.shields.io\u002Fstatic\u002Fv1?label=&message=docs&color=EE4C2C&style=for-the-badge)](https:\u002F\u002Fszymonmaszke.github.io\u002Ftorchlayers\u002F)  | ![测试](https:\u002F\u002Fimg.shields.io\u002Fgithub\u002Fworkflow\u002Fstatus\u002Fszymonmaszke\u002Ftorchlayers\u002Ftest?label=%20&style=for-the-badge) | [![codecov](https:\u002F\u002Fcodecov.io\u002Fgh\u002Fszymonmaszke\u002Ftorchlayers\u002Fbranch\u002Fmaster\u002Fgraph\u002Fbadge.svg?token=GbZmdqbTWM)](https:\u002F\u002Fcodecov.io\u002Fgh\u002Fszymonmaszke\u002Ftorchlayers) | [![codebeat badge](https:\u002F\u002Fcodebeat.co\u002Fbadges\u002F0e3d33b0-95a4-429c-8692-881a4ffeac6b)](https:\u002F\u002Fcodebeat.co\u002Fprojects\u002Fgithub-com-szymonmaszke-torchlayers-master) | [![PyPI](https:\u002F\u002Fimg.shields.io\u002Fstatic\u002Fv1?label=&message=PyPI&color=377EF0&style=for-the-badge)](https:\u002F\u002Fpypi.org\u002Fproject\u002Ftorchlayers\u002F) | [![Python](https:\u002F\u002Fimg.shields.io\u002Fstatic\u002Fv1?label=&message=>=3.7&color=377EF0&style=for-the-badge&logo=python&logoColor=F8C63D)](https:\u002F\u002Fwww.python.org\u002F) | [![PyTorch](https:\u002F\u002Fimg.shields.io\u002Fstatic\u002Fv1?label=&message=>=1.3.0&color=EE4C2C&style=for-the-badge)](https:\u002F\u002Fpytorch.org\u002F) | [![Docker](https:\u002F\u002Fimg.shields.io\u002Fstatic\u002Fv1?label=&message=docker&color=309cef&style=for-the-badge)](https:\u002F\u002Fhub.docker.com\u002Fr\u002Fszymonmaszke\u002Ftorchlayers) |\n\n[__torchlayers__](https:\u002F\u002Fszymonmaszke.github.io\u002Ftorchlayers\u002F) 是一个基于 [__PyTorch__](https:\u002F\u002Fpytorch.org\u002F) 的库，\n提供了 __自动推断 `torch.nn` 层的形状和维度__ 功能，同时还包含当前 SOTA 架构中使用的额外模块（例如 [Efficient-Net](https:\u002F\u002Farxiv.org\u002Fabs\u002F1905.11946)）。\n\n上述功能无需用户干预（除了调用一次 `torchlayers.build`），与 [__Keras__](https:\u002F\u002Fwww.tensorflow.org\u002Fguide\u002Fkeras) 中的做法类似。\n\n### 主要功能：\n\n* 对大多数 `torch.nn` 模块进行 __形状推断__（包括卷积层、循环层、Transformer、注意力机制和线性层等）\n* __维度推断__（例如，`torchlayers.Conv` 可根据输入形状自动选择 `torch.nn.Conv1d\u002F2d\u002F3d`）\n* __自定义模块的形状推断__（见示例部分）\n* __额外的[类似于 Keras 的]层__（如 `torchlayers.Reshape` 或 `torchlayers.StandardNormalNoise`）\n* __来自 ImageNet 竞赛的 SOTA 层__（如 [PolyNet](https:\u002F\u002Farxiv.org\u002Fabs\u002F1608.06993)、\n[Squeeze-And-Excitation](https:\u002F\u002Farxiv.org\u002Fabs\u002F1709.01507)、\n[StochasticDepth](www.arxiv.org\u002Fabs\u002F1512.03385>))\n* __实用的默认设置__（如 `Conv` 的 `\"same\"` 填充和默认 `kernel_size=3`，以及 Dropout 率等）\n* __零开销且支持 [torchscript](https:\u002F\u002Fpytorch.org\u002Fdocs\u002Fstable\u002Fjit.html)__ \n\n请记住，这个库的工作方式几乎与原生 PyTorch 完全一致。这意味着你可以使用 `Sequential`，__利用 `torch.nn.Module` 定义任意复杂度的网络__，\n创建具有形状推断功能的新层等等。\n\n_请参阅下方内容以更好地理解该库的功能_。\n\n# 示例\n\n如需了解完整功能，请查阅 [__torchlayers 文档__](https:\u002F\u002Fszymonmaszke.github.io\u002Ftorchlayers\u002F)。\n以下示例将介绍你需要掌握的所有基本概念。\n\n## 基础分类器\n\n通过 `torchlayers` 可以使用 __所有__ `torch.nn` 模块，并且 __每个带有输入形状的模块__\n都会被相应地替换为可推断输入的等效版本。\n\n\n```python\nimport torchlayers as tl\n\n\nclass Classifier(tl.Module):\n    def __init__(self):\n        super().__init__()\n        self.conv1 = tl.Conv2d(64, kernel_size=6)\n        self.conv2 = tl.Conv2d(128, kernel_size=3)\n        self.conv3 = tl.Conv2d(256, kernel_size=3, padding=1)\n        # 新增层，在下一个示例中会详细介绍\n        self.pooling = tl.GlobalMaxPool()\n        self.dense = tl.Linear(10)\n\n    def forward(self, x):\n        x = torch.relu(self.conv1(x))\n        x = torch.relu(self.conv2(x))\n        x = torch.relu(self.conv3(x))\n        return self.dense(self.pooling(x))\n\n# 传递模型及示例输入\nclf = tl.build(Classifier(), torch.randn(1, 3, 32, 32))\n```\n\n上述代码中使用了 `torchlayers.Linear(out_features=10)`。它“等价”于原生 PyTorch 的\n`torch.nn.Linear(in_features=?, out_features=10)`，其中 `in_features` 将在调用 `torchlayers.build` 时\n根据示例输入自动推断出来。\n\n同样的情况也适用于 `torch.nn.Conv2d(in_channels, out_channels, kernel_size, ...)`，\n可以直接用 `tl.Conv2d(out_channels, kernel_size, ...)` 替代。\n\n__只需记得将示例输入传递给网络即可！__\n\n## 图像与文本分类一体化！\n\n* 我们将使用同一个“模型”来处理这两项任务。首先让我们用 `torch.nn` 和 `torchlayers` 来定义它：\n\n```python\nimport torch\nimport torchlayers as tl\n\n# torch.nn 和 torchlayers 可以轻松混合使用\nmodel = torch.nn.Sequential(\n    tl.Conv(64),  # 只需指定输出通道数\n    torch.nn.ReLU(),  # 随意使用 torch.nn\n    tl.BatchNorm(),  # 根据输入推断 BatchNormNd\n    tl.Conv(128),  # 默认卷积核大小为 3\n    tl.ReLU(),\n    tl.Conv(256, kernel_size=11),  # 默认采用 \"same\" 填充\n    tl.GlobalMaxPool(),  \u002F\u002F 类似 Keras 中的全局池化\n    tl.Linear(10),  \u002F\u002F 输出 10 个类别的结果\n)\n\nprint(model)\n```\n\n上述代码会生成如下模型摘要（__注意尚未推断出的值用问号表示__）：\n\n```python\nSequential(\n  (0): Conv(in_channels=?, out_channels=64, kernel_size=3, stride=1, padding=same, dilation=1, groups=1, bias=True, padding_mode=zeros)\n  (1): ReLU()\n  (2): BatchNorm(num_features=?, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n  (3): Conv(in_channels=?, out_channels=128, kernel_size=3, stride=1, padding=same, dilation=1, groups=1, bias=True, padding_mode=zeros)\n  (4): ReLU()\n  (5): Conv(in_channels=?, out_channels=256, kernel_size=11, stride=1, padding=same, dilation=1, groups=1, bias=True, padding_mode=zeros)\n  (6): GlobalMaxPool()\n  (7): Linear(in_features=?, out_features=10, bias=True)\n)\n```\n\n* 现在你可以通过示例输入构建\u002F实例化你的模型（这里以类似 MNIST 的数据为例）：\n\n```python\nmnist_model = tl.build(model, torch.randn(1, 3, 28, 28))\n```\n\n* 如果你是在做文本分类，也可以用不同的 `input shape` 来构建相同的模型（例如，使用 300 维的预训练嵌入进行文本分类）：\n\n```python\n\n```python\n# [批次, 嵌入, 时间步], 第一维必须大于1，BatchNorm1d才能正常工作\ntext_model = tl.build(model, torch.randn(2, 300, 1))\n```\n\n* 最后，在实例化之后，你可以打印这两个模型，如下并排显示以提高可读性（__请注意维度的不同，例如 `torchlayers.build` 后的 `Conv2d` 和 `Conv1d`__）：\n\n```python\n                # 文本分类器                 MNIST分类器\n\n                Sequential(                       Sequential(\n                  (0): Conv1d(300, 64)              (0): Conv2d(3, 64)\n                  (1): ReLU()                       (1): ReLU()\n                  (2): BatchNorm1d(64)              (2): BatchNorm2d(64)\n                  (3): Conv1d(64, 128)              (3): Conv2d(64, 128)\n                  (4): ReLU()                       (4): ReLU()\n                  (5): Conv1d(128, 256)             (5): Conv2d(128, 256)\n                  (6): GlobalMaxPool()              (6): GlobalMaxPool()\n                  (7): Linear(256, 10)              (7): Linear(256, 10)\n                )                                 )\n```\n\n正如你所看到的，这两个模块都被“编译”成了原始的 PyTorch 层。\n\n## 具有形状推断能力的自定义模块\n\n用户可以定义任意模块，并使用 `torchlayers.infer` 函数使其具备形状推断能力：\n\n```python\n # 定义了一个带有 in_features 的类\n # 为了与可进行形状推断的版本区分开来，建议使用 _ 前缀和 Impl 后缀\nclass _MyLinearImpl(torch.nn.Module):\n    def __init__(self, in_features: int, out_features: int):\n        super().__init__()\n        self.weight = torch.nn.Parameter(torch.randn(out_features, in_features))\n        self.bias = torch.nn.Parameter(torch.randn(out_features))\n\n    def forward(self, inputs):\n        return torch.nn.functional.linear(inputs, self.weight, self.bias)\n\nMyLinear = tl.infer(_MyLinearImpl)\n\n# 构建并像使用本库中的其他层一样使用\nlayer =tl.build(MyLinear(out_features=32), torch.randn(1, 64))\nlayer(torch.randn(1, 64))\n```\n\n默认情况下，在首次 `forward` 传递过程中，会使用 `inputs.shape[1]` 作为 `in_features` 的值。如果你希望使用不同的索引（例如使用 `inputs.shape[3]` 进行推断），可以使用 `MyLayer = tl.infer(_MyLayerImpl, index=3)` 作为装饰器。\n\n## 具有倒残差瓶颈和像素洗牌的自动编码器\n\n如有需要，请查看代码注释和[__文档__](https:\u002F\u002Fszymonmaszke.github.io\u002Ftorchlayers\u002F)。如果你不确定什么是自动编码器，可以参考[__这篇示例博客文章__](https:\u002F\u002Ftowardsdatascience.com\u002Fauto-encoder-what-is-it-and-what-is-it-used-for-part-1-3e5c6f017726)。\n\n以下是针对类似 ImageNet 图像的卷积去噪自动编码器示例。可以将其视为展示 `torchlayers` 提供的各种层和构建块功能的一个演示。\n\n```python\n# 输入为 3 x 256 x 256，用于 ImageNet 重建\nclass AutoEncoder(torch.nn.Module):\n    def __init__(self):\n        super().__init__()\n        self.encoder = tl.Sequential(\n            tl.StandardNormalNoise(),  # 对输入图像添加噪声\n            tl.Conv(64, kernel_size=7),\n            tl.activations.Swish(),  # 直接访问模块 .activations\n            tl.InvertedResidualBottleneck(squeeze_excitation=False),\n            tl.AvgPool(),  # 形状为 64 x 128 x 128，默认内核大小为 2\n            tl.HardSwish(),  # 可直接通过 tl 访问\n            tl.SeparableConv(128),  # 将通道数增加到 128\n            tl.InvertedResidualBottleneck(),  # 默认带挤压激励机制\n            torch.nn.ReLU(),\n            tl.AvgPool(),  # 形状为 128 x 64 x 64，默认内核大小为 2\n            tl.DepthwiseConv(256),  # 深度可分离卷积更易于使用\n            # 像 PolyNet 一样将输入三次通过相同的权重\n            tl.Poly(tl.InvertedResidualBottleneck(), order=3),\n            tl.ReLU(),  # 所有 torch.nn 层都可以通过 tl 访问\n            tl.MaxPool(),  # 形状为 256 x 32 x 32\n            tl.Fire(out_channels=512),  # 形状为 512 x 32 x 32\n            tl.SqueezeExcitation(hidden=64),\n            tl.InvertedResidualBottleneck(),\n            tl.MaxPool(),  # 形状为 512 x 16 x 16\n            tl.InvertedResidualBottleneck(squeeze_excitation=False),\n            # 随机以 50% 的概率关闭最后两层\n            tl.StochasticDepth(\n                torch.nn.Sequential(\n                    tl.InvertedResidualBottleneck(squeeze_excitation=False),\n                    tl.InvertedResidualBottleneck(squeeze_excitation=False),\n                ),\n                p=0.5,\n            ),\n            tl.AvgPool(),  # 形状为 512 x 8 x 8\n        )\n\n        # 解码器部分更为“标准”\n        self.decoder = tl.Sequential(\n            tl.Poly(tl.InvertedResidualBottleneck(), order=2),\n            # 调用 `build` 后默认具有 ICNR 初始化\n            tl.ConvPixelShuffle(out_channels=512, upscale_factor=2),\n            # PixelShuffle 后形状为 512 x 16 x 16\n            tl.Poly(tl.InvertedResidualBottleneck(), order=3),\n            tl.ConvPixelShuffle(out_channels=256, upscale_factor=2),\n            # 形状为 256 x 32 x 32\n            tl.Poly(tl.InvertedResidualBottleneck(), order=3),\n            tl.ConvPixelShuffle(out_channels=128, upscale_factor=2),\n            # 形状为 128 x 64 x 64\n            tl.Poly(tl.InvertedResidualBottleneck(), order=4),\n            tl.ConvPixelShuffle(out_channels=64, upscale_factor=2),\n            # 形状为 64 x 128 x 128\n            tl.InvertedResidualBottleneck(),\n            tl.Conv(256),\n            tl.Dropout(),  # 默认为 0.5，且对图像使用 Dropout2d\n            tl.Swish(),\n            tl.InstanceNorm(),\n            tl.ConvPixelShuffle(out_channels=32, upscale_factor=2),\n            # 形状为 32 x 256 x 256\n            tl.Conv(16),\n            tl.Swish(),\n            tl.Conv(3),\n            # 最终形状为 3 x 256 x 256\n        )\n\n    def forward(self, inputs):\n        return self.decoder(self.encoder(inputs))\n```\n\n现在可以实例化该模块，并像往常一样使用 `torch.nn.MSELoss` 来训练它。\n\n```python\nautoencoder = tl.build(AutoEncoder(), torch.randn(1, 3, 256, 256))\n```\n\n# 安装\n\n## [pip](\u003Chttps:\u002F\u002Fpypi.org\u002Fproject\u002Ftorchlayers\u002F>)\n\n### 最新版本：\n\n```shell\npip install --user torchlayers\n```\n\n### 夜间版本：\n\n```shell\npip install --user torchlayers-nightly\n```\n\n## [Docker](https:\u002F\u002Fhub.docker.com\u002Fr\u002Fszymonmaszke\u002Ftorchlayers)\n\n在 [dockerhub](https:\u002F\u002Fhub.docker.com\u002Fr\u002Fszymonmaszke\u002Ftorchlayers\u002Ftags) 上提供了 __仅 CPU__ 版本以及各种 __支持 GPU__ 的镜像。\n\n对于 CPU 快速入门，执行以下命令：\n\n```shell\ndocker pull szymonmaszke\u002Ftorchlayers:18.04\n```\n\n夜间构建版本也可用，只需在标签前加上 `nightly_` 即可。如果要使用 `GPU` 镜像，请确保已安装 [nvidia\u002Fdocker](https:\u002F\u002Fgithub.com\u002FNVIDIA\u002Fnvidia-docker)，并正确配置其运行时环境。\n\n# 贡献\n\n如果您发现任何问题，或者希望添加某些功能（或自行实现），请[新建议题](https:\u002F\u002Fhelp.github.com\u002Fen\u002Farticles\u002Fcreating-an-issue)或[创建拉取请求](https:\u002F\u002Fhelp.github.com\u002Fen\u002Farticles\u002Fcreating-a-pull-request-from-a-fork)。","# torchlayers 快速上手指南\n\n**torchlayers** 是一个基于 PyTorch 的库，提供 `torch.nn` 层的**自动形状和维度推断**功能，并包含当前 SOTA 架构（如 Efficient-Net）中的额外构建模块。其使用体验类似 Keras，无需手动计算输入维度，只需调用一次 `torchlayers.build` 即可。\n\n## 环境准备\n\n在开始之前，请确保您的系统满足以下要求：\n\n*   **Python**: >= 3.7\n*   **PyTorch**: >= 1.3.0\n\n## 安装步骤\n\n您可以使用 pip 直接安装 torchlayers：\n\n```bash\npip install torchlayers\n```\n\n如果需要使用国内镜像源加速安装，可以使用以下命令：\n\n```bash\npip install torchlayers -i https:\u002F\u002Fpypi.tuna.tsinghua.edu.cn\u002Fsimple\n```\n\n## 基本使用\n\ntorchlayers 的核心优势在于自动推断层的大小（如 `in_channels` 或 `in_features`）。以下是两种常见的使用模式。\n\n### 1. 定义自定义模型类\n\n您可以继承 `tl.Module` 来定义网络，无需指定输入通道数，只需在构建时传入示例输入。\n\n```python\nimport torch\nimport torchlayers as tl\n\n\nclass Classifier(tl.Module):\n    def __init__(self):\n        super().__init__()\n        # 无需指定 in_channels，库会自动推断\n        self.conv1 = tl.Conv2d(64, kernel_size=6)\n        self.conv2 = tl.Conv2d(128, kernel_size=3)\n        self.conv3 = tl.Conv2d(256, kernel_size=3, padding=1)\n        \n        # 全局最大池化\n        self.pooling = tl.GlobalMaxPool()\n        \n        # 无需指定 in_features，库会自动推断\n        self.dense = tl.Linear(10)\n\n    def forward(self, x):\n        x = torch.relu(self.conv1(x))\n        x = torch.relu(self.conv2(x))\n        x = torch.relu(self.conv3(x))\n        return self.dense(self.pooling(x))\n\n# 关键步骤：传入模型实例和任意形状的示例输入进行构建\n# 此处示例输入为 batch_size=1, channels=3, height=32, width=32\nclf = tl.build(Classifier(), torch.randn(1, 3, 32, 32))\n```\n\n### 2. 使用 Sequential 快速搭建\n\ntorchlayers 可以与原生 `torch.nn` 模块混合使用。通过 `tl.build`，同一个模型结构可以根据不同的输入形状编译成不同维度的网络（例如从 1D 文本分类切换到 2D 图像分类）。\n\n```python\nimport torch\nimport torchlayers as tl\n\n# 定义模型结构\nmodel = torch.nn.Sequential(\n    tl.Conv(64),          # 仅指定输出通道，默认 kernel_size=3, padding=\"same\"\n    torch.nn.ReLU(),      # 可以混用原生 PyTorch 层\n    tl.BatchNorm(),       # 自动推断为 BatchNorm1d\u002F2d\u002F3d\n    tl.Conv(128),         # 默认 kernel_size=3\n    tl.ReLU(),\n    tl.Conv(256, kernel_size=11), \n    tl.GlobalMaxPool(),   # 类似 Keras 的全局池化\n    tl.Linear(10),        # 输出 10 类\n)\n\n# 场景 A: 构建用于 MNIST 图像分类的模型 (2D)\nmnist_model = tl.build(model, torch.randn(1, 3, 28, 28))\n\n# 场景 B: 构建用于文本分类的模型 (1D)\n# 输入形状: [batch, embedding_dim, timesteps]\ntext_model = tl.build(model, torch.randn(2, 300, 1))\n\n# 打印查看结构，你会发现 Conv 自动变为了 Conv2d 或 Conv1d\nprint(mnist_model)\nprint(text_model)\n```\n\n**核心提示：**\n*   所有 `torch.nn` 模块均可通过 `torchlayers` 使用。\n*   务必在定义好网络结构后，调用 `tl.build(model, example_input)` 来完成形状推断和初始化。\n*   构建后的模型即为标准的 PyTorch 模型，可正常进行训练、保存和导出 TorchScript。","某计算机视觉工程师正在基于 PyTorch 快速原型化一个用于医学影像分割的复杂卷积神经网络，需要频繁调整输入分辨率和网络层级深度以适配不同尺寸的 CT 扫描数据。\n\n### 没有 torchlayers 时\n- **手动计算维度繁琐且易错**：每增加或修改一个卷积层、池化层，开发者必须手工推导输出特征图的长宽和通道数，一旦公式记错或计算疏忽，运行时会直接抛出形状不匹配错误。\n- **代码冗余且缺乏灵活性**：为了适配不同的输入尺寸（如从 256x256 变为 512x512），往往需要硬编码多个网络变体，或在 `forward` 中编写大量动态 reshape 逻辑，导致代码臃肿难以维护。\n- **调试效率低下**：在构建深层网络时，经常因为中间某一层维度对不上而中断训练，排查过程如同“盲人摸象”，需逐层打印 tensor shape 才能定位问题，严重拖慢实验迭代速度。\n- **缺失高级封装特性**：PyTorch 原生不支持类似 Keras 的自动推断，无法直接使用 `\"same\"` 填充等便捷默认值，每次都要显式计算 padding 大小，增加了认知负担。\n\n### 使用 torchlayers 后\n- **自动推断形状，零手动计算**：只需定义层类型和参数，torchlayers 能在构建阶段自动推导每一层的输入输出维度，彻底消除人工计算误差，确保网络结构天生合法。\n- **动态适配输入，代码极简**：通过一次 `tl.build` 调用即可根据实际输入数据自动配置网络维度，同一套代码可无缝支持任意分辨率输入，无需重写模型类或添加复杂的条件判断。\n- **即时反馈，加速迭代**：在模型初始化阶段即可发现维度冲突，而非等到前向传播时报错；开发者能专注于架构设计本身，将原本数小时的调试时间缩短至几分钟。\n- **享受便捷默认值与高级层**：直接使用默认的 `\"same\"` 填充和常用卷积核大小，并轻松集成 SOTA 模块（如 SE 块），以更少的代码实现更先进的网络结构。\n\n核心价值在于将开发者从枯燥易错的维度数学计算中解放出来，让 PyTorch 拥有 Keras 般的开发流畅度，同时保留其底层的灵活性与高性能。","https:\u002F\u002Foss.gittoolsai.com\u002Fimages\u002Fszymonmaszke_torchlayers_c0a40056.png","szymonmaszke","Szymon Maszke","https:\u002F\u002Foss.gittoolsai.com\u002Favatars\u002Fszymonmaszke_1c8581a9.jpg","open source @open-nudge ",null,"github@maszke.co","https:\u002F\u002Fgithub.com\u002Fszymonmaszke",[84,88],{"name":85,"color":86,"percentage":87},"Python","#3572A5",99.8,{"name":89,"color":90,"percentage":91},"Shell","#89e051",0.2,570,44,"2026-02-23T14:32:35","MIT",1,"未说明",{"notes":99,"python":100,"dependencies":101},"该库基于 PyTorch，提供自动形状和维度推断功能。使用时需调用 tl.build() 并传入示例输入以完成模型构建。支持 TorchScript，无额外运行时开销。",">=3.7",[102],"torch>=1.3.0",[13],[105,106,107,108,109,110,111,112,113,114],"pytorch","inference","shape","keras","deep-learning","machine-learning","library","framework","sota","layers","2026-03-27T02:49:30.150509","2026-04-06T08:52:32.967822",[118,123,128,133,138,143],{"id":119,"question_zh":120,"answer_zh":121,"source_url":122},11309,"为什么在 Google Colab 或某些环境中导入 torchlayers 时会报错 \"AttributeError: module 'torchlayers' has no attribute 'Conv'\"？","这是因为 Python 版本不兼容。`torchlayers` 需要 Python 3.7 或更高版本，因为它依赖于 Python 3.7 引入的模块级 `__getattr__` 功能。Google Colab 当时默认提供的是 Python 3.6.9 运行时，因此不支持。解决方案是确保使用 Python 3.7+ 的环境，或者在 Paperspace 等平台上通过 Anaconda 设置 Python 3.7 环境。","https:\u002F\u002Fgithub.com\u002Fszymonmaszke\u002Ftorchlayers\u002Fissues\u002F5",{"id":124,"question_zh":125,"answer_zh":126,"source_url":127},11310,"执行 pip install torchlayers 时提示 \"ERROR: No matching distribution found for torchlayers\" 怎么办？","这通常是因为当前的 Python 版本不受支持。`torchlayers` 目前仅支持 Python 3.7 及以上版本。请检查你的 Python 版本（使用 `python --version`），如果低于 3.7，请升级 Python 版本后再尝试安装。","https:\u002F\u002Fgithub.com\u002Fszymonmaszke\u002Ftorchlayers\u002Fissues\u002F10",{"id":129,"question_zh":130,"answer_zh":131,"source_url":132},11311,"为什么会出现 \"AttributeError: module 'torchlayers' has no attribute 'Linear'\" 错误？","该错误同样是由于 Python 版本过低导致的。如果你的 Python 版本是 3.6，将无法使用 `torchlayers`。请确保安装并使用的是 Python 3.7 或更高版本。维护者已移除了允许在 Python 3.6 上安装的有问题的 wheel 包。","https:\u002F\u002Fgithub.com\u002Fszymonmaszke\u002Ftorchlayers\u002Fissues\u002F7",{"id":134,"question_zh":135,"answer_zh":136,"source_url":137},11312,"torchlayers 是否有官方推荐的简短别名？","是的，社区和维护者同意使用 `tl` 作为 `torchlayers` 的官方别名。建议在代码中使用 `import torchlayers as tl`，这样可以简化代码书写，例如使用 `tl.Conv()` 而不是 `torchlayers.Conv()`。","https:\u002F\u002Fgithub.com\u002Fszymonmaszke\u002Ftorchlayers\u002Fissues\u002F1",{"id":139,"question_zh":140,"answer_zh":141,"source_url":142},11313,"torchlayers 中的 SeparableConv 层是否比 PyTorch 原生的实现更快？","不，`torchlayers` 中的 `SeparableConv` 并没有提供性能优化。根据源码分析，它底层使用的仍然是 PyTorch 原生的分离卷积实现，而 PyTorch 原生的分离卷积已知存在速度较慢的问题。因此，它主要是一个提供便利性的层，而非性能优化层。","https:\u002F\u002Fgithub.com\u002Fszymonmaszke\u002Ftorchlayers\u002Fissues\u002F11",{"id":144,"question_zh":145,"answer_zh":146,"source_url":147},11314,"WeightDecay 的实现中直接替换梯度 (param.grad = ...) 是否正确？","虽然直接赋值看起来有问题，但维护者和用户讨论后指出，这种原地更新（in-place update）允许模型在后续步骤中简单地累加梯度，从而解决了部分问题。不过，由于 PyTorch 文档明确不鼓励原地修改梯度，且 Hook API 通常建议返回新的梯度值，因此在使用时需注意潜在的兼容性或最佳实践问题。目前的实现是为了让模型能正确累加后续梯度。","https:\u002F\u002Fgithub.com\u002Fszymonmaszke\u002Ftorchlayers\u002Fissues\u002F17",[149,154],{"id":150,"version":151,"summary_zh":152,"released_at":153},61801,"0.1.1","这是面向更广泛用户群体的首次发布前的一个小改动。\n\n# 改进\n\n- `torchlayers.Infer` 类现已成为 `torchlayers.infer` 函数，**不应再作为装饰器使用**，因为它会因 `pickle` 错误而破坏 `torch.build`（请参阅[此问题](https:\u002F\u002Fstackoverflow.com\u002Fquestions\u002F52185507\u002Fpickle-and-decorated-classes-picklingerror-not-the-same-object)以获取示例）。现在，应按照 [`README.md`](https:\u002F\u002Fgithub.com\u002Fszymonmaszke\u002Ftorchlayers\u002Fblob\u002Fmaster\u002FREADME.md#custom-modules-with-shape-inference-capabilities) 中的说明将其作为函数使用。\n- 已移除对 `Python 3.6` 的支持，因为 `torchlayers` 依赖于 `Python 3.7` 中模块级别的 `__getattr__` 和 `__dir__` 实现。\n\n","2020-03-27T22:10:49",{"id":155,"version":156,"summary_zh":157,"released_at":158},61802,"0.1.0","这是 `torchlayers` 的初始版本，该项目目前仍处于 Alpha 阶段。\n\n请参阅 [README.md](https:\u002F\u002Fgithub.com\u002Fszymonmaszke\u002Ftorchlayers\u002Fblob\u002Fmaster\u002FREADME.md)，以了解该库的功能和用途。\n\n如需深入了解提供的层及功能，请参阅 [文档](https:\u002F\u002Fszymonmaszke.github.io\u002Ftorchlayers\u002F)。","2020-03-16T16:24:57"]