in utils.py [0:0]
def spectral_normed_weight(w, name, lower_bound=False, iteration=1, fc=False):
if fc:
iteration = 2
w_shape = w.shape.as_list()
w = tf.reshape(w, [-1, w_shape[-1]])
iteration = FLAGS.spec_iter
sigma_new = FLAGS.spec_norm_val
u = tf.get_variable(name + "_u",
[1,
w_shape[-1]],
initializer=tf.random_normal_initializer(),
trainable=False)
u_hat = u
v_hat = None
for i in range(iteration):
"""
power iteration
Usually iteration = 1 will be enough
"""
v_ = tf.matmul(u_hat, tf.transpose(w))
v_hat = tf.nn.l2_normalize(v_)
u_ = tf.matmul(v_hat, w)
u_hat = tf.nn.l2_normalize(u_)
u_hat = tf.stop_gradient(u_hat)
v_hat = tf.stop_gradient(v_hat)
sigma = tf.matmul(tf.matmul(v_hat, w), tf.transpose(u_hat))
if FLAGS.spec_eval:
dep = []
else:
dep = [u.assign(u_hat)]
with tf.control_dependencies(dep):
if lower_bound:
sigma = sigma + 1e-6
w_norm = w / sigma * tf.minimum(sigma, 1) * sigma_new
else:
w_norm = w / sigma * sigma_new
w_norm = tf.reshape(w_norm, w_shape)
return w_norm