Python tensorflow.shape() Examples
The following are 30
code examples of tensorflow.shape().
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: sequence.py From icme2019 with MIT License | 6 votes |
def build(self, input_shape): if not self.supports_masking: if not isinstance(input_shape, list) or len(input_shape) != 3: raise ValueError('A `AttentionSequencePoolingLayer` layer should be called ' 'on a list of 3 inputs') if len(input_shape[0]) != 3 or len(input_shape[1]) != 3 or len(input_shape[2]) != 2: raise ValueError("Unexpected inputs dimensions,the 3 tensor dimensions are %d,%d and %d , expect to be 3,3 and 2" % ( len(input_shape[0]), len(input_shape[1]), len(input_shape[2]))) if input_shape[0][-1] != input_shape[1][-1] or input_shape[0][1] != 1 or input_shape[2][1] != 1: raise ValueError('A `AttentionSequencePoolingLayer` layer requires ' 'inputs of a 3 inputs with shape (None,1,embedding_size),(None,T,embedding_size) and (None,1)' 'Got different shapes: %s,%s and %s' % (input_shape)) else: pass super(AttentionSequencePoolingLayer, self).build( input_shape) # Be sure to call this somewhere!
Example #2
Source File: sequence.py From icme2019 with MIT License | 6 votes |
def call(self, x): if (self.size == None) or (self.mode == 'sum'): self.size = int(x.shape[-1]) position_j = 1. / \ K.pow(10000., 2 * K.arange(self.size / 2, dtype='float32') / self.size) position_j = K.expand_dims(position_j, 0) position_i = tf.cumsum(K.ones_like(x[:, :, 0]), 1) - 1 position_i = K.expand_dims(position_i, 2) position_ij = K.dot(position_i, position_j) outputs = K.concatenate( [K.cos(position_ij), K.sin(position_ij)], 2) if self.mode == 'sum': if self.scale: outputs = outputs * outputs ** 0.5 return x + outputs elif self.mode == 'concat': return K.concatenate([outputs, x], 2)
Example #3
Source File: dump_tfrecord.py From cwavegan with MIT License | 6 votes |
def _mapper(example_proto): features = { 'samples': tf.FixedLenSequenceFeature([1], tf.float32, allow_missing=True), 'label': tf.FixedLenSequenceFeature([], tf.string, allow_missing=True) } example = tf.parse_single_example(example_proto, features) wav = example['samples'][:, 0] wav = wav[:16384] wav_len = tf.shape(wav)[0] wav = tf.pad(wav, [[0, 16384 - wav_len]]) label = tf.reduce_join(example['label'], 0) return wav, label
Example #4
Source File: tfutil.py From disentangling_conditional_gans with MIT License | 6 votes |
def _create_autosummary_var(name, value_expr): assert not _autosummary_finalized v = tf.cast(value_expr, tf.float32) if v.shape.ndims is 0: v = [v, np.float32(1.0)] elif v.shape.ndims is 1: v = [tf.reduce_sum(v), tf.cast(tf.shape(v)[0], tf.float32)] else: v = [tf.reduce_sum(v), tf.reduce_prod(tf.cast(tf.shape(v), tf.float32))] v = tf.cond(tf.is_finite(v[0]), lambda: tf.stack(v), lambda: tf.zeros(2)) with tf.control_dependencies(None): var = tf.Variable(tf.zeros(2)) # [numerator, denominator] update_op = tf.cond(tf.is_variable_initialized(var), lambda: tf.assign_add(var, v), lambda: tf.assign(var, v)) if name in _autosummary_vars: _autosummary_vars[name].append(var) else: _autosummary_vars[name] = [var] return update_op #---------------------------------------------------------------------------- # Call filewriter.add_summary() with all summaries in the default graph, # automatically finalizing and merging them on the first call.
Example #5
Source File: tfutil.py From disentangling_conditional_gans with MIT License | 6 votes |
def print_layers(self, title=None, hide_layers_with_no_params=False): if title is None: title = self.name print() print('%-28s%-12s%-24s%-24s' % (title, 'Params', 'OutputShape', 'WeightShape')) print('%-28s%-12s%-24s%-24s' % (('---',) * 4)) total_params = 0 for layer_name, layer_output, layer_trainables in self.list_layers(): weights = [var for var in layer_trainables if var.name.endswith('/weight:0')] num_params = sum(np.prod(shape_to_list(var.shape)) for var in layer_trainables) total_params += num_params if hide_layers_with_no_params and num_params == 0: continue print('%-28s%-12s%-24s%-24s' % ( layer_name, num_params if num_params else '-', layer_output.shape, weights[0].shape if len(weights) == 1 else '-')) print('%-28s%-12s%-24s%-24s' % (('---',) * 4)) print('%-28s%-12s%-24s%-24s' % ('Total', total_params, '', '')) print() # Construct summary ops to include histograms of all trainable parameters in TensorBoard.
Example #6
Source File: loss.py From neural-fingerprinting with BSD 3-Clause "New" or "Revised" License | 6 votes |
def fprop(self, x, y, **kwargs): if self.attack is not None: x = x, self.attack(x) else: x = x, # Catching RuntimeError: Variable -= value not supported by tf.eager. try: y -= self.smoothing * (y - 1. / tf.cast(y.shape[-1], tf.float32)) except RuntimeError: y.assign_sub(self.smoothing * (y - 1. / tf.cast(y.shape[-1], tf.float32))) logits = [self.model.get_logits(x, **kwargs) for x in x] loss = sum( softmax_cross_entropy_with_logits(labels=y, logits=logit) for logit in logits) warnings.warn("LossCrossEntropy is deprecated, switch to " "CrossEntropy. LossCrossEntropy may be removed on " "or after 2019-03-06.") return loss
Example #7
Source File: utils_tf.py From neural-fingerprinting with BSD 3-Clause "New" or "Revised" License | 6 votes |
def model_argmax(sess, x, predictions, samples, feed=None): """ Helper function that computes the current class prediction :param sess: TF session :param x: the input placeholder :param predictions: the model's symbolic output :param samples: numpy array with input samples (dims must match x) :param feed: An optional dictionary that is appended to the feeding dictionary before the session runs. Can be used to feed the learning phase of a Keras model for instance. :return: the argmax output of predictions, i.e. the current predicted class """ feed_dict = {x: samples} if feed is not None: feed_dict.update(feed) probabilities = sess.run(predictions, feed_dict) if samples.shape[0] == 1: return np.argmax(probabilities) else: return np.argmax(probabilities, axis=1)
Example #8
Source File: tensor.py From spleeter with MIT License | 6 votes |
def pad_and_reshape(instr_spec, frame_length, F): """ :param instr_spec: :param frame_length: :param F: :returns: """ spec_shape = tf.shape(instr_spec) extension_row = tf.zeros((spec_shape[0], spec_shape[1], 1, spec_shape[-1])) n_extra_row = (frame_length) // 2 + 1 - F extension = tf.tile(extension_row, [1, 1, n_extra_row, 1]) extended_spec = tf.concat([instr_spec, extension], axis=2) old_shape = tf.shape(extended_spec) new_shape = tf.concat([ [old_shape[0] * old_shape[1]], old_shape[2:]], axis=0) processed_instr_spec = tf.reshape(extended_spec, new_shape) return processed_instr_spec
Example #9
Source File: tensor.py From spleeter with MIT License | 6 votes |
def check_tensor_shape(tensor_tf, target_shape): """ Return a Tensorflow boolean graph that indicates whether sample[features_key] has the specified target shape. Only check not None entries of target_shape. :param tensor_tf: Tensor to check shape for. :param target_shape: Target shape to compare tensor to. :returns: True if shape is valid, False otherwise (as TF boolean). """ result = tf.constant(True) for i, target_length in enumerate(target_shape): if target_length: result = tf.logical_and( result, tf.equal(tf.constant(target_length), tf.shape(tensor_tf)[i])) return result
Example #10
Source File: __init__.py From spleeter with MIT License | 6 votes |
def _inverse_stft(self, stft_t, time_crop=None): """ Inverse and reshape the given STFT :param stft_t: input STFT :returns: inverse STFT (waveform) """ inversed = inverse_stft( tf.transpose(stft_t, perm=[2, 0, 1]), self._frame_length, self._frame_step, window_fn=lambda frame_length, dtype: ( hann_window(frame_length, periodic=True, dtype=dtype)) ) * self.WINDOW_COMPENSATION_FACTOR reshaped = tf.transpose(inversed) if time_crop is None: time_crop = tf.shape(self._features['waveform'])[0] return reshaped[:time_crop, :]
Example #11
Source File: tf_logits.py From Black-Box-Audio with MIT License | 5 votes |
def get_logits(new_input, length, first=[]): """ Compute the logits for a given waveform. First, preprocess with the TF version of MFC above, and then call DeepSpeech on the features. """ # new_input = tf.Print(new_input, [tf.shape(new_input)]) # We need to init DeepSpeech the first time we're called if first == []: first.append(False) # Okay, so this is ugly again. # We just want it to not crash. tf.app.flags.FLAGS.alphabet_config_path = "DeepSpeech/data/alphabet.txt" DeepSpeech.initialize_globals() print('initialized deepspeech globals') batch_size = new_input.get_shape()[0] # 1. Compute the MFCCs for the input audio # (this is differentable with our implementation above) empty_context = np.zeros((batch_size, 9, 26), dtype=np.float32) new_input_to_mfcc = compute_mfcc(new_input)[:, ::2] features = tf.concat((empty_context, new_input_to_mfcc, empty_context), 1) # 2. We get to see 9 frames at a time to make our decision, # so concatenate them together. features = tf.reshape(features, [new_input.get_shape()[0], -1]) features = tf.stack([features[:, i:i+19*26] for i in range(0,features.shape[1]-19*26+1,26)],1) features = tf.reshape(features, [batch_size, -1, 19*26]) # 3. Whiten the data mean, var = tf.nn.moments(features, axes=[0,1,2]) features = (features-mean)/(var**.5) # 4. Finally we process it with DeepSpeech logits = DeepSpeech.BiRNN(features, length, [0]*10) return logits
Example #12
Source File: modeling.py From BERT-Classification-Tutorial with Apache License 2.0 | 5 votes |
def get_sequence_output(self): """Gets final hidden layer of encoder. Returns: float Tensor of shape [batch_size, seq_length, hidden_size] corresponding to the final hidden of the transformer encoder. """ return self.sequence_output
Example #13
Source File: modeling.py From BERT-Classification-Tutorial with Apache License 2.0 | 5 votes |
def get_embedding_output(self): """Gets output of the embedding lookup (i.e., input to the transformer). Returns: float Tensor of shape [batch_size, seq_length, hidden_size] corresponding to the output of the embedding layer, after summing the word embeddings with the positional embeddings and the token type embeddings, then performing layer normalization. This is the input to the transformer. """ return self.embedding_output
Example #14
Source File: modeling.py From BERT-Classification-Tutorial with Apache License 2.0 | 5 votes |
def create_attention_mask_from_input_mask(from_tensor, to_mask): """Create 3D attention mask from a 2D tensor mask. Args: from_tensor: 2D or 3D Tensor of shape [batch_size, from_seq_length, ...]. to_mask: int32 Tensor of shape [batch_size, to_seq_length]. Returns: float Tensor of shape [batch_size, from_seq_length, to_seq_length]. """ from_shape = get_shape_list(from_tensor, expected_rank=[2, 3]) batch_size = from_shape[0] from_seq_length = from_shape[1] to_shape = get_shape_list(to_mask, expected_rank=2) to_seq_length = to_shape[1] to_mask = tf.cast( tf.reshape(to_mask, [batch_size, 1, to_seq_length]), tf.float32) # We don't assume that `from_tensor` is a mask (although it could be). We # don't actually care if we attend *from* padding tokens (only *to* padding) # tokens so we create a tensor of all ones. # # `broadcast_ones` = [batch_size, from_seq_length, 1] broadcast_ones = tf.ones( shape=[batch_size, from_seq_length, 1], dtype=tf.float32) # Here we broadcast along two dimensions to create the mask. mask = broadcast_ones * to_mask return mask
Example #15
Source File: modeling.py From BERT-Classification-Tutorial with Apache License 2.0 | 5 votes |
def get_shape_list(tensor, expected_rank=None, name=None): """Returns a list of the shape of tensor, preferring static dimensions. Args: tensor: A tf.Tensor object to find the shape of. expected_rank: (optional) int. The expected rank of `tensor`. If this is specified and the `tensor` has a different rank, and exception will be thrown. name: Optional name of the tensor for the error message. Returns: A list of dimensions of the shape of tensor. All static dimensions will be returned as python integers, and dynamic dimensions will be returned as tf.Tensor scalars. """ if name is None: name = tensor.name if expected_rank is not None: assert_rank(tensor, expected_rank, name) shape = tensor.shape.as_list() non_static_indexes = [] for (index, dim) in enumerate(shape): if dim is None: non_static_indexes.append(index) if not non_static_indexes: return shape dyn_shape = tf.shape(tensor) for index in non_static_indexes: shape[index] = dyn_shape[index] return shape
Example #16
Source File: modeling.py From BERT-Classification-Tutorial with Apache License 2.0 | 5 votes |
def assert_rank(tensor, expected_rank, name=None): """Raises an exception if the tensor rank is not of the expected rank. Args: tensor: A tf.Tensor to check the rank of. expected_rank: Python integer or list of integers, expected rank. name: Optional name of the tensor for the error message. Raises: ValueError: If the expected shape doesn't match the actual shape. """ if name is None: name = tensor.name expected_rank_dict = {} if isinstance(expected_rank, six.integer_types): expected_rank_dict[expected_rank] = True else: for x in expected_rank: expected_rank_dict[x] = True actual_rank = tensor.shape.ndims if actual_rank not in expected_rank_dict: scope_name = tf.get_variable_scope().name raise ValueError( "For the tensor `%s` in scope `%s`, the actual rank " "`%d` (shape = %s) is not equal to the expected rank `%s`" % (name, scope_name, actual_rank, str(tensor.shape), str(expected_rank)))
Example #17
Source File: custom_objects.py From keras_mixnets with MIT License | 5 votes |
def __call__(self, shape, dtype=None): dtype = dtype or K.floatx() init_range = 1.0 / np.sqrt(shape[1]) return tf.random_uniform(shape, -init_range, init_range, dtype=dtype) # Obtained from https://github.com/tensorflow/tpu/blob/master/models/official/efficientnet/efficientnet_model.py
Example #18
Source File: custom_objects.py From keras_mixnets with MIT License | 5 votes |
def call(self, inputs, training=None): def drop_connect(): keep_prob = 1.0 - self.drop_connect_rate # Compute drop_connect tensor batch_size = tf.shape(inputs)[0] random_tensor = keep_prob random_tensor += tf.random_uniform([batch_size, 1, 1, 1], dtype=inputs.dtype) binary_tensor = tf.floor(random_tensor) output = (inputs / keep_prob) * binary_tensor return output return K.in_train_phase(drop_connect, inputs, training=training)
Example #19
Source File: tagger.py From convseg with MIT License | 5 votes |
def build_input_graph(self, vocab_size, emb_size, word_vocab_size, word_emb_size, word_window_size): """ Gather embeddings from lookup tables. """ seq_ids = tf.placeholder(dtype=INT_TYPE, shape=[None, None], name='seq_ids') seq_word_ids = [tf.placeholder(dtype=INT_TYPE, shape=[None, None], name='seq_feature_%d_ids' % i) for i in range(word_window_size)] embeddings = tf.get_variable('embeddings', [vocab_size, emb_size]) embedding_output = tf.nn.embedding_lookup([embeddings], seq_ids) word_outputs = [] word_embeddings = tf.get_variable('word_embeddings', [word_vocab_size, word_emb_size]) for i in range(word_window_size): word_outputs.append(tf.nn.embedding_lookup([word_embeddings], seq_word_ids[i])) return seq_ids, seq_word_ids, tf.concat([embedding_output] + word_outputs, 2, 'inputs')
Example #20
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 #21
Source File: sequence.py From icme2019 with MIT License | 5 votes |
def call(self, seq_value_len_list, mask=None, **kwargs): if self.supports_masking: if mask is None: raise ValueError( "When supports_masking=True,input must support masking") uiseq_embed_list = seq_value_len_list mask = tf.to_float(mask) user_behavior_length = tf.reduce_sum(mask, axis=-1, keep_dims=True) mask = tf.expand_dims(mask, axis=2) else: uiseq_embed_list, user_behavior_length = seq_value_len_list mask = tf.sequence_mask(user_behavior_length, self.seq_len_max, dtype=tf.float32) mask = tf.transpose(mask, (0, 2, 1)) embedding_size = uiseq_embed_list.shape[-1] mask = tf.tile(mask, [1, 1, embedding_size]) uiseq_embed_list *= mask hist = uiseq_embed_list if self.mode == "max": return tf.reduce_max(hist, 1, keep_dims=True) hist = tf.reduce_sum(hist, 1, keep_dims=False) if self.mode == "mean": hist = tf.div(hist, user_behavior_length+self.eps) hist = tf.expand_dims(hist, axis=1) return hist
Example #22
Source File: sequence.py From icme2019 with MIT License | 5 votes |
def build(self, input_shape): embedding_size = input_shape[0][-1].value self.seq_len_max = input_shape[0][-2].value self.W_Query = self.add_weight(name='query', shape=[embedding_size, self.att_embedding_size * self.head_num], dtype=tf.float32, initializer=tf.keras.initializers.TruncatedNormal(seed=self.seed)) self.W_key = self.add_weight(name='key', shape=[embedding_size, self.att_embedding_size * self.head_num], dtype=tf.float32, initializer=tf.keras.initializers.TruncatedNormal(seed=self.seed + 1)) self.W_Value = self.add_weight(name='value', shape=[embedding_size, self.att_embedding_size * self.head_num], dtype=tf.float32, initializer=tf.keras.initializers.TruncatedNormal(seed=self.seed + 2)) # if self.use_res: # self.W_Res = self.add_weight(name='res', shape=[embedding_size, self.att_embedding_size * self.head_num], dtype=tf.float32, # initializer=tf.keras.initializers.TruncatedNormal(seed=self.seed)) if self.use_feed_forward: self.fw1 = self.add_weight('fw1', shape=[self.num_units, 4 * self.num_units], dtype=tf.float32, initializer=tf.keras.initializers.glorot_uniform(seed=self.seed)) self.fw2 = self.add_weight('fw2', shape=[4 * self.num_units, self.num_units], dtype=tf.float32, initializer=tf.keras.initializers.glorot_uniform(seed=self.seed)) if self.use_positional_encoding: self.kpe = Position_Embedding(input_shape[0][-1].value) self.qpe = Position_Embedding(input_shape[1][-1].value) self.dropout = tf.keras.layers.Dropout( self.dropout_rate, seed=self.seed) self.ln = LayerNormalization() # Be sure to call this somewhere! super(Transformer, self).build(input_shape)
Example #23
Source File: wavegan.py From cwavegan with MIT License | 5 votes |
def conv1d_transpose( inputs, filters, kernel_width, stride=4, padding='same', upsample='zeros'): if upsample == 'zeros': return tf.layers.conv2d_transpose( tf.expand_dims(inputs, axis=1), filters, (1, kernel_width), strides=(1, stride), padding='same' )[:, 0] elif upsample == 'nn': batch_size = tf.shape(inputs)[0] _, w, nch = inputs.get_shape().as_list() x = inputs x = tf.expand_dims(x, axis=1) x = tf.image.resize_nearest_neighbor(x, [1, w * stride]) x = x[:, 0] return tf.layers.conv1d( x, filters, kernel_width, 1, padding='same') else: raise NotImplementedError
Example #24
Source File: tfutil.py From disentangling_conditional_gans with MIT License | 5 votes |
def shape_to_list(shape): return [dim.value for dim in shape]
Example #25
Source File: tfutil.py From disentangling_conditional_gans with MIT License | 5 votes |
def set_vars(var_to_value_dict): ops = [] feed_dict = {} for var, value in var_to_value_dict.items(): assert is_tf_expression(var) try: setter = tf.get_default_graph().get_tensor_by_name(var.name.replace(':0', '/setter:0')) # look for existing op except KeyError: with absolute_name_scope(var.name.split(':')[0]): with tf.control_dependencies(None): # ignore surrounding control_dependencies setter = tf.assign(var, tf.placeholder(var.dtype, var.shape, 'new_value'), name='setter') # create new setter ops.append(setter) feed_dict[setter.op.inputs[1]] = value run(ops, feed_dict) #---------------------------------------------------------------------------- # Autosummary creates an identity op that internally keeps track of the input # values and automatically shows up in TensorBoard. The reported value # represents an average over input components. The average is accumulated # constantly over time and flushed when save_summaries() is called. # # Notes: # - The output tensor must be used as an input for something else in the # graph. Otherwise, the autosummary op will not get executed, and the average # value will not get accumulated. # - It is perfectly fine to include autosummaries with the same name in # several places throughout the graph, even if they are executed concurrently. # - It is ok to also pass in a python scalar or numpy array. In this case, it # is added to the average immediately.
Example #26
Source File: tfutil.py From disentangling_conditional_gans with MIT License | 5 votes |
def __init__( self, name = 'Train', tf_optimizer = 'tf.train.AdamOptimizer', learning_rate = 0.001, use_loss_scaling = False, loss_scaling_init = 64.0, loss_scaling_inc = 0.0005, loss_scaling_dec = 1.0, **kwargs): # Init fields. self.name = name self.learning_rate = tf.convert_to_tensor(learning_rate) self.id = self.name.replace('/', '.') self.scope = tf.get_default_graph().unique_name(self.id) self.optimizer_class = import_obj(tf_optimizer) self.optimizer_kwargs = dict(kwargs) self.use_loss_scaling = use_loss_scaling self.loss_scaling_init = loss_scaling_init self.loss_scaling_inc = loss_scaling_inc self.loss_scaling_dec = loss_scaling_dec self._grad_shapes = None # [shape, ...] self._dev_opt = OrderedDict() # device => optimizer self._dev_grads = OrderedDict() # device => [[(grad, var), ...], ...] self._dev_ls_var = OrderedDict() # device => variable (log2 of loss scaling factor) self._updates_applied = False # Register the gradients of the given loss function with respect to the given variables. # Intended to be called once per GPU.
Example #27
Source File: tfutil.py From disentangling_conditional_gans with MIT License | 5 votes |
def register_gradients(self, loss, vars): assert not self._updates_applied # Validate arguments. if isinstance(vars, dict): vars = list(vars.values()) # allow passing in Network.trainables as vars assert isinstance(vars, list) and len(vars) >= 1 assert all(is_tf_expression(expr) for expr in vars + [loss]) if self._grad_shapes is None: self._grad_shapes = [shape_to_list(var.shape) for var in vars] assert len(vars) == len(self._grad_shapes) assert all(shape_to_list(var.shape) == var_shape for var, var_shape in zip(vars, self._grad_shapes)) dev = loss.device assert all(var.device == dev for var in vars) # Register device and compute gradients. with tf.name_scope(self.id + '_grad'), tf.device(dev): if dev not in self._dev_opt: opt_name = self.scope.replace('/', '_') + '_opt%d' % len(self._dev_opt) self._dev_opt[dev] = self.optimizer_class(name=opt_name, learning_rate=self.learning_rate, **self.optimizer_kwargs) self._dev_grads[dev] = [] loss = self.apply_loss_scaling(tf.cast(loss, tf.float32)) grads = self._dev_opt[dev].compute_gradients(loss, vars, gate_gradients=tf.train.Optimizer.GATE_NONE) # disable gating to reduce memory usage grads = [(g, v) if g is not None else (tf.zeros_like(v), v) for g, v in grads] # replace disconnected gradients with zeros self._dev_grads[dev].append(grads) # Construct training op to update the registered variables based on their gradients.
Example #28
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 #29
Source File: loss.py From disentangling_conditional_gans with MIT License | 5 votes |
def tf_repeat(tensor, repeats): expanded_tensor = tf.expand_dims(tensor, -1) multiples = [1] + repeats tiled_tensor = tf.tile(expanded_tensor, multiples = multiples) repeated_tensor = tf.reshape(tiled_tensor, tf.shape(tensor) * repeats) return repeated_tensor #---------------------------------------------------------------------------- # Generator loss function used in the paper (WGAN + AC-GAN).
Example #30
Source File: networks.py From disentangling_conditional_gans with MIT License | 5 votes |
def get_weight(shape, gain=np.sqrt(2), use_wscale=False, fan_in=None): if fan_in is None: fan_in = np.prod(shape[:-1]) std = gain / np.sqrt(fan_in) # He init if use_wscale: wscale = tf.constant(np.float32(std), name='wscale') return tf.get_variable('weight', shape=shape, initializer=tf.initializers.random_normal()) * wscale else: return tf.get_variable('weight', shape=shape, initializer=tf.initializers.random_normal(0, std)) #---------------------------------------------------------------------------- # Fully-connected layer.