[{"data":1,"prerenderedAt":-1},["ShallowReactive",2],{"similar-eduardoleao052--js-pytorch":3,"tool-eduardoleao052--js-pytorch":61},[4,18,26,36,44,53],{"id":5,"name":6,"github_repo":7,"description_zh":8,"stars":9,"difficulty_score":10,"last_commit_at":11,"category_tags":12,"status":17},4358,"openclaw","openclaw\u002Fopenclaw","OpenClaw 是一款专为个人打造的本地化 AI 助手，旨在让你在自己的设备上拥有完全可控的智能伙伴。它打破了传统 AI 助手局限于特定网页或应用的束缚，能够直接接入你日常使用的各类通讯渠道，包括微信、WhatsApp、Telegram、Discord、iMessage 等数十种平台。无论你在哪个聊天软件中发送消息，OpenClaw 都能即时响应，甚至支持在 macOS、iOS 和 Android 设备上进行语音交互，并提供实时的画布渲染功能供你操控。\n\n这款工具主要解决了用户对数据隐私、响应速度以及“始终在线”体验的需求。通过将 AI 部署在本地，用户无需依赖云端服务即可享受快速、私密的智能辅助，真正实现了“你的数据，你做主”。其独特的技术亮点在于强大的网关架构，将控制平面与核心助手分离，确保跨平台通信的流畅性与扩展性。\n\nOpenClaw 非常适合希望构建个性化工作流的技术爱好者、开发者，以及注重隐私保护且不愿被单一生态绑定的普通用户。只要具备基础的终端操作能力（支持 macOS、Linux 及 Windows WSL2），即可通过简单的命令行引导完成部署。如果你渴望拥有一个懂你",349277,3,"2026-04-06T06:32:30",[13,14,15,16],"Agent","开发框架","图像","数据工具","ready",{"id":19,"name":20,"github_repo":21,"description_zh":22,"stars":23,"difficulty_score":10,"last_commit_at":24,"category_tags":25,"status":17},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,"2026-04-05T11:01:52",[14,15,13],{"id":27,"name":28,"github_repo":29,"description_zh":30,"stars":31,"difficulty_score":32,"last_commit_at":33,"category_tags":34,"status":17},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 真正成长为懂上",147882,2,"2026-04-09T11:32:47",[14,13,35],"语言模型",{"id":37,"name":38,"github_repo":39,"description_zh":40,"stars":41,"difficulty_score":32,"last_commit_at":42,"category_tags":43,"status":17},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 都能提供强大的支持。其独特的模块化架构允许社区不断扩展新功能，使其成为当前最灵活、生态最丰富的开源扩散模型工具之一，帮助用户将创意高效转化为现实。",108111,"2026-04-08T11:23:26",[14,15,13],{"id":45,"name":46,"github_repo":47,"description_zh":48,"stars":49,"difficulty_score":32,"last_commit_at":50,"category_tags":51,"status":17},4721,"markitdown","microsoft\u002Fmarkitdown","MarkItDown 是一款由微软 AutoGen 团队打造的轻量级 Python 工具，专为将各类文件高效转换为 Markdown 格式而设计。它支持 PDF、Word、Excel、PPT、图片（含 OCR）、音频（含语音转录）、HTML 乃至 YouTube 链接等多种格式的解析，能够精准提取文档中的标题、列表、表格和链接等关键结构信息。\n\n在人工智能应用日益普及的今天，大语言模型（LLM）虽擅长处理文本，却难以直接读取复杂的二进制办公文档。MarkItDown 恰好解决了这一痛点，它将非结构化或半结构化的文件转化为模型“原生理解”且 Token 效率极高的 Markdown 格式，成为连接本地文件与 AI 分析 pipeline 的理想桥梁。此外，它还提供了 MCP（模型上下文协议）服务器，可无缝集成到 Claude Desktop 等 LLM 应用中。\n\n这款工具特别适合开发者、数据科学家及 AI 研究人员使用，尤其是那些需要构建文档检索增强生成（RAG）系统、进行批量文本分析或希望让 AI 助手直接“阅读”本地文件的用户。虽然生成的内容也具备一定可读性，但其核心优势在于为机器",93400,"2026-04-06T19:52:38",[52,14],"插件",{"id":54,"name":55,"github_repo":56,"description_zh":57,"stars":58,"difficulty_score":10,"last_commit_at":59,"category_tags":60,"status":17},4487,"LLMs-from-scratch","rasbt\u002FLLMs-from-scratch","LLMs-from-scratch 是一个基于 PyTorch 的开源教育项目，旨在引导用户从零开始一步步构建一个类似 ChatGPT 的大型语言模型（LLM）。它不仅是同名技术著作的官方代码库，更提供了一套完整的实践方案，涵盖模型开发、预训练及微调的全过程。\n\n该项目主要解决了大模型领域“黑盒化”的学习痛点。许多开发者虽能调用现成模型，却难以深入理解其内部架构与训练机制。通过亲手编写每一行核心代码，用户能够透彻掌握 Transformer 架构、注意力机制等关键原理，从而真正理解大模型是如何“思考”的。此外，项目还包含了加载大型预训练权重进行微调的代码，帮助用户将理论知识延伸至实际应用。\n\nLLMs-from-scratch 特别适合希望深入底层原理的 AI 开发者、研究人员以及计算机专业的学生。对于不满足于仅使用 API，而是渴望探究模型构建细节的技术人员而言，这是极佳的学习资源。其独特的技术亮点在于“循序渐进”的教学设计：将复杂的系统工程拆解为清晰的步骤，配合详细的图表与示例，让构建一个虽小但功能完备的大模型变得触手可及。无论你是想夯实理论基础，还是为未来研发更大规模的模型做准备",90106,"2026-04-06T11:19:32",[35,15,13,14],{"id":62,"github_repo":63,"name":64,"description_en":65,"description_zh":66,"ai_summary_zh":67,"readme_en":68,"readme_zh":69,"quickstart_zh":70,"use_case_zh":71,"hero_image_url":72,"owner_login":73,"owner_name":74,"owner_avatar_url":75,"owner_bio":76,"owner_company":77,"owner_location":78,"owner_email":77,"owner_twitter":77,"owner_website":77,"owner_url":79,"languages":80,"stars":93,"forks":94,"last_commit_at":95,"license":96,"difficulty_score":97,"env_os":98,"env_gpu":99,"env_ram":100,"env_deps":101,"category_tags":106,"github_topics":107,"view_count":32,"oss_zip_url":77,"oss_zip_packed_at":77,"status":17,"created_at":113,"updated_at":114,"faqs":115,"releases":146},5946,"eduardoleao052\u002Fjs-pytorch","js-pytorch","A JavaScript library like PyTorch, with GPU acceleration.","js-pytorch 是一款专为 JavaScript 环境打造的深度学习库，旨在让开发者能在前端或 Node.js 中体验类似 PyTorch 的开发流程。它从零构建，高度还原了 PyTorch 的语法风格，使得熟悉 Python 深度学习框架的用户能够无缝切换至 JavaScript 生态，无需重新学习复杂的 API 设计。\n\n该工具主要解决了传统深度学习模型难以直接在浏览器或纯 JavaScript 项目中运行与训练的痛点。通过集成 GPU.js，js-pytorch 支持 GPU 加速计算，显著提升了张量运算和神经网络训练的效率，让复杂的数学运算不再完全依赖 CPU。\n\njs-pytorch 非常适合 Web 全栈开发者、希望在浏览器端进行模型原型的算法研究人员，以及对前端人工智能感兴趣的学习者。无论是构建交互式 AI 演示，还是在服务端利用 JS 技术栈进行轻量级模型训练，它都能提供强有力的支持。\n\n在技术特性上，js-pytorch 不仅实现了加减乘除、矩阵乘法、转态等基础张量操作，还涵盖了线性层（nn.Linear）等核心深度学习模块，并提供了完整的文档与在线演示供用户快","js-pytorch 是一款专为 JavaScript 环境打造的深度学习库，旨在让开发者能在前端或 Node.js 中体验类似 PyTorch 的开发流程。它从零构建，高度还原了 PyTorch 的语法风格，使得熟悉 Python 深度学习框架的用户能够无缝切换至 JavaScript 生态，无需重新学习复杂的 API 设计。\n\n该工具主要解决了传统深度学习模型难以直接在浏览器或纯 JavaScript 项目中运行与训练的痛点。通过集成 GPU.js，js-pytorch 支持 GPU 加速计算，显著提升了张量运算和神经网络训练的效率，让复杂的数学运算不再完全依赖 CPU。\n\njs-pytorch 非常适合 Web 全栈开发者、希望在浏览器端进行模型原型的算法研究人员，以及对前端人工智能感兴趣的学习者。无论是构建交互式 AI 演示，还是在服务端利用 JS 技术栈进行轻量级模型训练，它都能提供强有力的支持。\n\n在技术特性上，js-pytorch 不仅实现了加减乘除、矩阵乘法、转态等基础张量操作，还涵盖了线性层（nn.Linear）等核心深度学习模块，并提供了完整的文档与在线演示供用户快速上手。作为一个开源项目，它为 JavaScript 社区填补了原生高性能深度学习框架的空白，让 AI 开发变得更加触手可及。","\u003Cp align=\"center\">\n  \u003Cimg src=\"https:\u002F\u002Foss.gittoolsai.com\u002Fimages\u002Feduardoleao052_js-pytorch_readme_e96630c005d6.png\" alt=\"js-torch\" height=\"135\">\n\u003C\u002Fp>\n\n\u003Cp align=\"center\">\n    \u003Ca href=\"https:\u002F\u002Fgithub.com\u002Feduardoleao052\u002Fjs-torch\u002Factions\u002Fworkflows\u002Ftest.yml\u002Fbadge.svg\" alt=\"Unit Tests\">\n        \u003Cimg src=\"https:\u002F\u002Fgithub.com\u002Feduardoleao052\u002Fjs-torch\u002Factions\u002Fworkflows\u002Ftest.yml\u002Fbadge.svg\" \u002F>\u003C\u002Fa>\n    \u003Ca href=\"https:\u002F\u002Fgithub.com\u002Feduardoleao052\u002Fjs-torch\u002Fpulse\" alt=\"Activity\">\n        \u003Cimg src=\"https:\u002F\u002Fimg.shields.io\u002Fgithub\u002Fcommit-activity\u002Fm\u002Feduardoleao052\u002Fjs-torch\" \u002F>\u003C\u002Fa>\n    \u003Ca href=\"https:\u002F\u002Fgithub.com\u002Feduardoleao052\u002Fjs-torch\u002Fgraphs\u002Fcontributors\" alt=\"Contributors\">\n        \u003Cimg src=\"https:\u002F\u002Fimg.shields.io\u002Fgithub\u002Fcontributors\u002Feduardoleao052\u002Fjs-torch\" \u002F>\u003C\u002Fa>\n    \u003Ca href=\"https:\u002F\u002Fgithub.com\u002Feduardoleao052\u002Fjs-torch\">\n        \u003Cimg src=\"https:\u002F\u002Fimg.shields.io\u002Fbadge\u002Flanguage-JavaScript-yellow\">\n    \u003C\u002Fa>\n    \u003Ca href=\"mailto:eduardoleao052@usp.br\">\n        \u003Cimg src=\"https:\u002F\u002Fimg.shields.io\u002Fbadge\u002F-Email-red?style=flat-square&logo=gmail&logoColor=white\">\n    \u003C\u002Fa>\n    \u003Ca href=\"https:\u002F\u002Fwww.linkedin.com\u002Fin\u002Feduardoleao052\u002F\">\n        \u003Cimg src=\"https:\u002F\u002Fimg.shields.io\u002Fbadge\u002F-Linkedin-blue?style=flat-square&logo=linkedin\">\n    \u003C\u002Fa>\n\u003C\u002Fp>\n\n# PyTorch in JavaScript\n\n- JS-PyTorch is a Deep Learning **JavaScript library** built from scratch, to closely follow PyTorch's syntax.\n- This library has **GPU support**, using GPU.js.\n- If you want to run it yourself, check out the \u003Ca href=\"https:\u002F\u002Feduardoleao052.github.io\u002Fjs-pytorch\u002Fsite\u002Findex.html\" target=\"blank\">Documentation\u003C\u002Fa>.\n- Try out the \u003Ca href=\"https:\u002F\u002Feduardoleao052.github.io\u002Fjs-pytorch\u002Fsite\u002Fdemo\u002F\" target=\"blank\">Web Demo\u003C\u002Fa>!\n\n> **Note:** You can install the package locally with: `npm install js-pytorch`\n\n\u003Cbr>\n\n\u003Cdetails>\n\u003Csummary> Implemented Tensor \u003Cb>Operations\u003C\u002Fb>: \u003C\u002Fsummary>\n\n\u003Cbr\u002F>\n\n- [Add](https:\u002F\u002Fgithub.com\u002Feduardoleao052\u002Fjs-torch\u002Fblob\u002F07c1286867b952f32c0e904033214253e8812090\u002Fsrc\u002Ftensor.js#L346-L401)\n- [Subtract](https:\u002F\u002Fgithub.com\u002Feduardoleao052\u002Fjs-torch\u002Fblob\u002F07c1286867b952f32c0e904033214253e8812090\u002Fsrc\u002Ftensor.js#L404-L438)\n- [Multiply](https:\u002F\u002Fgithub.com\u002Feduardoleao052\u002Fjs-torch\u002Fblob\u002F07c1286867b952f32c0e904033214253e8812090\u002Fsrc\u002Ftensor.js#L441-L496)\n- [Divide](https:\u002F\u002Fgithub.com\u002Feduardoleao052\u002Fjs-torch\u002Fblob\u002F07c1286867b952f32c0e904033214253e8812090\u002Fsrc\u002Ftensor.js#L498-L557)\n- [Matrix Multiply](https:\u002F\u002Fgithub.com\u002Feduardoleao052\u002Fjs-torch\u002Fblob\u002F07c1286867b952f32c0e904033214253e8812090\u002Fsrc\u002Ftensor.js#L560-L621)\n- [Power](https:\u002F\u002Fgithub.com\u002Feduardoleao052\u002Fjs-torch\u002Fblob\u002F07c1286867b952f32c0e904033214253e8812090\u002Fsrc\u002Ftensor.js#L625-L663)\n- [Square Root](https:\u002F\u002Fgithub.com\u002Feduardoleao052\u002Fjs-torch\u002Fblob\u002F07c1286867b952f32c0e904033214253e8812090\u002Fsrc\u002Ftensor.js#L666-L704)\n- [Exponentiate](https:\u002F\u002Fgithub.com\u002Feduardoleao052\u002Fjs-torch\u002Fblob\u002F07c1286867b952f32c0e904033214253e8812090\u002Fsrc\u002Ftensor.js#706-L744)\n- [Log](https:\u002F\u002Fgithub.com\u002Feduardoleao052\u002Fjs-torch\u002Fblob\u002F07c1286867b952f32c0e904033214253e8812090\u002Fsrc\u002Ftensor.js#L746-L785)\n- [Sum](https:\u002F\u002Fgithub.com\u002Feduardoleao052\u002Fjs-torch\u002Fblob\u002F07c1286867b952f32c0e904033214253e8812090\u002Fsrc\u002Ftensor.js#L790-L842)\n- [Mean](https:\u002F\u002Fgithub.com\u002Feduardoleao052\u002Fjs-torch\u002Fblob\u002F07c1286867b952f32c0e904033214253e8812090\u002Fsrc\u002Ftensor.js#L844-L894)\n- [Variance](https:\u002F\u002Fgithub.com\u002Feduardoleao052\u002Fjs-torch\u002Fblob\u002F07c1286867b952f32c0e904033214253e8812090\u002Fsrc\u002Ftensor.js#L896-L949)\n- [Transpose](https:\u002F\u002Fgithub.com\u002Feduardoleao052\u002Fjs-torch\u002Fblob\u002F07c1286867b952f32c0e904033214253e8812090\u002Fsrc\u002Ftensor.js#L953-L1008)\n- [At](https:\u002F\u002Fgithub.com\u002Feduardoleao052\u002Fjs-torch\u002Fblob\u002F07c1286867b952f32c0e904033214253e8812090\u002Fsrc\u002Ftensor.js#L1010-L1060)\n- [MaskedFill](https:\u002F\u002Fgithub.com\u002Feduardoleao052\u002Fjs-torch\u002Fblob\u002F07c1286867b952f32c0e904033214253e8812090\u002Fsrc\u002Ftensor.js#L1062-L1095)\n- [Reshape](https:\u002F\u002Fgithub.com\u002Feduardoleao052\u002Fjs-torch\u002Fblob\u002F07c1286867b952f32c0e904033214253e8812090\u002Fsrc\u002Ftensor.js#L1097-L1129)\n\n\u003C\u002Fdetails>\n\n\u003Cdetails>\n\u003Csummary> Implemented Deep Learning \u003Cb>Layers\u003C\u002Fb>: \u003C\u002Fsummary>\n\n\u003Cbr\u002F>\n\n- [nn.Linear](https:\u002F\u002Fgithub.com\u002Feduardoleao052\u002Fjs-torch\u002Fblob\u002Fa158c91db9775a88fae6ed2d0f76d6d8ee6f9d23\u002Fsrc\u002Flayers.js#L60-L88)\n- [nn.MultiHeadSelfAttention](https:\u002F\u002Fgithub.com\u002Feduardoleao052\u002Fjs-torch\u002Fblob\u002Fa158c91db9775a88fae6ed2d0f76d6d8ee6f9d23\u002Fsrc\u002Flayers.js#L90-L163)\n- [nn.FullyConnected](https:\u002F\u002Fgithub.com\u002Feduardoleao052\u002Fjs-torch\u002Fblob\u002Fa158c91db9775a88fae6ed2d0f76d6d8ee6f9d23\u002Fsrc\u002Flayers.js#L165-L194)\n- [nn.Block](https:\u002F\u002Fgithub.com\u002Feduardoleao052\u002Fjs-torch\u002Fblob\u002Fa158c91db9775a88fae6ed2d0f76d6d8ee6f9d23\u002Fsrc\u002Flayers.js#L196-L226)\n- [nn.Embedding](https:\u002F\u002Fgithub.com\u002Feduardoleao052\u002Fjs-torch\u002Fblob\u002Fa158c91db9775a88fae6ed2d0f76d6d8ee6f9d23\u002Fsrc\u002Flayers.js#L231-L260)\n- [nn.PositionalEmbedding](https:\u002F\u002Fgithub.com\u002Feduardoleao052\u002Fjs-torch\u002Fblob\u002Fa158c91db9775a88fae6ed2d0f76d6d8ee6f9d23\u002Fsrc\u002Flayers.js#L262-L291)\n- [nn.ReLU](https:\u002F\u002Fgithub.com\u002Feduardoleao052\u002Fjs-torch\u002Fblob\u002Fa158c91db9775a88fae6ed2d0f76d6d8ee6f9d23\u002Fsrc\u002Flayers.js#L296-L325)\n- [nn.Softmax](https:\u002F\u002Fgithub.com\u002Feduardoleao052\u002Fjs-torch\u002Fblob\u002Fa158c91db9775a88fae6ed2d0f76d6d8ee6f9d23\u002Fsrc\u002Flayers.js#L327-L346)\n- [nn.Dropout](https:\u002F\u002Fgithub.com\u002Feduardoleao052\u002Fjs-torch\u002Fblob\u002Fa158c91db9775a88fae6ed2d0f76d6d8ee6f9d23\u002Fsrc\u002Flayers.js#L351-L376)\n- [nn.LayerNorm](https:\u002F\u002Fgithub.com\u002Feduardoleao052\u002Fjs-torch\u002Fblob\u002Fa158c91db9775a88fae6ed2d0f76d6d8ee6f9d23\u002Fsrc\u002Flayers.js#L378-L397)\n- [nn.CrossEntropyLoss](https:\u002F\u002Fgithub.com\u002Feduardoleao052\u002Fjs-torch\u002Fblob\u002Fa158c91db9775a88fae6ed2d0f76d6d8ee6f9d23\u002Fsrc\u002Flayers.js#L400-L441)\n\n\u003C\u002Fdetails>\n\u003Cbr\u002F>\n\n## 1.Table of Contents\n\n* [Installation](#2-installation)\n* [Running it Yourself](#3-Running-it-Yourself)\n  * [Simple Autograd Example](#simple-autograd-example)\n  * [Complex Autograd Example (Transformer)](#complex-autograd-example-transformer)\n  * [Saving and Loading models](#saving-and-loading-models)\n* [Distribution & Devtools](#4-distribution--devtools)\n* [Future Work](#5-future-work)\n\n## 2. Installation\n\n- On **MacOS**, **Windows**, and **Ubuntu**, you can install the library with `npm install js-pytorch`.\n- On **Windows**, if you run into an error, you might need to install the latest version of [Visual Studio](https:\u002F\u002Fvisualstudio.microsoft.com\u002Fdownloads\u002F?cid=learn-navbar-download-cta), including the \"Desktop development with C++\" workload.\n- To run in the **Browser**, paste the following tag in the `\u003Chead>` of your HTML file:\n  \n```html\n\u003Cscript src=\"https:\u002F\u002Fcdnjs.cloudflare.com\u002Fajax\u002Flibs\u002Fjs-pytorch\u002F0.7.2\u002Fjs-pytorch-browser.js\"\n        integrity=\"sha512-l22t7GnqXvHBMCBvPUBdFO2TEYxnb1ziCGcDQcpTB2un16IPA4FE5SIZ8bUR+RwoDZGikQkWisO+fhnakXt9rg==\"\n        crossorigin=\"anonymous\"\n        referrerpolicy=\"no-referrer\">\u003C\u002Fscript>\n```\n\n- After that, you can use JS-PyTorch freely in any `\u003Cscript>` in your HTML file:\n\n```html\n\u003Chead>\n    \u003Ctitle>My Project\u003C\u002Ftitle>\n    \u003C!-- New script goes here -->\n    \u003Cscript src=\"https:\u002F\u002Fcdnjs.cloudflare.com\u002Fajax\u002Flibs\u002Fjs-pytorch\u002F0.7.2\u002Fjs-pytorch-browser.js\" \n            integrity=\"sha512-l22t7GnqXvHBMCBvPUBdFO2TEYxnb1ziCGcDQcpTB2un16IPA4FE5SIZ8bUR+RwoDZGikQkWisO+fhnakXt9rg==\"\n            crossorigin=\"anonymous\" \n            referrerpolicy=\"no-referrer\">\n    \u003C\u002Fscript>\n    \u003C!---->\n\u003C\u002Fhead>\n\u003Cbody>\n    \u003Cscript>\n        let x = torch.randn([10,5])\n        let linear = new torch.nn.Linear(5,1,'gpu',true)\n        let z = linear.forward(x)\n        console.log(z.data)\n    \u003C\u002Fscript>\n\u003C\u002Fbody>\n```\n\n## 3. Running it Yourself\n\n### Simple Autograd Example:\n\n```typescript\n\u002F\u002F Require the Library if running in node (not necessary in the browser):\nconst { torch } = require(\"js-pytorch\");\n\n\u002F\u002F Pass device as an argument to a Tensor or nn.Module (same as PyTorch):\nconst device = 'gpu';\n\n\u002F\u002F Instantiate Tensors:\nlet x = torch.randn([8, 4, 5]);\nlet w = torch.randn([8, 5, 4], true, device);\nlet b = torch.tensor([0.2, 0.5, 0.1, 0.0], true);\n\n\u002F\u002F Make calculations:\nlet out = torch.matmul(x, w);\nout = torch.add(out, b);\n\n\u002F\u002F Compute gradients on whole graph:\nout.backward();\n\n\u002F\u002F Get gradients from specific Tensors:\nconsole.log(w.grad);\nconsole.log(b.grad);\n```\n\n### Complex Autograd Example (Transformer):\n\n```typescript\n\u002F\u002F Require the Library if running in node (not necessary in the browser):\nconst { torch } = require(\"js-pytorch\");\nconst nn = torch.nn;\nconst optim = torch.optim;\n\nconst device = 'gpu';\n\n\u002F\u002F Define training hyperparameters:\nconst vocab_size = 52;\nconst hidden_size = 32;\nconst n_timesteps = 16;\nconst n_heads = 4;\nconst dropout_p = 0;\nconst batch_size = 8;\n\n\u002F\u002F Create Transformer decoder Module:\nclass Transformer extends nn.Module {\n  constructor(vocab_size, hidden_size, n_timesteps, n_heads, dropout_p, device) {\n    super();\n    \u002F\u002F Instantiate Transformer's Layers:\n    this.embed = new nn.Embedding(vocab_size, hidden_size);\n    this.pos_embed = new nn.PositionalEmbedding(n_timesteps, hidden_size);\n    this.b1 = new nn.Block(hidden_size, hidden_size, n_heads, n_timesteps, dropout_p, device);\n    this.b2 = new nn.Block(hidden_size, hidden_size, n_heads, n_timesteps, dropout_p, device);\n    this.ln = new nn.LayerNorm(hidden_size);\n    this.linear = new nn.Linear(hidden_size, vocab_size, device);\n  }\n\n  forward(x) {\n    let z;\n    z = torch.add(this.embed.forward(x), this.pos_embed.forward(x));\n    z = this.b1.forward(z);\n    z = this.b2.forward(z);\n    z = this.ln.forward(z);\n    z = this.linear.forward(z);\n    return z;\n  }\n}\n\n\u002F\u002F Instantiate your custom nn.Module:\nconst model = new Transformer(vocab_size, hidden_size, n_timesteps, n_heads, dropout_p, device);\n\n\u002F\u002F Define loss function and optimizer:\nconst loss_func = new nn.CrossEntropyLoss();\nconst optimizer = new optim.Adam(model.parameters(), (lr = 5e-3), (reg = 0));\n\n\u002F\u002F Instantiate sample input and output:\nlet x = torch.randint(0, vocab_size, [batch_size, n_timesteps, 1]);\nlet y = torch.randint(0, vocab_size, [batch_size, n_timesteps]);\nlet loss;\n\n\u002F\u002F Training Loop:\nfor (let i = 0; i \u003C 40; i++) {\n  \u002F\u002F Forward pass through the Transformer:\n  let z = model.forward(x);\n\n  \u002F\u002F Get loss:\n  loss = loss_func.forward(z, y);\n\n  \u002F\u002F Backpropagate the loss using torch.tensor's backward() method:\n  loss.backward();\n\n  \u002F\u002F Update the weights:\n  optimizer.step();\n\n  \u002F\u002F Reset the gradients to zero after each training step:\n  optimizer.zero_grad();\n\n  \u002F\u002F Print loss at every iteration:\n  console.log(`Iter ${i} - Loss ${loss.data[0].toFixed(4)}`)\n}\n```\n\n### Saving and Loading models:\n\n```typescript\n\u002F\u002F Instantiate your model:\nconst model = new Transformer(vocab_size, hidden_size, n_timesteps, n_heads, dropout_p);\n\n\u002F\u002F Train the model:\ntrainModel(model);\n\n\u002F\u002F Save model to JSON file:\ntorch.save(model, 'model.json')\n\n\u002F\u002F To load, instantiate placeHolder using the original model's architecture:\nconst placeHolder = new Transformer(vocab_size, hidden_size, n_timesteps, n_heads, dropout_p);\n\n\u002F\u002F Load weights into placeHolder:\nconst newModel = torch.load(placeHolder, 'model.json')\n```\n\n\n\u003Cbr\u002F>\n\n## 4. Distribution & Devtools\n\n- **Build for Distribution** by running `npm run build`. CJS and ESM modules and `index.d.ts` will be output in the `dist\u002F` folder.\n- **Check the Code** with ESLint at any time, running `npm run lint`.\n- **Run tests** run `npm test`.\n- **Improve Code Formatting** with prettier, running `npm run prettier`.\n- **Performance Benchmarks** are also included in the `tests\u002Fbenchmarks\u002F` directory. Run all benchmarks with `npm run bench` and save new benchmarks with `npm run bench:update`.\n\n\n## 5. Future Work\n\n- This package is not as optimized as PyTorch yet, but I tried making it more interpretable. Efficiency improvements are incoming!\n- Feel free to **contribute**! Create a merge request to the `develop` branch, and also feel free to reach out. I'll try to answer as soon as possible.\n- Hope you enjoy!\n\n\n","\u003Cp align=\"center\">\n  \u003Cimg src=\"https:\u002F\u002Foss.gittoolsai.com\u002Fimages\u002Feduardoleao052_js-pytorch_readme_e96630c005d6.png\" alt=\"js-torch\" height=\"135\">\n\u003C\u002Fp>\n\n\u003Cp align=\"center\">\n    \u003Ca href=\"https:\u002F\u002Fgithub.com\u002Feduardoleao052\u002Fjs-torch\u002Factions\u002Fworkflows\u002Ftest.yml\u002Fbadge.svg\" alt=\"单元测试\">\n        \u003Cimg src=\"https:\u002F\u002Fgithub.com\u002Feduardoleao052\u002Fjs-torch\u002Factions\u002Fworkflows\u002Ftest.yml\u002Fbadge.svg\" \u002F>\u003C\u002Fa>\n    \u003Ca href=\"https:\u002F\u002Fgithub.com\u002Feduardoleao052\u002Fjs-torch\u002Fpulse\" alt=\"活跃度\">\n        \u003Cimg src=\"https:\u002F\u002Fimg.shields.io\u002Fgithub\u002Fcommit-activity\u002Fm\u002Feduardoleao052\u002Fjs-torch\" \u002F>\u003C\u002Fa>\n    \u003Ca href=\"https:\u002F\u002Fgithub.com\u002Feduardoleao052\u002Fjs-torch\u002Fgraphs\u002Fcontributors\" alt=\"贡献者\">\n        \u003Cimg src=\"https:\u002F\u002Fimg.shields.io\u002Fgithub\u002Fcontributors\u002Feduardoleao052\u002Fjs-torch\" \u002F>\u003C\u002Fa>\n    \u003Ca href=\"https:\u002F\u002Fgithub.com\u002Feduardoleao052\u002Fjs-torch\">\n        \u003Cimg src=\"https:\u002F\u002Fimg.shields.io\u002Fbadge\u002Flanguage-JavaScript-yellow\">\n    \u003C\u002Fa>\n    \u003Ca href=\"mailto:eduardoleao052@usp.br\">\n        \u003Cimg src=\"https:\u002F\u002Fimg.shields.io\u002Fbadge\u002F-Email-red?style=flat-square&logo=gmail&logoColor=white\">\n    \u003C\u002Fa>\n    \u003Ca href=\"https:\u002F\u002Fwww.linkedin.com\u002Fin\u002Feduardoleao052\u002F\">\n        \u003Cimg src=\"https:\u002F\u002Fimg.shields.io\u002Fbadge\u002F-Linkedin-blue?style=flat-square&logo=linkedin\">\n    \u003C\u002Fa>\n\u003C\u002Fp>\n\n# PyTorch 在 JavaScript 中\n\n- JS-PyTorch 是一个从零开始构建的深度学习 **JavaScript 库**，其语法与 PyTorch 非常接近。\n- 该库支持 **GPU 加速**，使用 GPU.js 实现。\n- 如果您想亲自运行它，请查看 \u003Ca href=\"https:\u002F\u002Feduardoleao052.github.io\u002Fjs-pytorch\u002Fsite\u002Findex.html\" target=\"blank\">文档\u003C\u002Fa>。\n- 试试 \u003Ca href=\"https:\u002F\u002Feduardoleao052.github.io\u002Fjs-pytorch\u002Fsite\u002Fdemo\u002F\" target=\"blank\">在线演示\u003C\u002Fa>吧！\n\n> **注意:** 您可以通过 `npm install js-pytorch` 在本地安装该包。\n\n\u003Cbr>\n\n\u003Cdetails>\n\u003Csummary> 已实现的张量 \u003Cb>操作\u003C\u002Fb>： \u003C\u002Fsummary>\n\n\u003Cbr\u002F>\n\n- [加法](https:\u002F\u002Fgithub.com\u002Feduardoleao052\u002Fjs-torch\u002Fblob\u002F07c1286867b952f32c0e904033214253e8812090\u002Fsrc\u002Ftensor.js#L346-L401)\n- [减法](https:\u002F\u002Fgithub.com\u002Feduardoleao052\u002Fjs-torch\u002Fblob\u002F07c1286867b952f32c0e904033214253e8812090\u002Fsrc\u002Ftensor.js#L404-L438)\n- [乘法](https:\u002F\u002Fgithub.com\u002Feduardoleao052\u002Fjs-torch\u002Fblob\u002F07c1286867b952f32c0e904033214253e8812090\u002Fsrc\u002Ftensor.js#L441-L496)\n- [除法](https:\u002F\u002Fgithub.com\u002Feduardoleao052\u002Fjs-torch\u002Fblob\u002F07c1286867b952f32c0e904033214253e8812090\u002Fsrc\u002Ftensor.js#L498-L557)\n- [矩阵乘法](https:\u002F\u002Fgithub.com\u002Feduardoleao052\u002Fjs-torch\u002Fblob\u002F07c1286867b952f32c0e904033214253e8812090\u002Fsrc\u002Ftensor.js#L560-L621)\n- [幂运算](https:\u002F\u002Fgithub.com\u002Feduardoleao052\u002Fjs-torch\u002Fblob\u002F07c1286867b952f32c0e904033214253e8812090\u002Fsrc\u002Ftensor.js#L625-L663)\n- [平方根](https:\u002F\u002Fgithub.com\u002Feduardoleao052\u002Fjs-torch\u002Fblob\u002F07c1286867b952f32c0e904033214253e8812090\u002Fsrc\u002Ftensor.js#L666-L704)\n- [指数运算](https:\u002F\u002Fgithub.com\u002Feduardoleao052\u002Fjs-torch\u002Fblob\u002F07c1286867b952f32c0e904033214253e8812090\u002Fsrc\u002Ftensor.js#706-L744)\n- [对数](https:\u002F\u002Fgithub.com\u002Feduardoleao052\u002Fjs-torch\u002Fblob\u002F07c1286867b952f32c0e904033214253e8812090\u002Fsrc\u002Ftensor.js#L746-L785)\n- [求和](https:\u002F\u002Fgithub.com\u002Feduardoleao052\u002Fjs-torch\u002Fblob\u002F07c1286867b952f32c0e904033214253e8812090\u002Fsrc\u002Ftensor.js#L790-L842)\n- [平均值](https:\u002F\u002Fgithub.com\u002Feduardoleao052\u002Fjs-torch\u002Fblob\u002F07c1286867b952f32c0e904033214253e8812090\u002Fsrc\u002Ftensor.js#L844-L894)\n- [方差](https:\u002F\u002Fgithub.com\u002Feduardoleao052\u002Fjs-torch\u002Fblob\u002F07c1286867b952f32c0e904033214253e8812090\u002Fsrc\u002Ftensor.js#L896-L949)\n- [转置](https:\u002F\u002Fgithub.com\u002Feduardoleao052\u002Fjs-torch\u002Fblob\u002F07c1286867b952f32c0e904033214253e8812090\u002Fsrc\u002Ftensor.js#L953-L1008)\n- [取值](https:\u002F\u002Fgithub.com\u002Feduardoleao052\u002Fjs-torch\u002Fblob\u002F07c1286867b952f32c0e904033214253e8812090\u002Fsrc\u002Ftensor.js#L1010-L1060)\n- [掩码填充](https:\u002F\u002Fgithub.com\u002Feduardoleao052\u002Fjs-torch\u002Fblob\u002F07c1286867b952f32c0e904033214253e8812090\u002Fsrc\u002Ftensor.js#L1062-L1095)\n- [重塑](https:\u002F\u002Fgithub.com\u002Feduardoleao052\u002Fjs-torch\u002Fblob\u002F07c1286867b952f32c0e904033214253e8812090\u002Fsrc\u002Ftensor.js#L1097-L1129)\n\n\u003C\u002Fdetails>\n\n\u003Cdetails>\n\u003Csummary> 已实现的深度学习 \u003Cb>层\u003C\u002Fb>： \u003C\u002Fsummary>\n\n\u003Cbr\u002F>\n\n- [nn.Linear](https:\u002F\u002Fgithub.com\u002Feduardoleao052\u002Fjs-torch\u002Fblob\u002Fa158c91db9775a88fae6ed2d0f76d6d8ee6f9d23\u002Fsrc\u002Flayers.js#L60-L88)\n- [nn.MultiHeadSelfAttention](https:\u002F\u002Fgithub.com\u002Feduardoleao052\u002Fjs-torch\u002Fblob\u002Fa158c91db9775a88fae6ed2d0f76d6d8ee6f9d23\u002Fsrc\u002Flayers.js#L90-L163)\n- [nn.FullyConnected](https:\u002F\u002Fgithub.com\u002Feduardoleao052\u002Fjs-torch\u002Fblob\u002Fa158c91db9775a88fae6ed2d0f76d6d8ee6f9d23\u002Fsrc\u002Flayers.js#L165-L194)\n- [nn.Block](https:\u002F\u002Fgithub.com\u002Feduardoleao052\u002Fjs-torch\u002Fblob\u002Fa158c91db9775a88fae6ed2d0f76d6d8ee6f9d23\u002Fsrc\u002Flayers.js#L196-L226)\n- [nn.Embedding](https:\u002F\u002Fgithub.com\u002Feduardoleao052\u002Fjs-torch\u002Fblob\u002Fa158c91db9775a88fae6ed2d0f76d6d8ee6f9d23\u002Fsrc\u002Flayers.js#L231-L260)\n- [nn.PositionalEmbedding](https:\u002F\u002Fgithub.com\u002Feduardoleao052\u002Fjs-torch\u002Fblob\u002Fa158c91db9775a88fae6ed2d0f76d6d8ee6f9d23\u002Fsrc\u002Flayers.js#L262-L291)\n- [nn.ReLU](https:\u002F\u002Fgithub.com\u002Feduardoleao052\u002Fjs-torch\u002Fblob\u002Fa158c91db9775a88fae6ed2d0f76d6d8ee6f9d23\u002Fsrc\u002Flayers.js#L296-L325)\n- [nn.Softmax](https:\u002F\u002Fgithub.com\u002Feduardoleao052\u002Fjs-torch\u002Fblob\u002Fa158c91db9775a88fae6ed2d0f76d6d8ee6f9d23\u002Fsrc\u002Flayers.js#L327-L346)\n- [nn.Dropout](https:\u002F\u002Fgithub.com\u002Feduardoleao052\u002Fjs-torch\u002Fblob\u002Fa158c91db9775a88fae6ed2d0f76d6d8ee6f9d23\u002Fsrc\u002Flayers.js#L351-L376)\n- [nn.LayerNorm](https:\u002F\u002Fgithub.com\u002Feduardoleao052\u002Fjs-torch\u002Fblob\u002Fa158c91db9775a88fae6ed2d0f76d6d8ee6f9d23\u002Fsrc\u002Flayers.js#L378-L397)\n- [nn.CrossEntropyLoss](https:\u002F\u002Fgithub.com\u002Feduardoleao052\u002Fjs-torch\u002Fblob\u002Fa158c91db9775a88fae6ed2d0f76d6d8ee6f9d23\u002Fsrc\u002Flayers.js#L400-L441)\n\n\u003C\u002Fdetails>\n\u003Cbr\u002F>\n\n## 1.目录\n\n* [安装](#2-installation)\n* [自行运行](#3-Running-it-Yourself)\n  * [简单自动微分示例](#simple-autograd-example)\n  * [复杂自动微分示例（Transformer）](#complex-autograd-example-transformer)\n  * [保存和加载模型](#saving-and-loading-models)\n* [发布与开发工具](#4-distribution--devtools)\n* [未来工作](#5-future-work)\n\n## 2. 安装\n\n- 在 **MacOS**、**Windows** 和 **Ubuntu** 上，你可以使用 `npm install js-pytorch` 来安装该库。\n- 在 **Windows** 上，如果遇到错误，你可能需要安装最新版本的 [Visual Studio](https:\u002F\u002Fvisualstudio.microsoft.com\u002Fdownloads\u002F?cid=learn-navbar-download-cta)，并确保包含“使用 C++ 的桌面开发”工作负载。\n- 若要在 **浏览器** 中运行，请将以下标签粘贴到你的 HTML 文件的 `\u003Chead>` 部分：\n\n```html\n\u003Cscript src=\"https:\u002F\u002Fcdnjs.cloudflare.com\u002Fajax\u002Flibs\u002Fjs-pytorch\u002F0.7.2\u002Fjs-pytorch-browser.js\"\n        integrity=\"sha512-l22t7GnqXvHBMCBvPUBdFO2TEYxnb1ziCGcDQcpTB2un16IPA4FE5SIZ8bUR+RwoDZGikQkWisO+fhnakXt9rg==\"\n        crossorigin=\"anonymous\"\n        referrerpolicy=\"no-referrer\">\u003C\u002Fscript>\n```\n\n- 安装完成后，你可以在 HTML 文件中的任何 `\u003Cscript>` 标签内自由使用 JS-PyTorch：\n\n```html\n\u003Chead>\n    \u003Ctitle>我的项目\u003C\u002Ftitle>\n    \u003C!-- 新的脚本放在这里 -->\n    \u003Cscript src=\"https:\u002F\u002Fcdnjs.cloudflare.com\u002Fajax\u002Flibs\u002Fjs-pytorch\u002F0.7.2\u002Fjs-pytorch-browser.js\" \n            integrity=\"sha512-l22t7GnqXvHBMCBvPUBdFO2TEYxnb1ziCGcDQcpTB2un16IPA4FE5SIZ8bUR+RwoDZGikQkWisO+fhnakXt9rg==\"\n            crossorigin=\"anonymous\" \n            referrerpolicy=\"no-referrer\">\n    \u003C\u002Fscript>\n    \u003C!---->\n\u003C\u002Fhead>\n\u003Cbody>\n    \u003Cscript>\n        let x = torch.randn([10,5])\n        let linear = new torch.nn.Linear(5,1,'gpu',true)\n        let z = linear.forward(x)\n        console.log(z.data)\n    \u003C\u002Fscript>\n\u003C\u002Fbody>\n```\n\n## 3. 自行运行\n\n### 简单的 autograd 示例：\n\n```typescript\n\u002F\u002F 如果在 Node.js 中运行，需引入库（浏览器中无需）：\nconst { torch } = require(\"js-pytorch\");\n\n\u002F\u002F 将设备作为参数传递给 Tensor 或 nn.Module（与 PyTorch 相同）：\nconst device = 'gpu';\n\n\u002F\u002F 实例化张量：\nlet x = torch.randn([8, 4, 5]);\nlet w = torch.randn([8, 5, 4], true, device);\nlet b = torch.tensor([0.2, 0.5, 0.1, 0.0], true);\n\n\u002F\u002F 进行计算：\nlet out = torch.matmul(x, w);\nout = torch.add(out, b);\n\n\u002F\u002F 计算整个图的梯度：\nout.backward();\n\n\u002F\u002F 获取特定张量的梯度：\nconsole.log(w.grad);\nconsole.log(b.grad);\n```\n\n### 复杂的 autograd 示例（Transformer）：\n\n```typescript\n\u002F\u002F 如果在 Node.js 中运行，需引入库（浏览器中无需）：\nconst { torch } = require(\"js-pytorch\");\nconst nn = torch.nn;\nconst optim = torch.optim;\n\nconst device = 'gpu';\n\n\u002F\u002F 定义训练超参数：\nconst vocab_size = 52;\nconst hidden_size = 32;\nconst n_timesteps = 16;\nconst n_heads = 4;\nconst dropout_p = 0;\nconst batch_size = 8;\n\n\u002F\u002F 创建 Transformer 解码器模块：\nclass Transformer extends nn.Module {\n  constructor(vocab_size, hidden_size, n_timesteps, n_heads, dropout_p, device) {\n    super();\n    \u002F\u002F 实例化 Transformer 的各层：\n    this.embed = new nn.Embedding(vocab_size, hidden_size);\n    this.pos_embed = new nn.PositionalEmbedding(n_timesteps, hidden_size);\n    this.b1 = new nn.Block(hidden_size, hidden_size, n_heads, n_timesteps, dropout_p, device);\n    this.b2 = new nn.Block(hidden_size, hidden_size, n_heads, n_timesteps, dropout_p, device);\n    this.ln = new nn.LayerNorm(hidden_size);\n    this.linear = new nn.Linear(hidden_size, vocab_size, device);\n  }\n\n  forward(x) {\n    let z;\n    z = torch.add(this.embed.forward(x), this.pos_embed.forward(x));\n    z = this.b1.forward(z);\n    z = this.b2.forward(z);\n    z = this.ln.forward(z);\n    z = this.linear.forward(z);\n    return z;\n  }\n}\n\n\u002F\u002F 实例化自定义的 nn.Module：\nconst model = new Transformer(vocab_size, hidden_size, n_timesteps, n_heads, dropout_p, device);\n\n\u002F\u002F 定义损失函数和优化器：\nconst loss_func = new nn.CrossEntropyLoss();\nconst optimizer = new optim.Adam(model.parameters(), (lr = 5e-3), (reg = 0));\n\n\u002F\u002F 实例化样本输入和输出：\nlet x = torch.randint(0, vocab_size, [batch_size, n_timesteps, 1]);\nlet y = torch.randint(0, vocab_size, [batch_size, n_timesteps]);\nlet loss;\n\n\u002F\u002F 训练循环：\nfor (let i = 0; i \u003C 40; i++) {\n  \u002F\u002F 前向传播通过 Transformer：\n  let z = model.forward(x);\n\n  \u002F\u002F 计算损失：\n  loss = loss_func.forward(z, y);\n\n  \u002F\u002F 使用 torch.tensor 的 backward() 方法反向传播损失：\n  loss.backward();\n\n  \u002F\u002F 更新权重：\n  optimizer.step();\n\n  \u002F\u002F 每次训练步骤后将梯度重置为零：\n  optimizer.zero_grad();\n\n  \u002F\u002F 每次迭代打印损失：\n  console.log(`第 ${i} 次迭代 - 损失 ${loss.data[0].toFixed(4)}`)\n}\n```\n\n### 保存和加载模型：\n\n```typescript\n\u002F\u002F 实例化你的模型：\nconst model = new Transformer(vocab_size, hidden_size, n_timesteps, n_heads, dropout_p);\n\n\u002F\u002F 训练模型：\ntrainModel(model);\n\n\u002F\u002F 将模型保存为 JSON 文件：\ntorch.save(model, 'model.json')\n\n\u002F\u002F 加载时，先用原始模型的架构实例化占位符：\nconst placeHolder = new Transformer(vocab_size, hidden_size, n_timesteps, n_heads, dropout_p);\n\n\u002F\u002F 将权重加载到占位符中：\nconst newModel = torch.load(placeHolder, 'model.json')\n```\n\n\n\u003Cbr\u002F>\n\n## 4. 发布与开发工具\n\n- 通过运行 `npm run build` 可以构建用于发布的代码。CJS 和 ESM 模块以及 `index.d.ts` 文件将会输出到 `dist\u002F` 文件夹中。\n- 随时可以使用 ESLint 检查代码，运行 `npm run lint`。\n- 运行测试：`npm test`。\n- 使用 prettier 改善代码格式，运行 `npm run prettier`。\n- 性能基准测试也包含在 `tests\u002Fbenchmarks\u002F` 目录中。使用 `npm run bench` 运行所有基准测试，并使用 `npm run bench:update` 保存新的基准测试。\n\n\n## 5. 未来工作\n\n- 该包目前尚未像 PyTorch 那样经过高度优化，但我已尽力使其更易于理解。效率方面的改进即将到来！\n- 欢迎大家 **贡献代码**！请创建一个指向 `develop` 分支的合并请求，或者直接与我联系。我会尽快回复。\n- 希望你喜欢！","# js-pytorch 快速上手指南\n\njs-pytorch 是一个从零构建的 JavaScript 深度学习库，旨在紧密遵循 PyTorch 的语法风格，并支持通过 GPU.js 进行 GPU 加速。\n\n## 1. 环境准备\n\n*   **操作系统**：支持 MacOS、Windows 和 Ubuntu。\n*   **运行环境**：\n    *   **Node.js**：需安装 Node.js 以在服务器端或本地运行。\n    *   **浏览器**：可直接在现代浏览器中运行，无需构建工具。\n*   **前置依赖（Windows 用户特别注意）**：\n    *   如果在 Windows 上安装时遇到错误，请确保安装了最新版本的 [Visual Studio](https:\u002F\u002Fvisualstudio.microsoft.com\u002Fdownloads\u002F)，并在安装选项中勾选 **\"Desktop development with C++\"** 工作负载。\n\n## 2. 安装步骤\n\n### 方式一：Node.js 环境 (npm)\n\n在终端中运行以下命令安装：\n\n```bash\nnpm install js-pytorch\n```\n\n### 方式二：浏览器环境 (CDN)\n\n在 HTML 文件的 `\u003Chead>` 标签中引入以下脚本：\n\n```html\n\u003Cscript src=\"https:\u002F\u002Fcdnjs.cloudflare.com\u002Fajax\u002Flibs\u002Fjs-pytorch\u002F0.7.2\u002Fjs-pytorch-browser.js\"\n        integrity=\"sha512-l22t7GnqXvHBMCBvPUBdFO2TEYxnb1ziCGcDQcpTB2un16IPA4FE5SIZ8bUR+RwoDZGikQkWisO+fhnakXt9rg==\"\n        crossorigin=\"anonymous\"\n        referrerpolicy=\"no-referrer\">\u003C\u002Fscript>\n```\n\n## 3. 基本使用\n\n### Node.js 示例\n\n创建一个 `.js` 文件，编写以下代码即可体验自动微分（Autograd）功能：\n\n```typescript\n\u002F\u002F 引入库\nconst { torch } = require(\"js-pytorch\");\n\n\u002F\u002F 设置设备 ('gpu' 或 'cpu')\nconst device = 'gpu';\n\n\u002F\u002F 实例化张量 (Tensors)\nlet x = torch.randn([8, 4, 5]);\nlet w = torch.randn([8, 5, 4], true, device); \u002F\u002F true 表示需要计算梯度\nlet b = torch.tensor([0.2, 0.5, 0.1, 0.0], true);\n\n\u002F\u002F 执行计算\nlet out = torch.matmul(x, w);\nout = torch.add(out, b);\n\n\u002F\u002F 反向传播计算梯度\nout.backward();\n\n\u002F\u002F 查看特定张量的梯度\nconsole.log(w.grad);\nconsole.log(b.grad);\n```\n\n### 浏览器示例\n\n在引入 CDN 脚本后，直接在 `\u003Cscript>` 标签中使用：\n\n```html\n\u003Cbody>\n    \u003Cscript>\n        \u002F\u002F 创建随机张量\n        let x = torch.randn([10,5])\n        \n        \u002F\u002F 创建线性层 (输入 5, 输出 1, 使用 gpu, 追踪梯度)\n        let linear = new torch.nn.Linear(5,1,'gpu',true)\n        \n        \u002F\u002F 前向传播\n        let z = linear.forward(x)\n        \n        \u002F\u002F 输出结果\n        console.log(z.data)\n    \u003C\u002Fscript>\n\u003C\u002Fbody>\n```\n\n### 进阶提示\n*   **模型定义**：继承 `nn.Module` 类来构建自定义网络（如 Transformer）。\n*   **训练循环**：使用 `loss.backward()` 进行反向传播，配合 `optim.Adam` 等优化器更新权重。\n*   **模型保存**：使用 `torch.save(model, 'model.json')` 和 `torch.load()` 进行模型的持久化。","某前端团队希望在浏览器端直接运行轻量级图像分类模型，让用户上传照片后无需等待服务器响应即可实时获得分析结果。\n\n### 没有 js-pytorch 时\n- **架构复杂成本高**：必须搭建独立的 Python 后端服务来承载 PyTorch 模型，前端需通过 HTTP 请求往返传输图片数据，增加了服务器运维成本和网络延迟。\n- **技术栈割裂**：前端开发人员不熟悉 Python 生态，无法直接参与模型推理逻辑的优化或调试，必须依赖后端同事协作，沟通效率低下。\n- **隐私与离线受限**：用户图片必须上传至云端处理，无法满足对数据隐私敏感的场景需求，且在无网络环境下功能完全不可用。\n- **GPU 加速难实现**：纯 JavaScript 手动实现矩阵运算极其缓慢，无法利用客户端显卡资源，导致在浏览器中运行复杂模型时页面卡顿甚至崩溃。\n\n### 使用 js-pytorch 后\n- **全栈 JS 一体化**：直接复用熟悉的 PyTorch 语法在 JavaScript 中构建和运行模型，前端团队可独立闭环完成从数据处理到推理的全流程，无需维护额外后端。\n- **原生 GPU 加速**：借助内置的 GPU.js 支持，自动调用用户设备的显卡进行并行计算，将原本秒级的推理过程缩短至毫秒级，确保交互流畅。\n- **隐私安全且离线可用**：所有计算均在用户浏览器本地完成，图片数据不出设备，既满足了严格的隐私合规要求，也实现了真正的离线智能应用。\n- **开发体验无缝迁移**：支持线性层、矩阵乘法等核心算子与 PyTorch 高度一致，降低了算法工程师向 Web 端迁移模型的学习门槛和代码重构工作量。\n\njs-pytorch 成功打破了深度学习框架的语言壁垒，让高性能 AI 推理能像普通前端功能一样轻松嵌入网页。","https:\u002F\u002Foss.gittoolsai.com\u002Fimages\u002Feduardoleao052_js-pytorch_e96630c0.png","eduardoleao052","Eduardo Leitão da Cunha Opice Leão","https:\u002F\u002Foss.gittoolsai.com\u002Favatars\u002Feduardoleao052_c3ebba35.png","Engineering Student at the University of Sao Paulo",null,"São Paulo","https:\u002F\u002Fgithub.com\u002Feduardoleao052",[81,85,89],{"name":82,"color":83,"percentage":84},"JavaScript","#f1e05a",52.4,{"name":86,"color":87,"percentage":88},"TypeScript","#3178c6",40.9,{"name":90,"color":91,"percentage":92},"CSS","#663399",6.7,1223,56,"2026-04-09T10:36:58","MIT",1,"macOS, Windows, Ubuntu","非必需。支持通过 GPU.js 进行 GPU 加速，未指定具体显卡型号、显存大小或 CUDA 版本要求（基于 WebGL\u002FWebGPU 或 OpenCL 实现）。","未说明",{"notes":102,"python":103,"dependencies":104},"1. 该工具是纯 JavaScript 深度学习库，无需 Python 环境。2. 在 Windows 上若安装报错，可能需要安装包含'C++ 桌面开发'工作负载的 Visual Studio。3. 可直接在浏览器中通过 CDN 引入使用，也可在 Node.js 环境中通过 npm 安装使用。4. 实例化模块时需传入 'gpu' 或 'cpu' 字符串来指定设备。","不需要 (这是一个 JavaScript\u002FTypeScript 库)",[105],"gpu.js",[14],[108,109,110,111,112],"automatic-differentiation","deep-learning","javascript-library","neural-networks","pytorch","2026-03-27T02:49:30.150509","2026-04-10T02:44:50.715880",[116,121,126,131,136,141],{"id":117,"question_zh":118,"answer_zh":119,"source_url":120},26976,"如何在 TypeScript 实现中正确处理 `any` 类型和多维数组的类型定义？","应尽量避免使用 `any`，因为它会禁用类型检查。如果不确定类型，建议使用 `unknown` 类型，它更安全，必须经过类型收窄后才能操作。对于多维数组（如 n 维张量），虽然完全展开类型（如 `Array\u003CArray\u003C...>>`）很冗长，但可以尝试定义联合类型如 `number[] | number[][]` 来覆盖常见情况。此外，可以使用 ESLint 规则 `@typescript-eslint\u002Fno-explicit-any` 来强制检查，若需保留未使用的变量可调整相关 ESLint 配置。","https:\u002F\u002Fgithub.com\u002Feduardoleao052\u002Fjs-pytorch\u002Fissues\u002F9",{"id":122,"question_zh":123,"answer_zh":124,"source_url":125},26977,"是否应该使用对象（Object）代替数组（Array）来存储缓存（cache）数据以优化类型定义？","是的，建议将缓存字段从数组改为对象。例如，将 `cache` 定义为 `{ a: Tensor | number; b: Tensor | number }` 的形式。这样做虽然增加了代码行数，但能显著改善 TypeScript 的类型推断和 IntelliSense 支持，使调试更容易。由于 TypeScript 类型在编译为 JavaScript 时会被移除，因此不会增加最终运行时的代码体积。","https:\u002F\u002Fgithub.com\u002Feduardoleao052\u002Fjs-pytorch\u002Fissues\u002F19",{"id":127,"question_zh":128,"answer_zh":129,"source_url":130},26978,"项目是否计划与其他生态系统库（如 ndarray 或 Python Data API 标准）进行互操作？","维护者目前倾向于保持库的独立性，从零开始构建以避免引入额外的绑定和依赖。然而，社区建议如果未来有互操作目标，尽早遵循现有的数据结构接口（如 `@stdlib\u002Fstdlib` 中的最小化数组类型或 Python Data API 标准）会比后期重构更容易。目前可以借鉴 `ndarray` 等库的实现思路，但不直接依赖它们。","https:\u002F\u002Fgithub.com\u002Feduardoleao052\u002Fjs-pytorch\u002Fissues\u002F18",{"id":132,"question_zh":133,"answer_zh":134,"source_url":135},26979,"推荐添加哪些开发工具配置以提升代码质量和一致性？","建议添加以下开发依赖（devDependencies）：1. **Prettier** 和 **ESLint**：用于统一代码格式和执行静态检查；2. **Jest**：作为测试基础设施，可生成代码覆盖率报告；3. **lint-staged**：仅在暂存的文件上运行 lint 检查；4. **Husky**：在 commit 或 push 前自动运行脚本（如测试和 lint）。这些工具仅用于开发阶段，不会增加用户安装库时的依赖负担。","https:\u002F\u002Fgithub.com\u002Feduardoleao052\u002Fjs-pytorch\u002Fissues\u002F7",{"id":137,"question_zh":138,"answer_zh":139,"source_url":140},26980,"如何改进 Issue 模板和项目路线图（Roadmap）的管理？","可以使用 GitHub Projects 来整理思路和追踪进度。对于 Issue 模板，建议添加输入字段（input fields）以结构化用户提交的信息，使其更易于分类和处理。参考其他项目（如 KnowledgeCanvas）的 Feature Roadmap 示例，制定短期、中期和长期目标，有助于明确项目方向并降低新贡献者的参与门槛。","https:\u002F\u002Fgithub.com\u002Feduardoleao052\u002Fjs-pytorch\u002Fissues\u002F23",{"id":142,"question_zh":143,"answer_zh":144,"source_url":145},26981,"关于 GPU 加速和支持大型 Transformer 模型有什么建议或相关资源？","可以考虑集成 **GPU.js** 来实现 JavaScript 环境下的 GPU 加速。此外，WebGPU 标准（如 WGSL 语言中的向量构造函数）提供了类似的底层能力，值得参考。对于高性能计算流程，还可以研究 **Bend** 项目中的基于树的流式处理方式，以及斯坦福大学关于高效注意力机制（如 FlashAttention 变体）的最新研究成果，以优化大模型运行效率。","https:\u002F\u002Fgithub.com\u002Feduardoleao052\u002Fjs-pytorch\u002Fissues\u002F1",[147,152,157,162],{"id":148,"version":149,"summary_zh":150,"released_at":151},180117,"v0.7.2","新特性：\n\n- 提供了 `cdnjs` 标签，便于更轻松地集成到网页浏览器和应用程序中。\n- 在 [Seyronh](https:\u002F\u002Fgithub.com\u002FSeyronh) 的帮助下，新增了 `nn.MSELoss`。\n- 修复了在使用 `gpu` 内核时保存和加载模型的问题。","2024-10-30T02:50:50",{"id":153,"version":154,"summary_zh":155,"released_at":156},180118,"v0.4.7","在这个版本中，新增了两项主要功能：\n- **1. GPU 支持**\n  - 后端的 GPU 内核由 `gpu.js` 库管理。\n  - 要将张量添加到 GPU 上，只需将 `device` 参数设置为 `'gpu'`。\n  - 要将整个层（如 `nn.Linear`、`nn.Block`、`nn.MultiHeadSelfAttention`）添加到 GPU 上，同样将 `device` 参数设置为 `'gpu'`。\n  - 语法与 PyTorch 完全一致。\n- **2. 模型的保存与加载**\n  - 提供的函数是 `torch.save` 和 `torch.load`。\n  - 具体功能在 `README.md` 文件中已有详细说明。\n\n","2024-07-20T22:37:45",{"id":158,"version":159,"summary_zh":160,"released_at":161},180119,"v0.3.0","本次新版本包含以下功能：\n- __TypeScript__ 支持及源代码。\n- __开发者工具__，例如 `ESLint` 和 `prettier`。\n- 使用 `jest` 的自动化 __单元测试__。\n- 使用 `tiny-bench` 的 __基准测试__ 工具。\n\n感谢 @pparke 和 @medic-code 在实现过程中提供的帮助！\n\n@eduardoleao052","2024-04-02T18:04:00",{"id":163,"version":164,"summary_zh":165,"released_at":166},180120,"v0.1.1","初始发布。该包可在 npm 上获取，地址为：https:\u002F\u002Fwww.npmjs.com\u002Fpackage\u002Fjs-pytorch。","2024-03-26T14:28:20"]