def _test(self, loader, **kwargs): if self.ema: self.sess.run(self.ema_load) cm = ConfusionMatrix(self.model.labels) steps = len(loader) total_loss = 0 verbose = kwargs.get("verbose", False) pg = create_progress_bar(steps) for batch_dict in loader: y = batch_dict['y'] feed_dict = self.model.make_input(batch_dict) guess, lossv = self.sess.run([self.model.best, self.test_loss], feed_dict=feed_dict) total_loss += lossv cm.add_batch(y, guess) pg.update() pg.done() metrics = cm.get_all_metrics() metrics['avg_loss'] = total_loss / float(steps) if verbose: print(cm) return metrics
def _test(self, loader, **kwargs): if self.ema: self.sess.run(self.ema_load) cm = ConfusionMatrix(self.model.labels) steps = len(loader) total_loss = 0 total_norm = 0 verbose = kwargs.get("verbose", None) pg = create_progress_bar(steps) for batch_dict in pg(loader): y = batch_dict['y'] feed_dict = self.model.make_input(batch_dict) guess, lossv = self.sess.run([self.model.best, self.test_loss], feed_dict=feed_dict) batchsz = self._get_batchsz(batch_dict) total_loss += lossv * batchsz total_norm += batchsz cm.add_batch(y, guess) metrics = cm.get_all_metrics() metrics['avg_loss'] = total_loss / float(total_norm) verbose_output(verbose, cm) return metrics
def _test(self, loader): steps = len(loader) pg = create_progress_bar(steps) cm = ConfusionMatrix(self.model.labels) for batch_dict in loader: truth = batch_dict.pop('y') inputs = self.model.make_input(batch_dict) pred = self.model.impl.predict_on_batch(inputs) guess = np.argmax(pred, axis=-1) cm.add_batch(truth, guess) pg.update() pg.done() test_metrics = cm.get_all_metrics() #for k, v in test_metrics.items(): # test_metrics[k] /= steps return test_metrics
def _train(self, loader): cm = ConfusionMatrix(self.model.labels) total_loss = 0 steps = len(loader) pg = create_progress_bar(steps) for batch_dict in loader: y = batch_dict['y'] feed_dict = self.model.make_input(batch_dict, do_dropout=True) _, step, lossv, guess = self.sess.run([self.train_op, self.global_step, self.loss, self.model.best], feed_dict=feed_dict) cm.add_batch(y, guess) total_loss += lossv pg.update() pg.done() metrics = cm.get_all_metrics() metrics['avg_loss'] = total_loss/float(steps) return metrics
def _test(self, ts): total_loss = 0 steps = len(ts) cm = ConfusionMatrix(self.model.labels) pg = create_progress_bar(steps) for batch_dict in ts: y = fill_y(len(self.model.labels), batch_dict['y']) feed_dict = self.model.make_input(batch_dict, do_dropout=True) preds, lossv, = self.model.sess.run( [self.model.best, self.model.loss], feed_dict=feed_dict) # print(preds) cm.add_batch(y, preds) total_loss += lossv pg.update() pg.done() metrics = cm.get_all_metrics() metrics['avg_loss'] = float(total_loss) / steps return metrics
def _test(self, loader): total_loss = 0 cm = ConfusionMatrix(self.model.labels) steps = len(loader) pg = ProgressBar(steps) for x, y in loader: feed_dict = self.model.ex2dict(x, y) lossv, guess = self.sess.run([self.loss, self.model.best], feed_dict=feed_dict) cm.add_batch(y, guess) total_loss += lossv pg.update() pg.done() metrics = cm.get_all_metrics() metrics['avg_loss'] = total_loss / float(steps) return metrics
def _train(self, loader): cm = ConfusionMatrix(self.model.labels) total_loss = 0 steps = len(loader) pg = ProgressBar(steps) for x, y in loader: feed_dict = self.model.ex2dict(x, y, do_dropout=True) _, step, lossv, guess = self.sess.run( [self.train_op, self.global_step, self.loss, self.model.best], feed_dict=feed_dict) cm.add_batch(y, guess) total_loss += lossv pg.update() pg.done() metrics = cm.get_all_metrics() metrics['avg_loss'] = total_loss / float(steps) return metrics
def _test(self, loader, **kwargs): if self.ema: self.sess.run(self.ema_load) cm = ConfusionMatrix(self.model.labels) steps = len(loader) total_loss = 0 total_norm = 0 verbose = kwargs.get("verbose", None) output = kwargs.get('output') txts = kwargs.get('txts') handle = None line_number = 0 if output is not None and txts is not None: handle = open(output, "w") pg = create_progress_bar(steps) for batch_dict in pg(loader): y = batch_dict['y'] feed_dict = self.model.make_input(batch_dict) guess, lossv = self.sess.run([self.model.best, self.test_loss], feed_dict=feed_dict) batchsz = self._get_batchsz(batch_dict) if handle is not None: for predicted, gold in zip(guess, y): handle.write('{}\t{}\t{}\n'.format( " ".join(txts[line_number]), self.model.labels[predicted], self.model.labels[gold])) line_number += 1 total_loss += lossv * batchsz total_norm += batchsz cm.add_batch(y, guess) metrics = cm.get_all_metrics() metrics['avg_loss'] = total_loss / float(total_norm) verbose_output(verbose, cm) if handle is not None: handle.close() return metrics