一 搭建Torch 环境
1 在命令行依次执行下列命令:
# in a terminal, run the commands
curl -sk https://raw.githubusercontent.com/torch/ezinstall/master/install-deps | bash
git clone https://github.com/torch/distro.git ~/torch --recursive
cd ~/torch; ./install.sh
2 执行下列命令,使得安装过程中被更改的PATH生效:
source ~/.bashrc
3 执行下列命令查看torch是否安装成功:
$ th
______ __ | Torch7
/_ __/__ ________/ / | Scientific computing for Lua.
/ / / _ \/ __/ __/ _ \ |
/_/ \___/_/ \__/_//_/ | https://github.com/torch
| http://torch.ch
th> torch.Tensor{1,2,3}
1
2
3
[torch.DoubleTensor of dimension 3]
th>
注:(1)如果不使用torch,可以使用下列命令卸载:
rm -rf ~/torch
(2) 可以使用luarocks命令安装其他的torch包:
$ luarocks install image
$ luarocks list
二 运行char-cnn
1 执行下列命令安装必要的包:
$ luarocks install nngraph
$ luarocks install optim
2 如果使用CUDA GPU加速,则在安装好cuda toolkit后,执行下列命令安装相应的包:
$ luarocks install cutorch
$ luarocks install cunn
3 执行下列命令训练模型:
$ th train.lua -data_dir data/<code>tinyshakespeare</code> -gpuid -1
其中(1)文本数据input.txt存放在
data/tinyshakepeare目录下
(2)-gpuid -1表示使用cpu进行训练,-gpuid 0表示用gpu训练(使用gpu训练确实比cpu快好多,推荐使用gpu)
4 上述训练在cv文件夹下生成类似
lm_lstm_epoch0.95_2.0681.t7
的checkpoint文件,其中2.0681表示validation loss,该值小,则效果好,最后生成samples时选用validation loss最小的checkpoint文件,生成samples文件的命令如下所示:
h sample.lua cv/lm_lstm_epoch18.91_1.3895.t7 -gpuid 0
参考资料:
char-cnn:
https://github.com/karpathy/char-rnn
Torch:
http://torch.ch/docs/getting-started.html
The Unreasonable Effectiveness of Recurrent Neural Networks:
http://karpathy.github.io/2015/05/21/rnn-effectiveness/
汪峰老师作词机终于填坑了
http://phunters.lofter.com/post/86d56_732209b