# Source code for carpedm.nn.util

#
# Copyright (C) 2018 Neal Digre.
#
#
# Portions of this module are lightly modified or taken directly from
# the TensorFlow CIFAR-10 image tutorial, so here is their license.

#
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#
# Unless required by applicable law or agreed to in writing, software
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# =============================================================================

"""Utilities for managing and visualizing neural network layers."""
import re

import tensorflow as tf

TOWER_NAME = 'tower'

[docs]def name_nice(raw):
"""Convert tensor name to a nice format.

Remove 'tower_[0-9]/' from the name in case this is a multi-GPU
training session. This helps the clarity of presentation on
tensorboard.
"""
return re.sub(r'(%s_[0-9]*/)' % TOWER_NAME, '', raw)

[docs]def activation_summary(x):
"""Helper to create summaries for activations.
Creates a summary that provides a histogram of activations.
Creates a summary that measures the sparsity of activations.
Args:
x: Tensor
Returns:
nothing
"""
name = name_nice(x.op.name)
tf.summary.histogram(name + '/activations', x)
tf.summary.scalar(name + '/sparsity', tf.nn.zero_fraction(x))