I saw that the targeted dropout on the weights can either select train
or test
mode, instead the targeted dropout on the units is always on train
mode even when the model does the inference. Is there any reason for that?
- Targeted Weight Dropout Function:
def targeted_weight_dropout(w, params, is_training):
drop_rate = params.drop_rate
targ_perc = params.targ_rate
w_shape = w.shape
w = tf.reshape(w, [-1, w_shape[-1]])
norm = tf.abs(w)
idx = tf.to_int32(targ_perc * tf.to_float(tf.shape(w)[0]))
threshold = tf.contrib.framework.sort(norm, axis=0)[idx]
mask = norm < threshold[None, :]
if not is_training:
w = (1. - tf.to_float(mask)) * w
w = tf.reshape(w, w_shape)
return w
mask = tf.to_float(
tf.logical_and(tf.random_uniform(tf.shape(w)) < drop_rate, mask))
w = (1. - mask) * w
w = tf.reshape(w, w_shape)
return w
https://github.com/for-ai/TD/blob/87cf2a0682274badbfde962023c657779aa79975/models/utils/dropouts.py#L23
- Targeted Unit Dropout Function:
def targeted_unit_dropout(x, params, is_training):
w = tf.reshape(x, [-1, x.shape[-1]])
norm = tf.norm(w, axis=0)
idx = int(params.targ_rate * int(w.shape[1]))
sorted_norms = tf.contrib.framework.sort(norm)
threshold = sorted_norms[idx]
mask = (norm < threshold)[None, :]
mask = tf.tile(mask, [w.shape[0], 1])
mask = tf.where(
tf.logical_and((1. - params.drop_rate) < tf.random_uniform(tf.shape(w)),
mask), tf.ones_like(w, dtype=tf.float32),
tf.zeros_like(w, dtype=tf.float32))
x = tf.reshape((1 - mask) * w, x.shape)
return x
https://github.com/for-ai/TD/blob/87cf2a0682274badbfde962023c657779aa79975/models/utils/dropouts.py#L217