sys.path.append('..') 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_size = 1000 corpus = corpus[:corpus_size] vocab_size = int(max(corpus) + 1) xs = corpus[:-1] ts = corpus[1:] data_size = len(xs) print('corpus size: %d, vocabulary size: %d' % (corpus_size, vocab_size)) max_iters = data_size // (batch_size * time_size) time_idx = 0 total_loss = 0 loss_count = 0 ppl_list = [] model = SimpleRnnlm(vocab_size, wordvec_size, hidden_size) optimizer = SGD(lr) jump = (corpus_size - 1) // batch_size offsets = [i * jump for i in range(batch_size)] for epoch in range(max_epoch): for iter in range(max_iters): batch_x = np.empty((batch_size, time_size), dtype='i') batch_t = np.empty((batch_size, time_size), dtype='i') for t in range(time_size): for i, offset in enumerate(offsets): batch_x[i, t] = xs[(offset + time_idx) % data_size] batch_t[i, t] = ts[(offset + time_idx) % data_size] time_idx += 1