def add_param_summary(summary_lists):
    def perform(var, action):
        ndim = var.get_shape().ndims
        name = var.name.replace(':0', '')
        if action == 'scalar':
            assert ndim == 0, "Scalar summary on high-dimension data. Maybe you want 'mean'?"
            tf.scalar_summary(name, var)
            return
        assert ndim > 0, "Cannot perform {} summary on scalar data".format(action)
        if action == 'histogram':
            tf.histogram_summary(name, var)
            return
        if action == 'sparsity':
            tf.scalar_summary(name + '/sparsity', tf.nn.zero_fraction(var))
            return
        if action == 'mean':
            tf.scalar_summary(name + '/mean', tf.reduce_mean(var))
            return
        raise RuntimeError("Unknown action {}".format(action))

    import re
    params = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES)
    for p in params:
        name = p.name
        for rgx, actions in summary_lists:
            if re.search(rgx, name):
                for act in actions:
                    perform(p, act)
Add summary for all trainable variables matching the regex
dataset/ETHPy150Open ppwwyyxx/tensorpack/tensorpack/tfutils/summary.py/add_param_summary