继续积累lstm的理论知识
参考链接:
1.生成笑话:
[Pytorch实现基于LSTM的文本生成示例](https://www.yanxishe.com/TextTranslation/2699)
# [PyTorch LSTM: Text Generation Tutorial](https://www.kdnuggets.com/2020/07/pytorch-lstm-text-generation-tutorial.html)
# [PyTorch LSTM: Text Generation Tutorial](https://closeheat.com/blog/pytorch-lstm-text-generation-tutorial)
这三篇文章说的是一个
2.[Text Generation using Python](https://www.analyticsvidhya.com/blog/2018/03/text-generation-using-python-nlp/)
这里使用多是keras做深度学习写诗歌。
等同 [如何利用深度学习写诗歌(使用Python进行文本生成)](https://mp.weixin.qq.com/s/E9RZuBfom59eXFh76B-dCg)
学习LSTM [Essentials of Deep Learning : Introduction to Long Short Term Memory](https://www.analyticsvidhya.com/blog/2017/12/fundamentals-of-deep-learning-introduction-to-lstm/)
3.# P[ytorch的LSTM的理解](https://zhuanlan.zhihu.com/p/41261640)
4.# [人人都能看懂的LSTM介绍及反向传播算法推导](https://zhuanlan.zhihu.com/p/83496936)
5. [pytorch教程](https://www.w3cschool.cn/pytorch/pytorch-5yam3bof.html)
6.关于pytorch参数 [pytorch笔记:构建LSTM网络,实现训练验证和测试过程](https://blog.csdn.net/Leon_winter/article/details/92592622)
7.[PyTorch官方教程中文版](https://pytorch.panchuang.net/) 查看# 序列模型和长短句记忆(LSTM)模型
8.[pytorch加载数据集](https://pytorch.org/tutorials/beginner/data_loading_tutorial.html#dataset-class)
9.pytorch[ # Dataset和DataLoader加载数据集](https://blog.csdn.net/qq_38237214/article/details/109559791)
好的以上就是前期知识积累
代码1.文本向量化
复现的代码是[第一个链接:笑话生成](https://www.yanxishe.com/TextTranslation/2699)
按照数据进行加载
import pandas as pd
from collections import Counter # 统计词频
# from torch.utils.data import Dataset # Dataset是个抽象类,只能用于继承 类的名称一样了,先按照源码搞吧
from torch.utils.data import DataLoader # DataLoader需实例化,用于加载数据
class Dataset(torch.utils.data.Dataset):
def __init__(
self,
args,
):
self.args = args
self.words = self.load_words() # 返回 整个txt文本的字符的列表
self.uniq_words = self.get_uniq_words() # 统计列表文本的词频
self.index_to_word = {index: word for index, word in enumerate(self.uniq_words)} # 索引和单词的字典
self.word_to_index = {word: index for index, word in enumerate(self.uniq_words)} # 单词和索引的字典
self.words_indexes = [self.word_to_index[w] for w in self.words] # 将整个文本 的每个单词找到对应的词表的索引位置,成为一个大的列表
# 这一步其实可以放到类的外面
def load_words(self):
train_df = pd.read_csv('./data_text/reddit-cleanjokes.csv')
text = train_df['Joke'].str.cat(sep=' ')
return text.split(' ')
def get_uniq_words(self):
word_counts = Counter(self.words)
return sorted(word_counts, key=word_counts.get, reverse=True)
def __len__(self):
return len(self.words_indexes) - self.args.sequence_length
def __getitem__(self, index):
return (
torch.tensor(self.words_indexes[index:index+self.args.sequence_length]),
torch.tensor(self.words_indexes[index+1:index+self.args.sequence_length+1]),
)
追行解析:
使用 DataLoader加载数据
Argparse 教程
[文档链接](https://docs.python.org/zh-cn/3/howto/argparse.html)
import argparse
parser = argparse.ArgumentParser()
parser.parse_args()
在pycharm中正常
我总结了一下,需要再命令行上运行
遇事不明,谷歌一下吧。
总结:
源代码中的这个args是个字典即可:
带“--” 需要该参数这样运行:
长见识了
关于继承dataset类中
这一步我不是很明白。
有的人是除法
下面就是模型的训练和评估
下次更新