Example #1
0
def control(input_msg):
    tagger = Komoran()

    dataset = Dataset('nsmc/ratings.txt',
                      tagger,
                      max_length=MAX_LENGTH,
                      batch_size=BATCH_SIZE)

    Z_DIM = 40
    H_DIM = 300
    C_DIM = 2

    model = RNN_VAE(dataset.num_words,
                    H_DIM,
                    Z_DIM,
                    C_DIM,
                    freeze_embeddings=False,
                    gpu=USE_CUDA,
                    gpu_id=GPU_ID)

    test_data = torch.LongTensor(
        dataset.sentence2idxs(tagger.morphs(input_msg))).unsqueeze(1)

    model.load_state_dict(torch.load('models/vae_epoch_300_400.bin'))
    results = model.controlSentence(test_data, t=0.5)

    return (dataset.idxs2sentence(results[0], no_pad=True),
            dataset.idxs2sentence(results[1], no_pad=True))
Example #2
0
#
# 또한 표준 입력을 받아서 컨트롤된 문장의 결과를 출력합니다.

model = RNN_VAE(dataset.num_words,
                H_DIM,
                Z_DIM,
                C_DIM,
                freeze_embeddings=False,
                gpu=USE_CUDA,
                gpu_id=GPU_ID)

test_set = dataset.getTestData(100)

model.load_state_dict(torch.load('models/vae_epoch_300_400.bin'))
for test in test_set:
    results = model.controlSentence(test[0].unsqueeze(1), t=0.5)

    print('Original : ', dataset.idxs2sentence(test[0], no_pad=True))
    print('Positive : ', dataset.idxs2sentence(results[0], no_pad=True))
    print('Negative : ', dataset.idxs2sentence(results[1], no_pad=True))
    print()

tagger = Komoran()

while True:
    sentence = tagger.morphs(input())

    if len(sentence) == 0:
        break

    sentence = dataset.sentence2idxs(sentence).unsqueeze(dim=1)