1. 環境は、Window 10 Home (64bit) 上で行った。
2. Anaconda3 (64bit) – Spyder上で、動作確認を行った。
3. python の バージョンは、python 3.7.0 である。
4. pytorch の バージョンは、pytorch 0.4.1 である。
5. GPU は, NVIDIA社 の GeForce GTX 1050 である。
6. CPU は, Intel社 の Core(TM) i7-7700HQ である。
今回確認した内容は、現場で使える! PyTorch開発入門 深層学習モデルの作成とアプリケーションへの実装 (AI & TECHNOLOGY) の 5.4 RNNによる文章生成 (P.126 – P.134) である。
※1. プログラムの詳細は, 書籍を参考(P.126 – P.134)にして下さい.
※2. 動作確認時に躓いた箇所も, コメントした.
■関数(str2ints, ints2str)の動作確認(書籍から一部抜粋・加筆).
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
# -*- coding: utf-8 -*- # 1. library import. from __future__ import print_function import string ~(略)~ # 3. sample output. s = "This is a test character! is this string is checking for test? \ This string consists of test character strings, wow!" print('-string -> integers list---------------------------------------') print(str2ints(s, vocab_dict)) x = [55, 17, 18, 28, 94, 18, 28, 94, 10, 94, 29, 14, 28, 29, 94, 12, 17, 10, 27, 10, 12, 29, 14, 27, 62, 94, 18, 28, 94, 29, 17, 18, 28, 94, 28, 29, 27, 18, 23, 16, 94, 18, 28, 94, 12, 17, 14, 12, 20, 18, 23, 16, 94, 15, 24, 27, 94, 29, 14, 28, 29, 82, 94, 55, 17, 18, 28, 94, 28, 29, 27, 18, 23, 16, 94, 12, 24, 23, 28, 18, 28, 29, 28, 94, 24, 15, 94, 29, 14, 28, 29, 94, 12, 17, 10, 27, 10, 12, 29, 14, 27, 94, 28, 29, 27, 18, 23, 16, 28, 73, 94, 32, 24, 32, 62] print('-integers list -> string---------------------------------------') print(ints2str(x, all_chars)) |
■実行結果.
1 2 3 4 |
-string -> integers list--------------------------------------- [55, 17, 18, 28, 94, 18, 28, 94, 10, 94, 29, 14, 28, 29, 94, 12, 17, 10, 27, 10, 12, 29, 14, 27, 62, 94, 18, 28, 94, 29, 17, 18, 28, 94, 28, 29, 27, 18, 23, 16, 94, 18, 28, 94, 12, 17, 14, 12, 20, 18, 23, 16, 94, 15, 24, 27, 94, 29, 14, 28, 29, 82, 94, 55, 17, 18, 28, 94, 28, 29, 27, 18, 23, 16, 94, 12, 24, 23, 28, 18, 28, 29, 28, 94, 24, 15, 94, 29, 14, 28, 29, 94, 12, 17, 10, 27, 10, 12, 29, 14, 27, 94, 28, 29, 27, 18, 23, 16, 28, 73, 94, 32, 24, 32, 62] -integers list -> string--------------------------------------- This is a test character! is this string is checking for test? This string consists of test character strings, wow! |
■RNNによる文章生成(書籍から一部抜粋・加筆).
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# -*- coding: utf-8 -*- # 1. library import. from __future__ import print_function import torch, string, os, time from torch import nn, optim from torch.utils.data import (Dataset, DataLoader) from tqdm import tqdm from statistics import mean ~(略)~ # string -> integers list. # KeyError: '·' # -> 理由: tinyshakespeare.txt の 保管方法誤り. # https://github.com/karpathy/char-rnn/tree/master/data/tinyshakespeare # -> 上記サイトに表示されている input.txt を, [右クリック] - [名前を付けてリンク先を保存] # で, download すると, 大量のhtmlタグが含まれたものが download されてしまう. # # https://raw.githubusercontent.com/karpathy/char-rnn/master/data/tinyshakespeare/input.txt # -> 上記サイトを表示後, [右クリック] - [名前を付けて保存] とすることで, KeyError が解消した. ~(略)~ # 6. create Dataset Class. start = time.time() folder_path = os.path.expanduser('~') folder_path = folder_path + '\\.spyder-py3\\pytorch\\tinyshakespeare\\' # karpathy/char-rnn # https://github.com/karpathy/char-rnn/tree/master/data/tinyshakespeare ds = ShakespeareDataset(folder_path + "\\tinyshakespeare.txt", chunk_size = 200) loader = DataLoader(ds, batch_size = 32, shuffle = True, num_workers = 0) # 7. training. # TypeError: __init__() got an unexpected keyword argument 'num_layers' # -> ShakespeareDataset と 記載していたことが原因. # -> 全く別物の名前が似ているクラス ShakespeareDataset, SequenceGenerationNet を使う場合に, 要注意. net = SequenceGenerationNet(vocab_size, 20, 50, num_layers = 2, dropout = 0.1) ~(略)~ for epoch in range(50): net.train() losses = [] for data in tqdm(loader): ~(略)~ # RuntimeError: cudnn RNN backward can only be called in training mode. # -> 以下の通り, error原因は, コーディングに関するインデント誤りによる error. # -> error内容 の イメージとしては, # loader の ループ処理中(訓練中)に, train mode で設定されていた net が, # generate_seq関数 の 呼び出しで, eval modeに切り替わってしまったため, # loss.backward() の 処理で失敗したと推測している. # -> epoch単位 で, # epoch 0: train mode -> eval mode # epoch 1: train mode -> eval mode # ... # のように, net の mode が, 切り替わるのが正しい動作に見える. # print(epoch, mean(losses)) # with torch.no_grad(): # print(generate_seq(net, device = "cuda:0")) # -> 書籍を確認して修正したところ, 解消した. print('--------------------------------------------------') print('epoch: ' + str(epoch) + ' mean(loss): ' + str(mean(losses))) with torch.no_grad(): print(generate_seq(net, device = "cuda:0")) # 8. display processing time. end = time.time() print('--------------------------------------------------') print('Elapsed Time: ' + str(end - start) + "[sec]") |
■実行結果(epoch = 50).
文章と言われてみれば, 見えなくも無さそうだが, 精度は, まだまだ低そうに見える.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
~(略)~ -------------------------------------------------- epoch: 47 mean(loss): 1.6681576177052089 The King said stopating now toold; As joy whope great sholojy skeee ack: now fe't will A senough now us iT the have of his blict Scary, as this reis thee are will soverness Come, kind fathith me here of more ourh yo 100%|██████████| 175/175 [00:04<00:00, 35.31it/s] -------------------------------------------------- epoch: 48 mean(loss): 1.6644004263196672 The King said At traight is our fout noth and have To thy ender, your part; but that lord; as niverous fear; An than dake sugh he seemens me in by trant near to much here? ROMEO: MENENIUS: I'ch you I ranter, the w 100%|██████████| 175/175 [00:04<00:00, 35.11it/s] -------------------------------------------------- epoch: 49 mean(loss): 1.6615660510744368 The King said to lordter hiskiendy and Nor I will more, Like in tractor of his merner; but conserys sming but them Worlouth a fine, Now thy chiresheble in her Granter, Sciren, Are pardutersly, I hope dequricks. Ever -------------------------------------------------- Elapsed Time: 269.3071098327637[sec] |
■ハマった現象 & 解決策.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
1. str2ints関数におけるエラー. KeyError: '·' -> 本件は, 参照URL① から, input.txt を, [右クリック] - [名前を付けてリンク先を保存] した ことにより, 大量のhtmlタグが含まれたものが download されていたことが原因だった. -> 参照URL② を 表示させて, 表示後, [右クリック] - [名前を付けて保存] することで, KeyError が解消した. 2. netに関するエラー. TypeError: __init__() got an unexpected keyword argument 'num_layers' -> 本件は, net に 代入するクラスを, ShakespeareDataset としていたことが原因. -> net に 代入するクラスを, 書籍通りに, SequenceGenerationNet に修正して解消した. ※全く別物の名前が似ているクラスを取り扱う場合に要注意. 3. 訓練時のエラー. RuntimeError: cudnn RNN backward can only be called in training mode. -> 本件は, コーディング時のインデント誤りが原因. -> インデントを修正して解消した. |
■参照サイト
【参照URL①】karpathy/char-rnn
【参照URL②】tinyshakespeare.txt の 内容
■参考書籍
現場で使える! PyTorch開発入門 深層学習モデルの作成とアプリケーションへの実装 (AI & TECHNOLOGY)