RNN实践之唐诗创作

来源:互联网 发布:linux 查看系统时间 编辑:程序博客网 时间:2024/05/01 15:22

写在开头

两三天前,突然想用借助caffe做一下人脸检测,然而在我自己的电脑上连环境都没搭起来。所以想着还是回学校后用服务器跑吧。
这次,尝试先用RNN玩一下唐诗创作。

//update 201602100001本来是唐诗的,结果唐诗里面太多生僻字所以我刚刚从网上下载了一些歌词,想仿照参考资料【4】创作一首歌。不过,只有111KB,不知道训练模型够不够。同时,我傍晚训练唐诗时,因为买的服务器是1G1核的,根本难以训练,所以换了个4G的服务器。//当然,同样是只有CPU

安装Torch

买了台最便宜的云服务器,Ubuntu.
将torch装在/home/tom/torch/下;安装过程可以查看官方文档。

//新建用户tomuseradd tom -m -s /bin/bash//添加用户到sudo组sudo usermod -a -G sudo tom

由于我是刚买的服务器,可能在安装torch之前需要先执行以下命令

apt-get updateapt-get install curlapt-get install gitapt-get install cmake

安装过程中,出现了如下错误:

【错误1】

remote: Compressing objects: 100% (4/4), done.error: RPC failed; result=56, HTTP code = 200 | 924.00 KiB/sfatal: The remote end hung up unexpectedlyfatal: early EOFfatal: index-pack failedClone of 'https://github.com/torch/cutorch.git' into submodule path 'extra/cutorch' failed

解决方案为这个链接

git clone https://github.com/torch/distro.git ~/torch --recursive --depth 1

【错误2】

Error: Build error: Failed compiling object readline.o

解决方案为:

sudo apt-get install libreadline-dev

安装成功后输入th得到下图:

torch-install

Char-rnn

Char-rnn是一个基于RNN的字符级的语句生成工具。

这里的安装过程完全参照这个链接;作者是Andrej Karpathy,该作者同时写了一个介绍RNN的博客,可以访问这里查看。

按照上面安装了torch之后,就需要安装以下三个包。

$ luarocks install nngraph $ luarocks install optim$ luarocks install nn

训练过程中

creating vocabulary mapping...putting data into tensor...saving data/chinesepoetry/vocab.t7/home/tom/torch/install/bin/luajit: cannot open <data/chinesepoetry/vocab.t7> in mode  w at /home/tom/torch/pkg/torch/lib/TH/THDiskFile.c:640stack traceback:

这是权限错误

//终于可以训练了th train.lua -data_dir data/tinyshakespeare -rnn_size 112 -num_layers 2 -dropout 0.5

结果

//201602091728现在让它去训练吧,结果再说。//201602100011因为1唐诗中大量生僻字使得文本过于复杂;2之前那个云服务器只有1G内存。故重新训练。

训练了好几次,结果都是中文乱码,,,这个有点烦。
charset-err
简单来说,解决方案为,将input.txt的编码格式设置为下图(Notepad++)
UNIX UTF-8
charset


现在,终于成功了。
然而,惨不忍睹


linxi_lyric

参考资料

【1】torch
http://torch.ch/docs/getting-started.html
【2】char-rnn
https://github.com/karpathy/char-rnn
【3】让神经网络做唐诗
http://zhengwy.com/neural-network-for-tangshi/
【4】汪峰老师作词机
https://github.com/phunterlau/wangfeng-rnn

0 0