Python tensorflow.compat.v1.control_dependencies() Examples
The following are 30
code examples of tensorflow.compat.v1.control_dependencies().
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.compat.v1
, or try the search function
.
Example #1
Source File: transformer_memory.py From tensor2tensor with Apache License 2.0 | 6 votes |
def post_attention(self, token, x): """Called after self-attention. The memory can be updated here. Args: token: Data returned by pre_attention, which can be used to carry over state related to the current memory operation. x: a Tensor of data after self-attention and feed-forward Returns: a (possibly modified) version of the input x """ with tf.variable_scope(self.name + "/post_attention", reuse=tf.AUTO_REUSE): depth = common_layers.shape_list(x)[-1] actual_batch_size = common_layers.shape_list(x)[0] memory_output = tf.gather(token["retrieved_mem"], tf.range(actual_batch_size)) output = tf.add(tf.layers.dense(x, depth, use_bias=False), tf.layers.dense(memory_output, depth)) with tf.control_dependencies([output]): with tf.control_dependencies([ self.write(token["x"], token["access_logits"])]): return tf.identity(output)
Example #2
Source File: ppo_learner.py From tensor2tensor with Apache License 2.0 | 6 votes |
def simulate(self, action): # There is subtlety here. We need to collect data # obs, action = policy(obs), done, reward = env(abs, action) # Thus we need to enqueue data before assigning new observation reward, done = self._batch_env.simulate(action) with tf.control_dependencies([reward, done]): enqueue_op = self.speculum.enqueue( [self._observ.read_value(), reward, done, action]) with tf.control_dependencies([enqueue_op]): assign = self._observ.assign(self._batch_env.observ) with tf.control_dependencies([assign]): return tf.identity(reward), tf.identity(done)
Example #3
Source File: tf_atari_wrappers.py From tensor2tensor with Apache License 2.0 | 6 votes |
def simulate(self, action): reward, done = self._batch_env.simulate(action) with tf.control_dependencies([reward, done]): new_observ = tf.expand_dims(self._batch_env.observ, axis=1) # If we shouldn't stack, i.e. self.history == 1, then just assign # new_observ to self._observ and return from here. if self.history == 1: with tf.control_dependencies([self._observ.assign(new_observ)]): return tf.identity(reward), tf.identity(done) # If we should stack, then do the required work. old_observ = tf.gather( self._observ.read_value(), list(range(1, self.history)), axis=1) with tf.control_dependencies([new_observ, old_observ]): with tf.control_dependencies([self._observ.assign( tf.concat([old_observ, new_observ], axis=1))]): return tf.identity(reward), tf.identity(done)
Example #4
Source File: transformer_memory_test.py From tensor2tensor with Apache License 2.0 | 6 votes |
def testRead(self): batch_size = 2 key_depth = 3 val_depth = 5 memory_size = 4 window_size = 6 x_depth = 10 memory = transformer_memory.TransformerMemory( batch_size, key_depth, val_depth, memory_size) x = tf.random_uniform([batch_size, window_size, x_depth], minval=1.0) vals = tf.random_uniform([batch_size, memory_size, val_depth], minval=1.0) logits = tf.random_uniform([batch_size, memory_size], minval=1.0) update_op = memory.set(vals, logits) with tf.control_dependencies([update_op]): logits, retrieved_values = memory.read(x) with self.test_session() as session: session.run(tf.global_variables_initializer()) logits_values, values = session.run([logits, retrieved_values]) self.assertAllEqual([batch_size, window_size, memory_size], logits_values.shape) self.assertAllEqual([batch_size, window_size, val_depth], values.shape)
Example #5
Source File: transformer_memory_test.py From tensor2tensor with Apache License 2.0 | 6 votes |
def testWrite(self): batch_size = 2 key_depth = 3 val_depth = 5 memory_size = 4 window_size = 6 x_depth = 10 memory = transformer_memory.TransformerMemory( batch_size, key_depth, val_depth, memory_size) x = tf.random_uniform([batch_size, window_size, x_depth], minval=1.0) vals = tf.random_uniform([batch_size, memory_size, val_depth], minval=1.0) logits = tf.random_uniform([batch_size, memory_size], minval=1.0) update_op = memory.set(vals, logits) with tf.control_dependencies([update_op]): logits, _ = memory.read(x) write_op = memory.write(x, logits) mem_vals, mem_logits = memory.get() with self.test_session() as session: session.run(tf.global_variables_initializer()) session.run(write_op) updated_vals, updated_logits = session.run([mem_vals, mem_logits]) self.assertAllEqual([batch_size, memory_size, val_depth], updated_vals.shape) self.assertAllEqual([batch_size, memory_size], updated_logits.shape)
Example #6
Source File: transformer_memory_test.py From tensor2tensor with Apache License 2.0 | 6 votes |
def testLoss(self): batch_size = 2 key_depth = 5 val_depth = 5 memory_size = 4 window_size = 3 x_depth = 5 memory = transformer_memory.TransformerMemory( batch_size, key_depth, val_depth, memory_size) x = tf.random_uniform([batch_size, window_size, x_depth], minval=.0) memory_results, _, _, _ = ( memory.pre_attention( tf.random_uniform([batch_size], minval=0, maxval=1, dtype=tf.int32), x, None, None)) x = memory.post_attention(memory_results, x) with tf.control_dependencies([tf.print("x", x)]): is_nan = tf.reduce_any(tf.math.is_nan(x)) with self.test_session() as session: session.run(tf.global_variables_initializer()) for _ in range(100): is_nan_value, _ = session.run([is_nan, x]) self.assertEqual(is_nan_value, False)
Example #7
Source File: common_layers.py From tensor2tensor with Apache License 2.0 | 6 votes |
def fn_device_dependency(name, device=""): """Add control deps for name and device.""" key = name + "_" + device outs = [] def body(): with tf.control_dependencies(fn_device_dependency_dict()[key]): yield outs assert outs deps = outs if isinstance(outs[0], (list, tuple)): assert len(outs) == 1 deps = outs[0] fn_device_dependency_dict()[key] = deps if device: with tf.device(device): return body() else: return body()
Example #8
Source File: gaussian_query.py From privacy with Apache License 2.0 | 6 votes |
def get_noised_result(self, sample_state, global_state): """See base class.""" if LooseVersion(tf.__version__) < LooseVersion('2.0.0'): def add_noise(v): return v + tf.random.normal( tf.shape(input=v), stddev=global_state.stddev, dtype=v.dtype) else: random_normal = tf.random_normal_initializer( stddev=global_state.stddev) def add_noise(v): return v + tf.cast(random_normal(tf.shape(input=v)), dtype=v.dtype) if self._ledger: dependencies = [ self._ledger.record_sum_query( global_state.l2_norm_clip, global_state.stddev) ] else: dependencies = [] with tf.control_dependencies(dependencies): return tf.nest.map_structure(add_noise, sample_state), global_state
Example #9
Source File: privacy_ledger.py From privacy with Apache License 2.0 | 6 votes |
def record_sum_query(self, l2_norm_bound, noise_stddev): """Records that a query was issued. Args: l2_norm_bound: The maximum l2 norm of the tensor group in the query. noise_stddev: The standard deviation of the noise applied to the sum. Returns: An operation recording the sum query to the ledger. """ def _do_record_query(): with tf.control_dependencies( [tf.assign(self._query_count, self._query_count + 1)]): return self._query_buffer.append( [self._sample_count, l2_norm_bound, noise_stddev]) return self._cs.execute(_do_record_query)
Example #10
Source File: tensor_buffer_test_graph.py From privacy with Apache License 2.0 | 6 votes |
def test_noresize(self): """Test buffer does not resize if capacity is not exceeded.""" with self.cached_session() as sess: size, shape = 2, [2, 3] my_buffer = tensor_buffer.TensorBuffer(size, shape, name='my_buffer') value1 = [[1, 2, 3], [4, 5, 6]] with tf.control_dependencies([my_buffer.append(value1)]): value2 = [[7, 8, 9], [10, 11, 12]] with tf.control_dependencies([my_buffer.append(value2)]): values = my_buffer.values current_size = my_buffer.current_size capacity = my_buffer.capacity self.evaluate(tf.global_variables_initializer()) v, cs, cap = sess.run([values, current_size, capacity]) self.assertAllEqual(v, [value1, value2]) self.assertEqual(cs, 2) self.assertEqual(cap, 2)
Example #11
Source File: variable_mgr.py From benchmarks with Apache License 2.0 | 6 votes |
def append_apply_gradients_ops(self, gradient_state, opt, grads, training_ops, loss_scale_params): device_grads = gradient_state # From 2nd result of preprocess_device_grads. def get_apply_gradients_ops_func(): """Returns a list of ops for updating gradients.""" apply_gradients_ops = [] # For each variable, apply the combined gradients for this server on # the parameter server, and then wait for all other servers to do this. for i, (g, v) in enumerate(grads): apply_gradient_op = opt.apply_gradients([(g, v)]) barrier = self.benchmark_cnn.add_sync_queues_and_barrier( 'replicate_variable_%s' % i, [apply_gradient_op]) with tf.control_dependencies([barrier]): with tf.device(self.benchmark_cnn.cpu_device): updated_value = v.read_value() for my_d in range(len(self.benchmark_cnn.devices)): apply_gradients_ops.append( device_grads[my_d][i][1].assign(updated_value)) return apply_gradients_ops variable_mgr_util.append_gradients_with_loss_scale( training_ops, get_apply_gradients_ops_func, loss_scale_params, self.grad_has_inf_nan)
Example #12
Source File: tensor_buffer_test_graph.py From privacy with Apache License 2.0 | 6 votes |
def test_resize(self): """Test buffer resizes if capacity is exceeded.""" with self.cached_session() as sess: size, shape = 2, [2, 3] my_buffer = tensor_buffer.TensorBuffer(size, shape, name='my_buffer') value1 = [[1, 2, 3], [4, 5, 6]] with tf.control_dependencies([my_buffer.append(value1)]): value2 = [[7, 8, 9], [10, 11, 12]] with tf.control_dependencies([my_buffer.append(value2)]): value3 = [[13, 14, 15], [16, 17, 18]] with tf.control_dependencies([my_buffer.append(value3)]): values = my_buffer.values current_size = my_buffer.current_size capacity = my_buffer.capacity self.evaluate(tf.global_variables_initializer()) v, cs, cap = sess.run([values, current_size, capacity]) self.assertAllEqual(v, [value1, value2, value3]) self.assertEqual(cs, 3) self.assertEqual(cap, 4)
Example #13
Source File: transformer_memory_test.py From tensor2tensor with Apache License 2.0 | 6 votes |
def testReset(self): batch_size = 2 key_depth = 3 val_depth = 5 memory_size = 4 memory = transformer_memory.TransformerMemory( batch_size, key_depth, val_depth, memory_size) vals = tf.random_uniform([batch_size, memory_size, val_depth], minval=1.0) logits = tf.random_uniform([batch_size, memory_size], minval=1.0) update_op = memory.set(vals, logits) reset_op = memory.reset([1]) mem_vals, mem_logits = memory.get() assert_op1 = tf.assert_equal(mem_vals[0], vals[0]) assert_op2 = tf.assert_equal(mem_logits[0], logits[0]) with tf.control_dependencies([assert_op1, assert_op2]): all_zero1 = tf.reduce_sum(tf.abs(mem_vals[1])) all_zero2 = tf.reduce_sum(tf.abs(mem_logits[1])) with self.test_session() as session: session.run(tf.global_variables_initializer()) session.run(update_op) session.run(reset_op) zero1, zero2 = session.run([all_zero1, all_zero2]) self.assertAllEqual(0, zero1) self.assertAllEqual(0, zero2)
Example #14
Source File: dataloader.py From Object_Detection_Tracking with Apache License 2.0 | 6 votes |
def pad_to_fixed_size(data, pad_value, output_shape): """Pad data to a fixed length at the first dimension. Args: data: Tensor to be padded to output_shape. pad_value: A constant value assigned to the paddings. output_shape: The output shape of a 2D tensor. Returns: The Padded tensor with output_shape [max_num_instances, dimension]. """ max_num_instances = output_shape[0] dimension = output_shape[1] data = tf.reshape(data, [-1, dimension]) num_instances = tf.shape(data)[0] assert_length = tf.Assert( tf.less_equal(num_instances, max_num_instances), [num_instances]) with tf.control_dependencies([assert_length]): pad_length = max_num_instances - num_instances paddings = pad_value * tf.ones([pad_length, dimension]) padded_data = tf.concat([data, paddings], axis=0) padded_data = tf.reshape(padded_data, output_shape) return padded_data
Example #15
Source File: multistep_optimizer.py From tensor2tensor with Apache License 2.0 | 6 votes |
def _finish(self, update_ops, name_scope): """Updates beta_power variables every n batches and incrs counter.""" iter_ = self._get_iter_variable() beta1_power, beta2_power = self._get_beta_accumulators() with tf.control_dependencies(update_ops): with tf.colocate_with(iter_): def update_beta_op(): update_beta1 = beta1_power.assign( beta1_power * self._beta1_t, use_locking=self._use_locking) update_beta2 = beta2_power.assign( beta2_power * self._beta2_t, use_locking=self._use_locking) return tf.group(update_beta1, update_beta2) maybe_update_beta = tf.cond( tf.equal(iter_, 0), update_beta_op, tf.no_op) with tf.control_dependencies([maybe_update_beta]): update_iter = iter_.assign(tf.mod(iter_ + 1, self._n_t), use_locking=self._use_locking) return tf.group( *update_ops + [update_iter, maybe_update_beta], name=name_scope)
Example #16
Source File: simulated_batch_gym_env.py From tensor2tensor with Apache License 2.0 | 6 votes |
def __init__(self, *args, **kwargs): with tf.Graph().as_default(): self._batch_env = SimulatedBatchEnv(*args, **kwargs) self._actions_t = tf.placeholder(shape=(self.batch_size,), dtype=tf.int32) self._rewards_t, self._dones_t = self._batch_env.simulate(self._actions_t) with tf.control_dependencies([self._rewards_t]): self._obs_t = self._batch_env.observ self._indices_t = tf.placeholder(shape=(self.batch_size,), dtype=tf.int32) self._reset_op = self._batch_env.reset( tf.range(self.batch_size, dtype=tf.int32) ) self._sess = tf.Session() self._sess.run(tf.global_variables_initializer()) self._batch_env.initialize(self._sess)
Example #17
Source File: optimize.py From tensor2tensor with Apache License 2.0 | 6 votes |
def weight_decay_and_noise(loss, hparams, learning_rate, var_list=None): """Apply weight decay and weight noise.""" if var_list is None: var_list = tf.trainable_variables() decay_vars = [v for v in var_list] noise_vars = [v for v in var_list if "/body/" in v.name] weight_decay_loss = weight_decay(hparams.weight_decay, decay_vars) if hparams.weight_decay and common_layers.should_generate_summaries(): tf.summary.scalar("losses/weight_decay", weight_decay_loss) weight_noise_ops = weight_noise(hparams.weight_noise, learning_rate, noise_vars) with tf.control_dependencies(weight_noise_ops): loss = tf.identity(loss) loss += weight_decay_loss return loss
Example #18
Source File: multistep_optimizer.py From tensor2tensor with Apache License 2.0 | 6 votes |
def _apply_cond(self, apply_fn, grad, var, *args, **kwargs): """Apply conditionally if counter is zero.""" grad_acc = self.get_slot(var, "grad_acc") def apply_adam(grad_acc, apply_fn, grad, var, *args, **kwargs): total_grad = (grad_acc + grad) / tf.cast(self._n_t, grad.dtype) adam_op = apply_fn(total_grad, var, *args, **kwargs) with tf.control_dependencies([adam_op]): grad_acc_to_zero_op = grad_acc.assign(tf.zeros_like(grad_acc), use_locking=self._use_locking) return tf.group(adam_op, grad_acc_to_zero_op) def accumulate_gradient(grad_acc, grad): assign_op = tf.assign_add(grad_acc, grad, use_locking=self._use_locking) return tf.group(assign_op) # Strip return value return tf.cond( tf.equal(self._get_iter_variable(), 0), lambda: apply_adam(grad_acc, apply_fn, grad, var, *args, **kwargs), lambda: accumulate_gradient(grad_acc, grad))
Example #19
Source File: lstm_models.py From magenta with Apache License 2.0 | 6 votes |
def _merge_decode_results(self, decode_results): """Merge in the output dimension.""" output_axis = -1 assert decode_results zipped_results = lstm_utils.LstmDecodeResults(*list(zip(*decode_results))) with tf.control_dependencies([ tf.assert_equal( zipped_results.final_sequence_lengths, self.hparams.max_seq_len, message='Variable length not supported by ' 'MultiOutCategoricalLstmDecoder.')]): if zipped_results.final_state[0] is None: final_state = None else: final_state = tf.nest.map_structure( lambda x: tf.concat(x, axis=output_axis), zipped_results.final_state) return lstm_utils.LstmDecodeResults( rnn_output=tf.concat(zipped_results.rnn_output, axis=output_axis), rnn_input=tf.concat(zipped_results.rnn_input, axis=output_axis), samples=tf.concat(zipped_results.samples, axis=output_axis), final_state=final_state, final_sequence_lengths=zipped_results.final_sequence_lengths[0])
Example #20
Source File: transformer_memory.py From tensor2tensor with Apache License 2.0 | 6 votes |
def post_attention(self, token, x): """Called after self-attention. The memory can be updated here. Args: token: Data returned by pre_attention, which can be used to carry over state related to the current memory operation. x: a Tensor of data after self-attention and feed-forward Returns: a (possibly modified) version of the input x """ with tf.control_dependencies([ self.previous_segment.assign(token[0]), self.previous_vals.assign(token[1]), self.previous_bias.assign(token[2]), ]): return tf.identity(x)
Example #21
Source File: multistep_with_adamoptimizer.py From tensor2tensor with Apache License 2.0 | 6 votes |
def _apply_sparse_shared(self, grad, var, indices, scatter_add): beta1_power, beta2_power = self._get_beta_accumulators() beta1_power = tf.cast(beta1_power, var.dtype.base_dtype) beta2_power = tf.cast(beta2_power, var.dtype.base_dtype) lr_t = tf.cast(self._lr_t, var.dtype.base_dtype) beta1_t = tf.cast(self._beta1_t, var.dtype.base_dtype) beta2_t = tf.cast(self._beta2_t, var.dtype.base_dtype) epsilon_t = tf.cast(self._epsilon_t, var.dtype.base_dtype) lr = (lr_t * tf.sqrt(1 - beta2_power) / (1 - beta1_power)) # m_t = beta1 * m + (1 - beta1) * g_t m = self.get_slot(var, "m") m_scaled_g_values = grad * (1 - beta1_t) m_t = tf.assign(m, m * beta1_t, use_locking=self._use_locking) with tf.control_dependencies([m_t]): m_t = scatter_add(m, indices, m_scaled_g_values) # v_t = beta2 * v + (1 - beta2) * (g_t * g_t) v = self.get_slot(var, "v") v_scaled_g_values = (grad * grad) * (1 - beta2_t) v_t = tf.assign(v, v * beta2_t, use_locking=self._use_locking) with tf.control_dependencies([v_t]): v_t = scatter_add(v, indices, v_scaled_g_values) v_sqrt = tf.sqrt(v_t) var_update = tf.assign_sub( var, lr * m_t / (v_sqrt + epsilon_t), use_locking=self._use_locking) return tf.group(*[var_update, m_t, v_t])
Example #22
Source File: yellowfin.py From tensor2tensor with Apache License 2.0 | 6 votes |
def _grad_sparsity(self): """Gradient sparsity.""" # If the sparse minibatch gradient has 10 percent of its entries # non-zero, its sparsity is 0.1. # The norm of dense gradient averaged from full dataset # are roughly estimated norm of minibatch # sparse gradient norm * sqrt(sparsity) # An extension maybe only correct the sparse blob. non_zero_cnt = tf.add_n([tf.count_nonzero(g) for g in self._grad]) all_entry_cnt = tf.add_n([tf.size(g) for g in self._grad]) self._sparsity = tf.cast(non_zero_cnt, self._grad[0].dtype) self._sparsity /= tf.cast(all_entry_cnt, self._grad[0].dtype) avg_op = self._moving_averager.apply([self._sparsity,]) with tf.control_dependencies([avg_op]): self._sparsity_avg = self._moving_averager.average(self._sparsity) return avg_op
Example #23
Source File: multistep_with_adamoptimizer.py From tensor2tensor with Apache License 2.0 | 6 votes |
def _apply_cond(self, apply_fn, grad, var, *args, **kwargs): """Apply conditionally if counter is zero.""" grad_acc = self.get_slot(var, "grad_acc") def apply_adam(grad_acc, apply_fn, grad, var, *args, **kwargs): total_grad = (grad_acc + grad) / tf.cast(self._n_t, grad.dtype) adam_op = apply_fn(total_grad, var, *args, **kwargs) with tf.control_dependencies([adam_op]): grad_acc_to_zero_op = grad_acc.assign( tf.zeros_like(grad_acc), use_locking=self._use_locking) return tf.group(adam_op, grad_acc_to_zero_op) def accumulate_gradient(grad_acc, grad): assign_op = tf.assign_add(grad_acc, grad, use_locking=self._use_locking) return tf.group(assign_op) # Strip return value return tf.cond( tf.equal(self._get_iter_variable(), 0), lambda: apply_adam(grad_acc, apply_fn, grad, var, *args, **kwargs), lambda: accumulate_gradient(grad_acc, grad))
Example #24
Source File: layers.py From tf-slim with Apache License 2.0 | 6 votes |
def _dense_inner_flatten(inputs, new_rank): """Helper function for `inner_flatten`.""" rank_assertion = check_ops.assert_rank_at_least( inputs, new_rank, message='inputs has rank less than new_rank') with ops.control_dependencies([rank_assertion]): outer_dimensions = array_ops.strided_slice( array_ops.shape(inputs), [0], [new_rank - 1]) new_shape = array_ops.concat((outer_dimensions, [-1]), 0) reshaped = array_ops.reshape(inputs, new_shape) # if `new_rank` is an integer, try to calculate new shape. if isinstance(new_rank, six.integer_types): static_shape = inputs.get_shape() if static_shape is not None and static_shape.dims is not None: static_shape = static_shape.as_list() static_outer_dims = static_shape[:new_rank - 1] static_inner_dims = static_shape[new_rank - 1:] flattened_dimension = 1 for inner_dim in static_inner_dims: if inner_dim is None: flattened_dimension = None break flattened_dimension *= inner_dim reshaped.set_shape(static_outer_dims + [flattened_dimension]) return reshaped
Example #25
Source File: tpu_util.py From morph-net with Apache License 2.0 | 6 votes |
def write_to_variable(tensor, fail_if_exists=True): """Saves a tensor for later retrieval on CPU.""" # Only relevant for debugging. debug_name = 'tpu_util__' + tensor.name.split(':')[0] reuse = False if fail_if_exists else tf.compat.v1.AUTO_REUSE with tf.variable_scope(top_level_scope, reuse=reuse): variable = tf.get_variable( name=debug_name, shape=tensor.shape, dtype=tensor.dtype, trainable=False, use_resource=True) var_store[tensor] = variable with tf.control_dependencies([variable.assign(tensor)]): tensor_copy = tf.identity(tensor) var_store[tensor_copy] = variable return tensor_copy
Example #26
Source File: generator_utils.py From tensor2tensor with Apache License 2.0 | 6 votes |
def _finalize(self, _, contents): """Structure output and compute segment and position metadata.""" # The output shape information is lost during the filter; however we can # guarantee the shape. (That's the point of this exercise, after all!) contents.set_shape((self._packed_length, self._num_sequences * 2)) # Both the dummy branch of the scan step function and the eviction dataset # use vectors of minus one. The cost of this check is negligible and the # leakage of such dummy sequences would be difficult to debug downstream. check_leaks = tf.assert_none_equal(contents, -tf.ones_like(contents)) with tf.control_dependencies([check_leaks]): contents = tf.identity(contents) segment, position = self._compute_auxiliary_structure(contents) return {"contents": contents[:, :self._num_sequences], "segment": segment, "position": position}
Example #27
Source File: common_layers.py From tensor2tensor with Apache License 2.0 | 6 votes |
def smoothing_cross_entropy_factored(a, b, labels, confidence): """Memory-efficient computation of smoothing cross-entropy. Avoids realizing the entire logits matrix at once. Args: a: a Tensor with shape [batch, inner_dim] b: a Tensor with shape [vocab_size, inner_dim] labels: an integer Tensor with shape [batch] confidence: a float Returns: A Tensor with shape [batch] """ num_splits = 16 vocab_size = shape_list(b)[0] labels = approximate_split(labels, num_splits) a = approximate_split(a, num_splits) parts = [] for part in range(num_splits): with tf.control_dependencies(parts[-1:]): logits = tf.matmul(a[part], b, transpose_b=True) parts.append( smoothing_cross_entropy(logits, labels[part], vocab_size, confidence)) return tf.concat(parts, 0)
Example #28
Source File: common_layers.py From tensor2tensor with Apache License 2.0 | 5 votes |
def build(self, input_shape=None): """Build `Layer`.""" input_shape = tf.TensorShape(input_shape).as_list() self.input_spec = layers().InputSpec(shape=input_shape) if not self.layer.built: self.layer.build(input_shape) self.layer.built = False if not hasattr(self.layer, "kernel"): raise ValueError("`WeightNorm` must wrap a layer that" " contains a `kernel` for weights") # The kernel's filter or unit dimension is -1 self.layer_depth = int(self.layer.kernel.shape[-1]) self.norm_axes = list(range(self.layer.kernel.shape.ndims - 1)) self.layer.v = self.layer.kernel self.layer.g = self.layer.add_variable( name="g", shape=(self.layer_depth,), initializer=tf.ones_initializer, dtype=self.layer.kernel.dtype, trainable=True) # with ops.control_dependencies([self.layer.g.assign( # self._init_norm(self.layer.v))]): # self._compute_weights() self._compute_weights() self.layer.built = True super(WeightNorm, self).build() self.built = True
Example #29
Source File: data.py From magenta with Apache License 2.0 | 5 votes |
def tflite_compat_mel(wav_audio, hparams): """EXPERIMENTAL: Log mel spec with ops that can be made TFLite compatible.""" samples, decoded_sample_rate = tf.audio.decode_wav( wav_audio, desired_channels=1) samples = tf.squeeze(samples, axis=1) # Ensure that we decoded the samples at the expected sample rate. with tf.control_dependencies( [tf.assert_equal(decoded_sample_rate, hparams.sample_rate)]): return tflite_compat_mel_from_samples(samples, hparams)
Example #30
Source File: transformer_memory.py From tensor2tensor with Apache License 2.0 | 5 votes |
def write(self, x, access_logits): """Write to the memory based on a combination of similarity and least used. Based on arXiv:1607.00036v2 [cs.LG]. Args: x: a tensor in the shape of [batch_size, length, depth]. access_logits: the logits for accessing the memory. Returns: the update op. """ gamma = tf.layers.dense(x, 1, activation=tf.sigmoid, name="gamma") write_logits = access_logits - gamma * tf.expand_dims(self.mean_logits, 1) candidate_value = tf.layers.dense(x, self.val_depth, activation=tf.nn.relu, name="candidate_value") erase_gates = tf.layers.dense(x, self.memory_size, activation=tf.nn.sigmoid, name="erase") write_weights = tf.nn.softmax(write_logits) erase_weights = tf.expand_dims(1 - erase_gates * write_weights, 3) erase = tf.multiply(erase_weights, tf.expand_dims(self.mem_vals, 1)) addition = tf.multiply( tf.expand_dims(write_weights, 3), tf.expand_dims(candidate_value, 2)) update_value_op = self.mem_vals.assign( tf.reduce_mean(erase + addition, axis=1)) with tf.control_dependencies([update_value_op]): write_op = self.mean_logits.assign( self.mean_logits * 0.1 + tf.reduce_mean(write_logits * 0.9, axis=1)) return write_op