Пример #1
0
def main():
    logger = get_logger(LOGDIR)
    logger.info('start')

    logger.info('1. Load the trained model.')
    ae = AutoEncoder.load(MUSCLE_MODEL)
    encoder = ae.get_encoder()

    logger.info('2. Load the corpus.')
    corpus = ReutersMuscleCorpus.load(MUSCLE_CORPUS)

    logger.info('3. Set the infer model.')
    infer = Infer(encoder, corpus)

    qa_df = pd.read_csv(MUSCLE_QA)
    q_txts = qa_df['q_txt'].tolist()
    vecs = np.array([infer(d) for d in q_txts])

    # 超回復とは
    # 夏までに痩せたい
    # 睡眠時間はどのくらいが良いですか?
    while True:
        text = six.moves.input('>> ')
        if text == '':
            break
        vec = infer(text)
        sort_i, sim = get_sim_index([vec], vecs)

        df = qa_df.loc[sort_i]
        show_sim_faq(df, sim)

    logger.info('end')
Пример #2
0
def main():
    logger = get_logger(LOGDIR)
    logger.info('start')

    logger.info('1. Load Japanese word2vec embeddings.')
    embed_matrix, vocab = load_vectors(JAWIKI_MODEL)
    logger.info('embedding shape is {}'.format(embed_matrix.shape))

    logger.info('2. Prepare the corpus.')
    corpus = ReutersMuscleCorpus()
    corpus.build(embed_matrix, vocab, seq_size)
    corpus.save(MUSCLE_CORPUS)

    logger.info('3. Make autoencoder model.')
    ae = AutoEncoder(seq_size=seq_size,
                     embed_size=embed_matrix.shape[1],
                     latent_size=latent_size)
    ae.build()

    logger.info('4. Train model.')
    ae.model.compile(optimizer="adam", loss="mse")
    train_iter = corpus.batch_iter(batch_size)
    train_step = corpus.get_step_count(batch_size)
    valid_iter = corpus.batch_iter(batch_size)
    valid_step = corpus.get_step_count(batch_size)

    ae.model.fit_generator(train_iter,
                           train_step,
                           epochs=n_epoch,
                           validation_data=valid_iter,
                           validation_steps=valid_step,
                           callbacks=[
                               TensorBoard(log_dir=LOGDIR),
                               ModelCheckpoint(filepath=MUSCLE_MODEL,
                                               save_best_only=True)
                           ])

    logger.info('end')
Пример #3
0
mnist['data'] = mnist['data'].astype(xp.float32)
mnist['data'] /= 255

N = data.num_train
x_train, x_test = xp.split(mnist['data'], [N])
label_train, label_test = xp.split(mnist['target'], [N])

print('- number of training data: {}'.format(N))
print('done.')

# prepare layers
aes = []
for idx in range(len(n_units)):
    n_in = n_units[idx-1] if idx > 0 else 28*28
    n_out = n_units[idx]
    ae = AutoEncoder(n_in, n_out, activation, not(args.untied))
    aes.append(ae)

# layer-wise pre-training
print()
print('# layer-wise pre-training')
for idx in range(len(aes)):
    ae = aes[idx]
    if args.gpu >= 0:
        ae.to_gpu()

    print('training layer #{} ({} -> {})'.format(idx+1, ae.n_in, ae.n_out))

    # type of train_data : np.ndarray
    if idx == 0:
        train_data = x_train
