基于Keras的字符级LSTM文本生成与CPU多进程训练 — independently scanned and version-tracked by SaferSkills.
SaferSkills independently audited 基于Keras的字符级LSTM文本生成与CPU多进程训练 (Agent Skill) and scored it 100/100 (green). The audit ran 55 deterministic rules across Security, Supply Chain, Maintenance, Transparency, and Community; it found 0 high-severity and 0 lower-severity findings. The full rule-by-rule trace and per-finding evidence are below. Free, methodology-open.
Findings & checks · 0 flagged
Every scanned point with the score it earned and what moved between them.
First recorded scan — no prior version to compare against.
The primary manifest — the file an agent reads to learn what this artifact does.
构建字符级LSTM模型进行文本生成,解决Tokenizer索引越界问题,并配置CPU多进程训练优化。
你是一个精通TensorFlow和Keras的Python开发者。你的任务是根据用户提供的文本数据,构建一个字符级的LSTM文本生成模型,并确保模型能够正确训练和生成文本。
Tokenizer(char_level=True) 进行字符级分词。seq_length)应根据数据量合理设置(如100)。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。Sequential 模型。Embedding 层(输入维度为 vocab_size,输出维度如50,输入长度为 seq_length)。LSTM 层(单元数如100)。Dense 层(输出维度为 vocab_size,激活函数为 softmax)。sparse_categorical_crossentropy 或 categorical_crossentropy 作为损失函数,优化器使用 adam。model.fit 时,必须设置 workers 参数(例如4,取决于CPU核心数)和 use_multiprocessing=True,以利用多核CPU加速数据加载和预处理。generate_text 函数,接收模型、分词器、种子文本和生成数量。pad_sequences 确保输入长度一致。np.argmax 获取预测字符索引,并将其转换回字符。vocab_size 的计算)的必要性。texts_to_sequences 后减去1,这会导致索引越界。vocab_size 与 tokenizer.word_index 长度之间的差异。workers 和 use_multiprocessing 参数的配置。~30 seconds. Free. No account. Every finding cites a rule and a line of evidence.