def _fit_(self, X, Y, epochs, n_instances, tf_session, verbose): try: for epoch in range(epochs): for start in range(0, n_instances, self.batch_size): end = np.min([start + self.batch_size, n_instances]) tf_session.run( self.optimizer, feed_dict={ self.X: X[start:end], self.Y: Y[start:end] }, ) if verbose == 1: progress_bar(end, n_instances, status="Fitting") if verbose == 1: c = tf_session.run(self.loss, feed_dict={ self.X: X, self.Y: Y }) print("Epoch {}: cost {} ".format((epoch + 1), np.mean(c))) if (epoch + 1) % 100 == 0: c = tf_session.run(self.loss, feed_dict={ self.X: X, self.Y: Y }) self.logger.info("Epoch {}: cost {} ".format((epoch + 1), np.mean(c))) self.step_decay(epoch) except KeyboardInterrupt: self.logger.info("Interrupted") c = tf_session.run(self.loss, feed_dict={self.X: X, self.Y: Y}) self.logger.info("Epoch {}: cost {} ".format((epoch + 1), np.mean(c)))
def _tune_threshold(self, X_val, Y_val, thin_thresholds=1): scores = self.predict_scores(X_val) probabilities = np.unique(scores)[::thin_thresholds] threshold = 0.0 best = f1_measure(Y_val, scores > threshold) try: for i, p in enumerate(probabilities): pred = scores > p f1 = f1_measure(Y_val, pred) if f1 > best: threshold = p best = f1 progress_bar(i, len(probabilities), status='Tuning threshold') except KeyboardInterrupt: self.logger.info("Keyboard interrupted") self.logger.info('Tuned threshold, obtained {:.2f} which achieved' ' a micro F1-measure of {:.2f}'.format( threshold, best)) return threshold