Esempio n. 1
0
    def eval_batch(self, session, mtest, batch, batch_size, get_probs=False):
        batch_size_original = len(batch)
        ids = tf_utils.make_array(
          [item['ids'] for item in batch] + \
          [batch[0]['ids'] for _ in xrange(batch_size - len(batch))],
          length=self.memory_size)
        ids_lengths = np.array(
          [len(item['ids']) for item in batch] + \
          [len(batch[0]['ids']) for _ in xrange(batch_size - len(batch))])
        labels = tf_utils.make_array(
          [item['labels'] for item in batch] + \
          [batch[0]['labels'] for _ in xrange(batch_size - len(batch))])
        labels = labels[:, list(self.label_types)]
        if get_probs:
            all_probs = session.run(
                mtest.all_probs,
                tf_utils.filter_none_keys({
                    mtest.ids: ids,
                    mtest.ids_lengths: ids_lengths,
                    mtest.labels: labels,
                }))
            return all_probs[:batch_size_original]

        (preds, ) = session.run([mtest.preds],
                                tf_utils.filter_none_keys({
                                    mtest.ids: ids,
                                    mtest.ids_lengths: ids_lengths,
                                    mtest.labels: labels,
                                }))
        preds = preds[:batch_size_original]
        labels = labels[:batch_size_original]
        correct = (preds == labels)
        incorrects = []
        for i, e in enumerate(correct):
            if (False in e):
                incorrects.append({
                    'ids': ids[i],
                    'preds': preds[i],
                    'labels': labels[i]
                })
        if self.label_types == [0, 1, 2, 3]:
            correct = np.concatenate(
                (correct, np.all(correct[:, [0, 2]], axis=1)[:, np.newaxis],
                 np.all(correct[:, [1, 3]], axis=1)[:, np.newaxis]),
                axis=1)
        return correct, len(preds), preds, incorrects
Esempio n. 2
0
    def get_batch_full(self, batch, batch_size):
        ids = tf_utils.make_array([item['ids'] for item in batch], length=self.memory_size, batch_size=batch_size)
        ids_lengths = np.zeros(batch_size)
        for i, item in enumerate(batch):
            ids_lengths[i] = len(item['ids'])
        labels = []
        for i, item in enumerate(batch):
            lab = [x.lower() for x in item['label_names']]
            labels.append(lab)

        return ids, ids_lengths, labels
Esempio n. 3
0
    def read_batch(self, current_bucket, current_bucket_start):
        bucket = self.bucketed_train[current_bucket]
        batch = bucket[current_bucket_start:current_bucket_start +
                       self.optim_config['batch_size']]
        batch = batch + [
            random.choice(bucket)
            for _ in range(self.optim_config['batch_size'] - len(batch))
        ]
        # batch x time
        ids = tf_utils.make_array([item['ids'] for item in batch],
                                  length=self.memory_size)
        # batch
        ids_lengths = np.array([len(item['ids']) for item in batch])
        # batch x label types
        labels = tf_utils.make_array([item['labels']
                                      for item in batch])[:, self.label_types]
        # batch x time x label types
        expanded_labels = np.zeros(
            (labels.shape[0], ids.shape[1], labels.shape[1]))
        expanded_labels[:, ids_lengths - 1] = labels

        return ids, ids_lengths, labels, expanded_labels
Esempio n. 4
0
    def get_batch(self, batch):
        ids = tf_utils.make_array([item['ids'] for item in batch], length=self.memory_size)
        ids_lengths = np.array([len(item['ids']) for item in batch])                         # [batch_size(32)]
        labels = np.zeros((self.config['batch_size'], 4), dtype=np.int32)
        batch_mask = [np.zeros((self.config['batch_size'], self.label_size[i]), dtype=np.int32) for i in range(4)]
        for i, item in enumerate(batch):
            lab = [x.lower() for x in item['label_names']]
            lab = [self.label2index[j][lab[j]] for j in range(4)]  # for ext exp
            labels[i, :] = lab
            mask = self.get_mask(item['label_names'])
            for j in range(4):
                batch_mask[j][i, :] = mask[j]

        return ids, ids_lengths, labels, batch_mask
Esempio n. 5
0
    def pred_one(self, session, mtest, batch, batch_size, get_probs=False):
        batch_size_original = len(batch)
        ids = tf_utils.make_array(
          [item['ids'] for item in batch] + \
          [batch[0]['ids'] for _ in xrange(batch_size - len(batch))],
          length=self.memory_size)
        ids_lengths = np.array(
          [len(item['ids']) for item in batch] + \
          [len(batch[0]['ids']) for _ in xrange(batch_size - len(batch))])
        labels = tf_utils.make_array(
          [item['labels'] for item in batch] + \
          [batch[0]['labels'] for _ in xrange(batch_size - len(batch))])
        labels = labels[:, list(self.label_types)]

        (preds, ) = session.run([mtest.preds],
                                tf_utils.filter_none_keys({
                                    mtest.ids: ids,
                                    mtest.ids_lengths: ids_lengths,
                                    mtest.labels: labels,
                                }))

        preds = preds[:batch_size_original]
        return preds