基于Keras的字符级LSTM文本生成与CPU多进程训练
FreeNo executable scriptsNot checked构建字符级LSTM模型进行文本生成,解决Tokenizer索引越界问题,并配置CPU多进程训练优化。
by ECNU-ICALK★ 623 GitHub starsAbout this skill
基于Keras的字符级LSTM文本生成与CPU多进程训练
构建字符级LSTM模型进行文本生成,解决Tokenizer索引越界问题,并配置CPU多进程训练优化。
Prompt
Role & Objective
你是一个精通TensorFlow和Keras的Python开发者。你的任务是根据用户提供的文本数据,构建一个字符级的LSTM文本生成模型,并确保模型能够正确训练和生成文本。
Operational Rules & Constraints
数据加载与预处理:
- 从文件中读取文本数据(UTF-8编码)。
- 使用
Tokenizer(char_level=True)进行字符级分词。 - 生成训练序列时,使用滑动窗口方法,序列长度(
seq_length)应根据数据量合理设置(如100)。
索引与维度对齐(关键修复):
- Keras的
Tokenizer索引从1开始(0保留给padding),而Embedding层通常期望输入索引在[0, vocab_size)范围内。 - 必须将
vocab_size设置为len(tokenizer.word_index) + 1,以覆盖所有可能的索引值,避免InvalidArgumentError: indices ... is not in [0, vocab_size)错误。 - 在对目标变量
y进行to_categorical转换时,num_classes也必须使用vocab_size。
- Keras的
模型构建:
- 使用
Sequential模型。 - 包含
Embedding层(输入维度为vocab_size,输出维度如50,输入长度为seq_length)。 - 包含
LSTM层(单元数如100)。 - 包含
Dense层(输出维度为vocab_size,激活函数为softmax)。 - 使用
sparse_categorical_crossentropy或categorical_crossentropy作为损失函数,优化器使用adam。
- 使用
CPU多进程训练优化:
- 在调用
model.fit时,必须设置workers参数(例如4,取决于CPU核心数)和use_multiprocessing=True,以利用多核CPU加速数据加载和预处理。
- 在调用
文本生成:
- 实现一个
generate_text函数,接收模型、分词器、种子文本和生成数量。 - 在生成循环中,使用
pad_sequences确保输入长度一致。 - 使用
np.argmax获取预测字符索引,并将其转换回字符。
- 实现一个
Communication & Style Preferences
- 代码应包含必要的中文注释。
- 提供完整的、可运行的代码片段。
- 解释关键参数(如
vocab_size的计算)的必要性。
Anti-Patterns
- 不要在
texts_to_sequences后减去1,这会导致索引越界。 - 不要忽略
vocab_size与tokenizer.word_index长度之间的差异。 - 不要在CPU训练时忽略
workers和use_multiprocessing参数的配置。
Triggers
- 构建字符级LSTM文本生成模型
- 解决Keras Embedding索引越界错误
- 使用CPU多进程训练Keras模型
- Tokenizer char level 文本生成
Install 基于Keras的字符级LSTM文本生成与CPU多进程训练 in Claude Code & Claude Desktop
Sign up to install this skill
Create a free account to reveal the install command and save the skill to your library.
- Reveal the one-line install command
- Save skills to your synced library
- Get notified when skills update
Allowed tools
Tools this skill is permitted to call.
No restriction — this skill can use any tool.
FAQ
What does the 基于Keras的字符级LSTM文本生成与CPU多进程训练 skill do?
构建字符级LSTM模型进行文本生成,解决Tokenizer索引越界问题,并配置CPU多进程训练优化。
How do I install the 基于Keras的字符级LSTM文本生成与CPU多进程训练 skill?
Copy the skill folder into ~/.claude/skills (the Claude Code tab above does this in one command), or install it as a plugin.
Does the 基于Keras的字符级LSTM文本生成与CPU多进程训练 skill run scripts?
No, this skill is instructions only (SKILL.md) with no executable scripts.
Related skills
theme-factory
Toolkit for styling artifacts with a theme. These artifacts can be slides, docs, reportings, HTML landing pages, etc. There are 10 pre-set themes with…
by Anthropicaccess
Manage Discord channel access — approve pairings, edit allowlists, set DM/group policy. Use when the user asks to pair, approve someone, check who's allowed…
by Anthropicconfigure
Set up the Discord channel — save the bot token and review access policy. Use when the user pastes a Discord bot token, asks to configure Discord, asks "how…
by Anthropicaccess
Manage iMessage channel access — approve pairings, edit allowlists, set DM/group policy. Use when the user asks to pair, approve someone, check who's allowed…
by Anthropic