Ejemplo n.º 1
0
def adverse_validation(dev, batchez, glove):
    samples = len(batchez[1])
    discriminator = am.discriminator(glove, 50)
    ad_model = am.adverse_model(discriminator)
    res = ad_model.fit([dev[1][:samples], batchez[1]], np.zeros(samples), validation_split=0.1, 
                       verbose = 0, nb_epoch = 20, callbacks = [EarlyStopping(patience=2)])
    return np.min(res.history['val_loss'])
Ejemplo n.º 2
0
def adverse_model_validate(model_dir, dev, aug_dev, glove, dim):
    discriminator = am.discriminator(glove, dim)
    discriminator.load_weights(model_dir + '/adverse.weights')
    dev_len = len(aug_dev[1])
    preds_orig = discriminator.predict(dev[1][:dev_len])
    preds_aug =  discriminator.predict(aug_dev[1])

    return np.mean(preds_orig > preds_aug)
Ejemplo n.º 3
0
def adverse_model_train(model_dir, train, aug_train, dev, aug_dev, dim, glove):
    discriminator = am.discriminator(glove, dim)
    ad_model = am.adverse_model(discriminator)
    dev_len = len(aug_dev[1])
    res = ad_model.fit([train[1], aug_train[1]], np.zeros(len(train[1])),
                       validation_data=([dev[1][:dev_len], aug_dev[1]], np.zeros(dev_len)),
                       verbose = 1, nb_epoch = 5)
    discriminator.save_weights(model_dir + '/adverse.weights')
Ejemplo n.º 4
0
def adverse_validation(dev, batchez, glove):
    samples = len(batchez[1])
    discriminator = am.discriminator(glove, 50)
    ad_model = am.adverse_model(discriminator)
    res = ad_model.fit([dev[1][:samples], batchez[1]],
                       np.zeros(samples),
                       validation_split=0.1,
                       verbose=0,
                       nb_epoch=20,
                       callbacks=[EarlyStopping(patience=2)])
    return np.min(res.history['val_loss'])
Ejemplo n.º 5
0
def init_adverserial_threshold_function(target_dir, ad_threshold, glove, dim, wi, hypo_len):
    ### too slow to use
    discriminator = am.discriminator(glove, dim)
    discriminator.load_weights(target_dir + '/adverse.weights')
    padding_example = (['a'] * 25, ['a'] * 15, 'entailment')
    def t_func(example, loss, cpred, ctrue):
        if not ctrue:
            return False
        vec_ex = load_data.prepare_split_vec_dataset([example, padding_example], wi.index, True)
        pred = discriminator.predict(vec_ex[1][:1])[0][0]
        return pred > ad_threshold
    return t_func
Ejemplo n.º 6
0
def filter_adverserial(dataset, threshold, target_size, target_dir, glove, dim):
    arg = threshold[1] == 'b' 
    num = float(threshold[2:])
    label_size = target_size / 3
    discriminator = am.discriminator(glove, dim)
    discriminator.load_weights(target_dir + '/adverse.weights')
    ad_preds = discriminator.predict(dataset[1], verbose = 1)[:,0]
    indices =  (ad_preds < num) if arg else (ad_preds > num)
    final_indices = []
    for l in range(3):
        label_args = dataset[2][:, l] == 1
        final_indices += list(np.where(indices * label_args)[0][:label_size])
    ind = np.sort(final_indices)
    return (dataset[0][ind], dataset[1][ind], dataset[2][ind])