Python tensorflow.variable_scope() Examples
The following are 30
code examples of tensorflow.variable_scope().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
tensorflow
, or try the search function
.

Example #1
Source File: siamese_network_semantic.py From deep-siamese-text-similarity with MIT License | 6 votes |
def stackedRNN(self, x, dropout, scope, embedding_size, sequence_length, hidden_units): n_hidden=hidden_units n_layers=3 # Prepare data shape to match `static_rnn` function requirements x = tf.unstack(tf.transpose(x, perm=[1, 0, 2])) # print(x) # Define lstm cells with tensorflow # Forward direction cell with tf.name_scope("fw"+scope),tf.variable_scope("fw"+scope): stacked_rnn_fw = [] for _ in range(n_layers): fw_cell = tf.nn.rnn_cell.BasicLSTMCell(n_hidden, forget_bias=1.0, state_is_tuple=True) lstm_fw_cell = tf.contrib.rnn.DropoutWrapper(fw_cell,output_keep_prob=dropout) stacked_rnn_fw.append(lstm_fw_cell) lstm_fw_cell_m = tf.nn.rnn_cell.MultiRNNCell(cells=stacked_rnn_fw, state_is_tuple=True) outputs, _ = tf.nn.static_rnn(lstm_fw_cell_m, x, dtype=tf.float32) return outputs[-1]
Example #2
Source File: baseop.py From Traffic_sign_detection_YOLO with MIT License | 6 votes |
def wrap_variable(self, var): """wrap layer.w into variables""" val = self.lay.w.get(var, None) if val is None: shape = self.lay.wshape[var] args = [0., 1e-2, shape] if 'moving_mean' in var: val = np.zeros(shape) elif 'moving_variance' in var: val = np.ones(shape) else: val = np.random.normal(*args) self.lay.w[var] = val.astype(np.float32) self.act = 'Init ' if not self.var: return val = self.lay.w[var] self.lay.w[var] = tf.constant_initializer(val) if var in self._SLIM: return with tf.variable_scope(self.scope): self.lay.w[var] = tf.get_variable(var, shape = self.lay.wshape[var], dtype = tf.float32, initializer = self.lay.w[var])
Example #3
Source File: face_attack.py From Adversarial-Face-Attack with GNU General Public License v3.0 | 6 votes |
def build_pgd_attack(self, eps): victim_embeddings = tf.constant(self.victim_embeddings, dtype=tf.float32) def one_step_attack(image, grad): """ core components of this attack are: (a) PGD adversarial attack (https://arxiv.org/pdf/1706.06083.pdf) (b) momentum (https://arxiv.org/pdf/1710.06081.pdf) (c) input diversity (https://arxiv.org/pdf/1803.06978.pdf) """ orig_image = image image = self.structure(image) image = (image - 127.5) / 128.0 image = image + tf.random_uniform(tf.shape(image), minval=-1e-2, maxval=1e-2) prelogits, _ = self.network.inference(image, 1.0, False, bottleneck_layer_size=512) embeddings = tf.nn.l2_normalize(prelogits, 1, 1e-10, name='embeddings') embeddings = tf.reshape(embeddings[0], [512, 1]) objective = tf.reduce_mean(tf.matmul(victim_embeddings, embeddings)) # to be maximized noise, = tf.gradients(objective, orig_image) noise = noise / tf.reduce_mean(tf.abs(noise), [1, 2, 3], keep_dims=True) noise = 0.9 * grad + noise adv = tf.clip_by_value(orig_image + tf.sign(noise) * 1.0, lower_bound, upper_bound) return adv, noise input = tf.to_float(self.image_batch) lower_bound = tf.clip_by_value(input - eps, 0, 255.) upper_bound = tf.clip_by_value(input + eps, 0, 255.) with tf.variable_scope(tf.get_variable_scope(), reuse=tf.AUTO_REUSE): adv, _ = tf.while_loop( lambda _, __: True, one_step_attack, (input, tf.zeros_like(input)), back_prop=False, maximum_iterations=100, parallel_iterations=1) self.adv_image = adv return adv
Example #4
Source File: modules.py From dc_tts with Apache License 2.0 | 6 votes |
def normalize(inputs, scope="normalize", reuse=None): '''Applies layer normalization that normalizes along the last axis. Args: inputs: A tensor with 2 or more dimensions, where the first dimension has `batch_size`. The normalization is over the last dimension. scope: Optional scope for `variable_scope`. reuse: Boolean, whether to reuse the weights of a previous layer by the same name. Returns: A tensor with the same shape and data dtype as `inputs`. ''' outputs = tf.contrib.layers.layer_norm(inputs, begin_norm_axis=-1, scope=scope, reuse=reuse) return outputs
Example #5
Source File: modules.py From dc_tts with Apache License 2.0 | 6 votes |
def highwaynet(inputs, num_units=None, scope="highwaynet", reuse=None): '''Highway networks, see https://arxiv.org/abs/1505.00387 Args: inputs: A 3D tensor of shape [N, T, W]. num_units: An int or `None`. Specifies the number of units in the highway layer or uses the input size if `None`. scope: Optional scope for `variable_scope`. reuse: Boolean, whether to reuse the weights of a previous layer by the same name. Returns: A 3D tensor of shape [N, T, W]. ''' if not num_units: num_units = inputs.get_shape()[-1] with tf.variable_scope(scope, reuse=reuse): H = tf.layers.dense(inputs, units=num_units, activation=tf.nn.relu, name="dense1") T = tf.layers.dense(inputs, units=num_units, activation=tf.nn.sigmoid, bias_initializer=tf.constant_initializer(-1.0), name="dense2") outputs = H * T + inputs * (1. - T) return outputs
Example #6
Source File: inception_resnet_v2.py From neural-fingerprinting with BSD 3-Clause "New" or "Revised" License | 6 votes |
def block35(net, scale=1.0, activation_fn=tf.nn.relu, scope=None, reuse=None): """Builds the 35x35 resnet block.""" with tf.variable_scope(scope, 'Block35', [net], reuse=reuse): with tf.variable_scope('Branch_0'): tower_conv = slim.conv2d(net, 32, 1, scope='Conv2d_1x1') with tf.variable_scope('Branch_1'): tower_conv1_0 = slim.conv2d(net, 32, 1, scope='Conv2d_0a_1x1') tower_conv1_1 = slim.conv2d(tower_conv1_0, 32, 3, scope='Conv2d_0b_3x3') with tf.variable_scope('Branch_2'): tower_conv2_0 = slim.conv2d(net, 32, 1, scope='Conv2d_0a_1x1') tower_conv2_1 = slim.conv2d(tower_conv2_0, 48, 3, scope='Conv2d_0b_3x3') tower_conv2_2 = slim.conv2d(tower_conv2_1, 64, 3, scope='Conv2d_0c_3x3') mixed = tf.concat(axis=3, values=[tower_conv, tower_conv1_1, tower_conv2_2]) up = slim.conv2d(mixed, net.get_shape()[3], 1, normalizer_fn=None, activation_fn=None, scope='Conv2d_1x1') net += scale * up if activation_fn: net = activation_fn(net) return net
Example #7
Source File: inception_resnet_v2.py From neural-fingerprinting with BSD 3-Clause "New" or "Revised" License | 6 votes |
def block17(net, scale=1.0, activation_fn=tf.nn.relu, scope=None, reuse=None): """Builds the 17x17 resnet block.""" with tf.variable_scope(scope, 'Block17', [net], reuse=reuse): with tf.variable_scope('Branch_0'): tower_conv = slim.conv2d(net, 192, 1, scope='Conv2d_1x1') with tf.variable_scope('Branch_1'): tower_conv1_0 = slim.conv2d(net, 128, 1, scope='Conv2d_0a_1x1') tower_conv1_1 = slim.conv2d(tower_conv1_0, 160, [1, 7], scope='Conv2d_0b_1x7') tower_conv1_2 = slim.conv2d(tower_conv1_1, 192, [7, 1], scope='Conv2d_0c_7x1') mixed = tf.concat(axis=3, values=[tower_conv, tower_conv1_2]) up = slim.conv2d(mixed, net.get_shape()[3], 1, normalizer_fn=None, activation_fn=None, scope='Conv2d_1x1') net += scale * up if activation_fn: net = activation_fn(net) return net
Example #8
Source File: inception_resnet_v2.py From neural-fingerprinting with BSD 3-Clause "New" or "Revised" License | 6 votes |
def block8(net, scale=1.0, activation_fn=tf.nn.relu, scope=None, reuse=None): """Builds the 8x8 resnet block.""" with tf.variable_scope(scope, 'Block8', [net], reuse=reuse): with tf.variable_scope('Branch_0'): tower_conv = slim.conv2d(net, 192, 1, scope='Conv2d_1x1') with tf.variable_scope('Branch_1'): tower_conv1_0 = slim.conv2d(net, 192, 1, scope='Conv2d_0a_1x1') tower_conv1_1 = slim.conv2d(tower_conv1_0, 224, [1, 3], scope='Conv2d_0b_1x3') tower_conv1_2 = slim.conv2d(tower_conv1_1, 256, [3, 1], scope='Conv2d_0c_3x1') mixed = tf.concat(axis=3, values=[tower_conv, tower_conv1_2]) up = slim.conv2d(mixed, net.get_shape()[3], 1, normalizer_fn=None, activation_fn=None, scope='Conv2d_1x1') net += scale * up if activation_fn: net = activation_fn(net) return net
Example #9
Source File: model.py From neural-fingerprinting with BSD 3-Clause "New" or "Revised" License | 6 votes |
def fprop(self, x, **kwargs): del kwargs my_conv = functools.partial(tf.layers.conv2d, kernel_size=3, strides=2, padding='valid', activation=tf.nn.relu, kernel_initializer=HeReLuNormalInitializer) my_dense = functools.partial( tf.layers.dense, kernel_initializer=HeReLuNormalInitializer) with tf.variable_scope(self.scope, reuse=tf.AUTO_REUSE): for depth in [96, 256, 384, 384, 256]: x = my_conv(x, depth) y = tf.layers.flatten(x) y = my_dense(y, 4096, tf.nn.relu) y = fc7 = my_dense(y, 4096, tf.nn.relu) y = my_dense(y, 1000) return {'fc7': fc7, self.O_LOGITS: y, self.O_PROBS: tf.nn.softmax(logits=y)}
Example #10
Source File: test_runner.py From neural-fingerprinting with BSD 3-Clause "New" or "Revised" License | 6 votes |
def setUp(self): super(TestRunnerMultiGPU, self).setUp() self.sess = tf.Session() inputs = [] outputs = [] self.niter = 10 niter = self.niter # A Simple graph with `niter` sub-graphs. with tf.variable_scope(None, 'runner'): for i in range(niter): v = tf.get_variable('v%d' % i, shape=(100, 10)) w = tf.get_variable('w%d' % i, shape=(100, 1)) inputs += [{'v': v, 'w': w}] outputs += [{'v': v, 'w': w}] self.runner = RunnerMultiGPU(inputs, outputs, sess=self.sess)
Example #11
Source File: utils.py From neural-fingerprinting with BSD 3-Clause "New" or "Revised" License | 6 votes |
def preprocess_batch(images_batch, preproc_func=None): """ Creates a preprocessing graph for a batch given a function that processes a single image. :param images_batch: A tensor for an image batch. :param preproc_func: (optional function) A function that takes in a tensor and returns a preprocessed input. """ if preproc_func is None: return images_batch with tf.variable_scope('preprocess'): images_list = tf.split(images_batch, int(images_batch.shape[0])) result_list = [] for img in images_list: reshaped_img = tf.reshape(img, img.shape[1:]) processed_img = preproc_func(reshaped_img) result_list.append(tf.expand_dims(processed_img, axis=0)) result_images = tf.concat(result_list, axis=0) return result_images
Example #12
Source File: model.py From neural-fingerprinting with BSD 3-Clause "New" or "Revised" License | 6 votes |
def set_input_shape(self, input_shape): batch_size, rows, cols, input_channels = input_shape kernel_shape = tuple(self.kernel_shape) + (input_channels, self.output_channels) assert len(kernel_shape) == 4 assert all(isinstance(e, int) for e in kernel_shape), kernel_shape with tf.variable_scope(self.name): init = tf.truncated_normal(kernel_shape, stddev=0.1) self.kernels = self.get_variable(self.w_name, init) self.b = self.get_variable( 'b', .1 + np.zeros((self.output_channels,)).astype('float32')) input_shape = list(input_shape) self.input_shape = input_shape input_shape[0] = 1 dummy_batch = tf.zeros(input_shape) dummy_output = self.fprop(dummy_batch) output_shape = [int(e) for e in dummy_output.get_shape()] output_shape[0] = 1 self.output_shape = tuple(output_shape)
Example #13
Source File: resnet_tf.py From neural-fingerprinting with BSD 3-Clause "New" or "Revised" License | 6 votes |
def build_cost(self, labels, logits): """ Build the graph for cost from the logits if logits are provided. If predictions are provided, logits are extracted from the operation. """ op = logits.op if "softmax" in str(op).lower(): logits, = op.inputs with tf.variable_scope('costs'): xent = tf.nn.softmax_cross_entropy_with_logits( logits=logits, labels=labels) cost = tf.reduce_mean(xent, name='xent') cost += self._decay() cost = cost return cost
Example #14
Source File: simulate_sin.py From deep-learning-note with MIT License | 6 votes |
def run_eval(sess, test_X, test_y): ds = tf.data.Dataset.from_tensor_slices((test_X, test_y)) ds = ds.batch(1) X, y = ds.make_one_shot_iterator().get_next() with tf.variable_scope("model", reuse=True): prediction, _, _ = lstm_model(X, [0.0], False) predictions = [] labels = [] for i in range(TESTING_EXAMPLES): p, l = sess.run([prediction, y]) predictions.append(p) labels.append(l) predictions = np.array(predictions).squeeze() labels = np.array(labels).squeeze() rmse = np.sqrt(((predictions-labels) ** 2).mean(axis=0)) print("Mean Square Error is: %f" % rmse) plt.figure() plt.plot(predictions, label='predictions') plt.plot(labels, label='real_sin') plt.legend() plt.show()
Example #15
Source File: actor.py From neural-combinatorial-optimization-rl-tensorflow with MIT License | 6 votes |
def build_permutation(self): with tf.variable_scope("encoder"): with tf.variable_scope("embedding"): # Embed input sequence W_embed =tf.get_variable("weights", [1,self.input_dimension+2, self.input_embed], initializer=self.initializer) # +2 for TW feat. here too embedded_input = tf.nn.conv1d(self.input_, W_embed, 1, "VALID", name="embedded_input") # Batch Normalization embedded_input = tf.layers.batch_normalization(embedded_input, axis=2, training=self.is_training, name='layer_norm', reuse=None) with tf.variable_scope("dynamic_rnn"): # Encode input sequence cell1 = LSTMCell(self.num_neurons, initializer=self.initializer) # BNLSTMCell(self.num_neurons, self.training) or cell1 = DropoutWrapper(cell1, output_keep_prob=0.9) # Return the output activations [Batch size, Sequence Length, Num_neurons] and last hidden state as tensors. encoder_output, encoder_state = tf.nn.dynamic_rnn(cell1, embedded_input, dtype=tf.float32) with tf.variable_scope('decoder'): # Ptr-net returns permutations (self.positions), with their log-probability for backprop self.ptr = Pointer_decoder(encoder_output, self.config) self.positions, self.log_softmax, self.attending, self.pointing = self.ptr.loop_decode(encoder_state) variable_summaries('log_softmax',self.log_softmax, with_max_min = True)
Example #16
Source File: encoder.py From neural-combinatorial-optimization-rl-tensorflow with MIT License | 6 votes |
def feedforward(inputs, num_units=[2048, 512], is_training=True): with tf.variable_scope("ffn", reuse=None): # Inner layer params = {"inputs": inputs, "filters": num_units[0], "kernel_size": 1, "activation": tf.nn.relu, "use_bias": True} outputs = tf.layers.conv1d(**params) # Readout layer params = {"inputs": outputs, "filters": num_units[1], "kernel_size": 1, "activation": None, "use_bias": True} outputs = tf.layers.conv1d(**params) # Residual connection outputs += inputs # Normalize outputs = tf.layers.batch_normalization(outputs, axis=2, training=is_training, name='ln', reuse=None) # [batch_size, seq_length, n_hidden] return outputs
Example #17
Source File: encoder.py From neural-combinatorial-optimization-rl-tensorflow with MIT License | 6 votes |
def encode(self, inputs): # Tensor blocks holding the input sequences [Batch Size, Sequence Length, Features] #self.input_ = tf.placeholder(tf.float32, [self.batch_size, self.max_length, self.input_dimension], name="input_raw") with tf.variable_scope("embedding"): # Embed input sequence W_embed =tf.get_variable("weights",[1,self.input_dimension, self.input_embed], initializer=self.initializer) self.embedded_input = tf.nn.conv1d(inputs, W_embed, 1, "VALID", name="embedded_input") # Batch Normalization self.enc = tf.layers.batch_normalization(self.embedded_input, axis=2, training=self.is_training, name='layer_norm', reuse=None) with tf.variable_scope("stack"): # Blocks for i in range(self.num_stacks): # num blocks with tf.variable_scope("block_{}".format(i)): ### Multihead Attention self.enc = multihead_attention(self.enc, num_units=self.input_embed, num_heads=self.num_heads, dropout_rate=0.1, is_training=self.is_training) ### Feed Forward self.enc = feedforward(self.enc, num_units=[4*self.input_embed, self.input_embed], is_training=self.is_training) # Return the output activations [Batch size, Sequence Length, Num_neurons] as tensors. self.encoder_output = self.enc ### NOTE: encoder_output is the ref for attention ### return self.encoder_output
Example #18
Source File: aru_net.py From ARU-Net with GNU General Public License v2.0 | 6 votes |
def attCNN(input, channels, activation): """ Attention network :param input: :param channels: :param activation: :return: """ with tf.variable_scope('attPart') as scope: conv1 = layers.conv2d_bn_lrn_drop('conv1', input, [4, 4, channels, 12], activation=activation) pool1 = tf.nn.max_pool(conv1, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME', name='pool1') conv2 = layers.conv2d_bn_lrn_drop('conv2', pool1, [4, 4, 12, 16], activation=activation) pool2 = tf.nn.max_pool(conv2, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME', name='pool2') conv3 = layers.conv2d_bn_lrn_drop('conv3', pool2, [4, 4, 16, 32], activation=activation) pool3 = tf.nn.max_pool(conv3, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME', name='pool3') out_DS = layers.conv2d_bn_lrn_drop('conv4', pool3, [4, 4, 32, 1], activation=activation) return out_DS
Example #19
Source File: inception_resnet_v2.py From DOTA_models with Apache License 2.0 | 6 votes |
def block35(net, scale=1.0, activation_fn=tf.nn.relu, scope=None, reuse=None): """Builds the 35x35 resnet block.""" with tf.variable_scope(scope, 'Block35', [net], reuse=reuse): with tf.variable_scope('Branch_0'): tower_conv = slim.conv2d(net, 32, 1, scope='Conv2d_1x1') with tf.variable_scope('Branch_1'): tower_conv1_0 = slim.conv2d(net, 32, 1, scope='Conv2d_0a_1x1') tower_conv1_1 = slim.conv2d(tower_conv1_0, 32, 3, scope='Conv2d_0b_3x3') with tf.variable_scope('Branch_2'): tower_conv2_0 = slim.conv2d(net, 32, 1, scope='Conv2d_0a_1x1') tower_conv2_1 = slim.conv2d(tower_conv2_0, 48, 3, scope='Conv2d_0b_3x3') tower_conv2_2 = slim.conv2d(tower_conv2_1, 64, 3, scope='Conv2d_0c_3x3') mixed = tf.concat(axis=3, values=[tower_conv, tower_conv1_1, tower_conv2_2]) up = slim.conv2d(mixed, net.get_shape()[3], 1, normalizer_fn=None, activation_fn=None, scope='Conv2d_1x1') net += scale * up if activation_fn: net = activation_fn(net) return net
Example #20
Source File: run_audio_attack.py From Black-Box-Audio with MIT License | 5 votes |
def setup_graph(self, input_audio_batch, target_phrase): batch_size = input_audio_batch.shape[0] weird = (input_audio_batch.shape[1] - 1) // 320 logits_arg2 = np.tile(weird, batch_size) dense_arg1 = np.array(np.tile(target_phrase, (batch_size, 1)), dtype=np.int32) dense_arg2 = np.array(np.tile(target_phrase.shape[0], batch_size), dtype=np.int32) pass_in = np.clip(input_audio_batch, -2**15, 2**15-1) seq_len = np.tile(weird, batch_size).astype(np.int32) with tf.variable_scope('', reuse=tf.AUTO_REUSE): inputs = tf.placeholder(tf.float32, shape=pass_in.shape, name='a') len_batch = tf.placeholder(tf.float32, name='b') arg2_logits = tf.placeholder(tf.int32, shape=logits_arg2.shape, name='c') arg1_dense = tf.placeholder(tf.float32, shape=dense_arg1.shape, name='d') arg2_dense = tf.placeholder(tf.int32, shape=dense_arg2.shape, name='e') len_seq = tf.placeholder(tf.int32, shape=seq_len.shape, name='f') logits = get_logits(inputs, arg2_logits) target = ctc_label_dense_to_sparse(arg1_dense, arg2_dense, len_batch) ctcloss = tf.nn.ctc_loss(labels=tf.cast(target, tf.int32), inputs=logits, sequence_length=len_seq) decoded, _ = tf.nn.ctc_greedy_decoder(logits, arg2_logits, merge_repeated=True) sess = tf.Session() saver = tf.train.Saver(tf.global_variables()) saver.restore(sess, "models/session_dump") func1 = lambda a, b, c, d, e, f: sess.run(ctcloss, feed_dict={inputs: a, len_batch: b, arg2_logits: c, arg1_dense: d, arg2_dense: e, len_seq: f}) func2 = lambda a, b, c, d, e, f: sess.run([ctcloss, decoded], feed_dict={inputs: a, len_batch: b, arg2_logits: c, arg1_dense: d, arg2_dense: e, len_seq: f}) return (func1, func2)
Example #21
Source File: tagger.py From convseg with MIT License | 5 votes |
def build_graph(self): parameters = self.parameters with tf.variable_scope(name_or_scope=self.scope, initializer=tf.uniform_unit_scaling_initializer()): seq_ids_pl, seq_other_ids_pls, inputs = self.build_input_graph(vocab_size=parameters['vocab_size'], emb_size=parameters['emb_size'], word_window_size=parameters['word_window_size'], word_vocab_size=parameters['word_vocab_size'], word_emb_size=parameters['word_emb_size']) stag_ids_pl, seq_lengths_pl, is_train_pl, cost_op, train_cost_op, scores_op, summary_op = \ self.build_tagging_graph(inputs=inputs, num_tags=parameters['num_tags'], use_crf=parameters['use_crf'], lamd=parameters['lamd'], dropout_emb=parameters['dropout_emb'], dropout_hidden=parameters['dropout_hidden'], hidden_layers=parameters['hidden_layers'], channels=parameters['channels'], kernel_size=parameters['kernel_size'], use_bn=parameters['use_bn'], use_wn=parameters['use_wn'], active_type=parameters['active_type']) self.seq_ids_pl = seq_ids_pl self.seq_other_ids_pls = seq_other_ids_pls self.stag_ids_pl = stag_ids_pl self.seq_lengths_pl = seq_lengths_pl self.is_train_pl = is_train_pl self.cost_op = cost_op self.train_cost_op = train_cost_op self.scores_op = scores_op self.summary_op = summary_op
Example #22
Source File: tagger.py From convseg with MIT License | 5 votes |
def inference(self, scores, sequence_lengths=None): """ Inference label sequence given scores. If transitions is given, then perform veterbi search, else perform greedy search. Args: scores: A numpy array with shape (batch, max_length, num_tags). sequence_lengths: A numpy array with shape (batch,). Returns: A numpy array with shape (batch, max_length). """ if not self.parameters['use_crf']: return np.argmax(scores, 2) else: with tf.variable_scope(self.scope, reuse=True): transitions = tf.get_variable('transitions').eval(session=self.sess) paths = np.zeros(scores.shape[:2], dtype=INT_TYPE) for i in xrange(scores.shape[0]): tag_score, length = scores[i], sequence_lengths[i] if length == 0: continue path, _ = crf.viterbi_decode(tag_score[:length], transitions) paths[i, :length] = path return paths
Example #23
Source File: siamese_network.py From deep-siamese-text-similarity with MIT License | 5 votes |
def BiRNN(self, x, dropout, scope, embedding_size, sequence_length, hidden_units): n_hidden=hidden_units n_layers=3 # Prepare data shape to match `static_rnn` function requirements x = tf.unstack(tf.transpose(x, perm=[1, 0, 2])) print(x) # Define lstm cells with tensorflow # Forward direction cell with tf.name_scope("fw"+scope),tf.variable_scope("fw"+scope): stacked_rnn_fw = [] for _ in range(n_layers): fw_cell = tf.nn.rnn_cell.BasicLSTMCell(n_hidden, forget_bias=1.0, state_is_tuple=True) lstm_fw_cell = tf.contrib.rnn.DropoutWrapper(fw_cell,output_keep_prob=dropout) stacked_rnn_fw.append(lstm_fw_cell) lstm_fw_cell_m = tf.nn.rnn_cell.MultiRNNCell(cells=stacked_rnn_fw, state_is_tuple=True) with tf.name_scope("bw"+scope),tf.variable_scope("bw"+scope): stacked_rnn_bw = [] for _ in range(n_layers): bw_cell = tf.nn.rnn_cell.BasicLSTMCell(n_hidden, forget_bias=1.0, state_is_tuple=True) lstm_bw_cell = tf.contrib.rnn.DropoutWrapper(bw_cell,output_keep_prob=dropout) stacked_rnn_bw.append(lstm_bw_cell) lstm_bw_cell_m = tf.nn.rnn_cell.MultiRNNCell(cells=stacked_rnn_bw, state_is_tuple=True) # Get lstm cell output with tf.name_scope("bw"+scope),tf.variable_scope("bw"+scope): outputs, _, _ = tf.nn.static_bidirectional_rnn(lstm_fw_cell_m, lstm_bw_cell_m, x, dtype=tf.float32) return outputs[-1]
Example #24
Source File: tfutil.py From disentangling_conditional_gans with MIT License | 5 votes |
def _init_graph(self): # Collect inputs. self.input_names = [] for param in inspect.signature(self._build_func).parameters.values(): if param.kind == param.POSITIONAL_OR_KEYWORD and param.default is param.empty: self.input_names.append(param.name) self.num_inputs = len(self.input_names) assert self.num_inputs >= 1 # Choose name and scope. if self.name is None: self.name = self._build_func_name self.scope = tf.get_default_graph().unique_name(self.name.replace('/', '_'), mark_as_used=False) # Build template graph. with tf.variable_scope(self.scope, reuse=tf.AUTO_REUSE): assert tf.get_variable_scope().name == self.scope with absolute_name_scope(self.scope): # ignore surrounding name_scope with tf.control_dependencies(None): # ignore surrounding control_dependencies self.input_templates = [tf.placeholder(tf.float32, name=name) for name in self.input_names] out_expr = self._build_func(*self.input_templates, is_template_graph=True, **self.static_kwargs) # Collect outputs. assert is_tf_expression(out_expr) or isinstance(out_expr, tuple) self.output_templates = [out_expr] if is_tf_expression(out_expr) else list(out_expr) self.output_names = [t.name.split('/')[-1].split(':')[0] for t in self.output_templates] self.num_outputs = len(self.output_templates) assert self.num_outputs >= 1 # Populate remaining fields. self.input_shapes = [shape_to_list(t.shape) for t in self.input_templates] self.output_shapes = [shape_to_list(t.shape) for t in self.output_templates] self.input_shape = self.input_shapes[0] self.output_shape = self.output_shapes[0] self.vars = OrderedDict([(self.get_var_localname(var), var) for var in tf.global_variables(self.scope + '/')]) self.trainables = OrderedDict([(self.get_var_localname(var), var) for var in tf.trainable_variables(self.scope + '/')]) # Run initializers for all variables defined by this network.
Example #25
Source File: tfutil.py From disentangling_conditional_gans with MIT License | 5 votes |
def get_output_for(self, *in_expr, return_as_list=False, **dynamic_kwargs): assert len(in_expr) == self.num_inputs all_kwargs = dict(self.static_kwargs) all_kwargs.update(dynamic_kwargs) with tf.variable_scope(self.scope, reuse=True): assert tf.get_variable_scope().name == self.scope named_inputs = [tf.identity(expr, name=name) for expr, name in zip(in_expr, self.input_names)] out_expr = self._build_func(*named_inputs, **all_kwargs) assert is_tf_expression(out_expr) or isinstance(out_expr, tuple) if return_as_list: out_expr = [out_expr] if is_tf_expression(out_expr) else list(out_expr) return out_expr # Get the local name of a given variable, excluding any surrounding name scopes.
Example #26
Source File: networks.py From disentangling_conditional_gans with MIT License | 5 votes |
def upscale2d(x, factor=2): assert isinstance(factor, int) and factor >= 1 if factor == 1: return x with tf.variable_scope('Upscale2D'): s = x.shape x = tf.reshape(x, [-1, s[1], s[2], 1, s[3], 1]) x = tf.tile(x, [1, 1, 1, factor, 1, factor]) x = tf.reshape(x, [-1, s[1], s[2] * factor, s[3] * factor]) return x #---------------------------------------------------------------------------- # Fused upscale2d + conv2d. # Faster and uses less memory than performing the operations separately.
Example #27
Source File: networks.py From disentangling_conditional_gans with MIT License | 5 votes |
def downscale2d(x, factor=2): assert isinstance(factor, int) and factor >= 1 if factor == 1: return x with tf.variable_scope('Downscale2D'): ksize = [1, 1, factor, factor] return tf.nn.avg_pool(x, ksize=ksize, strides=ksize, padding='VALID', data_format='NCHW') # NOTE: requires tf_config['graph_options.place_pruned_graph'] = True #---------------------------------------------------------------------------- # Fused conv2d + downscale2d. # Faster and uses less memory than performing the operations separately.
Example #28
Source File: networks.py From disentangling_conditional_gans with MIT License | 5 votes |
def pixel_norm(x, epsilon=1e-8): with tf.variable_scope('PixelNorm'): return x * tf.rsqrt(tf.reduce_mean(tf.square(x), axis=1, keep_dims=True) + epsilon) #---------------------------------------------------------------------------- # Minibatch standard deviation.
Example #29
Source File: modules.py From dc_tts with Apache License 2.0 | 5 votes |
def embed(inputs, vocab_size, num_units, zero_pad=True, scope="embedding", reuse=None): '''Embeds a given tensor. Args: inputs: A `Tensor` with type `int32` or `int64` containing the ids to be looked up in `lookup table`. vocab_size: An int. Vocabulary size. num_units: An int. Number of embedding hidden units. zero_pad: A boolean. If True, all the values of the fist row (id 0) should be constant zeros. scope: Optional scope for `variable_scope`. reuse: Boolean, whether to reuse the weights of a previous layer by the same name. Returns: A `Tensor` with one more rank than inputs's. The last dimensionality should be `num_units`. ''' with tf.variable_scope(scope, reuse=reuse): lookup_table = tf.get_variable('lookup_table', dtype=tf.float32, shape=[vocab_size, num_units], initializer=tf.truncated_normal_initializer(mean=0.0, stddev=0.1)) if zero_pad: lookup_table = tf.concat((tf.zeros(shape=[1, num_units]), lookup_table[1:, :]), 0) outputs = tf.nn.embedding_lookup(lookup_table, inputs) return outputs
Example #30
Source File: test_defenses.py From neural-fingerprinting with BSD 3-Clause "New" or "Revised" License | 5 votes |
def fprop(self, x, **kwargs): del kwargs with tf.variable_scope(self.scope, reuse=tf.AUTO_REUSE): w1 = tf.constant([[1.5, .3], [-2, 0.3]], dtype=tf.as_dtype(x.dtype)) w2 = tf.constant([[-2.4, 1.2], [0.5, -2.3]], dtype=tf.as_dtype(x.dtype)) h1 = tf.nn.sigmoid(tf.matmul(x, w1)) res = tf.matmul(h1, w2) return {self.O_FEATURES: [h1, res], self.O_LOGITS: res, self.O_PROBS: tf.nn.softmax(res)}