[{"data":1,"prerenderedAt":-1},["ShallowReactive",2],{"similar-VincentStimper--normalizing-flows":3,"tool-VincentStimper--normalizing-flows":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 真正成长为懂上",138956,2,"2026-04-05T11:33:21",[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":69,"readme_en":70,"readme_zh":71,"quickstart_zh":72,"use_case_zh":73,"hero_image_url":74,"owner_login":75,"owner_name":76,"owner_avatar_url":77,"owner_bio":78,"owner_company":79,"owner_location":80,"owner_email":81,"owner_twitter":82,"owner_website":83,"owner_url":84,"languages":85,"stars":94,"forks":95,"last_commit_at":96,"license":97,"difficulty_score":23,"env_os":98,"env_gpu":99,"env_ram":98,"env_deps":100,"category_tags":105,"github_topics":106,"view_count":10,"oss_zip_url":81,"oss_zip_packed_at":81,"status":16,"created_at":117,"updated_at":118,"faqs":119,"releases":148},619,"VincentStimper\u002Fnormalizing-flows","normalizing-flows","PyTorch implementation of normalizing flow models","`normalizing-flows` 是一款基于 PyTorch 的高效开源库，专注于实现离散归一化流（Normalizing Flows）模型。在生成式人工智能领域，许多任务需要精确计算数据的概率密度或从复杂分布中高效采样，这正是归一化流的核心优势。然而，手动实现这些可逆变换链条往往繁琐且容易出错，`normalizing-flows` 通过封装多种主流架构解决了这一痛点。\n\n库中内置了包括 Glow、Real NVP、Neural Spline Flows 在内的丰富模型架构，用户只需少量代码即可搭建自定义流程。它不仅提供了详尽的文档和单元测试，还附带了多个 Jupyter Notebook 示例，如 CIFAR-10 图像生成和二维分布建模，方便用户在 Google Colab 上快速上手体验。\n\n这款库非常适合深度学习研究人员、算法工程师以及对概率建模感兴趣的开发者。无论你是想复现经典论文中的生成模型，还是探索新的流形学习应用，`normalizing-flows` 都能提供稳定可靠的底层支持，助你专注于模型创新而非重复造轮子。","# `normflows`: A PyTorch Package for Normalizing Flows\n\n[![documentation](https:\u002F\u002Fgithub.com\u002FVincentStimper\u002Fnormalizing-flows\u002Factions\u002Fworkflows\u002Fmkdocs.yaml\u002Fbadge.svg)](https:\u002F\u002Fvincentstimper.github.io\u002Fnormalizing-flows\u002F)\n![unit-tests](https:\u002F\u002Fgithub.com\u002FVincentStimper\u002Fnormalizing-flows\u002Factions\u002Fworkflows\u002Fpytest.yaml\u002Fbadge.svg)\n![code coverage](https:\u002F\u002Fraw.githubusercontent.com\u002FVincentStimper\u002Fnormalizing-flows\u002Fcoverage-badge\u002Fcoverage.svg?raw=true)\n[![License: MIT](https:\u002F\u002Fimg.shields.io\u002Fbadge\u002FLicence-MIT-b31b1b.svg)](https:\u002F\u002Fopensource.org\u002Flicenses\u002FMIT)\n[![DOI](https:\u002F\u002Fjoss.theoj.org\u002Fpapers\u002F10.21105\u002Fjoss.05361\u002Fstatus.svg)](https:\u002F\u002Fdoi.org\u002F10.21105\u002Fjoss.05361)\n[![PyPI](https:\u002F\u002Fimg.shields.io\u002Fbadge\u002FPyPI-1.7.3-blue.svg)](https:\u002F\u002Fpypi.org\u002Fproject\u002Fnormflows\u002F)\n[![Downloads](https:\u002F\u002Foss.gittoolsai.com\u002Fimages\u002FVincentStimper_normalizing-flows_readme_c95b5167a437.png)](https:\u002F\u002Fpepy.tech\u002Fproject\u002Fnormflows)\n\n\n`normflows` is a PyTorch implementation of discrete normalizing flows. Many popular flow architectures are implemented,\nsee the [list below](#implemented-flows). The package can be easily [installed via pip](#installation).\nThe basic usage is described [here](#usage), and a [full documentation](https:\u002F\u002Fvincentstimper.github.io\u002Fnormalizing-flows\u002F) \nis available as well. A more detailed description of this package is given in our\n[accompanying paper](https:\u002F\u002Fjoss.theoj.org\u002Fpapers\u002F10.21105\u002Fjoss.05361).\n\nSeveral sample use cases are provided in the \n[`examples` folder](https:\u002F\u002Fgithub.com\u002FVincentStimper\u002Fnormalizing-flows\u002Fblob\u002Fmaster\u002Fexamples), \nincluding [Glow](https:\u002F\u002Fgithub.com\u002FVincentStimper\u002Fnormalizing-flows\u002Fblob\u002Fmaster\u002Fexamples\u002Fglow.ipynb),\na [VAE](https:\u002F\u002Fgithub.com\u002FVincentStimper\u002Fnormalizing-flows\u002Fblob\u002Fmaster\u002Fexamples\u002Fvae.py), and\na [Residual Flow](https:\u002F\u002Fgithub.com\u002FVincentStimper\u002Fnormalizing-flows\u002Fblob\u002Fmaster\u002Fexamples\u002Fresidual.ipynb).\nMoreover, two simple applications are highlighed in the [examples section](#examples). You can run them \nyourself in Google Colab using the links below to get a feeling for `normflows`.\n\n| Link                                                                                                                                                                                                                                                  | Description                                                             |\n|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|-------------------------------------------------------------------------|\n| \u003Ca href=\"https:\u002F\u002Fcolab.research.google.com\u002Fgithub\u002FVincentStimper\u002Fnormalizing-flows\u002Fblob\u002Fmaster\u002Fexamples\u002Freal_nvp_colab.ipynb\" target=\"_parent\">\u003Cimg src=\"https:\u002F\u002Fcolab.research.google.com\u002Fassets\u002Fcolab-badge.svg\" alt=\"Open In Colab\"\u002F>\u003C\u002Fa>          | Real NVP applied to a 2D bimodal target distribution                    |\n| \u003Ca href=\"https:\u002F\u002Fcolab.research.google.com\u002Fgithub\u002FVincentStimper\u002Fnormalizing-flows\u002Fblob\u002Fmaster\u002Fexamples\u002Fpaper_example_nsf_colab.ipynb\" target=\"_parent\">\u003Cimg src=\"https:\u002F\u002Fcolab.research.google.com\u002Fassets\u002Fcolab-badge.svg\" alt=\"Open In Colab\"\u002F>\u003C\u002Fa> | Modeling a distribution on a cylinder surface with a neural spline flow |\n| \u003Ca href=\"https:\u002F\u002Fcolab.research.google.com\u002Fgithub\u002FVincentStimper\u002Fnormalizing-flows\u002Fblob\u002Fmaster\u002Fexamples\u002Fglow_colab.ipynb\" target=\"_parent\">\u003Cimg src=\"https:\u002F\u002Fcolab.research.google.com\u002Fassets\u002Fcolab-badge.svg\" alt=\"Open In Colab\"\u002F>\u003C\u002Fa>              | Modeling and generating CIFAR-10 images with Glow                       |\n\n\n## Implemented Flows\n\n| Architecture | Reference                                                                                                                 |\n|--------------|---------------------------------------------------------------------------------------------------------------------------|\n| Planar Flow  | [Rezende & Mohamed, 2015](http:\u002F\u002Fproceedings.mlr.press\u002Fv37\u002Frezende15.html)                                                |\n| Radial Flow  | [Rezende & Mohamed, 2015](http:\u002F\u002Fproceedings.mlr.press\u002Fv37\u002Frezende15.html)                                                |\n| NICE         | [Dinh et al., 2014](https:\u002F\u002Farxiv.org\u002Fabs\u002F1410.8516)                                                                      |\n| Real NVP     | [Dinh et al., 2017](https:\u002F\u002Fopenreview.net\u002Fforum?id=HkpbnH9lx)                                                            |\n| Glow         | [Kingma et al., 2018](https:\u002F\u002Fproceedings.neurips.cc\u002Fpaper\u002F2018\u002Fhash\u002Fd139db6a236200b21cc7f752979132d0-Abstract.html)                                                                   |\n| Masked Autoregressive Flow | [Papamakarios et al., 2017](https:\u002F\u002Fproceedings.neurips.cc\u002Fpaper\u002F2017\u002Fhash\u002F6c1da886822c67822bcf3679d04369fa-Abstract.html) |\n| Neural Spline Flow | [Durkan et al., 2019](https:\u002F\u002Fproceedings.neurips.cc\u002Fpaper\u002F2019\u002Fhash\u002F7ac71d433f282034e088473244df8c02-Abstract.html)                                                                    |\n| Circular Neural Spline Flow | [Rezende et al., 2020](http:\u002F\u002Fproceedings.mlr.press\u002Fv119\u002Frezende20a.html)                                                 |\n| Residual Flow | [Chen et al., 2019](https:\u002F\u002Fproceedings.neurips.cc\u002Fpaper\u002F2019\u002Fhash\u002F5d0d5594d24f0f955548f0fc0ff83d10-Abstract.html)                                                                     |\n| Stochastic Normalizing Flow | [Wu et al., 2020](https:\u002F\u002Fproceedings.neurips.cc\u002Fpaper\u002F2020\u002Fhash\u002F41d80bfc327ef980528426fc810a6d7a-Abstract.html)                                                                       |\n\nNote that Neural Spline Flows with circular and non-circular coordinates\nare supported as well.\n\n## Installation\n\nThe latest version of the package can be installed via pip\n\n```\npip install normflows\n```\n\nAt least Python 3.7 is required. If you want to use a GPU, make sure that\nPyTorch is set up correctly by following the instructions at the\n[PyTorch website](https:\u002F\u002Fpytorch.org\u002Fget-started\u002Flocally\u002F).\n\nTo run the example notebooks clone the repository first\n\n```\ngit clone https:\u002F\u002Fgithub.com\u002FVincentStimper\u002Fnormalizing-flows.git\n```\n\nand then install the dependencies.\n\n```\npip install -r requirements_examples.txt\n```\n\n## Usage\n\nA normalizing flow consists of a base distribution, defined in \n[`nf.distributions.base`](https:\u002F\u002Fgithub.com\u002FVincentStimper\u002Fnormalizing-flows\u002Fblob\u002Fmaster\u002Fnormflows\u002Fdistributions\u002Fbase.py),\nand a list of flows, given in\n[`nf.flows`](https:\u002F\u002Fgithub.com\u002FVincentStimper\u002Fnormalizing-flows\u002Fblob\u002Fmaster\u002Fnormflows\u002Fflows).\nLet's assume our target is a 2D distribution. We pick a diagonal Gaussian\nbase distribution, which is the most popular choice. Our flow shall be a\n[Real NVP model](https:\u002F\u002Fopenreview.net\u002Fforum?id=HkpbnH9lx) and, therefore, we need\nto define a neural network for computing the parameters of the affine coupling\nmap. One dimension is used to compute the scale and shift parameter for the\nother dimension. After each coupling layer we swap their roles.\n\n```python\nimport normflows as nf\n\n# Define 2D Gaussian base distribution\nbase = nf.distributions.base.DiagGaussian(2)\n\n# Define list of flows\nnum_layers = 32\nflows = []\nfor i in range(num_layers):\n    # Neural network with two hidden layers having 64 units each\n    # Last layer is initialized by zeros making training more stable\n    param_map = nf.nets.MLP([1, 64, 64, 2], init_zeros=True)\n    # Add flow layer\n    flows.append(nf.flows.AffineCouplingBlock(param_map))\n    # Swap dimensions\n    flows.append(nf.flows.Permute(2, mode='swap'))\n```\n\nOnce they are set up, we can define a\n[`nf.NormalizingFlow`](https:\u002F\u002Fgithub.com\u002FVincentStimper\u002Fnormalizing-flows\u002Fblob\u002Fmaster\u002Fnormflows\u002Fcore.py#L9)\nmodel. If the target density is available, it can be added to the model\nto be used during training. Sample target distributions are given in\n[`nf.distributions.target`](https:\u002F\u002Fgithub.com\u002FVincentStimper\u002Fnormalizing-flows\u002Fblob\u002Fmaster\u002Fnormflows\u002Fdistributions\u002Ftarget.py).\n\n```python\n# If the target density is not given\nmodel = nf.NormalizingFlow(base, flows)\n\n# If the target density is given\ntarget = nf.distributions.target.TwoMoons()\nmodel = nf.NormalizingFlow(base, flows, target)\n```\n\nThe loss can be computed with the methods of the model and minimized.\n\n```python\n# When doing maximum likelihood learning, i.e. minimizing the forward KLD\n# with no target distribution given\nloss = model.forward_kld(x)\n\n# When minimizing the reverse KLD based on the given target distribution\nloss = model.reverse_kld(num_samples=512)\n\n# Optimization as usual\nloss.backward()\noptimizer.step()\n```\n\n## Examples\n\nWe provide several illustrative examples of how to use the package in the\n[`examples`](https:\u002F\u002Fgithub.com\u002FVincentStimper\u002Fnormalizing-flows\u002Fblob\u002Fmaster\u002Fexamples)\ndirectory. Among them are implementations of \n[Glow](https:\u002F\u002Fgithub.com\u002FVincentStimper\u002Fnormalizing-flows\u002Fblob\u002Fmaster\u002Fexamples\u002Fglow.ipynb),\na [VAE](https:\u002F\u002Fgithub.com\u002FVincentStimper\u002Fnormalizing-flows\u002Fblob\u002Fmaster\u002Fexamples\u002Fvae.py), and\na [Residual Flow](https:\u002F\u002Fgithub.com\u002FVincentStimper\u002Fnormalizing-flows\u002Fblob\u002Fmaster\u002Fexamples\u002Fresidual.ipynb). \nMore advanced experiments can be done with the scripts listed in the\n[repository about resampled base distributions](https:\u002F\u002Fgithub.com\u002FVincentStimper\u002Fresampled-base-flows),\nsee its [`experiments`](https:\u002F\u002Fgithub.com\u002FVincentStimper\u002Fresampled-base-flows\u002Ftree\u002Fmaster\u002Fexperiments)\nfolder.\n\nBelow, we consider two simple 2D examples.\n\n### Real NVP applied to a 2D bimodal target distribution\n\n\u003Ca href=\"https:\u002F\u002Fcolab.research.google.com\u002Fgithub\u002FVincentStimper\u002Fnormalizing-flows\u002Fblob\u002Fmaster\u002Fexamples\u002Freal_nvp_colab.ipynb\" target=\"_parent\">\u003Cimg src=\"https:\u002F\u002Fcolab.research.google.com\u002Fassets\u002Fcolab-badge.svg\" alt=\"Open In Colab\"\u002F>\u003C\u002Fa>\n\nIn [this notebook](https:\u002F\u002Fgithub.com\u002FVincentStimper\u002Fnormalizing-flows\u002Fblob\u002Fmaster\u002Fexamples\u002Freal_nvp_colab.ipynb), \nwhich can directly be opened in \n[Colab](https:\u002F\u002Fcolab.research.google.com\u002Fgithub\u002FVincentStimper\u002Fnormalizing-flows\u002Fblob\u002Fmaster\u002Fexamples\u002Freal_nvp_colab.ipynb),\nwe consider a 2D distribution with two half-moon-shaped modes as a target. We approximate it with a Real NVP model\nand obtain the following results.\n\n![2D target distribution and Real NVP model](https:\u002F\u002Foss.gittoolsai.com\u002Fimages\u002FVincentStimper_normalizing-flows_readme_19b49f0e370e.png)\n\nNote that there might be a density filament connecting the two modes, which is due to an architectural limitation \nof normalizing flows, especially prominent in Real NVP. You can find out more about it in \n[this paper](https:\u002F\u002Fproceedings.mlr.press\u002Fv151\u002Fstimper22a).\n\n### Modeling a distribution on a cylinder surface with a neural spline flow\n\n\u003Ca href=\"https:\u002F\u002Fcolab.research.google.com\u002Fgithub\u002FVincentStimper\u002Fnormalizing-flows\u002Fblob\u002Fmaster\u002Fexamples\u002Fpaper_example_nsf_colab.ipynb\" target=\"_parent\">\u003Cimg src=\"https:\u002F\u002Fcolab.research.google.com\u002Fassets\u002Fcolab-badge.svg\" alt=\"Open In Colab\"\u002F>\u003C\u002Fa>\n\nIn [another example](https:\u002F\u002Fgithub.com\u002FVincentStimper\u002Fnormalizing-flows\u002Fblob\u002Fmaster\u002Fexamples\u002Fpaper_example_nsf_colab.ipynb),\nwhich is available in [Colab](https:\u002F\u002Fcolab.research.google.com\u002Fgithub\u002FVincentStimper\u002Fnormalizing-flows\u002Fblob\u002Fmaster\u002Fexamples\u002Fpaper_example_nsf_colab.ipynb)\nas well, we apply a Neural Spline Flow model to a distribution defined on a cylinder. The resulting density is visualized below.\n\n![Neural Spline Flow applied to target distribution on a cylinder](https:\u002F\u002Foss.gittoolsai.com\u002Fimages\u002FVincentStimper_normalizing-flows_readme_7a95b055640c.png)\n\nThis example is considered in the [paper](https:\u002F\u002Fjoss.theoj.org\u002Fpapers\u002F10.21105\u002Fjoss.05361) accompanying this repository.\n\n## Support\n\nIf you have problems, please read the [package documentation](https:\u002F\u002Fvincentstimper.github.io\u002Fnormalizing-flows\u002F)\nand check out the [examples section](#examples) above. You are also welcome to \n[create issues on GitHub](https:\u002F\u002Fgithub.com\u002FVincentStimper\u002Fnormalizing-flows\u002Fissues) to get help. Note that it is\nworthwhile browsing the existing [open](https:\u002F\u002Fgithub.com\u002FVincentStimper\u002Fnormalizing-flows\u002Fissues?q=is%3Aopen+is%3Aissue) \nand [closed](https:\u002F\u002Fgithub.com\u002FVincentStimper\u002Fnormalizing-flows\u002Fissues?q=is%3Aissue+is%3Aclosed) issues, which might\naddress the problem you are facing.\n\n## Contributing\n\nIf you find a bug or have a feature request, please \n[file an issue on GitHub](https:\u002F\u002Fgithub.com\u002FVincentStimper\u002Fnormalizing-flows\u002Fissues).\n\nYou are welcome to contribute to the package by fixing the bug or adding the feature yourself. If you want to \ncontribute, please add tests for the code you added or modified and ensure it passes successfully by running `pytest`.\nThis can be done by simply executing\n```\npytest\n```\nwithin your local version of the repository. Make sure you code is well documented, and we also encourage contributions\nto the existing documentation. Once you finished coding and testing, please \n[create a pull request on GitHub](https:\u002F\u002Fdocs.github.com\u002Fen\u002Fpull-requests\u002Fcollaborating-with-pull-requests\u002Fproposing-changes-to-your-work-with-pull-requests\u002Fcreating-a-pull-request).\n\n## Used by\n\nThe package has been used in several research papers. Some of them are listed below.\n\n> Andrew Campbell, Wenlong Chen, Vincent Stimper, José Miguel Hernández-Lobato, and Yichuan Zhang. \n> [A gradient based strategy for Hamiltonian Monte Carlo hyperparameter optimization](https:\u002F\u002Fproceedings.mlr.press\u002Fv139\u002Fcampbell21a.html). \n> In Proceedings of the 38th International Conference on Machine Learning, pp. 1238–1248. PMLR, 2021.\n> \n> [Code available on GitHub.](https:\u002F\u002Fgithub.com\u002FVincentStimper\u002Fhmc-hyperparameter-tuning)\n\n> Vincent Stimper, Bernhard Schölkopf, and José Miguel Hernández-Lobato. \n> [Resampling Base Distributions of Normalizing Flows](https:\u002F\u002Fproceedings.mlr.press\u002Fv151\u002Fstimper22a). \n> In Proceedings of The 25th International Conference on Artificial Intelligence and Statistics, volume 151, pp. 4915–4936, 2022.\n> \n> [Code available on GitHub.](https:\u002F\u002Fgithub.com\u002FVincentStimper\u002Fresampled-base-flows)\n\n> Laurence I. Midgley, Vincent Stimper, Gregor N. C. Simm, Bernhard Schölkopf, and José Miguel Hernández-Lobato. \n> [Flow Annealed Importance Sampling Bootstrap](https:\u002F\u002Farxiv.org\u002Fabs\u002F2208.01893). \n> The Eleventh International Conference on Learning Representations, 2023.\n> \n> [Code available on GitHub.](https:\u002F\u002Fgithub.com\u002Flollcat\u002Ffab-torch)\n\n> Arnau Quera-Bofarull, Joel Dyer, Anisoara Calinescu, J. Doyne Farmer, and Michael Wooldridge.\n> [BlackBIRDS: Black-Box Inference foR Differentiable Simulators](https:\u002F\u002Fjoss.theoj.org\u002Fpapers\u002F10.21105\u002Fjoss.05776).\n> Journal of Open Source Software, 8(89), 5776, 2023.\n> \n> [Code available on GitHub.](https:\u002F\u002Fgithub.com\u002Farnauqb\u002Fblackbirds)\n\n> Utkarsh Singhal, Carlos Esteves, Ameesh Makadia, and Stella X. Yu.\n> [Learning to Transform for Generalizable Instance-wise Invariance](https:\u002F\u002Fopenaccess.thecvf.com\u002Fcontent\u002FICCV2023\u002Fhtml\u002FSinghal_Learning_to_Transform_for_Generalizable_Instance-wise_Invariance_ICCV_2023_paper.html).\n> Proceedings of the IEEE\u002FCVF International Conference on Computer Vision (ICCV), pp. 6211-6221, 2023.\n> \n> [Code available on GitHub.](https:\u002F\u002Fgithub.com\u002Fsutkarsh\u002Fflow_inv)\n\n> Ba-Hien Tran, Giulio Franzese, Pietro Michiardi, and Maurizio Filippone.\n> [One-Line-of-Code Data Mollification Improves Optimization of Likelihood-based Generative Models](https:\u002F\u002Fproceedings.neurips.cc\u002Fpaper_files\u002Fpaper\u002F2023\u002Fhash\u002F1516a7f7507d5550db5c7f29e995ec8c-Abstract-Conference.html).\n>  Advances in Neural Information Processing Systems 36, pp. 6545–6567, 2023.\n> \n> [Code available on GitHub.](https:\u002F\u002Fgithub.com\u002Ftranbahien\u002Fdata-mollification)\n\nMoreover, the [`boltzgen`](https:\u002F\u002Fgithub.com\u002FVincentStimper\u002Fboltzmann-generators) package\nhas been build upon `normflows`.\n\n## Citation\n\nIf you use `normflows`, please cite the \n[corresponding paper](https:\u002F\u002Fjoss.theoj.org\u002Fpapers\u002F10.21105\u002Fjoss.05361) as follows.\n\n> Stimper et al., (2023). normflows: A PyTorch Package for Normalizing Flows. \n> Journal of Open Source Software, 8(86), 5361, https:\u002F\u002Fdoi.org\u002F10.21105\u002Fjoss.05361\n\n**Bibtex**\n\n```\n@article{Stimper2023, \n  author = {Vincent Stimper and David Liu and Andrew Campbell and Vincent Berenz and Lukas Ryll and Bernhard Schölkopf and José Miguel Hernández-Lobato}, \n  title = {normflows: A PyTorch Package for Normalizing Flows}, \n  journal = {Journal of Open Source Software}, \n  volume = {8},\n  number = {86}, \n  pages = {5361}, \n  publisher = {The Open Journal}, \n  doi = {10.21105\u002Fjoss.05361}, \n  url = {https:\u002F\u002Fdoi.org\u002F10.21105\u002Fjoss.05361}, \n  year = {2023}\n} \n```\n\n\n\n\n\n","# `normflows`: 一个基于 PyTorch 的归一化流（Normalizing Flows）包\n\n[![documentation](https:\u002F\u002Fgithub.com\u002FVincentStimper\u002Fnormalizing-flows\u002Factions\u002Fworkflows\u002Fmkdocs.yaml\u002Fbadge.svg)](https:\u002F\u002Fvincentstimper.github.io\u002Fnormalizing-flows\u002F)\n![unit-tests](https:\u002F\u002Fgithub.com\u002FVincentStimper\u002Fnormalizing-flows\u002Factions\u002Fworkflows\u002Fpytest.yaml\u002Fbadge.svg)\n![code coverage](https:\u002F\u002Fraw.githubusercontent.com\u002FVincentStimper\u002Fnormalizing-flows\u002Fcoverage-badge\u002Fcoverage.svg?raw=true)\n[![License: MIT](https:\u002F\u002Fimg.shields.io\u002Fbadge\u002FLicence-MIT-b31b1b.svg)](https:\u002F\u002Fopensource.org\u002Flicenses\u002FMIT)\n[![DOI](https:\u002F\u002Fjoss.theoj.org\u002Fpapers\u002F10.21105\u002Fjoss.05361\u002Fstatus.svg)](https:\u002F\u002Fdoi.org\u002F10.21105\u002Fjoss.05361)\n[![PyPI](https:\u002F\u002Fimg.shields.io\u002Fbadge\u002FPyPI-1.7.3-blue.svg)](https:\u002F\u002Fpypi.org\u002Fproject\u002Fnormflows\u002F)\n[![Downloads](https:\u002F\u002Foss.gittoolsai.com\u002Fimages\u002FVincentStimper_normalizing-flows_readme_c95b5167a437.png)](https:\u002F\u002Fpepy.tech\u002Fproject\u002Fnormflows)\n\n\n`normflows` 是一个离散归一化流（discrete normalizing flows）的 PyTorch 实现。实现了许多流行的流架构，详见 [下方列表](#implemented-flows)。该包可以通过 [pip 轻松安装](#installation)。基本用法在 [此处](#usage) 描述，同时也提供 [完整文档](https:\u002F\u002Fvincentstimper.github.io\u002Fnormalizing-flows\u002F)。关于本包的更详细描述见我们的 [配套论文](https:\u002F\u002Fjoss.theoj.org\u002Fpapers\u002F10.21105\u002Fjoss.05361)。\n\n在 [`examples` 文件夹](https:\u002F\u002Fgithub.com\u002FVincentStimper\u002Fnormalizing-flows\u002Fblob\u002Fmaster\u002Fexamples) 中提供了几个示例用例，包括 [Glow](https:\u002F\u002Fgithub.com\u002FVincentStimper\u002Fnormalizing-flows\u002Fblob\u002Fmaster\u002Fexamples\u002Fglow.ipynb)、[VAE](https:\u002F\u002Fgithub.com\u002FVincentStimper\u002Fnormalizing-flows\u002Fblob\u002Fmaster\u002Fexamples\u002Fvae.py) 和 [Residual Flow](https:\u002F\u002Fgithub.com\u002FVincentStimper\u002Fnormalizing-flows\u002Fblob\u002Fmaster\u002Fexamples\u002Fresidual.ipynb)。此外，两个简单应用在 [示例部分](#examples) 中被突出展示。你可以使用下方的链接自己在 Google Colab 中运行它们，以熟悉 `normflows`。\n\n| Link                                                                                                                                                                                                                                                  | Description                                                             |\n|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|-------------------------------------------------------------------------|\n| \u003Ca href=\"https:\u002F\u002Fcolab.research.google.com\u002Fgithub\u002FVincentStimper\u002Fnormalizing-flows\u002Fblob\u002Fmaster\u002Fexamples\u002Freal_nvp_colab.ipynb\" target=\"_parent\">\u003Cimg src=\"https:\u002F\u002Fcolab.research.google.com\u002Fassets\u002Fcolab-badge.svg\" alt=\"Open In Colab\"\u002F>\u003C\u002Fa>          | Real NVP 应用于双模态二维目标分布                    |\n| \u003Ca href=\"https:\u002F\u002Fcolab.research.google.com\u002Fgithub\u002FVincentStimper\u002Fnormalizing-flows\u002Fblob\u002Fmaster\u002Fexamples\u002Fpaper_example_nsf_colab.ipynb\" target=\"_parent\">\u003Cimg src=\"https:\u002F\u002Fcolab.research.google.com\u002Fassets\u002Fcolab-badge.svg\" alt=\"Open In Colab\"\u002F>\u003C\u002Fa> | 使用神经样条流（Neural Spline Flow）对圆柱面上的分布进行建模 |\n| \u003Ca href=\"https:\u002F\u002Fcolab.research.google.com\u002Fgithub\u002FVincentStimper\u002Fnormalizing-flows\u002Fblob\u002Fmaster\u002Fexamples\u002Fglow_colab.ipynb\" target=\"_parent\">\u003Cimg src=\"https:\u002F\u002Fcolab.research.google.com\u002Fassets\u002Fcolab-badge.svg\" alt=\"Open In Colab\"\u002F>\u003C\u002Fa>              | 使用 Glow 建模和生成 CIFAR-10 图像                       |\n\n\n## Implemented Flows\n\n| Architecture | Reference                                                                                                                 |\n|--------------|---------------------------------------------------------------------------------------------------------------------------|\n| Planar Flow (平面流)  | [Rezende & Mohamed, 2015](http:\u002F\u002Fproceedings.mlr.press\u002Fv37\u002Frezende15.html)                                                |\n| Radial Flow (径向流)  | [Rezende & Mohamed, 2015](http:\u002F\u002Fproceedings.mlr.press\u002Fv37\u002Frezende15.html)                                                |\n| NICE         | [Dinh et al., 2014](https:\u002F\u002Farxiv.org\u002Fabs\u002F1410.8516)                                                                      |\n| Real NVP     | [Dinh et al., 2017](https:\u002F\u002Fopenreview.net\u002Fforum?id=HkpbnH9lx)                                                            |\n| Glow         | [Kingma et al., 2018](https:\u002F\u002Fproceedings.neurips.cc\u002Fpaper\u002F2018\u002Fhash\u002Fd139db6a236200b21cc7f752979132d0-Abstract.html)                                                                   |\n| Masked Autoregressive Flow (掩码自回归流) | [Papamakarios et al., 2017](https:\u002F\u002Fproceedings.neurips.cc\u002Fpaper\u002F2017\u002Fhash\u002F6c1da886822c67822bcf3679d04369fa-Abstract.html) |\n| Neural Spline Flow (神经样条流) | [Durkan et al., 2019](https:\u002F\u002Fproceedings.neurips.cc\u002Fpaper\u002F2019\u002Fhash\u002F7ac71d433f282034e088473244df8c02-Abstract.html)                                                                    |\n| Circular Neural Spline Flow (圆形神经样条流) | [Rezende et al., 2020](http:\u002F\u002Fproceedings.mlr.press\u002Fv119\u002Frezende20a.html)                                                 |\n| Residual Flow (残差流) | [Chen et al., 2019](https:\u002F\u002Fproceedings.neurips.cc\u002Fpaper\u002F2019\u002Fhash\u002F5d0d5594d24f0f955548f0fc0ff83d10-Abstract.html)                                                                     |\n| Stochastic Normalizing Flow (随机归一化流) | [Wu et al., 2020](https:\u002F\u002Fproceedings.neurips.cc\u002Fpaper\u002F2020\u002Fhash\u002F41d80bfc327ef980528426fc810a6d7a-Abstract.html)                                                                       |\n\n注意，也支持具有圆形和非圆形坐标的神经样条流（Neural Spline Flows）。\n\n## Installation\n\n该包的最新版可通过 pip 安装\n\n```\npip install normflows\n```\n\n至少需要 Python 3.7。如果你打算使用 GPU，请按照 [PyTorch 网站](https:\u002F\u002Fpytorch.org\u002Fget-started\u002Flocally\u002F) 上的说明确保 PyTorch 配置正确。\n\n要运行示例笔记本，请先克隆仓库\n\n```\ngit clone https:\u002F\u002Fgithub.com\u002FVincentStimper\u002Fnormalizing-flows.git\n```\n\n然后安装依赖项。\n\n```\npip install -r requirements_examples.txt\n```\n\n## 使用\n\n一个归一化流（Normalizing Flow）由定义在\n[`nf.distributions.base`](https:\u002F\u002Fgithub.com\u002FVincentStimper\u002Fnormalizing-flows\u002Fblob\u002Fmaster\u002Fnormflows\u002Fdistributions\u002Fbase.py)\n的基础分布和 [`nf.flows`](https:\u002F\u002Fgithub.com\u002FVincentStimper\u002Fnormalizing-flows\u002Fblob\u002Fmaster\u002Fnormflows\u002Fflows) 中给出的流列表组成。\n假设我们的目标是二维分布。我们选择一个对角高斯基础分布，这是最常见的选择。我们的流将是一个\n[Real NVP 模型](https:\u002F\u002Fopenreview.net\u002Fforum?id=HkpbnH9lx)，因此我们需要\n定义一个神经网络来计算仿射耦合映射的参数。一个维度用于计算另一个维度的缩放和平移参数。在每个耦合层之后，我们交换它们的角色。\n\n```python\nimport normflows as nf\n\n# Define 2D Gaussian base distribution\nbase = nf.distributions.base.DiagGaussian(2)\n\n# Define list of flows\nnum_layers = 32\nflows = []\nfor i in range(num_layers):\n    # Neural network with two hidden layers having 64 units each\n    # Last layer is initialized by zeros making training more stable\n    param_map = nf.nets.MLP([1, 64, 64, 2], init_zeros=True)\n    # Add flow layer\n    flows.append(nf.flows.AffineCouplingBlock(param_map))\n    # Swap dimensions\n    flows.append(nf.flows.Permute(2, mode='swap'))\n```\n\n设置完成后，我们可以定义一个\n[`nf.NormalizingFlow`](https:\u002F\u002Fgithub.com\u002FVincentStimper\u002Fnormalizing-flows\u002Fblob\u002Fmaster\u002Fnormflows\u002Fcore.py#L9)\n模型。如果目标密度可用，可以将其添加到模型中以便在训练期间使用。示例目标分布在\n[`nf.distributions.target`](https:\u002F\u002Fgithub.com\u002FVincentStimper\u002Fnormalizing-flows\u002Fblob\u002Fmaster\u002Fnormflows\u002Fdistributions\u002Ftarget.py) 中给出。\n\n```python\n# If the target density is not given\nmodel = nf.NormalizingFlow(base, flows)\n\n# If the target density is given\ntarget = nf.distributions.target.TwoMoons()\nmodel = nf.NormalizingFlow(base, flows, target)\n```\n\n可以使用模型的这些方法计算损失并进行最小化。\n\n```python\n# When doing maximum likelihood learning, i.e. minimizing the forward KLD\n# with no target distribution given\nloss = model.forward_kld(x)\n\n# When minimizing the reverse KLD based on the given target distribution\nloss = model.reverse_kld(num_samples=512)\n\n# Optimization as usual\nloss.backward()\noptimizer.step()\n```\n\n## 示例\n\n我们在 [`examples`](https:\u002F\u002Fgithub.com\u002FVincentStimper\u002Fnormalizing-flows\u002Fblob\u002Fmaster\u002Fexamples)\n目录下提供了几个关于如何使用该包的说明性示例。其中包括 \n[Glow](https:\u002F\u002Fgithub.com\u002FVincentStimper\u002Fnormalizing-flows\u002Fblob\u002Fmaster\u002Fexamples\u002Fglow.ipynb)、\n[变分自编码器（VAE）](https:\u002F\u002Fgithub.com\u002FVincentStimper\u002Fnormalizing-flows\u002Fblob\u002Fmaster\u002Fexamples\u002Fvae.py) 的实现，以及\n[残差流（Residual Flow）](https:\u002F\u002Fgithub.com\u002FVincentStimper\u002Fnormalizing-flows\u002Fblob\u002Fmaster\u002Fexamples\u002Fresidual.ipynb)。\n更高级的实验可以在 [重采样基础分布的仓库](https:\u002F\u002Fgithub.com\u002FVincentStimper\u002Fresampled-base-flows)\n中列出的脚本中进行，请参阅其 [`experiments`](https:\u002F\u002Fgithub.com\u002FVincentStimper\u002Fresampled-base-flows\u002Ftree\u002Fmaster\u002Fexperiments)\n文件夹。\n\n下面，我们考虑两个简单的二维示例。\n\n### Real NVP 应用于二维双峰目标分布\n\n\u003Ca href=\"https:\u002F\u002Fcolab.research.google.com\u002Fgithub\u002FVincentStimper\u002Fnormalizing-flows\u002Fblob\u002Fmaster\u002Fexamples\u002Freal_nvp_colab.ipynb\" target=\"_parent\">\u003Cimg src=\"https:\u002F\u002Fcolab.research.google.com\u002Fassets\u002Fcolab-badge.svg\" alt=\"Open In Colab\"\u002F>\u003C\u002Fa>\n\n在 [这个笔记本](https:\u002F\u002Fgithub.com\u002FVincentStimper\u002Fnormalizing-flows\u002Fblob\u002Fmaster\u002Fexamples\u002Freal_nvp_colab.ipynb) 中，\n可以直接在 [Colab](https:\u002F\u002Fcolab.research.google.com\u002Fgithub\u002FVincentStimper\u002Fnormalizing-flows\u002Fblob\u002Fmaster\u002Fexamples\u002Freal_nvp_colab.ipynb) 中打开，\n我们将具有两个半月形模式的二维分布作为目标。我们用 Real NVP 模型对其进行近似，并获得以下结果。\n\n![2D target distribution and Real NVP model](https:\u002F\u002Foss.gittoolsai.com\u002Fimages\u002FVincentStimper_normalizing-flows_readme_19b49f0e370e.png)\n\n请注意，两个模式之间可能存在连接密度细丝，这是由于归一化流的架构限制所致，特别是在 Real NVP 中尤为明显。你可以在\n[这篇论文](https:\u002F\u002Fproceedings.mlr.press\u002Fv151\u002Fstimper22a) 中找到更多关于它的信息。\n\n### 使用神经样条流（Neural Spline Flow）对圆柱面上的分布进行建模\n\n\u003Ca href=\"https:\u002F\u002Fcolab.research.google.com\u002Fgithub\u002FVincentStimper\u002Fnormalizing-flows\u002Fblob\u002Fmaster\u002Fexamples\u002Fpaper_example_nsf_colab.ipynb\" target=\"_parent\">\u003Cimg src=\"https:\u002F\u002Fcolab.research.google.com\u002Fassets\u002Fcolab-badge.svg\" alt=\"Open In Colab\"\u002F>\u003C\u002Fa>\n\n在 [另一个示例](https:\u002F\u002Fgithub.com\u002FVincentStimper\u002Fnormalizing-flows\u002Fblob\u002Fmaster\u002Fexamples\u002Fpaper_example_nsf_colab.ipynb) 中，\n该示例也可在 [Colab](https:\u002F\u002Fcolab.research.google.com\u002Fgithub\u002FVincentStimper\u002Fnormalizing-flows\u002Fblob\u002Fmaster\u002Fexamples\u002Fpaper_example_nsf_colab.ipynb) 上获得，\n我们将神经样条流（Neural Spline Flow）模型应用于圆柱上定义的分布。生成的密度如下图所示。\n\n![Neural Spline Flow applied to target distribution on a cylinder](https:\u002F\u002Foss.gittoolsai.com\u002Fimages\u002FVincentStimper_normalizing-flows_readme_7a95b055640c.png)\n\n本示例包含在与本仓库配套的 [论文](https:\u002F\u002Fjoss.theoj.org\u002Fpapers\u002F10.21105\u002Fjoss.05361) 中。\n\n## 支持\n\n如果您遇到问题，请阅读 [包文档](https:\u002F\u002Fvincentstimper.github.io\u002Fnormalizing-flows\u002F)\n并查看上面的 [示例部分](#examples)。也欢迎您在 [GitHub 上创建问题](https:\u002F\u002Fgithub.com\u002FVincentStimper\u002Fnormalizing-flows\u002Fissues) 以获取帮助。请注意，浏览现有的 [开放](https:\u002F\u002Fgithub.com\u002FVincentStimper\u002Fnormalizing-flows\u002Fissues?q=is%3Aopen+is%3Aissue) \n和 [已关闭](https:\u002F\u002Fgithub.com\u002FVincentStimper\u002Fnormalizing-flows\u002Fissues?q=is%3Aissue+is%3Aclosed) 问题是值得的，这些问题可能解决了您面临的问题。\n\n## 贡献\n\n如果您发现错误或有功能请求，请在 [GitHub 上提交问题](https:\u002F\u002Fgithub.com\u002FVincentStimper\u002Fnormalizing-flows\u002Fissues)。\n\n欢迎您通过修复错误或添加功能来为包做出贡献。如果您想贡献，请为您添加或修改的代码添加测试，并通过运行 `pytest` 确保其成功通过。\n这可以通过在您的本地仓库版本中简单地执行以下命令来完成：\n```\npytest\n```\n确保您的代码文档完善，我们也鼓励对现有文档做出贡献。完成编码和测试后，请在 [GitHub 上创建拉取请求](https:\u002F\u002Fdocs.github.com\u002Fen\u002Fpull-requests\u002Fcollaborating-with-pull-requests\u002Fproposing-changes-to-your-work-with-pull-requests\u002Fcreating-a-pull-request)。\n\n## 使用情况\n\n该包已被应用于多项研究论文中。其中部分列举如下。\n\n> Andrew Campbell, Wenlong Chen, Vincent Stimper, José Miguel Hernández-Lobato, and Yichuan Zhang. \n> [一种基于梯度的哈密顿蒙特卡洛超参数优化策略](https:\u002F\u002Fproceedings.mlr.press\u002Fv139\u002Fcampbell21a.html). \n> 发表于第 38 届国际机器学习大会论文集，pp. 1238–1248. PMLR, 2021.\n> \n> [代码托管于 GitHub。](https:\u002F\u002Fgithub.com\u002FVincentStimper\u002Fhmc-hyperparameter-tuning)\n\n> Vincent Stimper, Bernhard Schölkopf, and José Miguel Hernández-Lobato. \n> [重采样归一化流的基分布](https:\u002F\u002Fproceedings.mlr.press\u002Fv151\u002Fstimper22a). \n> 发表于第 25 届人工智能与统计国际会议论文集，volume 151, pp. 4915–4936, 2022.\n> \n> [代码托管于 GitHub。](https:\u002F\u002Fgithub.com\u002FVincentStimper\u002Fresampled-base-flows)\n\n> Laurence I. Midgley, Vincent Stimper, Gregor N. C. Simm, Bernhard Schölkopf, and José Miguel Hernández-Lobato. \n> [流退火重要性采样 Bootstrap](https:\u002F\u002Farxiv.org\u002Fabs\u002F2208.01893). \n> 第十一届表示学习国际会议，2023.\n> \n> [代码托管于 GitHub。](https:\u002F\u002Fgithub.com\u002Flollcat\u002Ffab-torch)\n\n> Arnau Quera-Bofarull, Joel Dyer, Anisoara Calinescu, J. Doyne Farmer, and Michael Wooldridge.\n> [BlackBIRDS：可微模拟器的黑盒推理](https:\u002F\u002Fjoss.theoj.org\u002Fpapers\u002F10.21105\u002Fjoss.05776).\n> 开源软件期刊，8(89), 5776, 2023.\n> \n> [代码托管于 GitHub。](https:\u002F\u002Fgithub.com\u002Farnauqb\u002Fblackbirds)\n\n> Utkarsh Singhal, Carlos Esteves, Ameesh Makadia, and Stella X. Yu.\n> [学习变换以实现可泛化的实例级不变性](https:\u002F\u002Fopenaccess.thecvf.com\u002Fcontent\u002FICCV2023\u002Fhtml\u002FSinghal_Learning_to_Transform_for_Generalizable_Instance-wise_Invariance_ICCV_2023_paper.html).\n> IEEE\u002FCVF 国际计算机视觉会议论文集 (ICCV)，pp. 6211-6221, 2023.\n> \n> [代码托管于 GitHub。](https:\u002F\u002Fgithub.com\u002Fsutkarsh\u002Fflow_inv)\n\n> Ba-Hien Tran, Giulio Franzese, Pietro Michiardi, and Maurizio Filippone.\n> [一行代码的数据平滑改进基于似然的生成模型优化](https:\u002F\u002Fproceedings.neurips.cc\u002Fpaper_files\u002Fpaper\u002F2023\u002Fhash\u002F1516a7f7507d5550db5c7f29e995ec8c-Abstract-Conference.html).\n> 神经信息处理系统进展第 36 卷，pp. 6545–6567, 2023.\n> \n> [代码托管于 GitHub。](https:\u002F\u002Fgithub.com\u002Ftranbahien\u002Fdata-mollification)\n\n此外，[`boltzgen`](https:\u002F\u002Fgithub.com\u002FVincentStimper\u002Fboltzmann-generators) 包也是基于 `normflows` 构建的。\n\n## 引用\n\n如果您使用了 `normflows`，请按照以下方式引用[相关论文](https:\u002F\u002Fjoss.theoj.org\u002Fpapers\u002F10.21105\u002Fjoss.05361)。\n\n> Stimper 等人，(2023)。normflows：一个用于归一化流（Normalizing Flows）的 PyTorch 包。开源软件期刊，8(86), 5361, https:\u002F\u002Fdoi.org\u002F10.21105\u002Fjoss.05361\n\n**BibTeX**\n\n```\n@article{Stimper2023, \n  author = {Vincent Stimper and David Liu and Andrew Campbell and Vincent Berenz and Lukas Ryll and Bernhard Schölkopf and José Miguel Hernández-Lobato}, \n  title = {normflows: A PyTorch Package for Normalizing Flows}, \n  journal = {Journal of Open Source Software}, \n  volume = {8},\n  number = {86}, \n  pages = {5361}, \n  publisher = {The Open Journal}, \n  doi = {10.21105\u002Fjoss.05361}, \n  url = {https:\u002F\u002Fdoi.org\u002F10.21105\u002Fjoss.05361}, \n  year = {2023}\n} \n```","# `normflows` 快速上手指南\n\n`normflows` 是一个基于 PyTorch 实现的离散归一化流（Normalizing Flows）包。它支持多种流行的流架构，适用于概率建模、生成模型等任务。\n\n## 环境准备\n\n- **Python 版本**: 至少需要 Python 3.7。\n- **深度学习框架**: 需预先安装 PyTorch。如需使用 GPU 加速，请确保按照 [PyTorch 官网](https:\u002F\u002Fpytorch.org\u002Fget-started\u002Flocally\u002F) 正确配置 CUDA 环境。\n- **依赖库**: 基础功能仅需 `normflows`，运行示例 notebook 需额外安装示例依赖。\n\n## 安装步骤\n\n通过 pip 安装最新版本：\n\n```bash\npip install normflows\n```\n\n若需运行官方提供的示例 Notebook，请先克隆仓库并安装示例依赖：\n\n```bash\ngit clone https:\u002F\u002Fgithub.com\u002FVincentStimper\u002Fnormalizing-flows.git\ncd normalizing-flows\npip install -r requirements_examples.txt\n```\n\n## 基本使用\n\n以下示例展示如何构建一个基于 Real NVP 模型的归一化流，用于拟合 2D 分布。\n\n### 1. 导入库与定义基础分布\n\n```python\nimport normflows as nf\n\n# 定义 2D 高斯基础分布\nbase = nf.distributions.base.DiagGaussian(2)\n```\n\n### 2. 定义流层序列\n\n```python\nnum_layers = 32\nflows = []\nfor i in range(num_layers):\n    # 创建神经网络参数映射（两层隐藏层，每层 64 单元）\n    param_map = nf.nets.MLP([1, 64, 64, 2], init_zeros=True)\n    # 添加仿射耦合层\n    flows.append(nf.flows.AffineCouplingBlock(param_map))\n    # 交换维度\n    flows.append(nf.flows.Permute(2, mode='swap'))\n```\n\n### 3. 构建模型与训练\n\n```python\n# 初始化模型（可选传入目标分布密度）\ntarget = nf.distributions.target.TwoMoons()\nmodel = nf.NormalizingFlow(base, flows, target)\n\n# 计算损失函数\n# 无目标分布时使用前向 KL 散度\n# loss = model.forward_kld(x)\n\n# 有目标分布时反向 KL 散度\nloss = model.reverse_kld(num_samples=512)\n\n# 执行优化\nloss.backward()\noptimizer.step()\n```\n\n更多详细用法及高级示例（如 Glow, VAE, Residual Flow），请参考 [官方文档](https:\u002F\u002Fvincentstimper.github.io\u002Fnormalizing-flows\u002F) 或 [GitHub Examples](https:\u002F\u002Fgithub.com\u002FVincentStimper\u002Fnormalizing-flows\u002Ftree\u002Fmaster\u002Fexamples)。","某金融科技公司数据团队面临欺诈样本稀缺问题，急需生成高质量合成数据来增强异常检测模型的泛化能力。\n\n### 没有 normalizing-flows 时\n- 传统 GAN 模型难以保证生成数据的概率分布稳定性，容易出现模式坍塌导致样本单一。\n- 手动推导复杂分布的似然函数计算量巨大，开发周期长且极易在数学公式上出错。\n- 缺乏现成架构支持，每次尝试新的流形结构都需要从零编写底层反向传播代码。\n- 生成的样本多样性不足，无法有效覆盖真实欺诈行为中那些罕见的边缘情况。\n\n### 使用 normalizing-flows 后\n- normalizing-flows 内置多种成熟架构（如 Glow、Real NVP），直接调用即可实现稳定训练而无需重复造轮子。\n- 自动提供可逆变换的对数似然计算，无需手动推导复杂的雅可比行列式，大幅减少调试时间。\n- 模块化设计允许快速替换耦合层与激活函数，灵活适配不同维度的金融数据特征空间。\n- 能够精确建模高维复杂分布，生成的合成数据在统计特性上与真实欺诈样本高度一致，显著提升模型鲁棒性。\n\n通过降低概率密度建模门槛，normalizing-flows 显著提升了合成数据的质量与算法研发效率。","https:\u002F\u002Foss.gittoolsai.com\u002Fimages\u002FVincentStimper_normalizing-flows_19b49f0e.png","VincentStimper","Vincent Stimper","https:\u002F\u002Foss.gittoolsai.com\u002Favatars\u002FVincentStimper_c2669d18.jpg","Senior Research Scientist @ Isomorphic Labs","Isomorphic Labs","Lausanne, Switzerland",null,"VStimper","https:\u002F\u002Fvincentstimper.com\u002F","https:\u002F\u002Fgithub.com\u002FVincentStimper",[86,90],{"name":87,"color":88,"percentage":89},"Python","#3572A5",96.9,{"name":91,"color":92,"percentage":93},"TeX","#3D6117",3.1,938,133,"2026-03-28T16:56:59","MIT","未说明","非必需，需确保 PyTorch 正确配置 GPU 及 CUDA 环境",{"notes":101,"python":102,"dependencies":103},"1. 核心功能通过 pip install normflows 安装；2. 运行示例需克隆仓库并安装 requirements_examples.txt 依赖；3. 提供 Google Colab 在线运行链接；4. 基于 PyTorch 实现，需遵循 PyTorch 官方硬件要求","3.7+",[104],"torch",[13],[107,108,109,110,111,112,113,114,115,116],"normalizing-flow","variational-autoencoder","variational-inference","real-nvp","residual-flow","glow","pytorch","neural-spline-flow","density-estimation","invertible-neural-networks","2026-03-27T02:49:30.150509","2026-04-06T06:44:05.306183",[120,125,129,134,138,143],{"id":121,"question_zh":122,"answer_zh":123,"source_url":124},2540,"为什么 AutoregressiveRationalQuadraticSpline 的 log_prob 值会很大或者是正数？","log_prob 大于 0 是完全合理的。概率密度函数（PDF）的值本身可以大于 1，因此取对数后结果为正。这并不代表模型有问题或分布未归一化。只要通过其他方式（如积分）确认分布是归一化的即可。","https:\u002F\u002Fgithub.com\u002FVincentStimper\u002Fnormalizing-flows\u002Fissues\u002F24",{"id":126,"question_zh":127,"answer_zh":128,"source_url":124},2541,"如何验证归一化流模型的分布确实是归一化的？","不要仅凭 log_prob 判断。在计算积分的方法中，需要将项乘以 bin 的体积（通常很小），从而使求和结果小于 1。进行重要性采样时计算均值，某些项虽大于 1 但结果仍可为 1。建议实现这些方法来自行验证分布确实被归一化了。",{"id":130,"question_zh":131,"answer_zh":132,"source_url":133},2542,"训练过程中 KL 散度损失出现负值是否正常？","是正常的。优化器实际上是在最小化 $-\\mathbf{E}_p[\\log q(x)]$，这等同于最大化模型对目标样本的似然性。由于常数项不影响梯度计算，尽管损失值为负，优化过程依然正确且稳定。","https:\u002F\u002Fgithub.com\u002FVincentStimper\u002Fnormalizing-flows\u002Fissues\u002F44",{"id":135,"question_zh":136,"answer_zh":137,"source_url":133},2543,"如何在 normflows 中计算实际的向前 KL 散度（Forward KL Divergence）？","库中的损失计算包含一个未知常数偏移，主要用于优化而非直接获取 KL 值。若需计算实际 KL 散度值（例如用于计算总变差距离界限），可能需要自行实现相关逻辑，因为库目前侧重于最大化似然估计的训练方式。",{"id":139,"question_zh":140,"answer_zh":141,"source_url":142},2544,"使用 `torch.save()` 保存模型时报错（涉及 Lambda）如何解决？","旧版本 MADE 类中的 `preprocessing` 属性使用了 lambda，导致无法序列化。该问题已在后续发布的 PyPI 新版本中修复。请确保安装并升级到最新的 package 版本。","https:\u002F\u002Fgithub.com\u002FVincentStimper\u002Fnormalizing-flows\u002Fissues\u002F43",{"id":144,"question_zh":145,"answer_zh":146,"source_url":147},2545,"如何实现条件归一化流（Conditional Normalizing Flow）或在 forward_kld 中传递上下文？","新版本已支持条件归一化流功能。请参考官方仓库中的示例 notebook (`examples\u002Fconditional_flow.ipynb`) 查看具体实现代码和详细说明。","https:\u002F\u002Fgithub.com\u002FVincentStimper\u002Fnormalizing-flows\u002Fissues\u002F9",[149,154,159,164,169,174,179,184,189,194,199,204],{"id":150,"version":151,"summary_zh":152,"released_at":153},102074,"v1.7.3","* [CoupledRationalQuadraticSpline](https:\u002F\u002Fgithub.com\u002FVincentStimper\u002Fnormalizing-flows\u002Fblob\u002F1d9707e2f17449107c477801cbb7ce30d9ddb001\u002Fnormflows\u002Fflows\u002Fneural_spline\u002Fwrapper.py#L14) flow can now be used with conditioning\r\n* Neural spline flow can be used with automatic mixed precision\r\n* Updated [Used by](https:\u002F\u002Fgithub.com\u002FVincentStimper\u002Fnormalizing-flows#used-by) list","2023-11-16T14:28:18",{"id":155,"version":156,"summary_zh":157,"released_at":158},102075,"v1.7.2","* Added [`ConditionalNormalizingFlow`](https:\u002F\u002Fgithub.com\u002FVincentStimper\u002Fnormalizing-flows\u002Fblob\u002Fd1a6af7b15bf0245f1bc4c18e1694541872b29d8\u002Fnormflows\u002Fcore.py#L216) addressing the issues #9 and #41 \r\n* Removed lambda functions used in neural spline flow to allow flow models to be pickled, see issue #43 ","2023-07-23T09:37:57",{"id":160,"version":161,"summary_zh":162,"released_at":163},102076,"v1.7.1","* Added reference to [paper published in JOSS](https:\u002F\u002Fjoss.theoj.org\u002Fpapers\u002F10.21105\u002Fjoss.05361)\r\n* Added [CITATION.cff](https:\u002F\u002Fgithub.com\u002FVincentStimper\u002Fnormalizing-flows\u002Fblob\u002Fmaster\u002FCITATION.cff) file","2023-06-26T13:16:12",{"id":165,"version":166,"summary_zh":167,"released_at":168},102077,"v1.7.0","* Added examples, including multiscale architecture and change of base distribution\r\n* Added examples to the documentation\r\n* Forward and inverse with log det method to multiscale architecture\r\n* Target distribution for augmented normalizing flow ","2023-06-12T12:10:51",{"id":170,"version":171,"summary_zh":172,"released_at":173},102078,"v1.6.2","* Removed debugging print statement\r\n* Fixed bug in `forward_and_log_det` method, that has recently been introduced","2023-04-28T14:55:48",{"id":175,"version":176,"summary_zh":177,"released_at":178},102079,"v1.6.1","* Paper about the package published on arXiv\r\n* Citation note added","2023-02-24T11:07:24",{"id":180,"version":181,"summary_zh":182,"released_at":183},102080,"v1.6","* Added forward and inverse method to flow module\r\n* Added more tests and fix bugs, e.g. relating variational autoencoder\r\n* Added automatic tests and coverage analysis on GitHub","2023-02-18T12:55:12",{"id":185,"version":186,"summary_zh":187,"released_at":188},102081,"v1.5","A rendered documentation is added to the repository, which is available on [https:\u002F\u002Fvincentstimper.github.io\u002Fnormalizing-flows\u002F](https:\u002F\u002Fvincentstimper.github.io\u002Fnormalizing-flows\u002F). \r\n\r\nTest were added for several flow modules, which can be run via `pytest`. With these new tests, several bugs were detected and fixed. The current coverage is about 61%. More tests will be added in the future as well as automated testing and coverage analysis on GitHub.\r\n\r\nMoreover, the code is adapted to the syntax of newer PyTorch Versions.","2022-12-21T10:50:06",{"id":190,"version":191,"summary_zh":192,"released_at":193},102082,"v1.4","The package is now available on [PyPI](https:\u002F\u002Fpypi.org\u002Fproject\u002Fnormflows\u002F), which means that it can just be installed with \r\n```\r\npip install normflows\r\n```\r\nfrom now on. The code was reformatted to conform to the `black` coding style. \r\n\r\nMoreover, the following fixes and additions are included:\r\n* The computation of the alpha-divergence objective was corrected.\r\n* A bug regarding sampling from the mixture of Gaussian base distribution was fixed.\r\n* A flow layer to warp periodic variables was added.\r\n* The dependency from the [Residual Flow repository](https:\u002F\u002Fgithub.com\u002FVincentStimper\u002Fresidual-flows) was removed.","2022-07-26T15:00:17",{"id":195,"version":196,"summary_zh":197,"released_at":198},102083,"v1.2","The code was reorganized to be more hierarchical and readable. Also all required functionality for Neural Spline Flows were added to the repository to remove the dependency on the original Neural Spline Flow repository.\r\n\r\nFurthermore, the following features were introduced:\r\n* Class to reverse a flow layer\r\n* Class to build a chain of flow layers\r\n* Affine Masked Autoregressive Flows (MAF)\r\n* Circular Neural Spline Flows\r\n* Neural Spline Flows with circular and non-circular coordinates","2022-04-05T08:19:18",{"id":200,"version":201,"summary_zh":202,"released_at":203},102084,"v1.1","* Added mixing for neural spline flows\r\n* Improved documentation","2022-02-06T16:17:11",{"id":205,"version":206,"summary_zh":207,"released_at":208},102085,"v1.0","Normalizing flow library comprising the most popular flow architectures, among them Real NVP, Glow, Neural Spline Flow, and Residual Flow.","2021-11-25T15:05:57"]