예제 #1
0
def main():
    batch_size = 20
    wordvec_size = 650
    hidden_size = 650
    time_size = 35
    lr = 20.0
    #max_epoch = 40
    max_epoch = 1
    max_grad = 0.25
    dropout = 0.5

    corpus, word_to_id, _ = ptb.load_data('train')
    corpus_val, _, _ = ptb.load_data('val')
    corpus_test, _, _ = ptb.load_data('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 _ in range(max_epoch):
        trainer.fit(xs,
                    ts,
                    1,
                    batch_size,
                    time_size,
                    max_grad,
                    eval_interval=20)
        model.reset_state()

        ppl = eval_perplexity(model, corpus_val)
        print(f'valid perplexity: {ppl}')

        if best_ppl > ppl:
            best_ppl = ppl
            model.save_params()
        else:
            lr /= 4.0
            optimizer.lr = lr
        model.reset_state()
        print('-' * 50)

    model.reset_state()
    ppl_test = eval_perplexity(model, corpus_test)
    print(f'valid perplexity: {ppl_test}')
    print('DONE')
예제 #2
0
def main():
    # ハイパーパラメータの設定
    batch_size = 20
    wordvec_size = 650
    hidden_size = 650
    time_size = 35
    lr = 20.0
    max_epoch = 40
    max_grad = 0.25
    dropout = 0.5

    # 学習データの読み込み
    corpus, word_to_id, id_to_word = ptb.load_data('train')
    corpus_val, _, _ = ptb.load_data('val')
    corpus_test, _, _ = ptb.load_data('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
            optimizer.lr = lr

        model.reset_state()
        print('-' * 50)
예제 #3
0
hidden_size = 650
time_size = 35
lr = 20.0
max_epoch = 40
max_grad = 0.25
dropout = 0.5

# 学習データの読み込み
corpus, word_to_id, id_to_word = ptb.load_data('train')
corpus_val, _, _ = ptb.load_data('val')
corpus_test, _, _ = ptb.load_data('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)
# coding: utf-8
import sys

sys.path.append('..')
from rnnlm import Rnnlm
from better_rnnlm import BetterRnnlm
from dataset import ptb
from common.util import eval_perplexity

if __name__ == '__main__':
    # model = Rnnlm()
    model = BetterRnnlm()

    model.load_params()

    corpus, _, _ = ptb.load_data('test')

    model.reset_state()
    ppl_test = eval_perplexity(model, corpus)
    print('test perplexity: ', ppl_test)