Ejemplo n.º 1
0
    train_data_iter = NDArrayIter(train_X,
                                  train_Y,
                                  batch_size=batch_size,
                                  shuffle=True)
    test_data_iter = NDArrayIter(test_X,
                                 test_Y,
                                 batch_size=batch_size,
                                 shuffle=False)

    model = LMModel(vocab_size=vocab_size, H_DIM=200, EMB_DIM=200)
    updater = Updater(model, update_rule='sgd', learning_rate=0.001)

    mt = time.time()
    iter_num = 0
    for ep in xrange(50):
        train_data_iter.reset()
        for batch in train_data_iter:
            iter_num += 1

            data, labels = unpack_batch(batch)
            loss = model(data, labels=labels)
            #grad_dict, loss = model.grad_and_loss(data, labels)
            grad_dict = model.backward()
            updater(grad_dict)

            if iter_num % 10 == 0:
                print 'Iteration {0:} Time {1:.2f}, loss {2:.4f}'.format(
                    iter_num,
                    time.time() - mt, float(loss.asnumpy()))
                mt = time.time()
Ejemplo n.º 2
0
                                 shuffle=False)

    if args.rnn == 'RNN': model = RNNModel(128)
    elif args.rnn == 'LSTM': model = LSTMModel(128)

    updater = Updater(model, update_rule='rmsprop', learning_rate=0.002)

    iteration_number = 0
    for epoch_number in range(50):
        for iteration, batch in enumerate(train_data_iter):
            iteration_number += 1

            data, labels = unpack_batch(batch)
            grad_dict, loss = model.grad_and_loss(data, labels)
            updater(grad_dict)

            if iteration_number % 100 == 0:
                print 'iteration %d loss %f' % (iteration_number, loss)

        test_data_iter.reset()
        errors, samples = 0, 0
        for batch in test_data_iter:
            data, labels = unpack_batch(batch)
            scores = model.forward(data, 'inference')
            predictions = np.argmax(scores, axis=1)
            errors += np.count_nonzero(predictions - labels)
            samples += data.shape[0]

        print 'epoch %d validation error %f' % (epoch_number,
                                                errors / float(samples))
Ejemplo n.º 3
0
    train_data_iter = NDArrayIter(train_data, data['train_label'][:sample_number], batch_size, shuffle=True)
    test_data_iter = NDArrayIter(test_data, data['test_label'][:sample_number], batch_size, shuffle=False)

    if args.rnn == 'RNN': model = RNNModel(128)
    elif args.rnn == 'LSTM' : model = LSTMModel(128)

    updater = Updater(model, update_rule='rmsprop', learning_rate=0.002)
    
    iteration_number = 0
    for epoch_number in range(50):
        for iteration, batch in enumerate(train_data_iter):
            iteration_number += 1

            data, labels = unpack_batch(batch)
            grad_dict, loss = model.grad_and_loss(data, labels)
            updater(grad_dict)

            if iteration_number % 100 == 0:
                print 'iteration %d loss %f' % (iteration_number, loss)

        test_data_iter.reset()
        errors, samples = 0, 0
        for batch in test_data_iter:
            data, labels = unpack_batch(batch)
            scores = model.forward(data, 'inference')
            predictions = np.argmax(scores, axis=1)
            errors += np.count_nonzero(predictions - labels)
            samples += data.shape[0]

        print 'epoch %d validation error %f' % (epoch_number, errors / float(samples))