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
def train_feed_dict(self, m, data): ids, ids_lengths, labels, expanded_labels = data feed_dict = tf_utils.filter_none_keys({ m.ids: ids, m.ids_lengths: ids_lengths, m.labels: labels, m.expanded_labels: expanded_labels }) return feed_dict
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