def test_dnn_accuracy(snli, dnn): classifier = LogisticRegression(n_features=2 * dnn.dim, n_labels=3) model = CompositeModel(snli, dnn, classifier) model.train(iters=2, bsize=1, rate=0.01, log_interval=2) assert len(model.acc) == 2 assert 0 < model.acc[0] < 0.65
def test_bow_accuracy(snli): dim = 50 encoder = BagOfWords(dim=dim, vocab=snli.vocab) classifier = LogisticRegression(n_features=2 * dim, n_labels=3) model = CompositeModel(snli, encoder, classifier) model.train(iters=2, bsize=1, rate=0.01, log_interval=2) assert len(model.acc) == 2 assert 0 < model.acc[0] < 0.65
def test_predict(snli, dnn, rnn): labels = {'entailment', 'contradiction', 'neutral'} classifier = LogisticRegression(n_features=2 * dnn.dim, n_labels=3) model = CompositeModel(snli, dnn, classifier) model.train(iters=2, bsize=1, rate=0.01, log_interval=2) label = model.predict(s1='The boy ran.', s2='The boy moved.') assert label in labels model = CompositeModel(snli, rnn, classifier) model.train(iters=2, bsize=1, rate=0.01, log_interval=2) label = model.predict(s1='The boy ran.', s2='The boy moved.') assert label in labels
import pickle import time from pysem.corpora import SNLI from pysem.utils.ml import MultiLayerPerceptron from pysem.utils.snli import CompositeModel, BagOfWords snli = SNLI('/Users/peterblouw/corpora/snli_1.0/') snli.load_vocab('snli_words.pickle') dim = 300 pretrained = 'pretrained_snli_embeddings.pickle' encoder = BagOfWords(dim=dim, vocab=snli.vocab, pretrained=pretrained) classifier = MultiLayerPerceptron(di=2 * dim, dh=dim, do=3) start_time = time.time() model = CompositeModel(snli, encoder, classifier) model.train(iters=50, bsize=100, rate=0.1, log_interval=10, schedule=12000) print('Test: ', model.rnn_accuracy(model.test_data)) print('Train: ', model.rnn_accuracy(model.train_data)) print('Dev: ', model.rnn_accuracy(model.dev_data)) print('Total runtime: ', ((time.time() - start_time) / 3600.0)) with open('bow_model', 'wb') as pfile: pickle.dump(model, pfile)
def test_average(snli): array = list(range(100)) assert len(CompositeModel.average(array, [], 10)) == 10