mirror of
https://github.com/wassname/ray.git
synced 2026-07-29 11:26:04 +08:00
48 lines
1.6 KiB
Python
48 lines
1.6 KiB
Python
from tensorflow import keras
|
|
|
|
|
|
class TuneReporterCallback(keras.callbacks.Callback):
|
|
"""Tune Callback for Keras."""
|
|
|
|
def __init__(self, reporter=None, freq="batch", logs=None):
|
|
"""Initializer.
|
|
|
|
Args:
|
|
freq (str): Sets the frequency of reporting intermediate results.
|
|
One of ["batch", "epoch"].
|
|
"""
|
|
self.iteration = 0
|
|
logs = logs or {}
|
|
if freq not in ["batch", "epoch"]:
|
|
raise ValueError("{} not supported as a frequency.".format(freq))
|
|
self.freq = freq
|
|
super(TuneReporterCallback, self).__init__()
|
|
|
|
def on_batch_end(self, batch, logs=None):
|
|
from ray import tune
|
|
logs = logs or {}
|
|
if not self.freq == "batch":
|
|
return
|
|
self.iteration += 1
|
|
for metric in list(logs):
|
|
if "loss" in metric and "neg_" not in metric:
|
|
logs["neg_" + metric] = -logs[metric]
|
|
if "acc" in logs:
|
|
tune.report(keras_info=logs, mean_accuracy=logs["acc"])
|
|
else:
|
|
tune.report(keras_info=logs, mean_accuracy=logs.get("accuracy"))
|
|
|
|
def on_epoch_end(self, batch, logs=None):
|
|
from ray import tune
|
|
logs = logs or {}
|
|
if not self.freq == "epoch":
|
|
return
|
|
self.iteration += 1
|
|
for metric in list(logs):
|
|
if "loss" in metric and "neg_" not in metric:
|
|
logs["neg_" + metric] = -logs[metric]
|
|
if "acc" in logs:
|
|
tune.report(keras_info=logs, mean_accuracy=logs["acc"])
|
|
else:
|
|
tune.report(keras_info=logs, mean_accuracy=logs.get("accuracy"))
|