diff --git a/data/anli.py b/data/anli.py index 7b84035..a8c0030 100644 --- a/data/anli.py +++ b/data/anli.py @@ -1,5 +1,8 @@ -import pandas as pd +import os + import jsonlines +import pandas as pd + from data import dataset @@ -7,10 +10,25 @@ class ANLIDataset(dataset.DatasetExperiment): def __init__(self, *args): super().__init__(*args) - with jsonlines.open('./corpora/anli_v0.1/R3/test.jsonl') as jsonl_reader: - for o in jsonl_reader: - print(o) + self.hypothesis = [] + self.reason = [] + self.label = [] + with jsonlines.open(os.path.join(self.data_dir, 'train.jsonl')) as jsonl_reader: + for instance in jsonl_reader: + self.hypothesis.append(instance['hypothesis']) + self.reason.append(instance['reason']) + self.label.append(instance['label']) + dataset = pd.DataFrame( + list( + zip( + self.hypothesis, + self.reason, + self.label, + ) + ), + columns=['hypothesis', 'reason', 'label'] + ) num_instances = len(dataset) self.num_train = num_instances * (1 - self.dev_ratio - self.test_ratio) self.num_dev = num_instances * self.dev_ratio @@ -24,28 +42,28 @@ def train_set(self): return self.train def train_set_pairs(self): - return self.train[['question1', 'question2']].as_matrix() + return self.train[['hypothesis', 'reason']].as_matrix() def train_labels(self): - return self.train['is_duplicate'].as_matrix() + return pd.get_dummies(self.train['label']).as_matrix() def dev_set(self): return self.dev def dev_set_pairs(self): - return self.dev[['question1', 'question2']].as_matrix() + return self.dev[['hypothesis', 'reason']].as_matrix() def dev_labels(self): - return self.dev['is_duplicate'].as_matrix() + return self.dev['label'].as_matrix() def test_set(self): return self.test def test_set_pairs(self): - return self.test[['question1', 'question2']].as_matrix() + return self.test[['hypothesis', 'reason']].as_matrix() def test_labels(self): - return self.test['is_duplicate'].as_matrix() + return self.test['label'].as_matrix() def _data_path(self): - return 'corpora/QQP/' + return 'corpora/ANLI/R3' diff --git a/data/dataset.py b/data/dataset.py index 3a3119b..95a9012 100644 --- a/data/dataset.py +++ b/data/dataset.py @@ -79,12 +79,14 @@ def pick_train_mini_batch(self): train_idxs = np.arange(len(self._train_labels)) np.random.shuffle(train_idxs) train_idxs = train_idxs[:self.num_dev_instances()] - return self.train_sen1[train_idxs], self.train_sen2[train_idxs], self._train_labels[ - train_idxs] - - def __str__(self): - return 'Dataset properties:\n ' \ - 'Number of training instances: {}\n ' \ - 'Number of dev instances: {}\n ' \ - 'Number of test instances: {}\n' \ - .format(len(self._train_labels), len(self._dev_labels), len(self._test_labels)) + mini_train1 = self.train_sen1[train_idxs] + mini_train2 = self.train_sen2[train_idxs] + mini_labels = self._train_labels[train_idxs] + return mini_train1, mini_train2, mini_labels + + def __str__(self): + return 'Dataset properties:\n ' \ + 'Number of training instances: {}\n ' \ + 'Number of dev instances: {}\n ' \ + 'Number of test instances: {}\n' \ + .format(len(self._train_labels), len(self._dev_labels), len(self._test_labels)) diff --git a/run.py b/run.py index 8052ce6..a911a61 100644 --- a/run.py +++ b/run.py @@ -15,6 +15,8 @@ from utils.model_saver import ModelSaver from utils.other_utils import timer, set_visible_gpu, init_config +log = tf.logging.info + def train(main_config, model_config, model_name, experiment_name, dataset_name): main_cfg = MainConfig(main_config) @@ -38,8 +40,10 @@ def train(main_config, model_config, model_name, experiment_name, dataset_name): num_batches = dataset_helper.num_batches model = model(max_sentence_len, vocabulary_size, main_config, model_config) model_saver = ModelSaver(main_cfg.model_dir, experiment_name, main_cfg.checkpoints_to_keep) - config = tf.ConfigProto(allow_soft_placement=True, - log_device_placement=main_cfg.log_device_placement) + config = tf.ConfigProto( + allow_soft_placement=True, + log_device_placement=main_cfg.log_device_placement, + ) with tf.Session(config=config) as session: global_step = 0 @@ -50,6 +54,8 @@ def train(main_config, model_config, model_name, experiment_name, dataset_name): metrics = {'acc': 0.0} time_per_epoch = [] + + log('Training model for {} epochs'.format(main_cfg.num_epochs)) for epoch in tqdm(range(main_cfg.num_epochs), desc='Epochs'): start_time = time.time() diff --git a/utils/model_evaluator.py b/utils/model_evaluator.py index f640a6e..9ce604f 100644 --- a/utils/model_evaluator.py +++ b/utils/model_evaluator.py @@ -17,10 +17,12 @@ def _evaluate(self, x1, x2, labels, batch_size=100): accuracy = 0.0 for batch in range(num_batches): x1_batch, x2_batch, y_batch = batch_helper.next(batch) - feed_dict = {self._model.x1: x1_batch, - self._model.x2: x2_batch, - self._model.is_training: False, - self._model.labels: y_batch} + feed_dict = { + self._model.x1: x1_batch, + self._model.x2: x2_batch, + self._model.is_training: False, + self._model.labels: y_batch + } accuracy += self._session.run(self._model.accuracy, feed_dict=feed_dict) accuracy /= num_batches return accuracy