Пример #4
0
def main():
    logger = get_logger(LOGDIR)
    logger.info('start')

    logger.info('1. Load WikiQA text')
    wikiqa_text = load_wikiqa()
    min_w = min([len(i.split()) for i in wikiqa_text])
    max_w = max([len(i.split()) for i in wikiqa_text])
    logger.info('{0} sentence, {1}-{2} words'.format(len(wikiqa_text), min_w, max_w))

    logger.info('2. Load GloVe embeddings.')
    embed_matrix, vocab = load_glove_vectors(GLOVE_MODEL, d=GLOVE_SIZE)
    logger.info('embedding shape is {}'.format(embed_matrix.shape))

    logger.info('3. Prepare the corpus.')
    corpus = ReutersMuscleCorpus()
    corpus.build(embed_matrix, vocab, seq_size)
    corpus.documents = wikiqa_text
    corpus.save(WIKIQA_CORPUS)

    logger.info('4. Make autoencoder model.')
    ae = AutoEncoder(seq_size=seq_size, embed_size=embed_matrix.shape[1], latent_size=latent_size)
    ae.build()

    logger.info('5. Train model.')
    ae.model.compile(optimizer="adam", loss="mse")
    train_iter = corpus.batch_iter(batch_size)
    train_step = corpus.get_step_count(batch_size)

    ae.model.fit_generator(
        train_iter,
        train_step,
        epochs=n_epoch,
        # validation_data=train_iter,
        # validation_steps=train_step,
        callbacks=[
            TensorBoard(log_dir=LOGDIR),
            ModelCheckpoint(filepath=WIKIQA_MODEL, save_best_only=True)
        ]
    )

    logger.info('6. Load the encoder.')
    encoder = ae.get_encoder()

    logger.info('7. Set the infer model.')
    infer = Infer(encoder, corpus)

    logger.info('8. Evaluate the model.')
    qa_df = pd.read_csv(WIKIQA_DIR + '/WikiQA-test.tsv', sep='\t')
    maps = []
    mrrs = []
    for q_id in qa_df['QuestionID'].unique():
        df = qa_df[qa_df['QuestionID'] == q_id]
        if 1 not in df['Label'].unique():
            logger.debug('{0}: not answer'.format(q_id))
            continue
        q_doc = df['Question'].iloc[0].lower()
        q_vec = infer(q_doc)
        a_docs = df['Sentence'].map(lambda x: x.lower()).tolist()
        a_vecs = [infer(d) for d in a_docs]
        sort_i, sim = get_sim_index([q_vec], a_vecs)
        labels = [i for i, v in enumerate(df['Label']) if v == 1]
        rank = [i + 1 for i, v in enumerate(sort_i) if v in labels]
        _mrr = 1 / rank[0]
        _map = sum([1 / i for i in rank]) / len(rank)
        maps.append(_map)
        mrrs.append(_mrr)
        logger.info('{0}: MAP {1}, MRR {2}'.format(q_id, _map, _mrr))
    map_avg = sum(maps) / len(maps)
    mrr_avg = sum(mrrs) / len(mrrs)
    logger.info('MAP AVG {0} / MRR AVG {1}'.format(map_avg, mrr_avg))

    logger.info('end')
Пример #5
0
# add noise
if args.noise > 0:
    for data in mnist['data']:
        perm = np.random.permutation(
            mnist['data'].shape[1])[:int(mnist['data'].shape[1] * args.noise)]
        data[perm] = 0.0
x_train, x_test = np.split(mnist['data'], [N])  # pixels

# convert to tensor repr
x_train = x_train.reshape((len(x_train), 1, 28, 28))
x_test = x_test.reshape((len(x_test), 1, 28, 28))
y_train = y_train.reshape((len(y_train), 1, 28, 28))
y_test = y_test.reshape((len(y_test), 1, 28, 28))

# initialize model
model = Regression(AutoEncoder(28, n_filters, n_units, filter_size,
                               activation))

# initialize optimizer
optimizer = optimizers.Adam(args.alpha)
optimizer.setup(model)
if args.gpu >= 0:
    model.to_gpu()

for epoch in range(0, n_epoch):
    print('epoch', epoch + 1)

    perm = np.random.permutation(N)
    permed_x = xp.array(x_train[perm])
    permed_y = xp.array(y_train[perm])

    sum_loss = 0
Пример #6
0
mnist = data.load_mnist_data()
mnist['data'] = mnist['data'].astype(np.float32)
mnist['data'] /= 255

N = data.num_train
y_train, y_test = np.split(mnist['data'].copy(), [N])   # same pixels for auto-encoding

# add noise
if args.noise > 0:
    for data in mnist['data']:
        perm = np.random.permutation(mnist['data'].shape[1])[:int(mnist['data'].shape[1] * args.noise)]
        data[perm] = 0.0
x_train, x_test = np.split(mnist['data'], [N])          # pixels

# initialize model
model = Regression(AutoEncoder(784, n_units, activation))

# initialize optimizer
optimizer = optimizers.Adam()
optimizer.setup(model)

for epoch in range(0, n_epoch):
    print ('epoch', epoch+1)

    perm = np.random.permutation(N)

    sum_loss = 0

    start = time.time()
    for i in range(0, N, batchsize):
        x = chainer.Variable(np.asarray(x_train[perm[i:i+batchsize]]))
Пример #7
0
mnist['data'] = mnist['data'].astype(xp.float32)
mnist['data'] /= 255

