这次,尝试先用RNN玩一下唐诗创作。
//update 201602100001
本来是唐诗的,结果唐诗里面太多生僻字
所以我刚刚从网上下载了一些歌词,想仿照参考资料【4】创作一首歌。
不过,只有111KB,不知道训练模型够不够。
同时,我傍晚训练唐诗时,因为买的服务器是1G1核的,根本难以训练,所以换了个4G的服务器。
//当然,同样是只有CPU
安装Torch
买了台最便宜的云服务器,Ubuntu.
将torch装在/home/tom/torch/下;安装过程可以查看官方文档。
//新建用户tom useradd tom -m -s /bin/bash //添加用户到sudo组
sudo usermod -a -G sudo tom
由于我是刚买的服务器,可能在安装torch之前需要先执行以下命令
apt-get update
apt-get install curl
apt-get install git
apt-get install cmake
安装过程中,出现了如下错误:
【错误1】
remote: Compressing objects: 100% (4/4), done.
error: RPC failed; result=56, HTTP code = 200 | 924.00 KiB/s
fatal: The remote end hung up unexpectedly
fatal: early EOF
fatal: index-pack failed
Clone 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得到下图:
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:640
stack traceback:
这是权限错误。
//终于可以训练了
th train.lua -data_dir data/tinyshakespeare -rnn_size 112 -num_layers 2 -dropout 0.5
结果
//201602091728
现在让它去训练吧,结果再说。
//201602100011
因为1唐诗中大量生僻字使得文本过于复杂;2之前那个云服务器只有1G内存。故重新训练。
训练了好几次,结果都是中文乱码,,,这个有点烦。
简单来说,解决方案为,将input.txt的编码格式设置为下图(Notepad++)
UNIX UTF-8
现在,终于成功了。
然而,惨不忍睹。
参考资料
【1】torch
Torch | Getting started with Torch
【2】char-rnn
GitHub - karpathy/char-rnn: Multi-layer Recurrent Neural Networks (LSTM, GRU, RNN) for character-level language models in Torch
【3】让神经网络做唐诗
http://zhengwy.com/neural-network-for-tangshi/
【4】汪峰老师作词机
GitHub - phunterlau/wangfeng-rnn: Multi-layer RNN building Wang Feng style lyric