[{"data":1,"prerenderedAt":-1},["ShallowReactive",2],{"similar-karpathy--char-rnn":3,"tool-karpathy--char-rnn":64},[4,17,27,35,48,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},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 真正成长为懂上",145895,2,"2026-04-08T11:32:59",[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},4487,"LLMs-from-scratch","rasbt\u002FLLMs-from-scratch","LLMs-from-scratch 是一个基于 PyTorch 的开源教育项目，旨在引导用户从零开始一步步构建一个类似 ChatGPT 的大型语言模型（LLM）。它不仅是同名技术著作的官方代码库，更提供了一套完整的实践方案，涵盖模型开发、预训练及微调的全过程。\n\n该项目主要解决了大模型领域“黑盒化”的学习痛点。许多开发者虽能调用现成模型，却难以深入理解其内部架构与训练机制。通过亲手编写每一行核心代码，用户能够透彻掌握 Transformer 架构、注意力机制等关键原理，从而真正理解大模型是如何“思考”的。此外，项目还包含了加载大型预训练权重进行微调的代码，帮助用户将理论知识延伸至实际应用。\n\nLLMs-from-scratch 特别适合希望深入底层原理的 AI 开发者、研究人员以及计算机专业的学生。对于不满足于仅使用 API，而是渴望探究模型构建细节的技术人员而言，这是极佳的学习资源。其独特的技术亮点在于“循序渐进”的教学设计：将复杂的系统工程拆解为清晰的步骤，配合详细的图表与示例，让构建一个虽小但功能完备的大模型变得触手可及。无论你是想夯实理论基础，还是为未来研发更大规模的模型做准备",90106,3,"2026-04-06T11:19:32",[15,26,14,13],"图像",{"id":28,"name":29,"github_repo":30,"description_zh":31,"stars":32,"difficulty_score":10,"last_commit_at":33,"category_tags":34,"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,15],{"id":36,"name":37,"github_repo":38,"description_zh":39,"stars":40,"difficulty_score":10,"last_commit_at":41,"category_tags":42,"status":16},2268,"ML-For-Beginners","microsoft\u002FML-For-Beginners","ML-For-Beginners 是由微软推出的一套系统化机器学习入门课程，旨在帮助零基础用户轻松掌握经典机器学习知识。这套课程将学习路径规划为 12 周，包含 26 节精炼课程和 52 道配套测验，内容涵盖从基础概念到实际应用的完整流程，有效解决了初学者面对庞大知识体系时无从下手、缺乏结构化指导的痛点。\n\n无论是希望转型的开发者、需要补充算法背景的研究人员，还是对人工智能充满好奇的普通爱好者，都能从中受益。课程不仅提供了清晰的理论讲解，还强调动手实践，让用户在循序渐进中建立扎实的技能基础。其独特的亮点在于强大的多语言支持，通过自动化机制提供了包括简体中文在内的 50 多种语言版本，极大地降低了全球不同背景用户的学习门槛。此外，项目采用开源协作模式，社区活跃且内容持续更新，确保学习者能获取前沿且准确的技术资讯。如果你正寻找一条清晰、友好且专业的机器学习入门之路，ML-For-Beginners 将是理想的起点。",85052,"2026-04-08T11:03:08",[26,43,44,45,14,46,15,13,47],"数据工具","视频","插件","其他","音频",{"id":49,"name":50,"github_repo":51,"description_zh":52,"stars":53,"difficulty_score":23,"last_commit_at":54,"category_tags":55,"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",[14,26,13,15,46],{"id":57,"name":58,"github_repo":59,"description_zh":60,"stars":61,"difficulty_score":23,"last_commit_at":62,"category_tags":63,"status":16},519,"PaddleOCR","PaddlePaddle\u002FPaddleOCR","PaddleOCR 是一款基于百度飞桨框架开发的高性能开源光学字符识别工具包。它的核心能力是将图片、PDF 等文档中的文字提取出来，转换成计算机可读取的结构化数据，让机器真正“看懂”图文内容。\n\n面对海量纸质或电子文档，PaddleOCR 解决了人工录入效率低、数字化成本高的问题。尤其在人工智能领域，它扮演着连接图像与大型语言模型（LLM）的桥梁角色，能将视觉信息直接转化为文本输入，助力智能问答、文档分析等应用场景落地。\n\nPaddleOCR 适合开发者、算法研究人员以及有文档自动化需求的普通用户。其技术优势十分明显：不仅支持全球 100 多种语言的识别，还能在 Windows、Linux、macOS 等多个系统上运行，并灵活适配 CPU、GPU、NPU 等各类硬件。作为一个轻量级且社区活跃的开源项目，PaddleOCR 既能满足快速集成的需求，也能支撑前沿的视觉语言研究，是处理文字识别任务的理想选择。",75149,"2026-04-08T11:09:19",[15,26,13,46],{"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":81,"owner_email":82,"owner_twitter":80,"owner_website":83,"owner_url":84,"languages":85,"stars":90,"forks":91,"last_commit_at":92,"license":80,"difficulty_score":93,"env_os":94,"env_gpu":95,"env_ram":96,"env_deps":97,"category_tags":110,"github_topics":80,"view_count":10,"oss_zip_url":80,"oss_zip_packed_at":80,"status":16,"created_at":111,"updated_at":112,"faqs":113,"releases":144},5605,"karpathy\u002Fchar-rnn","char-rnn","Multi-layer Recurrent Neural Networks (LSTM, GRU, RNN) for character-level language models in Torch","char-rnn 是一个基于 Torch 框架开发的开源项目，旨在构建多层循环神经网络（包括 LSTM、GRU 和传统 RNN），用于训练字符级的语言模型。它的核心功能是读取任意文本文件，学习预测序列中的下一个字符，从而生成在风格和结构上高度模仿原始训练数据的新文本。\n\n这一工具主要解决了从零开始构建和训练递归神经网络的复杂性难题。它将复杂的深度学习概念封装为可执行的代码，让用户无需深入底层数学推导，即可通过简单的配置实现文本生成任务。无论是创作诗歌、编写代码还是模拟对话风格，char-rnn 都能提供强大的生成能力。\n\nchar-rnn 特别适合对深度学习感兴趣的开发者、研究人员以及希望探索 AI 生成内容可能性的技术爱好者使用。虽然普通用户也能尝试运行，但具备一定的命令行操作基础和机器学习常识会让体验更加顺畅。\n\n其技术亮点在于支持多层网络架构以提升模型表达能力，并兼容 GPU 加速（CUDA 或 OpenCL），显著缩短训练时间。值得一提的是，该项目由知名学者 Andrej Karpathy 开发，代码清晰且配有详尽博客解读，是学习循环神经网络原理的经典入门案例。尽管后续有更优化","char-rnn 是一个基于 Torch 框架开发的开源项目，旨在构建多层循环神经网络（包括 LSTM、GRU 和传统 RNN），用于训练字符级的语言模型。它的核心功能是读取任意文本文件，学习预测序列中的下一个字符，从而生成在风格和结构上高度模仿原始训练数据的新文本。\n\n这一工具主要解决了从零开始构建和训练递归神经网络的复杂性难题。它将复杂的深度学习概念封装为可执行的代码，让用户无需深入底层数学推导，即可通过简单的配置实现文本生成任务。无论是创作诗歌、编写代码还是模拟对话风格，char-rnn 都能提供强大的生成能力。\n\nchar-rnn 特别适合对深度学习感兴趣的开发者、研究人员以及希望探索 AI 生成内容可能性的技术爱好者使用。虽然普通用户也能尝试运行，但具备一定的命令行操作基础和机器学习常识会让体验更加顺畅。\n\n其技术亮点在于支持多层网络架构以提升模型表达能力，并兼容 GPU 加速（CUDA 或 OpenCL），显著缩短训练时间。值得一提的是，该项目由知名学者 Andrej Karpathy 开发，代码清晰且配有详尽博客解读，是学习循环神经网络原理的经典入门案例。尽管后续有更优化的 torch-rnn 出现，char-rnn 凭借其教育价值和历史地位，依然是理解字符级建模的重要资源。","\n# char-rnn\n\nThis code implements **multi-layer Recurrent Neural Network** (RNN, LSTM, and GRU) for training\u002Fsampling from character-level language models. In other words the model takes one text file as input and trains a Recurrent Neural Network that learns to predict the next character in a sequence. The RNN can then be used to generate text character by character that will look like the original training data. The context of this code base is described in detail in my [blog post](http:\u002F\u002Fkarpathy.github.io\u002F2015\u002F05\u002F21\u002Frnn-effectiveness\u002F).\n\nIf you are new to Torch\u002FLua\u002FNeural Nets, it might be helpful to know that this code is really just a slightly more fancy version of this [100-line gist](https:\u002F\u002Fgist.github.com\u002Fkarpathy\u002Fd4dee566867f8291f086) that I wrote in Python\u002Fnumpy. The code in this repo additionally: allows for multiple layers, uses an LSTM instead of a vanilla RNN, has more supporting code for model checkpointing, and is of course much more efficient since it uses mini-batches and can run on a GPU.\n\n## Update: torch-rnn\n\n[Justin Johnson](http:\u002F\u002Fcs.stanford.edu\u002Fpeople\u002Fjcjohns\u002F) (@jcjohnson) recently re-implemented char-rnn from scratch with a much nicer\u002Fsmaller\u002Fcleaner\u002Ffaster Torch code base. It's under the name [torch-rnn](https:\u002F\u002Fgithub.com\u002Fjcjohnson\u002Ftorch-rnn). It uses Adam for optimization and hard-codes the RNN\u002FLSTM forward\u002Fbackward passes for space\u002Ftime efficiency. This also avoids headaches with cloning models in this repo. In other words, torch-rnn should be the default char-rnn implemention to use now instead of the one in this code base.\n\n## Requirements\n\nThis code is written in Lua and requires [Torch](http:\u002F\u002Ftorch.ch\u002F). If you're on Ubuntu, installing Torch in your home directory may look something like: \n\n```bash\n$ curl -s https:\u002F\u002Fraw.githubusercontent.com\u002Ftorch\u002Fezinstall\u002Fmaster\u002Finstall-deps | bash\n$ git clone https:\u002F\u002Fgithub.com\u002Ftorch\u002Fdistro.git ~\u002Ftorch --recursive\n$ cd ~\u002Ftorch; \n$ .\u002Finstall.sh      # and enter \"yes\" at the end to modify your bashrc\n$ source ~\u002F.bashrc\n```\n\nSee the Torch installation documentation for more details. After Torch is installed we need to get a few more packages using [LuaRocks](https:\u002F\u002Fluarocks.org\u002F) (which already came with the Torch install). In particular:\n\n```bash\n$ luarocks install nngraph \n$ luarocks install optim\n$ luarocks install nn\n```\n\nIf you'd like to train on an NVIDIA GPU using CUDA (this can be to about 15x faster), you'll of course need the GPU, and you will have to install the [CUDA Toolkit](https:\u002F\u002Fdeveloper.nvidia.com\u002Fcuda-toolkit). Then get the `cutorch` and `cunn` packages:\n\n```bash\n$ luarocks install cutorch\n$ luarocks install cunn\n```\n\nIf you'd like to use OpenCL GPU instead (e.g. ATI cards), you will instead need to install the `cltorch` and `clnn` packages, and then use the option `-opencl 1` during training ([cltorch issues](https:\u002F\u002Fgithub.com\u002Fhughperkins\u002Fcltorch\u002Fissues)):\n\n```bash\n$ luarocks install cltorch\n$ luarocks install clnn\n```\n\n## Usage\n\n### Data\n\nAll input data is stored inside the `data\u002F` directory. You'll notice that there is an example dataset included in the repo (in folder `data\u002Ftinyshakespeare`) which consists of a subset of works of Shakespeare. I'm providing a few more datasets on [this page](http:\u002F\u002Fcs.stanford.edu\u002Fpeople\u002Fkarpathy\u002Fchar-rnn\u002F).\n\n**Your own data**: If you'd like to use your own data then create a single file `input.txt` and place it into a folder in the `data\u002F` directory. For example, `data\u002Fsome_folder\u002Finput.txt`. The first time you run the training script it will do some preprocessing and write two more convenience cache files into `data\u002Fsome_folder`.\n\n**Dataset sizes**: Note that if your data is too small (1MB is already considered very small) the RNN won't learn very effectively. Remember that it has to learn everything completely from scratch. Conversely if your data is large (more than about 2MB), feel confident to increase `rnn_size` and train a bigger model (see details of training below). It will work *significantly better*. For example with 6MB you can easily go up to `rnn_size` 300 or even more. The biggest that fits on my GPU and that I've trained with this code is `rnn_size` 700 with `num_layers` 3 (2 is default).\n\n### Training\n\nStart training the model using `train.lua`. As a sanity check, to run on the included example dataset simply try:\n\n```\n$ th train.lua -gpuid -1\n```\n\nNotice that here we are setting the flag `gpuid` to -1, which tells the code to train using CPU, otherwise it defaults to GPU 0.  There are many other flags for various options. Consult `$ th train.lua -help` for comprehensive settings. Here's another example that trains a bigger network and also shows how you can run on your own custom dataset (this already assumes that `data\u002Fsome_folder\u002Finput.txt` exists):\n\n```\n$ th train.lua -data_dir data\u002Fsome_folder -rnn_size 512 -num_layers 2 -dropout 0.5\n```\n\n**Checkpoints.** While the model is training it will periodically write checkpoint files to the `cv` folder. The frequency with which these checkpoints are written is controlled with number of iterations, as specified with the `eval_val_every` option (e.g. if this is 1 then a checkpoint is written every iteration). The filename of these checkpoints contains a very important number: the **loss**. For example, a checkpoint with filename `lm_lstm_epoch0.95_2.0681.t7` indicates that at this point the model was on epoch 0.95 (i.e. it has almost done one full pass over the training data), and the loss on validation data was 2.0681. This number is very important because the lower it is, the better the checkpoint works. Once you start to generate data (discussed below), you will want to use the model checkpoint that reports the lowest validation loss. Notice that this might not necessarily be the last checkpoint at the end of training (due to possible overfitting).\n\nAnother important quantities to be aware of are `batch_size` (call it B), `seq_length` (call it S), and the `train_frac` and `val_frac` settings. The batch size specifies how many streams of data are processed in parallel at one time. The sequence length specifies the length of each stream, which is also the limit at which the gradients can propagate backwards in time. For example, if `seq_length` is 20, then the gradient signal will never backpropagate more than 20 time steps, and the model might not *find* dependencies longer than this length in number of characters. Thus, if you have a very difficult dataset where there are a lot of long-term dependencies you will want to increase this setting. Now, if at runtime your input text file has N characters, these first all get split into chunks of size `BxS`. These chunks then get allocated across three splits: train\u002Fval\u002Ftest according to the `frac` settings. By default `train_frac` is 0.95 and `val_frac` is 0.05, which means that 95% of our data chunks will be trained on and 5% of the chunks will be used to estimate the validation loss (and hence the generalization). If your data is small, it's possible that with the default settings you'll only have very few chunks in total (for example 100). This is bad: In these cases you may want to decrease batch size or sequence length.\n\nNote that you can also initialize parameters from a previously saved checkpoint using `init_from`.\n\n### Sampling\n\nGiven a checkpoint file (such as those written to `cv`) we can generate new text. For example:\n\n```\n$ th sample.lua cv\u002Fsome_checkpoint.t7 -gpuid -1\n```\n\nMake sure that if your checkpoint was trained with GPU it is also sampled from with GPU, or vice versa. Otherwise the code will (currently) complain. As with the train script, see `$ th sample.lua -help` for full options. One important one is (for example) `-length 10000` which would generate 10,000 characters (default = 2000).\n\n**Temperature**. An important parameter you may want to play with is `-temperature`, which takes a number in range \\(0, 1\\] (0 not included), default = 1. The temperature is dividing the predicted log probabilities before the Softmax, so lower temperature will cause the model to make more likely, but also more boring and conservative predictions. Higher temperatures cause the model to take more chances and increase diversity of results, but at a cost of more mistakes.\n\n**Priming**. It's also possible to prime the model with some starting text using `-primetext`. This starts out the RNN with some hardcoded characters to *warm* it up with some context before it starts generating text. E.g. a fun primetext might be `-primetext \"the meaning of life is \"`. \n\n**Training with GPU but sampling on CPU**. Right now the solution is to use the `convert_gpu_cpu_checkpoint.lua` script to convert your GPU checkpoint to a CPU checkpoint. In near future you will not have to do this explicitly. E.g.:\n\n```\n$ th convert_gpu_cpu_checkpoint.lua cv\u002Flm_lstm_epoch30.00_1.3950.t7\n```\n\nwill create a new file `cv\u002Flm_lstm_epoch30.00_1.3950.t7_cpu.t7` that you can use with the sample script and with `-gpuid -1` for CPU mode.\n\nHappy sampling!\n\n## Tips and Tricks\n\n### Monitoring Validation Loss vs. Training Loss\nIf you're somewhat new to Machine Learning or Neural Networks it can take a bit of expertise to get good models. The most important quantity to keep track of is the difference between your training loss (printed during training) and the validation loss (printed once in a while when the RNN is run on the validation data (by default every 1000 iterations)). In particular:\n\n- If your training loss is much lower than validation loss then this means the network might be **overfitting**. Solutions to this are to decrease your network size, or to increase dropout. For example you could try dropout of 0.5 and so on.\n- If your training\u002Fvalidation loss are about equal then your model is **underfitting**. Increase the size of your model (either number of layers or the raw number of neurons per layer)\n\n### Approximate number of parameters\n\nThe two most important parameters that control the model are `rnn_size` and `num_layers`. I would advise that you always use `num_layers` of either 2\u002F3. The `rnn_size` can be adjusted based on how much data you have. The two important quantities to keep track of here are:\n\n- The number of parameters in your model. This is printed when you start training.\n- The size of your dataset. 1MB file is approximately 1 million characters.\n\nThese two should be about the same order of magnitude. It's a little tricky to tell. Here are some examples:\n\n- I have a 100MB dataset and I'm using the default parameter settings (which currently print 150K parameters). My data size is significantly larger (100 mil >> 0.15 mil), so I expect to heavily underfit. I am thinking I can comfortably afford to make `rnn_size` larger.\n- I have a 10MB dataset and running a 10 million parameter model. I'm slightly nervous and I'm carefully monitoring my validation loss. If it's larger than my training loss then I may want to try to increase dropout a bit and see if that heps the validation loss.\n\n### Best models strategy\n\nThe winning strategy to obtaining very good models (if you have the compute time) is to always err on making the network larger (as large as you're willing to wait for it to compute) and then try different dropout values (between 0,1). Whatever model has the best validation performance (the loss, written in the checkpoint filename, low is good) is the one you should use in the end.\n\nIt is very common in deep learning to run many different models with many different hyperparameter settings, and in the end take whatever checkpoint gave the best validation performance.\n\nBy the way, the size of your training and validation splits are also parameters. Make sure you have a decent amount of data in your validation set or otherwise the validation performance will be noisy and not very informative.\n\n## Additional Pointers and Acknowledgements\n\nThis code was originally based on Oxford University Machine Learning class [practical 6](https:\u002F\u002Fgithub.com\u002Foxford-cs-ml-2015\u002Fpractical6), which is in turn based on [learning to execute](https:\u002F\u002Fgithub.com\u002Fwojciechz\u002Flearning_to_execute) code from Wojciech Zaremba. Chunks of it were also developed in collaboration with my labmate [Justin Johnson](http:\u002F\u002Fcs.stanford.edu\u002Fpeople\u002Fjcjohns\u002F).\n\nTo learn more about RNN language models I recommend looking at:\n\n- [My recent talk](https:\u002F\u002Fskillsmatter.com\u002Fskillscasts\u002F6611-visualizing-and-understanding-recurrent-networks) on char-rnn\n- [Generating Sequences With Recurrent Neural Networks](http:\u002F\u002Farxiv.org\u002Fabs\u002F1308.0850) by Alex Graves\n- [Generating Text with Recurrent Neural Networks](http:\u002F\u002Fwww.cs.utoronto.ca\u002F~ilya\u002Fpubs\u002F2011\u002FLANG-RNN.pdf) by Ilya Sutskever\n- [Tomas Mikolov's Thesis](http:\u002F\u002Fwww.fit.vutbr.cz\u002F~imikolov\u002Frnnlm\u002Fthesis.pdf)\n\n## License\n\nMIT\n","# char-rnn\n\n这段代码实现了用于字符级语言模型训练和采样的**多层循环神经网络**（RNN、LSTM 和 GRU）。换句话说，该模型以一个文本文件作为输入，训练一个循环神经网络，使其学会预测序列中的下一个字符。随后，可以使用这个 RNN 逐字符生成与原始训练数据风格相似的文本。关于该代码库的背景和详细说明，请参阅我的[博客文章](http:\u002F\u002Fkarpathy.github.io\u002F2015\u002F05\u002F21\u002Frnn-effectiveness\u002F)。\n\n如果你是 Torch\u002FLua\u002F神经网络的新手，可能需要了解的是，这段代码实际上只是我用 Python\u002Fnumpy 编写的[100 行 gist](https:\u002F\u002Fgist.github.com\u002Fkarpathy\u002Fd4dee566867f8291f086)的一个稍微复杂一些的版本。本仓库中的代码还额外支持多层结构、使用 LSTM 而不是普通的 RNN、增加了模型检查点保存等辅助功能，并且由于采用了小批量训练和 GPU 加速，效率也更高。\n\n## 更新：torch-rnn\n\n[Justin Johnson](http:\u002F\u002Fcs.stanford.edu\u002Fpeople\u002Fjcjohns\u002F) (@jcjohnson) 最近从头重新实现了一个更简洁、更高效、更易用的 Torch 版本的 char-rnn，项目名为 [torch-rnn](https:\u002F\u002Fgithub.com\u002Fjcjohnson\u002Ftorch-rnn)。它使用 Adam 优化器，并为提高空间和时间效率而硬编码了 RNN\u002FLSTM 的前向和反向传播过程。这同时也避免了本仓库中克隆模型时可能出现的问题。因此，现在默认应该使用 torch-rnn 而不是本代码库中的实现。\n\n## 环境要求\n\n此代码使用 Lua 编写，需要依赖 [Torch](http:\u002F\u002Ftorch.ch\u002F) 框架。如果你使用 Ubuntu 系统，在家目录下安装 Torch 的步骤大致如下：\n\n```bash\n$ curl -s https:\u002F\u002Fraw.githubusercontent.com\u002Ftorch\u002Fezinstall\u002Fmaster\u002Finstall-deps | bash\n$ git clone https:\u002F\u002Fgithub.com\u002Ftorch\u002Fdistro.git ~\u002Ftorch --recursive\n$ cd ~\u002Ftorch; \n$ .\u002Finstall.sh      # 最后回答“yes”以修改你的 .bashrc 文件\n$ source ~\u002F.bashrc\n```\n\n更多细节请参考 Torch 的官方安装文档。安装完 Torch 后，还需要通过 [LuaRocks](https:\u002F\u002Fluarocks.org\u002F) 安装几个额外的包（这些工具在 Torch 安装时已一并提供）：\n\n```bash\n$ luarocks install nngraph \n$ luarocks install optim\n$ luarocks install nn\n```\n\n如果你想使用 NVIDIA GPU 并借助 CUDA 进行加速（通常可提升约 15 倍的速度），则需要配备相应的 GPU，并安装 [CUDA 工具包](https:\u002F\u002Fdeveloper.nvidia.com\u002Fcuda-toolkit)。接着再安装 `cutorch` 和 `cunn` 包：\n\n```bash\n$ luarocks install cutorch\n$ luarocks install cunn\n```\n\n如果你希望使用 OpenCL GPU（例如 ATI 显卡），则需要安装 `cltorch` 和 `clnn` 包，并在训练时添加 `-opencl 1` 参数（相关问题请参见 [cltorch 的 GitHub 问题页面](https:\u002F\u002Fgithub.com\u002Fhughperkins\u002Fcltorch\u002Fissues))：\n\n```bash\n$ luarocks install cltorch\n$ luarocks install clnn\n```\n\n## 使用方法\n\n### 数据\n\n所有输入数据都存储在 `data\u002F` 目录下。你会注意到仓库中包含了一个示例数据集（位于 `data\u002Ftinyshakespeare` 文件夹），它是莎士比亚作品的一部分。此外，我还提供了更多数据集，详情请访问[此页面](http:\u002F\u002Fcs.stanford.edu\u002Fpeople\u002Fkarpathy\u002Fchar-rnn\u002F)。\n\n**自定义数据**：如果想使用自己的数据，只需创建一个名为 `input.txt` 的单个文件，并将其放入 `data\u002F` 目录下的某个文件夹中，例如 `data\u002Fsome_folder\u002Finput.txt`。首次运行训练脚本时，程序会进行预处理，并在 `data\u002Fsome_folder` 中生成两个方便使用的缓存文件。\n\n**数据规模**：需要注意的是，如果数据量过小（1MB 已经算非常小了），RNN 很难有效学习，因为它必须完全从零开始学习所有内容。相反，如果数据量较大（超过约 2MB），则可以放心地增加 `rnn_size` 参数，训练一个更大的模型（具体参数设置见下文的训练部分），这样效果会显著更好。例如，当数据量达到 6MB 时，可以将 `rnn_size` 提高到 300 甚至更高。我在自己的 GPU 上尝试过的最大配置是 `rnn_size` 700、`num_layers` 3（默认值为 2）。\n\n### 训练\n\n使用 `train.lua` 开始训练模型。作为 sanity check，要运行附带的示例数据集，只需尝试：\n\n```\n$ th train.lua -gpuid -1\n```\n\n请注意，这里我们将标志 `gpuid` 设置为 -1，这会指示代码使用 CPU 进行训练；否则默认使用 GPU 0。还有许多其他标志用于各种选项。请参阅 `$ th train.lua -help` 获取全面的设置说明。以下是另一个示例，它训练一个更大的网络，并展示了如何在您自己的自定义数据集上运行（此操作已假定 `data\u002Fsome_folder\u002Finput.txt` 存在）：\n\n```\n$ th train.lua -data_dir data\u002Fsome_folder -rnn_size 512 -num_layers 2 -dropout 0.5\n```\n\n**检查点。** 在模型训练期间，它会定期将检查点文件写入 `cv` 文件夹。这些检查点的写入频率由迭代次数控制，具体由 `eval_val_every` 选项指定（例如，如果该值为 1，则每迭代都会写入一个检查点）。这些检查点的文件名包含一个非常重要的数字：**损失值**。例如，文件名为 `lm_lstm_epoch0.95_2.0681.t7` 的检查点表示此时模型处于第 0.95 个 epoch（即几乎完成了一轮完整的训练数据遍历），且验证集上的损失值为 2.0681。这个数值非常重要，因为其越低，检查点的效果越好。一旦开始生成数据（如下所述），您将希望使用报告最低验证损失的模型检查点。请注意，这并不一定是训练结束时的最后一个检查点（由于可能过拟合）。\n\n另外需要关注的重要参数包括 `batch_size`（记作 B）、`seq_length`（记作 S），以及 `train_frac` 和 `val_frac` 设置。批大小指一次并行处理的数据流数量。序列长度则指每个数据流的长度，同时也是梯度可以反向传播的时间上限。例如，若 `seq_length` 为 20，则梯度信号永远不会回传超过 20 个时间步，模型也可能无法“发现”超过该字符数的长期依赖关系。因此，如果您有一个非常困难的数据集，其中存在大量长期依赖关系，那么您应该提高此设置。现在，如果您的输入文本文件有 N 个字符，它们首先会被分割成大小为 `BxS` 的块。这些块随后会根据 `frac` 设置被分配到训练\u002F验证\u002F测试三个部分中。默认情况下，`train_frac` 为 0.95，`val_frac` 为 0.05，这意味着我们 95% 的数据块将用于训练，而 5% 的块则用于估计验证损失（从而评估泛化能力）。如果您的数据量较小，按照默认设置，您可能总共只有很少的块（例如 100 块）。这是不好的：在这种情况下，您可能需要减小批大小或序列长度。\n\n请注意，您还可以使用 `init_from` 从先前保存的检查点初始化参数。\n\n### 采样\n\n给定一个检查点文件（例如写入 `cv` 文件夹中的那些），我们可以生成新文本。例如：\n\n```\n$ th sample.lua cv\u002Fsome_checkpoint.t7 -gpuid -1\n```\n\n请确保如果您的检查点是用 GPU 训练的，那么采样时也应使用 GPU，反之亦然。否则代码（目前）会报错。与训练脚本类似，可参阅 `$ th sample.lua -help` 查看完整选项。其中一个重要选项是 `-length 10000`，它将生成 10,000 个字符（默认为 2000）。\n\n**温度。** 您可能想要尝试的一个重要参数是 `-temperature`，它取范围为 \\(0, 1\\] 的数值（不包括 0），默认值为 1。温度会在 Softmax 之前对预测的对数概率进行归一化，因此较低的温度会使模型做出更可能但同时也更单调和保守的预测。较高的温度则会让模型承担更多风险，增加结果的多样性，但代价是可能出现更多错误。\n\n**预热。** 您还可以使用 `-primetext` 参数用一些起始文本对模型进行预热。这会用一些硬编码的字符初始化 RNN，以便在开始生成文本之前为其提供上下文。例如，一个有趣的预热文本可能是 `-primetext \"生命的意义是 \"`。\n\n**用 GPU 训练但在 CPU 上采样。** 目前的解决方法是使用 `convert_gpu_cpu_checkpoint.lua` 脚本将 GPU 检查点转换为 CPU 检查点。在不久的将来，您将无需再手动执行此操作。例如：\n\n```\n$ th convert_gpu_cpu_checkpoint.lua cv\u002Flm_lstm_epoch30.00_1.3950.t7\n```\n\n将会创建一个新的文件 `cv\u002Flm_lstm_epoch30.00_1.3950.t7_cpu.t7`，您可以将其与采样脚本一起使用，并通过 `-gpuid -1` 以 CPU 模式运行。\n\n祝您采样愉快！\n\n## 小贴士与技巧\n\n### 监控验证损失与训练损失\n如果您对机器学习或神经网络不太熟悉，获得良好的模型可能需要一定的经验积累。最重要的是要跟踪您的训练损失（在训练过程中打印）与验证损失（当 RNN 在验证数据上运行时偶尔打印， 默认每 1000 次迭代打印一次）之间的差异。具体来说：\n\n- 如果您的训练损失远低于验证损失，则表明网络可能存在 **过拟合** 问题。解决办法是减小网络规模或增加 dropout。例如，您可以尝试将 dropout 设置为 0.5 等。\n- 如果您的训练损失和验证损失大致相等，则说明您的模型正在 **欠拟合**。此时应增大模型规模（无论是层数还是每层的神经元数量）。\n\n### 参数数量的近似估算\n控制模型的两个最重要的参数是 `rnn_size` 和 `num_layers`。我建议您始终将 `num_layers` 设置为 2 或 3。`rnn_size` 可根据您拥有的数据量进行调整。在此过程中，有两个重要指标需要关注：\n\n- 您的模型中的参数数量。这会在您开始训练时打印出来。\n- 您的数据集大小。1MB 的文件大约包含 100 万个字符。\n\n这两者应在数量级上大致相当。判断起来可能有些棘手。以下是一些例子：\n\n- 我有一个 100MB 的数据集，使用的是默认参数设置（当前打印出约 15 万个参数）。我的数据量明显更大（1000 万 >> 15 万），因此预计会严重欠拟合。我认为我可以放心地增大 `rnn_size`。\n- 我有一个 10MB 的数据集，却运行着一个拥有 1000 万个参数的模型。我有些紧张，正在仔细监控验证损失。如果验证损失高于训练损失，或许可以尝试稍微增加 dropout，看看是否有助于降低验证损失。\n\n### 最佳模型策略\n\n要想获得非常好的模型（如果你有足够的计算时间），最佳策略就是尽可能地把网络做得更大（大到你能接受的等待时间为止），然后尝试不同的 Dropout 值（在 0 到 1 之间）。最终选择验证性能最好的模型，即检查点文件名中记录的损失值最低的那个。\n\n在深度学习中，通常会运行许多不同超参数设置的模型，并最终选择验证性能最好的那个检查点。\n\n顺便提一下，训练集和验证集的划分比例也是超参数之一。请确保验证集中有足够的数据，否则验证性能会非常不稳定，难以提供有价值的参考。\n\n## 补充说明与致谢\n\n这段代码最初基于牛津大学机器学习课程的实践作业第 6 题 [practical 6](https:\u002F\u002Fgithub.com\u002Foxford-cs-ml-2015\u002Fpractical6)，而该实践作业又源自 Wojciech Zaremba 的 [learning to execute](https:\u002F\u002Fgithub.com\u002Fwojciechz\u002Flearning_to_execute) 项目。此外，代码的部分内容也与我的实验室伙伴 Justin Johnson 合作开发而成，他的个人主页是 [http:\u002F\u002Fcs.stanford.edu\u002Fpeople\u002Fjcjohns\u002F](http:\u002F\u002Fcs.stanford.edu\u002Fpeople\u002Fjcjohns\u002F)。\n\n若想深入了解 RNN 语言模型，我推荐以下资源：\n\n- 我最近关于 char-RNN 的演讲：[Visualizing and Understanding Recurrent Networks](https:\u002F\u002Fskillsmatter.com\u002Fskillscasts\u002F6611-visualizing-and-understanding-recurrent-networks)\n- Alex Graves 的论文 [Generating Sequences With Recurrent Neural Networks](http:\u002F\u002Farxiv.org\u002Fabs\u002F1308.0850)\n- Ilya Sutskever 的论文 [Generating Text with Recurrent Neural Networks](http:\u002F\u002Fwww.cs.utoronto.ca\u002F~ilya\u002Fpubs\u002F2011\u002FLANG-RNN.pdf)\n- Tomas Mikolov 的博士论文 [Tomas Mikolov's Thesis](http:\u002F\u002Fwww.fit.vutbr.cz\u002F~imikolov\u002Frnnlm\u002Fthesis.pdf)\n\n## 许可证\n\nMIT","# char-rnn 快速上手指南\n\n`char-rnn` 是一个基于多层循环神经网络（RNN、LSTM、GRU）的字符级语言模型工具。它通过学习文本文件中字符的序列规律，能够生成风格相似的新文本。\n\n> **注意**：本项目基于 Lua\u002FTorch 框架。原作者推荐的新版本实现为 [torch-rnn](https:\u002F\u002Fgithub.com\u002Fjcjohnson\u002Ftorch-rnn)，性能更优且代码更简洁，建议新项目优先尝试 torch-rnn。若需使用本仓库代码，请参考以下指南。\n\n## 环境准备\n\n本工具依赖 **Lua** 和 **Torch** 深度学习框架。支持 CPU 训练，若拥有 NVIDIA GPU 并安装 CUDA，可获得约 15 倍的加速效果。\n\n**前置依赖：**\n*   操作系统：Linux (推荐 Ubuntu) 或 macOS\n*   GPU 加速（可选）：NVIDIA GPU + CUDA Toolkit 或 ATI\u002FAMD GPU + OpenCL\n*   包管理器：LuaRocks (随 Torch 自动安装)\n\n## 安装步骤\n\n### 1. 安装 Torch 框架\n在终端执行以下命令安装 Torch 及其依赖（以 Ubuntu 为例，安装到用户主目录）：\n\n```bash\n$ curl -s https:\u002F\u002Fraw.githubusercontent.com\u002Ftorch\u002Fezinstall\u002Fmaster\u002Finstall-deps | bash\n$ git clone https:\u002F\u002Fgithub.com\u002Ftorch\u002Fdistro.git ~\u002Ftorch --recursive\n$ cd ~\u002Ftorch; \n$ .\u002Finstall.sh      # 末尾提示时输入 \"yes\" 以修改 bashrc\n$ source ~\u002F.bashrc\n```\n\n### 2. 安装必要的 Lua 模块\n使用 `luarocks` 安装核心依赖包：\n\n```bash\n$ luarocks install nngraph \n$ luarocks install optim\n$ luarocks install nn\n```\n\n### 3. 安装 GPU 支持（可选）\n若需使用 GPU 加速，请根据显卡类型选择其一：\n\n**NVIDIA GPU (CUDA):**\n先安装 [CUDA Toolkit](https:\u002F\u002Fdeveloper.nvidia.com\u002Fcuda-toolkit)，然后运行：\n```bash\n$ luarocks install cutorch\n$ luarocks install cunn\n```\n\n**AMD\u002FATI GPU (OpenCL):**\n```bash\n$ luarocks install cltorch\n$ luarocks install clnn\n```\n*注：使用 OpenCL 训练时需添加 `-opencl 1` 参数。*\n\n## 基本使用\n\n### 1. 准备数据\n所有数据存放在 `data\u002F` 目录下。\n*   **使用示例数据**：仓库已内置 `data\u002Ftinyshakespeare`（莎士比亚作品子集）。\n*   **使用自定义数据**：在 `data\u002F` 下新建文件夹（如 `data\u002Fmy_data`），将你的文本文件重命名为 `input.txt` 放入其中。\n    *   *提示*：数据集建议大于 1MB，否则模型难以有效学习；若数据超过 2MB，可适当增大模型参数。\n\n### 2. 训练模型\n使用 `train.lua` 脚本开始训练。\n\n**最简单的 CPU 训练示例（使用内置数据）：**\n```bash\n$ th train.lua -gpuid -1\n```\n*   `-gpuid -1`：强制使用 CPU 训练。若不加此参数，默认使用 GPU 0。\n\n**自定义数据与更大模型示例：**\n```bash\n$ th train.lua -data_dir data\u002Fmy_data -rnn_size 512 -num_layers 2 -dropout 0.5\n```\n*   训练过程中会在 `cv\u002F` 目录生成检查点文件（`.t7`）。文件名包含验证集损失值（loss），**数值越低代表模型效果越好**。\n\n### 3. 生成文本\n使用 `sample.lua` 脚本加载训练好的检查点生成新文本。\n\n**基本生成命令：**\n```bash\n$ th sample.lua cv\u002Flm_lstm_epochX.XX_YYYYYY.t7 -gpuid -1\n```\n*   请确保采样时的设备设置（CPU\u002FGPU）与训练时一致。若训练用 GPU 而采样用 CPU，需先运行 `convert_gpu_cpu_checkpoint.lua` 转换模型文件。\n\n**常用参数：**\n*   `-length 10000`：生成 10,000 个字符（默认 2000）。\n*   `-temperature 0.8`：控制生成的随机性。范围 (0, 1]。\n    *   值越小（如 0.5）：输出更保守、确定性强，但可能枯燥。\n    *   值越大（如 1.2）：输出更多样化，但可能出现错误。\n*   `-primetext \"hello\"`：提供起始文本，让模型基于此上下文继续生成。","某独立游戏开发者希望为奇幻 RPG 游戏中的古代遗迹石碑生成大量风格统一、看似有深意但实为虚构的铭文，以丰富世界观细节。\n\n### 没有 char-rnn 时\n- 开发者需手动撰写数百条铭文，耗时数周且极易陷入创意枯竭，导致文本重复度高。\n- 若尝试用简单的随机字符串拼接，生成的内容毫无语言逻辑，玩家一眼就能看出是乱码，破坏沉浸感。\n- 聘请专业文案团队成本高昂，且难以保证所有铭文在生僻词汇和句式结构上保持高度一致的“古语”风格。\n- 每次需要新增内容时都必须重新投入人力，无法实现根据现有文本库进行自动化、批量的无限扩展。\n\n### 使用 char-rnn 后\n- 只需将现有的几十页奇幻小说或自创的古语语料库作为输入，char-rnn 即可自动学习其字符级的语言规律。\n- 模型能逐字符生成全新的铭文，不仅拼写符合设定，连句法结构和语气都完美复刻了训练数据的“古奥”风格。\n- 利用 GPU 加速训练后，几分钟内即可产出成千上万条不重复的高质量文本，极大降低了内容制作的时间成本。\n- 通过调整采样温度参数，开发者可以灵活控制生成内容的创造性与保守度，轻松获得从“晦涩难懂”到“朗朗上口”的不同变体。\n\nchar-rnn 的核心价值在于它能以极低的成本，将少量风格化文本转化为无限的同类创意内容，彻底解决了垂直领域小样本文本生成的难题。","https:\u002F\u002Foss.gittoolsai.com\u002Fimages\u002Fkarpathy_char-rnn_b415b5fe.png","karpathy","Andrej","https:\u002F\u002Foss.gittoolsai.com\u002Favatars\u002Fkarpathy_75f033eb.jpg","I like to train Deep Neural Nets on large datasets.",null,"Stanford","andrej.karpathy@gmail.com","https:\u002F\u002Ftwitter.com\u002Fkarpathy","https:\u002F\u002Fgithub.com\u002Fkarpathy",[86],{"name":87,"color":88,"percentage":89},"Lua","#000080",100,12020,2626,"2026-04-08T04:33:54",5,"Linux, macOS","可选。若使用 NVIDIA GPU 需安装 CUDA Toolkit 及 cutorch\u002Fcunn 包（可提速约 15 倍）；若使用 ATI\u002FAMD GPU 需安装 OpenCL 及 cltorch\u002Fclnn 包。具体显存大小取决于模型规模（例如 rnn_size=700, num_layers=3 需较大显存），未指定具体 CUDA 版本。","未说明（取决于数据集大小和模型规模，大数据集和大模型需要更多内存）",{"notes":98,"python":99,"dependencies":100},"1. 该工具基于 Lua 语言和 Torch 框架，而非 Python。2. 推荐使用 Ubuntu 系统，通过官方脚本安装 Torch。3. 数据集建议至少 2MB 以上以获得良好效果，若数据量大可增大 rnn_size 参数。4. 若在 GPU 上训练并在 CPU 上采样，需使用 convert_gpu_cpu_checkpoint.lua 脚本转换模型检查点文件。5. 默认使用 GPU 0，若使用 CPU 需设置 -gpuid -1 参数。","不适用 (该项目基于 Lua\u002FTorch，非 Python)",[101,102,103,104,105,106,107,108,109],"Torch","LuaRocks","nngraph","optim","nn","cutorch (GPU 模式)","cunn (GPU 模式)","cltorch (OpenCL 模式)","clnn (OpenCL 模式)",[15],"2026-03-27T02:49:30.150509","2026-04-09T01:26:00.502279",[114,119,124,129,134,139],{"id":115,"question_zh":116,"answer_zh":117,"source_url":118},25448,"如何确认模型正在使用 GPU 而不是 CPU 进行训练？","默认情况下，如果不指定 `-gpuid` 选项，程序会尝试使用 GPU。如果 `nvidia-smi` 显示 GPU 未被占用，可能是 Torch 的 CUDA 模块（cutorch\u002Fcunn）未正确编译或加载。解决方案包括：\n1. 确保已安装 `cutorch` 和 `cunn`：运行 `luarocks install cutorch` 和 `luarocks install cunn`。\n2. 检查编译环境：有时需要使用特定版本的 GCC（如 4.9 或 4.4）重新编译 Torch 的相关部分。\n3. 在 Torch REPL 中测试：输入 `require 'cutorch'`，如果报错则说明 CUDA 支持未生效，需检查驱动和 CUDA 版本兼容性。","https:\u002F\u002Fgithub.com\u002Fkarpathy\u002Fchar-rnn\u002Fissues\u002F89",{"id":120,"question_zh":121,"answer_zh":122,"source_url":123},25449,"为什么安装成功了 cutorch 和 cunn，但运行时仍提示 'package not found' 并回退到 CPU？","这通常是由于 CUDA 驱动版本不匹配或缺少必要的头文件导致的。常见解决方法：\n1. 升级 `cmake` 到最新版本。\n2. 检查 NVIDIA 驱动版本是否与安装的 CUDA 版本兼容（例如 GTS 250M 可能需要较旧的驱动）。\n3. 在 Kali Linux 等发行版上，可能缺少 `version.h` 等完整头文件，建议切换到 Ubuntu 等更稳定的发行版。\n4. 在 Torch 中手动运行 `require 'cutorch'` 查看具体错误信息（如 cuda runtime error 30），根据错误调整驱动或重装 CUDA。","https:\u002F\u002Fgithub.com\u002Fkarpathy\u002Fchar-rnn\u002Fissues\u002F37",{"id":125,"question_zh":126,"answer_zh":127,"source_url":128},25450,"运行命令时提示 'th: command not found' 怎么办？","这是因为 Torch 的可执行文件路径未添加到系统环境变量中。解决方法：\n1. 激活 Torch 环境：运行 `source ~\u002F.bashrc`（bash）、`source ~\u002F.zshrc`（zsh）或 `source ~\u002F.profile`。\n2. 如果仍未解决，尝试手动激活：`.\u002Finstall\u002Fbin\u002Ftorch-activate`（进入 torch 安装目录后）。\n3. 验证安装：运行 `.\u002Finstall\u002Fbin\u002Fth` 看是否进入 Torch 交互界面。\n4. 若问题依旧，建议删除旧安装 (`rm -rf ~\u002Ftorch`) 并严格按照官方文档重新安装。","https:\u002F\u002Fgithub.com\u002Fkarpathy\u002Fchar-rnn\u002Fissues\u002F209",{"id":130,"question_zh":131,"answer_zh":132,"source_url":133},25451,"在 GPU 上训练时报错 'ClassNLLCriterion.lua:34: bad argument #1 (field weights does not exist)' 如何解决？","此错误通常发生在 GPU 模式下，当 `ClassNLLCriterion` 期望有权重字段但实际数据未提供时。虽然在 CPU 模式（`-gpuid -1`）下能正常运行，但在 GPU 模式下对数据类型检查更严格。建议检查数据集预处理步骤，确保标签数据格式正确，或者在代码中显式初始化权重字段。此外，确保使用的 `torch\u002Fnn` 版本与 char-rnn 兼容，某些新版 `nn` 库的变更可能导致此问题。","https:\u002F\u002Fgithub.com\u002Fkarpathy\u002Fchar-rnn\u002Fissues\u002F76",{"id":135,"question_zh":136,"answer_zh":137,"source_url":138},25452,"序列长度（seq_length）增加导致梯度爆炸或不稳定怎么办？","梯度范数确实会随序列长度线性增长。虽然对于 RMSprop 优化器来说，按比例缩放梯度（如 `grad_params:div(opt.seq_length)`）在数学上可能被分母抵消从而效果不明显，但在其他优化器（如带动量的 SGD）中非常关键。为了代码的通用性和正确性，建议在 `train.lua` 的第 267 行附近添加 `grad_params:div(opt.seq_length)` 来归一化梯度，这样无论序列多长，梯度幅度都能保持相对稳定。","https:\u002F\u002Fgithub.com\u002Fkarpathy\u002Fchar-rnn\u002Fissues\u002F70",{"id":140,"question_zh":141,"answer_zh":142,"source_url":143},25453,"当前的 torch\u002Fnn 更新导致 CUDA 支持失效怎么办？","如果发现 `torch\u002Fnn` 的最新提交（如 PR #329）破坏了 char-rnn 的兼容性，导致 GPU 无法使用，临时解决方案是检出（checkout）一个较早版本的 `nn` 库。可以进入 `torch\u002Fpkg\u002Fnn` 目录，使用 git 回退到之前的稳定版本，直到 char-rnn 修复了相关兼容性问题。","https:\u002F\u002Fgithub.com\u002Fkarpathy\u002Fchar-rnn\u002Fissues\u002F71",[]]