Beispiel #1
0
def evaluation(model, supervisor):
    if cfg.dataset == 'imdb':
        teX, teY, num_te_batch = load_imdb(cfg.batch_size,
                                           cfg.words,
                                           cfg.length,
                                           is_training=False)
    elif cfg.dataset == 'ag':
        teX, teY, num_te_batch = load_ag(cfg.batch_size,
                                         cfg.length,
                                         is_training=False)

    cfg.is_training = False
    f_test_acc = save_to()

    with supervisor.managed_session(config=tf.ConfigProto(
            allow_soft_placement=True)) as sess:
        supervisor.saver.restore(sess, tf.train.latest_checkpoint(cfg.logdir))
        tf.logging.info('Model restored...')

        test_acc = 0

        for i in tqdm(range(num_te_batch),
                      total=num_te_batch,
                      ncols=70,
                      leave=False,
                      unit='b'):
            start = i * cfg.batch_size
            end = start + cfg.batch_size
            acc = sess.run(model.accuracy, {
                model.X: teX[start:end],
                model.Y: teY[start:end]
            })
            test_acc += acc
        test_acc = test_acc / num_te_batch
        f_test_acc.write(str(test_acc))
        f_test_acc.close()
        print('Test accuracy saved to ' + cfg.results + '/test_acc.csv')
        print('Test accuracy:', test_acc)

    return test_acc
from keras.preprocessing import sequence
from keras.models import Sequential
from keras.layers import Dense, Dropout, Activation, Embedding, LSTM, SimpleRNN, GRU
from keras.layers import Convolution1D, GlobalMaxPooling1D
from utils import load_imdb

##########################
## Preparing data
##########################
# some parameters
vocab_size = 5000  # number of words considered in the vocabulary
train_split = 0.7  # ratio of train sentences

# Preparing data is usually the most time-consuming part of machine learning.
# Luckily for you, the imdb dataset has already been preprocessed and included in Keras.
(X_train, y_train), (X_test, y_test) = load_imdb(nb_words=vocab_size,
                                                 train_split=train_split)

print(len(X_train), 'train sequences')
print(len(X_test), 'test sequences')

## Padding input data
# Models in Keras (and elsewhere) usually take as input batches of sentences of the same length.
# Since sentences usually have different sizes, we "pad" sentences (we add a dummy "padding" token at the end of the
# sentences. The input thus has this size : (batchsize, maxseqlen) where maxseqlen is the maximum length of a sentence
# in the batch.

maxlen = 80  # cut texts after this number of words (among top vocab_size most common words)
X_train = sequence.pad_sequences(X_train, maxlen=maxlen)
X_test = sequence.pad_sequences(X_test, maxlen=maxlen)
print('X_train shape:', X_train.shape)
print('X_test shape:', X_test.shape)
Beispiel #3
0
import numpy as np
from sklearn.feature_extraction.text import CountVectorizer
from scipy.sparse import csr_matrix
from time import time
from classifiers import TransparentMultinomialNB
from utils import load_imdb, ce_squared, ClassifierArchive
from pickle import dump


# In[3]:

t0 = time()

vect = CountVectorizer(min_df=5, max_df=1.0, binary=False, ngram_range=(1, 1))

X_train, y_train, X_val, y_val, X_test, y_test, *_ = load_imdb("./aclImdb", shuffle=True, vectorizer=vect)

feature_names = vect.get_feature_names()

clf = TransparentMultinomialNB()
clf.fit(X_train, y_train)
ctrl_clf = clf

y_test_na = y_test[:, np.newaxis]
y_test_na = np.append(y_test_na, 1-y_test_na, axis=1)

y_val_na = y_val[:, np.newaxis]
y_val_na = np.append(y_val_na, 1-y_val_na, axis=1)

y_modified = np.copy(y_train)
Beispiel #4
0
from classifiers import TransparentMultinomialNB
from utils import ce_squared, load_imdb, ClassifierArchive
from sklearn.feature_extraction.text import TfidfVectorizer
from time import time
from pickle import load, dump
import numpy as np


# In[2]:

t0 = time()

vect = TfidfVectorizer(min_df=5, max_df=1.0, binary=False, ngram_range=(1, 1)) 

X_train, y_train, X_test, y_test, train_corpus, test_corpus = load_imdb("./aclImdb", shuffle=True, vectorizer=vect)

feature_names = vect.get_feature_names()
y_test_na = y_test[:, np.newaxis]
y_test_na = np.append(y_test_na, 1-y_test_na, axis=1)

