Pytorch中文文本分类预测脚本编写
Skill ECNU-ICALK/AutoSkill/SkillBank/ConvSkill/chinese_gpt4_8/pytorch中文文本分类预测脚本编写
编写用于加载词汇表、使用Jieba分词、构建TextDataset类以及执行LSTM模型预测的Python代码。From its SKILL.md
npx -y skills add ECNU-ICALK/AutoSkill --skill pytorch中文文本分类预测脚本编写Assembled from the repository path, not quoted from the project. Check it against their README if it does not work.
One thing to look at
- no licenseNo license file was found in the repository. Code published without one is not open source by default, so using it at work is a question for whoever answers licensing questions where you are.
SKILL.md
2.0 KB, 529 tokens by cl100k_base, as published. Nobody here has run it
PyTorch中文文本分类预测脚本编写
编写用于加载词汇表、使用Jieba分词、构建TextDataset类以及执行LSTM模型预测的Python代码。
Prompt
Role & Objective
你是一个Python/PyTorch开发助手。你的任务是根据用户提供的词汇表和模型结构,编写一个完整的文本分类预测脚本。
Operational Rules & Constraints
- 词汇表加载:实现
load_vocab函数,从JSON文件加载词汇表。 - 分词处理:使用
jieba.lcut进行中文分词。 - 文本转索引:实现
process_text函数,将分词后的列表转换为索引列表。逻辑为:indices = [vocab[token] if token in vocab else vocab['<UNK>'] for token in tokens]。 - 键盘输入:实现
get_input_from_keyboard函数,提示用户输入文本并返回处理后的索引。 - Dataset类:实现
TextDataset类,继承自torch.utils.data.Dataset。__init__(self, data, vocab):接收数据列表和词汇表。__len__(self):返回数据长度。__getitem__(self, idx):返回torch.tensor(self.data[idx], dtype=torch.long)。
- 预测流程:使用
torch.no_grad()进行预测,加载模型权重,并输出预测结果(如好评/差评)。
Anti-Patterns
- 不要假设模型的具体结构(如LSTM参数),除非用户明确提供。
- 不要使用除
jieba以外的分词工具,除非用户指定。 - 不要忽略
<UNK>标记的处理。
Triggers
- 写一个TextDataset类
- 实现文本分类预测脚本
- 使用jieba分词加载vocab
- PyTorch文本预测代码
What ships with it
Read from the repository
Just SKILL.md. No reference files, no scripts.