if this_validation_loss < best_validation_loss:
        with open(output_file_name + '.pickle', 'wb') as f:
            cPickle.dump(nnet, f, -1)
        # improve patience if loss improvement is good enough
        if (this_validation_loss < best_validation_loss *
            improvement_threshold):
            patience = max(patience, iteration * patience_increase)
        # save best validation score and iteration number
        best_validation_loss = this_validation_loss
        # test it on the test set
        test_losses = test_scoref()
        test_score = np.mean(test_losses)  # TODO this is a mean of means (with different lengths)
        print(('  epoch %i, test error of best model %f') %
              (epoch, test_score))

        pred_counts = np.bincount(np.concatenate([nnet.predict(x) for x, _ in test_set_iterator]), minlength=n_outs)
        print pred_counts
        pl.figure(figsize=(24,18))
        pl.bar(np.arange(n_outs), pred_counts, width=1.)
        pl.xticks(np.arange(n_outs), le.inverse_transform(np.arange(n_outs)), rotation=90, fontsize=9)
        pl.savefig("hist_preds_" + str(epoch) + ".png")

    if patience <= iteration:  # TODO correct that
        done_looping = True
        break

end_time = time.clock()
print(('Optimization complete with best validation score of %f, '
       'with test performance %f') %
             (best_validation_loss, test_score))
print >> sys.stderr, ('The fine tuning code for file ' +