@@ -0,0 +1,2358 @@
+# coding=utf-8%0A# Copyright 2019 The Tensor2Robot Authors.%0A#%0A# Licensed under the Apache License, Version 2.0 (the %22License%22);%0A# you may not use this file except in compliance with the License.%0A# You may obtain a copy of the License at%0A#%0A#     http://www.apache.org/licenses/LICENSE-2.0%0A#%0A# Unless required by applicable law or agreed to in writing, software%0A# distributed under the License is distributed on an %22AS IS%22 BASIS,%0A# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.%0A# See the License for the specific language governing permissions and%0A# limitations under the License.%0A%0A# Lint as: python3%0A%22%22%22A hook to log all variables.%22%22%22%0A%0Afrom __future__ import absolute_import%0Afrom __future__ import division%0A%0Afrom __future__ import print_function%0A%0Afrom typing import Optional%0A%0Afrom absl import logging%0A%0Aimport numpy as np%0Aimport tensorflow as tf%0A%0A%0Aclass VariableLoggerHook(tf.train.SessionRunHook):%0A  %22%22%22A hook to log variables via a session run hook.%22%22%22%0A%0A  def __init__(self, max_num_variable_values = None):%0A    %22%22%22Initializes a VariableLoggerHook.%0A%0A    Args:%0A      max_num_variable_values: If not None, at most max_num_variable_values will%0A        be logged per variable.%0A    %22%22%22%0A    super(VariableLoggerHook, self).__init__()%0A    self._max_num_variable_values = max_num_variable_values%0A%0A  def begin(self):%0A    %22%22%22Captures all variables to be read out during the session run.%22%22%22%0A    self._variables_to_log = tf.contrib.framework.get_variables()%0A%0A  def before_run(self, run_context):%0A    %22%22%22Adds the variables to the run args.%22%22%22%0A    return tf.train.SessionRunArgs(self._variables_to_log)%0A%0A  def after_run(self, run_context, run_values):%0A    del run_context%0A    original = np.get_printoptions()%0A    np.set_printoptions(suppress=True)%0A    for variable, variable_value in zip(self._variables_to_log,%0A                                        run_values.results):%0A      if not isinstance(variable_value, np.ndarray):%0A        continue%0A      variable_value = variable_value.ravel()%0A      logging.info('%25s.mean = %25s', variable.op.name, np.mean(variable_value))%0A      logging.info('%25s.std = %25s', variable.op.name, np.std(variable_value))%0A      if self._max_variable_values:%0A        variable_value = variable_value%5B:self._max_variable_values%5D%0A      logging.info('%25s = %25s', variable.op.name, variable_value)%0A    np.set_printoptions(**original)%0A

Introduce a variable logging hook which will log the values of all variables during training via a session run hook.