Python tensorflow.clip_by_average_norm() Examples
The following are 9
code examples of tensorflow.clip_by_average_norm().
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: clip_ops_test.py From deep_image_model with Apache License 2.0 | 5 votes |
def testClipByAverageNormClipped(self): # Norm clipping when average clip_norm < 0.83333333 with self.test_session(): x = tf.constant([-3.0, 0.0, 0.0, 4.0, 0.0, 0.0], shape=[2, 3]) # Average norm of x = sqrt(3^2 + 4^2) / 6 = 0.83333333 np_ans = [[-2.88, 0.0, 0.0], [3.84, 0.0, 0.0]] clip_norm = 0.8 ans = tf.clip_by_average_norm(x, clip_norm) tf_ans = ans.eval() self.assertAllClose(np_ans, tf_ans)
Example #2
Source File: clip_ops_test.py From deep_image_model with Apache License 2.0 | 5 votes |
def testClipByAverageNormClippedTensor(self): # Norm clipping when average clip_norm < 0.83333333 with self.test_session(): x = tf.constant([-3.0, 0.0, 0.0, 4.0, 0.0, 0.0], shape=[2, 3]) # Average norm of x = sqrt(3^2 + 4^2) / 6 = 0.83333333 np_ans = [[-2.88, 0.0, 0.0], [3.84, 0.0, 0.0]] clip_norm = tf.constant(0.8) ans = tf.clip_by_average_norm(x, clip_norm) tf_ans = ans.eval() self.assertAllClose(np_ans, tf_ans)
Example #3
Source File: clip_ops_test.py From deep_image_model with Apache License 2.0 | 5 votes |
def testClipByAverageNormNotClipped(self): # No norm clipping when average clip_norm >= 0.83333333 with self.test_session(): x = tf.constant([-3.0, 0.0, 0.0, 4.0, 0.0, 0.0], shape=[2, 3]) # Average norm of x = sqrt(3^2 + 4^2) / 6 = 0.83333333 np_ans = [[-3.0, 0.0, 0.0], [4.0, 0.0, 0.0]] clip_norm = 0.9 ans = tf.clip_by_average_norm(x, clip_norm) tf_ans = ans.eval() self.assertAllClose(np_ans, tf_ans)
Example #4
Source File: clip_ops_test.py From deep_image_model with Apache License 2.0 | 5 votes |
def testClipByAverageNormZero(self): # No norm clipping when average clip_norm = 0 with self.test_session(): x = tf.constant([0.0, 0.0, 0.0, 0.0, 0.0, 0.0], shape=[2, 3]) # Average norm = 0, no changes np_ans = [[0.0, 0.0, 0.0], [0.0, 0.0, 0.0]] clip_norm = 0.9 ans = tf.clip_by_average_norm(x, clip_norm) tf_ans = ans.eval() self.assertAllClose(np_ans, tf_ans)
Example #5
Source File: loss.py From NS-Outpainting with MIT License | 5 votes |
def average_gradients(self, tower_grads): average_grads = [] for grad_and_vars in zip(*tower_grads): # Note that each grad_and_vars looks like the following: # ((grad0_gpu0, var0_gpu0), ... , (grad0_gpuN, var0_gpuN)) grads = [] # Average over the 'tower' dimension. g, _ = grad_and_vars[0] for g, _ in grad_and_vars: expanded_g = tf.expand_dims(g, 0) grads.append(expanded_g) grad = tf.concat(grads, axis=0) grad = tf.reduce_mean(grad, 0) # Keep in mind that the Variables are redundant because they are shared # across towers. So .. we will just return the first tower's pointer to # the Variable. v = grad_and_vars[0][1] grad_and_var = (grad, v) average_grads.append(grad_and_var) # clip if self.cfg.clip_gradient: gradients, variables = zip(*average_grads) gradients = [ None if gradient is None else tf.clip_by_average_norm(gradient, self.cfg.clip_gradient_value) for gradient in gradients] average_grads = zip(gradients, variables) return average_grads
Example #6
Source File: network.py From tensorflow-rl with Apache License 2.0 | 5 votes |
def _clip_grads(self, grads): if self.clip_norm_type == 'ignore': return grads elif self.clip_norm_type == 'global': return tf.clip_by_global_norm(grads, self.clip_norm)[0] elif self.clip_norm_type == 'avg': return tf.clip_by_average_norm(grads, self.clip_norm)[0] elif self.clip_norm_type == 'local': return [tf.clip_by_norm(g, self.clip_norm) for g in grads]
Example #7
Source File: network.py From tf-a3c-gpu with MIT License | 5 votes |
def __init__(self, nA, learning_rate,decay,grad_clip,entropy_beta, state_shape=[84,84,4], master=None, device_name='/gpu:0', scope_name='master'): with tf.device(device_name) : self.state = tf.placeholder(tf.float32,[None]+state_shape) block, self.scope = ActorCritic._build_shared_block(self.state,scope_name) self.policy, self.log_softmax_policy = ActorCritic._build_policy(block,nA,scope_name) self.value = ActorCritic._build_value(block,scope_name) self.train_vars = sorted(tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES, self.scope.name), key=lambda v:v.name) if( master is not None ) : self.sync_op= self._sync_op(master) self.action = tf.placeholder(tf.int32,[None,]) self.target_value = tf.placeholder(tf.float32,[None,]) advantage = self.target_value - self.value entropy = tf.reduce_sum(-1. * self.policy * self.log_softmax_policy,axis=1) log_p_s_a = tf.reduce_sum(self.log_softmax_policy * tf.one_hot(self.action,nA),axis=1) self.policy_loss = tf.reduce_mean(tf.stop_gradient(advantage)*log_p_s_a) self.entropy_loss = tf.reduce_mean(entropy) self.value_loss = tf.reduce_mean(advantage**2) loss = -self.policy_loss - entropy_beta* self.entropy_loss + self.value_loss self.gradients = tf.gradients(loss,self.train_vars) clipped_gs = [tf.clip_by_average_norm(g,grad_clip) for g in self.gradients] self.train_op = master.optimizer.apply_gradients(zip(clipped_gs,master.train_vars)) else : #self.optimizer = tf.train.AdamOptimizer(learning_rate,beta1=BETA) self.optimizer = tf.train.RMSPropOptimizer(learning_rate,decay=decay,use_locking=True)
Example #8
Source File: optimization.py From Counterfactual-StoryRW with MIT License | 4 votes |
def get_gradient_clip_fn(hparams=None): """Creates a gradient clipping function based on the hyperparameters. See the :attr:`gradient_clip` field in :meth:`~texar.core.default_optimization_hparams` for all hyperparameters and default values. The gradient clipping function takes a list of `(gradients, variables)` tuples and returns a list of `(clipped_gradients, variables)` tuples. Typical examples include :tf_main:`tf.clip_by_global_norm <clip_by_global_norm>`, :tf_main:`tf.clip_by_value <clip_by_value>`, :tf_main:`tf.clip_by_norm <clip_by_norm>`, :tf_main:`tf.clip_by_average_norm <clip_by_average_norm>`, etc. Args: hparams (dict or HParams, optional): hyperparameters. Missing hyperparameters are set to default values automatically. Returns: function or `None`: If hparams["type"] is specified, returns the respective function. If hparams["type"] is empty, returns `None`. """ if hparams is None or isinstance(hparams, dict): hparams = HParams( hparams, default_optimization_hparams()["gradient_clip"]) fn_type = hparams["type"] if fn_type is None or fn_type == "": return None fn_modules = ["tensorflow", "texar.custom"] clip_fn = utils.get_function(fn_type, fn_modules) clip_fn_args = utils.get_args(clip_fn) fn_kwargs = hparams["kwargs"] if isinstance(fn_kwargs, HParams): fn_kwargs = fn_kwargs.todict() def grad_clip_fn(grads_and_vars): """Gradient clipping function. Args: grads_and_vars (list): A list of `(gradients, variables)` tuples. Returns: list: A list of `(clipped_gradients, variables)` tuples. """ grads, vars_ = zip(*grads_and_vars) if clip_fn == tf.clip_by_global_norm: clipped_grads, _ = clip_fn(t_list=grads, **fn_kwargs) elif 't_list' in clip_fn_args: clipped_grads = clip_fn(t_list=grads, **fn_kwargs) elif 't' in clip_fn_args: # e.g., tf.clip_by_value clipped_grads = [clip_fn(t=grad, **fn_kwargs) for grad in grads] return list(zip(clipped_grads, vars_)) return grad_clip_fn
Example #9
Source File: optimization.py From texar with Apache License 2.0 | 4 votes |
def get_gradient_clip_fn(hparams=None): """Creates a gradient clipping function based on the hyperparameters. See the :attr:`gradient_clip` field in :meth:`~texar.tf.core.default_optimization_hparams` for all hyperparameters and default values. The gradient clipping function takes a list of `(gradients, variables)` tuples and returns a list of `(clipped_gradients, variables)` tuples. Typical examples include :tf_main:`tf.clip_by_global_norm <clip_by_global_norm>`, :tf_main:`tf.clip_by_value <clip_by_value>`, :tf_main:`tf.clip_by_norm <clip_by_norm>`, :tf_main:`tf.clip_by_average_norm <clip_by_average_norm>`, etc. Args: hparams (dict or HParams, optional): hyperparameters. Missing hyperparameters are set to default values automatically. Returns: function or `None`: If hparams["type"] is specified, returns the respective function. If hparams["type"] is empty, returns `None`. """ if hparams is None or isinstance(hparams, dict): hparams = HParams( hparams, default_optimization_hparams()["gradient_clip"]) fn_type = hparams["type"] if fn_type is None or fn_type == "": return None fn_modules = ["tensorflow", "texar.tf.custom"] clip_fn = utils.get_function(fn_type, fn_modules) clip_fn_args = utils.get_args(clip_fn) fn_kwargs = hparams["kwargs"] if isinstance(fn_kwargs, HParams): fn_kwargs = fn_kwargs.todict() def grad_clip_fn(grads_and_vars): """Gradient clipping function. Args: grads_and_vars (list): A list of `(gradients, variables)` tuples. Returns: list: A list of `(clipped_gradients, variables)` tuples. """ grads, vars_ = zip(*grads_and_vars) if clip_fn == tf.clip_by_global_norm: clipped_grads, _ = clip_fn(t_list=grads, **fn_kwargs) elif 't_list' in clip_fn_args: clipped_grads = clip_fn(t_list=grads, **fn_kwargs) elif 't' in clip_fn_args: # e.g., tf.clip_by_value clipped_grads = [clip_fn(t=grad, **fn_kwargs) for grad in grads] return list(zip(clipped_grads, vars_)) return grad_clip_fn