Commit 9fe18ff8 authored by Yuxin Wu's avatar Yuxin Wu

better param summary

parent 565404ec
...@@ -76,8 +76,6 @@ class Model(ModelDesc): ...@@ -76,8 +76,6 @@ class Model(ModelDesc):
regularize_cost('fc.*/W', tf.nn.l2_loss), regularize_cost('fc.*/W', tf.nn.l2_loss),
name='regularize_loss') name='regularize_loss')
tf.add_to_collection(MOVING_SUMMARY_VARS_KEY, wd_cost) tf.add_to_collection(MOVING_SUMMARY_VARS_KEY, wd_cost)
add_param_summary('.*/W') # monitor histogram of all W
return tf.add_n([wd_cost, cost], name='cost') return tf.add_n([wd_cost, cost], name='cost')
def get_config(): def get_config():
......
...@@ -87,7 +87,7 @@ class Model(ModelDesc): ...@@ -87,7 +87,7 @@ class Model(ModelDesc):
name='regularize_loss') name='regularize_loss')
tf.add_to_collection(MOVING_SUMMARY_VARS_KEY, wd_cost) tf.add_to_collection(MOVING_SUMMARY_VARS_KEY, wd_cost)
add_param_summary('.*') # monitor all variables add_param_summary([('.*/W', ['histogram', 'sparsity'])]) # monitor W
return tf.add_n([cost, wd_cost], name='cost') return tf.add_n([cost, wd_cost], name='cost')
def get_config(): def get_config():
......
...@@ -77,7 +77,7 @@ class Model(ModelDesc): ...@@ -77,7 +77,7 @@ class Model(ModelDesc):
name='regularize_loss') name='regularize_loss')
tf.add_to_collection(MOVING_SUMMARY_VARS_KEY, wd_cost) tf.add_to_collection(MOVING_SUMMARY_VARS_KEY, wd_cost)
add_param_summary('.*/W') # monitor histogram of all W add_param_summary([('.*/W', ['histogram', 'sparsity'])]) # monitor histogram of all W
return tf.add_n([wd_cost, cost], name='cost') return tf.add_n([wd_cost, cost], name='cost')
def get_config(): def get_config():
......
...@@ -33,20 +33,38 @@ def add_activation_summary(x, name=None): ...@@ -33,20 +33,38 @@ def add_activation_summary(x, name=None):
tf.histogram_summary(name + '/activation', x) tf.histogram_summary(name + '/activation', x)
tf.scalar_summary(name + '/activation_sparsity', tf.nn.zero_fraction(x)) tf.scalar_summary(name + '/activation_sparsity', tf.nn.zero_fraction(x))
def add_param_summary(regex): def add_param_summary(summary_lists):
""" """
summary_lists: list of (regex, [list of action to perform])
action can be 'mean', 'scalar', 'histogram', 'sparsity'
Add summary for all trainable variables matching the regex Add summary for all trainable variables matching the regex
""" """
def perform(var, action):
ndim = var.get_shape().ndims
if action == 'scalar':
assert ndim == 0, "Scalar summary on high-dimension data. Maybe you want 'mean'?"
tf.scalar_summary(var.name, var)
return
assert ndim > 0, "Cannot perform {} summary on scalar data".format(action)
if action == 'histogram':
tf.histogram_summary(var.name, var)
return
if action == 'sparsity':
tf.scalar_summary(var.name + '/sparsity', tf.nn.zero_fraction(var))
return
if action == 'mean':
tf.scalar_summary(var.name + '/mean', tf.reduce_mean(var))
return
raise RuntimeError("Unknown action {}".format(action))
import re import re
params = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES) params = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES)
for p in params: for p in params:
name = p.name name = p.name
if re.search(regex, name): for rgx, actions in summary_lists:
if p.get_shape().ndims == 0: if re.search(rgx, name):
tf.scalar_summary(name, p) for act in actions:
else: perform(p, act)
#tf.scalar_summary(name + '/sparsity', tf.nn.zero_fraction(p))
tf.histogram_summary(name, p)
def summary_moving_average(cost_var): def summary_moving_average(cost_var):
""" Create a MovingAverage op and summary for all variables in """ Create a MovingAverage op and summary for all variables in
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment