-
Notifications
You must be signed in to change notification settings - Fork 45.1k
Expand file tree
/
Copy pathperiodic_action.py
More file actions
44 lines (36 loc) · 1.47 KB
/
periodic_action.py
File metadata and controls
44 lines (36 loc) · 1.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# Copyright 2025 The Orbit Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Provides the `PeriodicAction` abstraction."""
from typing import Callable
from orbit import runner
import tensorflow as tf
class PeriodicAction:
"""Wraps an action to be executed only at a specific step interval."""
def __init__(self,
action: Callable[[runner.Output], None],
interval: int,
global_step: tf.Variable):
"""Initializes the instance.
Args:
action: The action (callable) to wrap.
interval: The interval (in global steps) at which to execute the action.
global_step: The global step variable.
"""
self._action = action
self._interval = interval
self._global_step = global_step
def __call__(self, output: runner.Output) -> None:
# Execute action only if the current step is divisible by the interval.
if self._global_step.numpy() % self._interval == 0:
self._action(output)