def run_evaluate(self, inputs, targets, epoch): """Runs evaluation on validation inputs and targets. Optionally: - writes summary to Tensorboard. """ if self.summary_dir is not None: eval_losses = [] for inps, trgts in summarizer_model_utils.minibatches( inputs, targets, self.batch_size): fd, sl, s2 = self.get_feed_dict(inps, trgts) eval_loss = self.sess.run([self.eval_loss], feed_dict=fd) eval_losses.append(eval_loss) avg_eval_loss = self.sess.run(tf.reduce_mean(eval_losses)) print('Eval_loss: {}\n'.format(avg_eval_loss)) eval_summ = self.sess.run([self.eval_summary], feed_dict=fd) self.eval_writer.add_summary(eval_summ, epoch) else: eval_losses = [] for inps, trgts in summarizer_model_utils.minibatches( inputs, targets, self.batch_size): fd, sl, s2 = self.get_feed_dict(inps, trgts) eval_loss = self.sess.run([self.eval_loss], feed_dict=fd) eval_losses.append(eval_loss) avg_eval_loss = self.sess.run(tf.reduce_mean(eval_losses)) print('Eval_loss: {}\n'.format(avg_eval_loss))
def run_epoch(self, inputs, targets, epoch): """Runs a single epoch. Returns the average loss value on the epoch.""" batch_size = self.batch_size nbatches = (len(inputs) + batch_size - 1) // batch_size losses = [] for i, (inps, trgts) in enumerate( summarizer_model_utils.minibatches(inputs, targets, batch_size)): if inps is not None and trgts is not None: fd, sl, s2 = self.get_feed_dict(inps, trgts=trgts) if i % 10 == 0 and self.summary_dir is not None: _, train_loss, training_summ = self.sess.run([ self.train_op, self.train_loss, self.training_summary ], feed_dict=fd) self.training_writer.add_summary(training_summ, epoch * nbatches + i) else: _, train_loss = self.sess.run( [self.train_op, self.train_loss], feed_dict=fd) if i % 2 == 0 or i == (nbatches - 1): print('Iteration: {} of {}\ttrain_loss: {:.4f}'.format( i, nbatches - 1, train_loss)) losses.append(train_loss) else: print('Minibatch empty.') continue avg_loss = self.sess.run(tf.reduce_mean(losses)) print('Average Score for this Epoch: {}'.format(avg_loss)) return avg_loss