Skip to content

Commit eed1aa3

Browse files
author
xyliao
committed
update
1 parent b2b4fe0 commit eed1aa3

File tree

12 files changed

+79693
-2
lines changed

12 files changed

+79693
-2
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ Learn Deep Learning with PyTorch
8181
- Deep Dream: 探索卷积网络眼中的世界
8282

8383
- Chapter 10: 自然语言处理
84-
- [Char RNN 实现文本生成](https://github.com/SherlockLiao/code-of-learn-deep-learning-with-pytorch/blob/master/chapter10_Natural-Language-Process/Char-RNN/)
84+
- [Char RNN 实现文本生成](https://github.com/SherlockLiao/code-of-learn-deep-learning-with-pytorch/blob/master/chapter10_Natural-Language-Process/char_rnn/)
8585
- Image Caption: 实现图片字幕生成
8686
- seq2seq 实现机器翻译
8787
- cnn + rnn + attention 实现文本识别
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
# Char-RNN-PyTorch
2+
使用字符级别的RNN进行文本生成,使用PyTorch框架。[Gluon实现](https://github.com/SherlockLiao/Char-RNN-Gluon)
3+
4+
## Requirements
5+
[PyTorch 0.3](http://pytorch.org/)
6+
7+
[MxTorch](https://github.com/SherlockLiao/mxtorch)
8+
9+
[tensorboardX](https://github.com/lanpa/tensorboard-pytorch)
10+
11+
按照 pytorch 官网安装 pytorch,将 mxtorch 下载下来,放到根目录,安装 tensorboardX 实现 tensorboard 可视化
12+
13+
```bash
14+
\Char-RNN-PyTorch
15+
\mxtorch
16+
\data
17+
\dataset
18+
\models
19+
config.py
20+
main.py
21+
```
22+
23+
24+
25+
### 训练模型
26+
27+
所有的配置文件都放在 config.py 里面,通过下面的代码来训练模型
28+
29+
```bash
30+
python main.py train
31+
```
32+
33+
也可以在终端修改配置
34+
35+
```bash
36+
python main.py train \
37+
--txt='./dataset/poetry.txt' \ # 训练用的txt文本
38+
--batch=128 \ # batch_size
39+
--max_epoch=300 \
40+
--len=30 \ # 输入RNN的序列长度
41+
--max_vocab=5000 \ # 最大的字符数量
42+
--embed_dim=512 \ # 词向量的维度
43+
--hidden_size=512 \ # 网络的输出维度
44+
--num_layers=2 \ # RNN的层数
45+
--dropout=0.5
46+
```
47+
48+
如果希望使用训练好的网络进行文本生成,使用下面的代码
49+
50+
```bash
51+
python main.py predict \
52+
--begin='天青色等烟雨' \ # 生成文本的开始,可以是一个字符,也可以一段话
53+
--predict_len=100 \ # 希望生成文本的长度
54+
--load_model='./checkpoints/CharRNN_best_model.pth' # 读取训练模型的位置
55+
```
56+
57+
## Result
58+
如果使用古诗的数据集进行训练,可以得到下面的结果
59+
60+
```bash
61+
天青色等烟雨翩 黄望堪魄弦夜 逐奏文明际天月辉 豪天明月天趣 天外何山重满 遥天明上天 心空游无拂天外空寂室叨
62+
```
63+
64+
如果使用周杰伦的歌词作为训练集,可以得到下面的结果
65+
66+
```bash
67+
这感觉得可能 我这玻童来 城堡药比生对这些年风天 脚剧飘逐在尘里里步的路 麦缘日下一经经 听觉得远回白择
68+
```

0 commit comments

Comments
 (0)