tf.keras.utils.TimedThread
Stay organized with collections
Save and categorize content based on your preferences.
Time-based interval Threads.
tf.keras.utils.TimedThread(
interval, **kwargs
)
Runs a timed thread every x seconds. It can be used to run a threaded
function alongside model training or any other snippet of code.
Args |
interval
|
The interval, in seconds, to wait between calls to the
on_interval function.
|
**kwargs
|
additional args that are passed to threading.Thread . By
default, Thread is started as a daemon thread unless
overridden by the user in kwargs .
|
Examples:
class TimedLogIterations(keras.utils.TimedThread):
def __init__(self, model, interval):
self.model = model
super().__init__(interval)
def on_interval(self):
# Logs Optimizer iterations every x seconds
try:
opt_iterations = self.model.optimizer.iterations.numpy()
print(f"Epoch: {epoch}, Optimizer Iterations: {opt_iterations}")
except Exception as e:
print(str(e)) # To prevent thread from getting killed
# `start` and `stop` the `TimerThread` manually. If the `on_interval` call
# requires access to `model` or other objects, override `__init__` method.
# Wrap it in a `try-except` to handle exceptions and `stop` the thread run.
timed_logs = TimedLogIterations(model=model, interval=5)
timed_logs.start()
try:
model.fit(...)
finally:
timed_logs.stop()
# Alternatively, run the `TimedThread` in a context manager
with TimedLogIterations(model=model, interval=5):
model.fit(...)
# If the timed thread instance needs access to callback events,
# subclass both `TimedThread` and `Callback`. Note that when calling
# `super`, they will have to called for each parent class if both of them
# have the method that needs to be run. Also, note that `Callback` has
# access to `model` as an attribute and need not be explictly provided.
class LogThreadCallback(
keras.utils.TimedThread, keras.callbacks.Callback
):
def __init__(self, interval):
self._epoch = 0
keras.utils.TimedThread.__init__(self, interval)
keras.callbacks.Callback.__init__(self)
def on_interval(self):
if self.epoch:
opt_iter = self.model.optimizer.iterations.numpy()
logging.info(f"Epoch: {self._epoch}, Opt Iteration: {opt_iter}")
def on_epoch_begin(self, epoch, logs=None):
self._epoch = epoch
with LogThreadCallback(interval=5) as thread_callback:
# It's required to pass `thread_callback` to also `callbacks` arg of
# `model.fit` to be triggered on callback events.
model.fit(..., callbacks=[thread_callback])
Methods
is_alive
View source
is_alive()
Returns True if thread is running. Otherwise returns False.
on_interval
View source
@abc.abstractmethod
on_interval()
User-defined behavior that is called in the thread.
start
View source
start()
Creates and starts the thread run.
stop
View source
stop()
Stops the thread run.
__enter__
View source
__enter__()
__exit__
View source
__exit__(
*args, **kwargs
)
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates. Some content is licensed under the numpy license.
Last updated 2023-10-06 UTC.
[null,null,["Last updated 2023-10-06 UTC."],[],[],null,["# tf.keras.utils.TimedThread\n\n\u003cbr /\u003e\n\n|-----------------------------------------------------------------------------------------------------------------|\n| [View source on GitHub](https://github.com/keras-team/keras/tree/v2.14.0/keras/utils/timed_threads.py#L24-L148) |\n\nTime-based interval Threads. \n\n tf.keras.utils.TimedThread(\n interval, **kwargs\n )\n\nRuns a timed thread every x seconds. It can be used to run a threaded\nfunction alongside model training or any other snippet of code.\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Args ---- ||\n|------------|--------------------------------------------------------------------------------------------------------------------------------------------------------|\n| `interval` | The interval, in seconds, to wait between calls to the `on_interval` function. |\n| `**kwargs` | additional args that are passed to `threading.Thread`. By default, `Thread` is started as a `daemon` thread unless overridden by the user in `kwargs`. |\n\n\u003cbr /\u003e\n\n#### Examples:\n\n class TimedLogIterations(keras.utils.TimedThread):\n def __init__(self, model, interval):\n self.model = model\n super().__init__(interval)\n\n def on_interval(self):\n # Logs Optimizer iterations every x seconds\n try:\n opt_iterations = self.model.optimizer.iterations.numpy()\n print(f\"Epoch: {epoch}, Optimizer Iterations: {opt_iterations}\")\n except Exception as e:\n print(str(e)) # To prevent thread from getting killed\n\n # `start` and `stop` the `TimerThread` manually. If the `on_interval` call\n # requires access to `model` or other objects, override `__init__` method.\n # Wrap it in a `try-except` to handle exceptions and `stop` the thread run.\n timed_logs = TimedLogIterations(model=model, interval=5)\n timed_logs.start()\n try:\n model.fit(...)\n finally:\n timed_logs.stop()\n\n # Alternatively, run the `TimedThread` in a context manager\n with TimedLogIterations(model=model, interval=5):\n model.fit(...)\n\n # If the timed thread instance needs access to callback events,\n # subclass both `TimedThread` and `Callback`. Note that when calling\n # `super`, they will have to called for each parent class if both of them\n # have the method that needs to be run. Also, note that `Callback` has\n # access to `model` as an attribute and need not be explictly provided.\n class LogThreadCallback(\n keras.utils.TimedThread, keras.callbacks.Callback\n ):\n def __init__(self, interval):\n self._epoch = 0\n keras.utils.TimedThread.__init__(self, interval)\n keras.callbacks.Callback.__init__(self)\n\n def on_interval(self):\n if self.epoch:\n opt_iter = self.model.optimizer.iterations.numpy()\n logging.info(f\"Epoch: {self._epoch}, Opt Iteration: {opt_iter}\")\n\n def on_epoch_begin(self, epoch, logs=None):\n self._epoch = epoch\n\n with LogThreadCallback(interval=5) as thread_callback:\n # It's required to pass `thread_callback` to also `callbacks` arg of\n # `model.fit` to be triggered on callback events.\n model.fit(..., callbacks=[thread_callback])\n\nMethods\n-------\n\n### `is_alive`\n\n[View source](https://github.com/keras-team/keras/tree/v2.14.0/keras/utils/timed_threads.py#L127-L131) \n\n is_alive()\n\nReturns True if thread is running. Otherwise returns False.\n\n### `on_interval`\n\n[View source](https://github.com/keras-team/keras/tree/v2.14.0/keras/utils/timed_threads.py#L142-L148) \n\n @abc.abstractmethod\n on_interval()\n\nUser-defined behavior that is called in the thread.\n\n### `start`\n\n[View source](https://github.com/keras-team/keras/tree/v2.14.0/keras/utils/timed_threads.py#L109-L120) \n\n start()\n\nCreates and starts the thread run.\n\n### `stop`\n\n[View source](https://github.com/keras-team/keras/tree/v2.14.0/keras/utils/timed_threads.py#L122-L125) \n\n stop()\n\nStops the thread run.\n\n### `__enter__`\n\n[View source](https://github.com/keras-team/keras/tree/v2.14.0/keras/utils/timed_threads.py#L133-L136) \n\n __enter__()\n\n### `__exit__`\n\n[View source](https://github.com/keras-team/keras/tree/v2.14.0/keras/utils/timed_threads.py#L138-L140) \n\n __exit__(\n *args, **kwargs\n )"]]