def finite_mean_metric(value, name): """Compute Mean Metric""" # Variables acc = base.get_metric_variable(name=f"{name}_acc", shape=(), dtype=tf.float32) num = base.get_metric_variable(name=f"{name}_num", shape=(), dtype=tf.int64) # New Variables Values is_finite = tf.math.is_finite(value) new_acc = tf.cond(is_finite, lambda: acc + value, lambda: acc) new_num = tf.cond(is_finite, lambda: num + 1, lambda: num) # Return value and update op update_op = tf.group(tf.assign(acc, new_acc), tf.assign(num, new_num)) val = tf.div_no_nan(acc, tf.to_float(num)) return (val, update_op)
def __call__(self, tensors: Dict[str, tf.Tensor]) -> Dict[str, Tuple]: # pylint: disable=unused-argument value = base.get_metric_variable(name=self.name, shape=(), dtype=tf.int64) update_op = tf.compat.v1.assign(value, value + 1) return {self.name: (value, update_op)}
def decay_mean_metric(value, decay: float, name: str): last = base.get_metric_variable(name=f"{name}_decayed_mean", shape=(), dtype=value.dtype) new_value = tf.cond(tf.equal(last, 0), lambda: value, lambda: decay * last + (1.0 - decay) * value) update_op = tf.assign(last, new_value) return (last, update_op)
def max_value_metric(value, name): max_value = base.get_metric_variable(name=f"{name}_max", shape=(), dtype=value.dtype) update_op = tf.assign(max_value, tf.maximum(value, max_value)) return (max_value, update_op)
def last_value_metric(value, name): last_value = base.get_metric_variable(name=f"{name}_last", shape=(), dtype=value.dtype) update_op = tf.assign(last_value, value) return (last_value, update_op)