duration = time() - t0

print("Loading took {:0.2f}s.\n".format(duration))


# # Experiment

# In[3]:

with open('clf3.arch', 'rb') as f:
Beispiel #5
0
import numpy as np
from sklearn.feature_extraction.text import CountVectorizer
from time import time
from scipy.sparse.construct import diags

from utils import load_imdb

if __name__ == '__main__':
    # Load the data
    
    print "Loading the data"
    
    t0 = time()
    
    vect = CountVectorizer(min_df=5, max_df=1.0, binary=True, ngram_range=(1, 1))
    X_train, y_train, X_test, y_test, train_corpus, test_corpus = load_imdb("C:\\Users\\Mustafa\\Desktop\\aclImdb", shuffle=True, vectorizer=vect)
    feature_names = vect.get_feature_names()
    
    duration = time() - t0

    print
    print "Loading took %0.2fs." % duration
    print
    
    print "Fitting the classifier"
    
    t0 = time()
    clf = TransparentLogisticRegression(penalty='l1', C=0.1)
    clf.fit(X_train, y_train)
    
    duration = time() - t0
Beispiel #6
0
if args.context == 'cudnn':
    from nnabla.ext_utils import get_extension_context
    ctx = get_extension_context('cudnn', device_id=args.device)
    nn.set_default_context(ctx)

batch_size = 128
max_len = 400
embedding_size = 32
vocab_size = 20000
head_num = 8
hopping_num = 1
max_epoch = 20
l2_penalty_coef = 1e-4

x_train, x_test, y_train, y_test = load_imdb(vocab_size)
for i, sentence in enumerate(tqdm(x_train)):
    x_train[i] = [vocab_size] + sentence
for i, sentence in enumerate(tqdm(x_test)):
    x_test[i] = [vocab_size] + sentence

x_train = with_padding(x_train,
                       padding_type='post',
                       max_sequence_length=max_len)
x_test = with_padding(x_test, padding_type='post', max_sequence_length=max_len)
y_train = y_train[:, None]
y_test = y_test[:, None]

num_train_batch = len(x_train) // batch_size
num_dev_batch = len(x_test) // batch_size
Beispiel #7
0
def testMNB():
    print "Loading the data"
    
    t0 = time()
    
    vect = CountVectorizer(min_df=5, max_df=1.0, binary=True, ngram_range=(1, 1))
    X_train, y_train, X_test, y_test, train_corpus, test_corpus = load_imdb("C:\\Users\\mbilgic\\Desktop\\aclImdb", shuffle=True, vectorizer=vect)
    feature_names = vect.get_feature_names()
    
    duration = time() - t0

    print
    print "Loading took %0.2fs." % duration
    print
    
    print "Fitting the classifier"
    
    t0 = time()
    clf = TransparentMultinomialNB()
    clf.fit(X_train, y_train)
    
    duration = time() - t0

    print
    print "Fitting took %0.2fs." % duration
    print
    
    print "Predicting the evidences"
    
    t0 = time()
    neg_evi, pos_evi = clf.predict_evidences(X_test)
    
    duration = time() - t0

    print
    print "Predicting evidences took %0.2fs." % duration
    print
    
    print "Predicting the probs"
    
    t0 = time()
    probs = clf.predict_proba(X_test)
    
    duration = time() - t0

    print
    print "Predicting probs took %0.2fs." % duration
    print
    
    ti = TopInstances(neg_evi, pos_evi, clf.get_bias())
    
    total_evi = neg_evi + pos_evi
    
    print
    print "Most negative"
    print
    i = ti.most_negatives()[0]
    print total_evi[i], neg_evi[i], pos_evi[i], probs[i]
    print test_corpus[i]
    
    print
    print "Most positive"
    print
    i = ti.most_positives()[0]
    print total_evi[i], neg_evi[i], pos_evi[i], probs[i]
    print test_corpus[i]