N = data.num_train
x_train, x_test = xp.split(mnist['data'], [N])
label_train, label_test = xp.split(mnist['target'], [N])

print('- number of training data: {}'.format(N))
print('done.')

# prepare layers
aes = []
for idx in range(len(n_units)):
    n_in = n_units[idx - 1] if idx > 0 else 28 * 28
    n_out = n_units[idx]
    ae = AutoEncoder(n_in, n_out, activation, not (args.untied))
    aes.append(ae)

# layer-wise pre-training
print()
print('# layer-wise pre-training')
for idx in range(len(aes)):
    ae = aes[idx]
    if args.gpu >= 0:
        ae.to_gpu()

    print('training layer #{} ({} -> {})'.format(idx + 1, ae.n_in, ae.n_out))

    # type of train_data : np.ndarray
    if idx == 0:
        train_data = x_train
Пример #8
0
mnist = data.load_mnist_data()
mnist['data'] = mnist['data'].astype(np.float32)
mnist['data'] /= 255

N = data.num_train
x_train, x_test = np.split(mnist['data'], [N])
label_train, label_test = np.split(mnist['target'], [N])

print('done.')

# initialize model
aes = []
for idx in range(len(n_units)):
    n_in = n_units[idx - 1] if idx > 0 else 28 * 28
    n_out = n_units[idx]
    aes.append(AutoEncoder(n_in, n_out, activation, not (args.untied)))

model = Regression(StackedAutoEncoder(aes))
serializers.load_npz(args.model, model)

# process
perm = np.random.permutation(N)
x = chainer.Variable(np.asarray(x_train[perm[0:args.num]]))

# dump
print()
print('# dumping images')
for l in range(1, len(model.predictor) + 1):
    print('layer %d' % l)
    y = model.predictor(x, train=False, depth=l)
    for i in range(math.ceil(args.num / 10)):
Пример #9
0
def main():
    args = argparser()

    # Check data
    train_path = "./data/{}_train.pickle".format(args.env)
    test_path = "./data/{}_test.pickle".format(args.env)
    if os.path.exists(train_path) and os.path.exists(test_path):
        print("load data from ./data/")
    else:
        print("make dataset ...")
        make_dataset(env_name=args.env)

    # Setup iterator
    train = pickle.load(open(train_path, "rb"))
    train = [(t, t) for t in train]
    train_iter = chainer.iterators.SerialIterator(train, args.batchsize)
    test = pickle.load(open(test_path, "rb"))
    test = [(t, t) for t in test]
    test_iter = chainer.iterators.SerialIterator(test, args.batchsize, repeat=False, shuffle=False)

    if test[0][0].shape == (3, 210, 160):
        n_linear_dim = 30704
    elif test[0][0].shape == (3, 250, 160):
        n_linear_dim = 36784
    else:
        raise Exception

    # Set up a neural network to train
    ae = AutoEncoder(n_hidden=args.n_hidden, n_linear_dim=n_linear_dim)
    if args.gpu >= 0:
        chainer.cuda.check_cuda_available()
        chainer.cuda.get_device_from_id(args.gpu).use()
        xp = chainer.cuda.cupy
        ae.to_gpu()
    else:
        xp = np
    ae = L.Classifier(ae, lossfun=F.mean_squared_error)
    ae.compute_accuracy = False

    # Setup an optimizer
    optimizer = chainer.optimizers.Adam(alpha=0.0002, beta1=0.5)
    optimizer.setup(ae)

    # Setup trainer
    updater = training.StandardUpdater(train_iter, optimizer, device=args.gpu)
    trainer = training.Trainer(updater, (args.epoch, 'epoch'), out=args.out)

    # Extenstion
    snapshot_interval = (args.snapshot_interval, 'iteration')
    display_interval = (args.display_interval, 'iteration')
    trainer.extend(extensions.snapshot(
                filename='snapshot_{0}_iter-{1}'.format(args.env, updater.iteration)),
                trigger=snapshot_interval)
    trainer.extend(extensions.Evaluator(test_iter, ae, device=args.gpu), name='val')
    trainer.extend(extensions.LogReport(trigger=display_interval))
    trainer.extend(extensions.PrintReport([
        'epoch', 'iteration', 'main/loss', 'val/main/loss'
    ]), trigger=display_interval)
    trainer.extend(extensions.ProgressBar(update_interval=10))
    trainer.extend(out_generated_image(ae, updater, test[0][0], args.env), trigger=display_interval)

    # Run trainer
    trainer.run()