from common.optimizer import SGD from common.trainer import RnnlmTrainer from dataset import ptb from ch05.simple_rnnlm import SimpleRnnlm # 하이퍼파라미터 설정 batch_size = 10 wordvec_size = 100 hidden_size = 100 # RNN의 은닉 상태 벡터의 원소 수 time_size = 5 # RNN을 펼치는 크기 lr = 0.1 max_epoch = 100 # 학습 데이터 읽기 corpus, word_to_id, id_to_word = ptb.load_data('train') corpus_size = 1000 # 테스트 데이터셋을 작게 설정 corpus = corpus[:corpus_size] vocab_size = int(max(corpus) + 1) xs = corpus[:-1] # 입력 ts = corpus[1:] # 출력(정답 레이블) # 모델 생성 model = SimpleRnnlm(vocab_size, wordvec_size, hidden_size) optimizer = SGD(lr) trainer = RnnlmTrainer(model, optimizer) trainer.fit(xs, ts, max_epoch, batch_size, time_size) trainer.plot()
corpus, word_to_id, id_to_word = ptb.load_data('train') corpus_val, _, _ = ptb.load_data('val') corpus_test, _, _ = ptb.load_data('test') if config.GPU: corpus = to_gpu(corpus) corpus_val = to_gpu(corpus_val) corpus_test = to_gpu(corpus_test) vocab_size = len(word_to_id) xs = corpus[:-1] ts = corpus[1:] model = BetterRnnlm(vocab_size, wordvec_size, hidden_size, dropout) optimizer = SGD(lr) trainer = RnnlmTrainer(model, optimizer) best_ppl = float('inf') for epoch in range(max_epoch): trainer.fit(xs, ts, max_epoch=1, batch_size=batch_size, time_size=time_size, max_grad=max_grad) model.reset_state() ppl = eval_perplexity(model, corpus_val) print('valid perplexity: ', ppl) if best_ppl > ppl: best_ppl = ppl model.save_params() else: lr /= 4.0
time_size = 35 # RNNを展開するサイズ lr = 20.0 max_epoch = 4 max_grad = 0.25 # 学習データの読み込み corpus, word_to_id, id_to_word = ptb.load_data('train') corpus_test, _, _ = ptb.load_data('test') vocab_size = len(word_to_id) xs = corpus[:-1] ts = corpus[1:] # モデルの生成 model = Rnnln(vocab_size, wordvec_size, hidden_size) optimizer = SGD(lr) trainer = RnnlmTrainer(model, optimizer) # 勾配クリッピングを適用して学習 trainer.fit(xs, ts, max_epoch, batch_size, time_size, max_grad, eval_interval=20) trainer.plot(ylim=(0, 500)) # テストデータで評価 model.reset_state() ppl_test = eval_perplexity(model, corpus_test) print('test perplexity: ', ppl_test) # パラメーターの保存 model.save_params() # 結果 # test perplexity: 134.61532778421517
import sys sys.path.append('..') from common.optimizer import SGD from common.trainer import RnnlmTrainer from dataset import ptb from simple_rnnlm import SimpleRnnlm # ハイパーパラメータの設定 batch_size = 10 wordvec_size = 100 hidden_size = 100 # RNNの隠れ状態ベクトルの要素数 time_size = 5 # RNNを展開するサイズ lr = 0.1 max_epoch = 100 # 学習データの読み込み corpus, word_to_id, id_to_word = ptb.load_data('train') corpus_size = 1000 # テスト用にデータセットを小さくする corpus = corpus[:corpus_size] vocab_size = int(max(corpus) + 1) xs = corpus[:-1] # 入力 ts = corpus[1:] # 出力(教師ラベル) # モデルの生成 model = SimpleRnnlm(vocab_size, wordvec_size, hidden_size) optimizer = SGD(lr) trainer = RnnlmTrainer(model, optimizer) trainer.fit(xs, ts, max_epoch, batch_size, time_size) trainer.plot(path='../output/05-train.png')
sys.path.append('..') from common.optimizer import SGD from common.trainer import RnnlmTrainer # RnnlmTrainer 는 RNNLM 의 학습을 클래스 안으로 숨겨준다. from dataset import ptb from simple_rnnlm import SimpleRnnlm # 하이퍼파라미터 설정 batch_size = 10 wordvec_size = 100 hidden_size = 100 # RNN의 은닉 상태 벡터의 원소 수 time_size = 5 # RNN을 펼치는 크기 lr = 0.1 max_epoch = 100 # 학습 데이터 읽기 corpus, word_to_id, id_to_word = ptb.load_data('train') corpus_size = 1000 # 테스트 데이터셋을 작게 설정 corpus = corpus[:corpus_size] vocab_size = int(max(corpus) + 1) xs = corpus[:-1] # 입력 ts = corpus[1:] # 출력(정답 레이블) # 모델 생성 model = SimpleRnnlm(vocab_size, wordvec_size, hidden_size) optimizer = SGD(lr) trainer = RnnlmTrainer(model, optimizer) # 먼저 model, optimzer 을 주어 초기화 trainer.fit(xs, ts, max_epoch, batch_size, time_size) # fit 메서드를 통해 학습을 수행한다. trainer.plot()
from dataset import ptb from rnnlm import Rnnlm batch_size = 20 wordvec_size = 100 hidden_size = 100 time_size = 35 lr = 20.0 max_epoch = 4 max_grad = 0.25 corpus, word_to_id, id_to_word = ptb.load_data('train') corpus_test, _, _ = ptb.load_data('test') vocab_size = len(word_to_id) xs = corpus[:-1] ts = corpus[1:] model = Rnnlm(vocab_size, wordvec_size, hidden_size) optimizer = SGD(lr) trainer = RnnlmTrainer(model, optimizer) trainer.fit(xs, ts, max_epoch, batch_size, time_size, max_grad, eval_interval=20) trainer.plot(ylim=(0, 500), path='../output/06-train.png') model.reset_state() ppl_test = eval_perplexity(model, corpus_test) print('Test perplexity: ', ppl_test) model.save_params()