Beispiel #8
0
def train(model, supervisor):
    losses = []
    accs = []
    steps = []
    val_accs = []
    val_steps = []

    if cfg.dataset == 'imdb':
        trX, trY, num_tr_batch, valX, valY, num_val_batch = load_imdb(
            cfg.batch_size, cfg.words, cfg.length, is_training=True)
    elif cfg.dataset == 'ag':
        trX, trY, num_tr_batch, valX, valY, num_val_batch = load_ag(
            cfg.batch_size, cfg.length, is_training=True)

    f_loss, f_train_acc, f_val_acc = save_to()
    config = tf.ConfigProto()
    config.gpu_options.allow_growth = True

    with supervisor.managed_session(config=config) as sess:
        print('\nSupervisor Prepared')

        for epoch in range(cfg.epoch):
            print('Training for epoch ' + str(epoch + 1) + '/' +
                  str(cfg.epoch) + ':')

            if supervisor.should_stop():
                print('Supervisor stopped')
                break

            for step in tqdm(range(num_tr_batch),
                             total=num_tr_batch,
                             ncols=70,
                             leave=False,
                             unit='b'):
                start = step * cfg.batch_size
                end = start + cfg.batch_size
                global_step = epoch * num_tr_batch + step

                if global_step % cfg.train_sum_freq == 0:
                    _, loss, train_acc, summaries = sess.run([
                        model.train_op, model.total_loss, model.accuracy,
                        model.train_summary
                    ])

                    losses.append(loss)
                    accs.append(train_acc)
                    steps.append(global_step)

                    assert not np.isnan(loss), 'loss is nan...'
                    supervisor.summary_writer.add_summary(
                        summaries, global_step)

                    f_loss.write(str(global_step) + ',' + str(loss) + '\n')
                    f_loss.flush()
                    f_train_acc.write(
                        str(global_step) + ',' + str(train_acc) + '\n')
                    f_train_acc.flush()

                else:
                    sess.run(model.train_op)

                if cfg.val_sum_freq != 0 and (
                        global_step) % cfg.val_sum_freq == 0:
                    val_acc = 0
                    for i in range(num_val_batch):
                        start = i * cfg.batch_size
                        end = start + cfg.batch_size
                        acc = sess.run(model.accuracy, {
                            model.X: valX[start:end],
                            model.Y: valY[start:end]
                        })
                        val_acc += acc
                    val_acc = val_acc / num_val_batch
                    f_val_acc.write(
                        str(global_step) + ',' + str(val_acc) + '\n')
                    f_val_acc.flush()

                    val_accs.append(val_acc)
                    val_steps.append(global_step)

            if (epoch + 1) % cfg.save_freq == 0 and cfg.save:
                supervisor.saver.save(
                    sess,
                    cfg.logdir + '/model_epoch_{0:.4g}_step_{1:.2g}'.format(
                        epoch, global_step))

        if cfg.save:
            supervisor.saver.save(
                sess, cfg.logdir +
                '/model_epoch_{0:.4g}_step_{1:.2g}'.format(epoch, global_step))

        f, (ax1, ax2) = plt.subplots(1, 2, sharey=False)
        ax1.set_title('Loss')
        ax1.plot(steps, losses)
        ax1.set_xlabel('Global step')
        ax1.set_ylabel('Loss')

        ax2.set_title('Accuracy')
        ax2.plot(val_steps, val_accs, color='b', label='val_accs')
        ax2.plot(steps, accs, color='r', label='train_accs')
        ax2.set_xlabel('Global step')
        ax2.set_ylabel('Accuracy')
        ax2.legend(loc='lower right')
        plt.show()

        f_loss.close()
        f_train_acc.close()
        f_val_acc.close()

        return losses[-1], accs[-1]
Beispiel #9
0
import torch
from torch.autograd import Variable
from utils import load_imdb
from utils import bag_of_words
import numpy as np


batch_size = 100
train_iter, val_iter, test_iter, text_field = load_imdb(imdb_path='imdb.zip', imdb_dir='imdb', batch_size=batch_size, gpu=True, reuse=True, repeat=False, shuffle=True)
V = len(text_field.vocab) # vocab size
num_labels = 2
vocab_list = text_field.vocab.itos


def build_model(input_dim, output_dim):
    model = torch.nn.Sequential()
    # computes w_c^T x + b_c 
    model.add_module("linear", torch.nn.Linear(input_dim, output_dim).cuda())
    # Compute our log softmax term.
    model.add_module("softmax", torch.nn.LogSoftmax().cuda())
    return model


def l1_logistic_loss(model, lambda_, fx, y):
    log_loss = torch.nn.NLLLoss(size_average = True)
    log_loss = log_loss.forward(fx, y)
    
    lasso_part = torch.nn.L1Loss(size_average = False)
    params = next(model.parameters())
    target = Variable(torch.zeros(params.size()[0], params.size()[1]).cuda(), requires_grad=False)
    lasso_part = lasso_part.forward(params, target)