Created
June 26, 2018 09:09
-
-
Save davidhughhenrymack/a450cb6779a5b540b4e9fc3b70bcfc73 to your computer and use it in GitHub Desktop.
An easy way to graph your tensorflow metric ops in FloydHub
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import tensorflow as tf | |
import numpy as np | |
class FloydHubMetricHook(tf.train.SessionRunHook): | |
"""An easy way to output your metric_ops to FloydHub's training metric graphs | |
This is designed to fit into TensorFlow's EstimatorSpec. Assuming you've | |
already defined some metric_ops for monitoring your training/evaluation, | |
this helper class will compute those operations then print them out in | |
the format that FloydHub is expecting. For example: | |
``` | |
def model_fn(features, labels, mode, params): | |
# Set up your model | |
loss = ... | |
my_predictions = ... | |
eval_metric_ops = { | |
"accuracy": tf.metrics.accuracy(labels=labels, predictions=my_predictions) | |
"loss": tf.metrics.mean(loss) | |
} | |
return EstimatorSpec(mode, | |
eval_metric_ops = eval_metric_ops, | |
# **Here it is! The magic!! ** | |
eval_hooks = [FloydHubMetricHook(eval_metric_ops)] | |
) | |
``` | |
FloydHubMetricHook has one optional parameter, *prefix* for using it multiple times | |
(e.g. prefix="train_" for training metrics, prefix="eval_" for evaluation metrics). | |
""" | |
def __init__(self, metric_ops, prefix=""): | |
self.metric_ops = metric_ops | |
self.prefix = prefix | |
self.readings = {} | |
def before_run(self, run_context): | |
return tf.train.SessionRunArgs(self.metric_ops) | |
def after_run(self, run_context, run_values): | |
if run_values.results is not None: | |
for k,v in run_values.results.items(): | |
try: | |
self.readings[k].append(v[1]) | |
except KeyError: | |
self.readings[k] = [v[1]] | |
def end(self, session): | |
for k, v in self.readings.items(): | |
a = np.average(v) | |
print(f'{{"metric": "{self.prefix}{k}", "value": {a}}}') | |
self.readings = {} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment