RNN實踐之唐詩創作

寫在開頭

兩三天前,突然想用藉助caffe做一下人臉檢測,然而在我自己的電腦上連環境都沒搭起來。所以想着還是回學校後用服務器跑吧。
這次,嘗試先用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得到下圖:

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:640
stack 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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章