(w, iteration=1)
| 21 | |
| 22 | |
| 23 | def spectral_norm(w, iteration=1): |
| 24 | w_shape = w.shape.as_list() |
| 25 | w = tf.reshape(w, [-1, w_shape[-1]]) |
| 26 | |
| 27 | u = tf.get_variable("u", [1, w_shape[-1]], |
| 28 | initializer=tf.random_normal_initializer(), trainable=False) |
| 29 | |
| 30 | u_hat = u |
| 31 | v_hat = None |
| 32 | for i in range(iteration): |
| 33 | """ |
| 34 | power iteration |
| 35 | Usually iteration = 1 will be enough |
| 36 | """ |
| 37 | v_ = tf.matmul(u_hat, tf.transpose(w)) |
| 38 | v_hat = tf.nn.l2_normalize(v_) |
| 39 | |
| 40 | u_ = tf.matmul(v_hat, w) |
| 41 | u_hat = tf.nn.l2_normalize(u_) |
| 42 | |
| 43 | u_hat = tf.stop_gradient(u_hat) |
| 44 | v_hat = tf.stop_gradient(v_hat) |
| 45 | |
| 46 | sigma = tf.matmul(tf.matmul(v_hat, w), tf.transpose(u_hat)) |
| 47 | |
| 48 | with tf.control_dependencies([u.assign(u_hat)]): |
| 49 | w_norm = w / sigma |
| 50 | w_norm = tf.reshape(w_norm, w_shape) |
| 51 | |
| 52 | return w_norm |
| 53 | |
| 54 | |
| 55 | def conv_spectral_norm(x, channel, k_size, stride=1, name='conv_snorm'): |
no outgoing calls
no test coverage detected