# Copyright 2018 Google LLC
#
# 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.

"""Finite difference approximations of the Fisher-Rao norm regularizer.

The provided utility routines are used to create Fisher-Rao norm regularizers.
The implementations use finite difference perturbations of the parameters in the
original loss to approximate the necessary gradient-vector products.
"""

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import tensorflow as tf


class VariableCollector(object):
  """Helper class with custom getter to collect `Variable` objects in a scope.

  When called for the first time the custom getter stores the corresponding
  `Variable` object in dictionary.
  When called subsequently for the same `Variable` name the getter will return
  the object from the dictionary instead of calling the original getter.
  """

  def __init__(self):
    self.variables = {}

  def collector_getter(self, getter, name, *args, **kwargs):
    """Custom getter for `VariableScope` that stores `Variable` in dictionary.

    Args:
      getter: Function, original getter function
      name: String, name of `Variable` in the scope
      *args: Additional arguments, currently only passed forward on first
        call of the original getter
      **kwargs: Additional arguments, currently only passed forward on first
        call of the original getter

    Returns:
      A `Tensor` object that contains the named `Variable` either from calling
      the original getter or if available from the dictionary.
    """