Coverage for hypergan/ops/tensorflow/ops.py : 58%
Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
|
else: self.initializer = self.random_initializer(random_stddev)
raise Exception("Expected a Tensor but received", net)
weights = [weights]
def _build(): return tf.random_normal_initializer(0, stddev, dtype=self.dtype) return _build
self._reuse = True self.reuse_scope_count = 0 self.reuse_context += 1
self.reuse_context -= 1 if self.reuse_context == 0: self._reuse = False
self.reuse_scope_count += 1 return str(self.reuse_scope_count)
if type(dtype) == Function: return dtype if dtype == 'float32': return tf.float32 elif dtype == 'float16': return tf.float16 else: raise Exception("dtype not defined: "+str(dtype))
else: weight = tf.get_variable(name, dtype=self.dtype, initializer=initializer)
elif dtype == 'float16': return tf.float16 else: raise Exception("dtype not defined: "+str(dtype))
with tf.variable_scope(self.generate_name(), reuse=self._reuse): w = self.get_weight([filter_h, filter_w, net.get_shape()[-1], output_dim]) conv = tf.nn.conv2d(net, w, strides=[1, 1, 1, 1], padding='SAME') biases = self.get_bias([output_dim], 0.001) conv = tf.nn.bias_add(conv, biases)
w_square = tf.square(w) #w_sum = tf.reduce_sum(w_square, [0,1,2]) w_conv = tf.nn.conv2d(tf.ones_like(net), w_square, strides=[1, 1, 1, 1], padding='SAME') w_norm = tf.sqrt(w_conv + 1e-4)
net_square = tf.square(net) w_ones = tf.ones_like(w) net_sum = tf.nn.conv2d(net_square, w_ones, strides=[1, 1, 1, 1], padding='SAME') net_norm = tf.sqrt(net_sum + 1e-4)
return conv / (w_norm * net_norm)
with tf.variable_scope(self.generate_name(), reuse=self._reuse): w = self.get_weight([filter_h, filter_w, net.get_shape()[-1], output_dim]) g = self.get_weight(name='g', shape=[1,output_dim]) conv = tf.nn.conv2d(net, w, strides=[1, 1, 1, 1], padding='SAME') b = self.get_bias([output_dim], 0.001)
w_square = tf.square(w) #w_sum = tf.reduce_sum(w_square, [0,1,2]) w_conv = tf.nn.conv2d(tf.ones_like(net), w_square, strides=[1, 1, 1, 1], padding='SAME') w_norm = tf.sqrt(w_conv + 1e-4)
return (conv*g+b) / (w_norm)
#def weightnorm_conv2d(self, net, filter_w, filter_h, stride_w, stride_h, output_dim): # with tf.variable_scope(self.generate_name(), reuse=self._reuse): # w = self.get_weight([filter_h, filter_w, net.get_shape()[-1], output_dim]) # g = self.get_weight(name='g', shape=[1,output_dim]) # b = self.get_bias([output_dim])
# # use weight normalization (Salimans & Kingma, 2016) # W = tf.reshape(g,[1,1,1,output_dim])*tf.nn.l2_normalize(w,[0,1,2])
# # calculate convolutional layer output # return tf.nn.bias_add(tf.nn.conv2d(net, W, [1, stride_h, stride_w, 1], padding='SAME'), b)
with tf.variable_scope(self.generate_name(), reuse=self._reuse): # modified from https://github.com/openai/weightnorm/blob/master/tensorflow/nn.py # data based initialization of parameters g = self.get_weight(name='g', shape=[1,1,1,output_dim])#, initializer=scale_init) b = self.get_bias(shape=[output_dim])#, initializer=-m_init*scale_init) shape = [filter_h, filter_w, int(net.get_shape()[-1]),output_dim] V = self.get_weight(name='v', shape=shape) V_norm = tf.nn.l2_normalize(V, [0,1,2]) x_init = tf.nn.conv2d(net, V_norm, [1, stride_h, stride_w, 1], padding="SAME") x_init = tf.nn.bias_add(x_init, b) m_init, v_init = tf.nn.moments(x_init, [0,1,2]) scale_init = 1.0/tf.sqrt(v_init + 1e-8) x_init = tf.reshape(scale_init,[1,1,1,output_dim])*(x_init-tf.reshape(m_init,[1,1,1,output_dim])) return g*x_init
with tf.variable_scope(self.generate_name(), reuse=self._reuse): # modified from https://github.com/openai/weightnorm/blob/master/tensorflow/nn.py # data based initialization of parameters g = self.get_weight(name='g', shape=[1,1,1,output_dim])#, initializer=scale_init) b = self.get_bias(shape=[output_dim])#, initializer=-m_init*scale_init) shape = [filter_h, filter_w, output_dim, int(net.get_shape()[-1])] V = self.get_weight(name='v', shape=shape) V_norm = tf.nn.l2_normalize(V, [0,1,2])
net_shape = self.shape(net) target_shape = [net_shape[0], net_shape[1]*stride_h, net_shape[2]*stride_w, output_dim] print(net, target_shape, V_norm) x_init = tf.nn.conv2d_transpose(net, V_norm, target_shape, [1, stride_h, stride_w, 1], padding="SAME") x_init = tf.nn.bias_add(x_init, b) m_init, v_init = tf.nn.moments(x_init, [0,1,2]) scale_init = 1.0/tf.sqrt(v_init + 1e-8) x_init = tf.reshape(scale_init,[1,1,1,output_dim])*(x_init-tf.reshape(m_init,[1,1,1,output_dim])) return g*x_init
print("F S ", filter_w, stride_w) print("COSINE NORM") return self.cosine_conv2d(net, filter_w, filter_h, stride_w, stride_h, output_dim) print("WEIGHT NORM") return self.weightnorm_conv2d(net, filter_w, filter_h, stride_w, stride_h, output_dim)
self.assert_tensor(net) initializer = self.initializer() shape = self.shape(net) if self.config.layer_regularizer == 'weight_norm': return self.weightnorm_deconv2d(net, filter_w, filter_h, stride_w, stride_h, output_dim) output_shape = [shape[0], shape[1]*stride_h, shape[2]*stride_w, output_dim] init_bias = 0. with tf.variable_scope(self.generate_name(), reuse=self._reuse): # filter : [height, width, output_channels, in_channels] w = self.get_weight([filter_h, filter_w, output_dim, shape[3]])
deconv = tf.nn.conv2d_transpose(net, w, output_shape=output_shape, strides=[1, stride_h, stride_w, 1])
biases = self.get_bias([output_shape[-1]]) deconv = tf.reshape(tf.nn.bias_add(deconv, biases), deconv.get_shape())
return deconv
with tf.variable_scope(self.generate_name(), reuse=self._reuse): w = self.get_weight([self.shape(net)[1], output_dim], name='cos_w') b = self.get_bias([output_dim], constant=0.001) w_norm = tf.sqrt(tf.reduce_sum(w**2, axis=0, keep_dims=True) + b ** 2)+0.000001 x_norm = tf.sqrt(tf.reduce_sum(net**2, axis=1, keep_dims=True) + 0.000001) return (tf.matmul(net, w) + 0.001 * b) / w_norm / x_norm
return self.cosine_linear(net, output_dim)
def _build(net, axis=1): return self.linear(net, 1) return _build
orig_shape = self.shape(_x) _x = tf.reshape(_x, [orig_shape[0], -1])
with tf.variable_scope(self.generate_name(), reuse=self._reuse): alphas = tf.get_variable('prelu', _x.get_shape()[-1], initializer=tf.random_normal_initializer(mean=0.0,stddev=0.01), dtype=tf.float32) pos = tf.nn.relu(_x) neg = alphas * (_x - abs(_x)) * 0.5
self.add_weights(alphas) return tf.reshape(pos + neg, orig_shape)
def _trelu(_x): activation = self.lookup(self.config.trelu_activation or 'relu') orig_shape = self.shape(_x) _x = tf.reshape(_x, [orig_shape[0], -1])
with tf.variable_scope(self.generate_name(), reuse=self._reuse): alphas = tf.get_variable('trelu', _x.get_shape()[-1], initializer=tf.random_normal_initializer(mean=0.0,stddev=0.01), dtype=tf.float32) net = activation(_x - alphas) + alphas
self.add_weights(alphas) return tf.reshape(net, orig_shape)
return _trelu
""" Takes any size tensor and reduces it to a single value using `reduce`. """
return tf.nn.sigmoid return "cosine_norm" return tf.nn.crelu return self.trelu() return tf.nn.relu return tf.reduce_min return tf.reduce_logsumexp return self.reduce_linear()
return l1_distance
with tf.device(self.device): if len(self.variables()) == 0: return init = tf.variables_initializer(self.variables()) session.run(init) self.initialized = True
#tfconfig = tf.ConfigProto(log_device_placement=True)
|