本文整理汇总了Python中tensorflow.global_norm函数的典型用法代码示例。如果您正苦于以下问题:Python global_norm函数的具体用法?Python global_norm怎么用?Python global_norm使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了global_norm函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Python代码示例。
示例1: clip_by_global_norm_summary
def clip_by_global_norm_summary(t_list, clip_norm, norm_name, variables):
# wrapper around tf.clip_by_global_norm that also does summary ops of norms
# compute norms
# use global_norm with one element to handle IndexedSlices vs dense
norms = [tf.global_norm([t]) for t in t_list]
# summary ops before clipping
summary_ops = []
for ns, v in zip(norms, variables):
name = 'norm_pre_clip/' + v.name.replace(":", "_")
summary_ops.append(tf.summary.scalar(name, ns))
# clip
clipped_t_list, tf_norm = tf.clip_by_global_norm(t_list, clip_norm)
# summary ops after clipping
norms_post = [tf.global_norm([t]) for t in clipped_t_list]
for ns, v in zip(norms_post, variables):
name = 'norm_post_clip/' + v.name.replace(":", "_")
summary_ops.append(tf.summary.scalar(name, ns))
summary_ops.append(tf.summary.scalar(norm_name, tf_norm))
return clipped_t_list, tf_norm, summary_ops
开发者ID:RileyShe,项目名称:DeepPavlov,代码行数:25,代码来源:train_utils.py
示例2: setup_loss_critic
def setup_loss_critic(critic):
# we are starting with critic.outputs symbol (after logistic layer)
with tf.variable_scope("rl", initializer=tf.uniform_unit_scaling_initializer(1.0)):
# loss setup
# None to timestep
critic.target_qt = tf.placeholder(tf.float32, shape=[None, None, critic.vocab_size],
name="q_action_score")
# p_actions is the target_token, and it's already [T, batch_size]
# q_t needs to be expanded...
# critic.outputs [T, batch_size, vocab_size]
# let's populate (expand) target tokens to fill up qt (just like what we did with one-hot labels)
critic.q_loss = tf.reduce_mean(tf.square(critic.outputs - critic.target_qt)) # Note: not adding lambda*C yet (variance)
opt = nlc_model.get_optimizer(FLAGS.optimizer)(critic.learning_rate)
# update
params = tf.trainable_variables()
gradients = tf.gradients(critic.q_loss, params)
clipped_gradients, _ = tf.clip_by_global_norm(gradients, FLAGS.max_gradient_norm)
# self.gradient_norm = tf.global_norm(clipped_gradients)
critic.gradient_norm = tf.global_norm(gradients)
critic.param_norm = tf.global_norm(params)
critic.updates = opt.apply_gradients(
zip(clipped_gradients, params), global_step=critic.global_step)
开发者ID:windweller,项目名称:nlc,代码行数:26,代码来源:rl_train.py
示例3: _update_step
def _update_step(self, observ, action, old_mean, old_logstd, reward, advantage, length):
"""Compute the current combined loss and perform a gradient update step.
Args:
observ: Sequences of observations.
action: Sequences of actions.
old_mean: Sequences of action means of the behavioral policy.
old_logstd: Sequences of action log stddevs of the behavioral policy.
reward: Sequences of reward.
advantage: Sequences of advantages.
length: Batch of sequence lengths.
Returns:
Tuple of value loss, policy loss, and summary tensor.
"""
value_loss, value_summary = self._value_loss(observ, reward, length)
network = self._network(observ, length)
policy_loss, policy_summary = self._policy_loss(network.mean, network.logstd, old_mean,
old_logstd, action, advantage, length)
value_gradients, value_variables = (zip(*self._optimizer.compute_gradients(value_loss)))
policy_gradients, policy_variables = (zip(*self._optimizer.compute_gradients(policy_loss)))
all_gradients = value_gradients + policy_gradients
all_variables = value_variables + policy_variables
optimize = self._optimizer.apply_gradients(zip(all_gradients, all_variables))
summary = tf.summary.merge([
value_summary, policy_summary,
tf.summary.scalar('value_gradient_norm', tf.global_norm(value_gradients)),
tf.summary.scalar('policy_gradient_norm', tf.global_norm(policy_gradients)),
utility.gradient_summaries(zip(value_gradients, value_variables), dict(value=r'.*')),
utility.gradient_summaries(zip(policy_gradients, policy_variables), dict(policy=r'.*'))
])
with tf.control_dependencies([optimize]):
return [tf.identity(x) for x in (value_loss, policy_loss, summary)]
开发者ID:bulletphysics,项目名称:bullet3,代码行数:33,代码来源:algorithm.py
示例4: create_variables_for_optimization
def create_variables_for_optimization(self):
with tf.name_scope("optimization"):
with tf.name_scope("masker"):
self.mask = tf.sequence_mask(self.seq_len, self.num_step)
self.mask = tf.reshape(tf.cast(self.mask, tf.float32), (-1,))
if self.loss_function == "cross_entropy":
self.pl_loss = tf.nn.sparse_softmax_cross_entropy_with_logits(
logits=self.logit,
labels=self.actions_flatten)
elif self.loss_function == "l2":
self.one_hot_actions = tf.one_hot(self.actions_flatten, self.num_actions)
self.pl_loss = tf.reduce_mean((self.probs - self.one_hot_actions) ** 2,
axis=1)
else:
raise ValueError("loss function type is not defined")
self.pl_loss = tf.multiply(self.pl_loss, self.mask)
self.pl_loss = tf.reduce_mean(tf.multiply(self.pl_loss, self.returns_flatten))
self.entropy = tf.multiply(self.entropy, self.mask)
self.entropy = tf.reduce_mean(self.entropy)
self.loss = self.pl_loss - self.entropy_bonus * self.entropy
self.trainable_variables = tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES, scope="policy_network")
self.gradients = self.optimizer.compute_gradients(self.loss, var_list=self.trainable_variables)
self.clipped_gradients = [(tf.clip_by_norm(grad, self.max_gradient), var)
for grad, var in self.gradients]
self.train_op = self.optimizer.apply_gradients(self.clipped_gradients,
self.global_step)
self.grad_norm = tf.global_norm([grad for grad, var in self.gradients])
self.var_norm = tf.global_norm(self.trainable_variables)
开发者ID:csawtelle,项目名称:pg_rnn,代码行数:32,代码来源:pg_rnn.py
示例5: setup_actor_update
def setup_actor_update(actor):
with tf.variable_scope("rl"):
actor.critic_output = tf.placeholder(tf.float32, [None, None, actor.vocab_size], name='critic_output')
# action_gradients is passed in by Q_network...
# and in DDPG, it's the gradients of Q w.r.t. policy's chosen actions
# but in AC, it's the output of Q network w.r.t. all actions
opt = nlc_model.get_optimizer(FLAGS.optimizer)(actor.learning_rate)
# update
params = tf.trainable_variables()
# TODO: hope this would work
with tf.variable_scope("Loss"):
doshape = tf.shape(actor.decoder_output)
T, batch_size = doshape[0], doshape[1]
do2d = tf.reshape(actor.decoder_output, [-1, actor.size])
logits2d = rnn_cell._linear(do2d, actor.vocab_size, True, 1.0)
# outputs2d = tf.nn.log_softmax(logits2d)
# apply Q-network's score here (similar to advantage function)
# 1. reshape critic_output like decoder_output (same shape anyway)
# TODO: hope this is correct
critic_do2d = tf.reshape(actor.critic_output, [-1, actor.vocab_size]) # should reshape according to critic
# 2. multiply this with actor's logitis
rl_logits2d = logits2d * critic_do2d
# actor.outputs = tf.reshape(outputs2d, tf.pack([T, batch_size, actor.vocab_size]))
targets_no_GO = tf.slice(actor.target_tokens, [1, 0], [-1, -1])
masks_no_GO = tf.slice(actor.target_mask, [1, 0], [-1, -1])
# easier to pad target/mask than to split decoder input since tensorflow does not support negative indexing
labels1d = tf.reshape(tf.pad(targets_no_GO, [[0, 1], [0, 0]]), [-1])
mask1d = tf.reshape(tf.pad(masks_no_GO, [[0, 1], [0, 0]]), [-1])
losses1d = tf.nn.sparse_softmax_cross_entropy_with_logits(rl_logits2d, labels1d) * tf.to_float(mask1d)
losses2d = tf.reshape(losses1d, tf.pack([T, batch_size]))
actor.rl_losses = tf.reduce_sum(losses2d) / tf.to_float(batch_size)
# http://pemami4911.github.io/blog/2016/08/21/ddpg-rl.html (DDPG update)
gradients = tf.gradients(actor.rl_losses, params) # step 7: update
# Not sure if I understood this part lol
clipped_gradients, _ = tf.clip_by_global_norm(gradients, FLAGS.max_gradient_norm)
# clip, then multiply, otherwise we are not learning the signals from critic
# clipped_gradients: [T, batch_size, vocab_size]
# updated_gradients = clipped_gradients * actor.critic_output
# pass in as input
actor.rl_gradient_norm = tf.global_norm(clipped_gradients)
actor.rl_param_norm = tf.global_norm(params)
actor.rl_updates = opt.apply_gradients(
zip(clipped_gradients, params), global_step=actor.global_step)
开发者ID:windweller,项目名称:nlc,代码行数:56,代码来源:rl_train.py
示例6: __init__
def __init__(self, vocab_size, label_size, size, num_layers, batch_size, learning_rate,
learning_rate_decay_factor, dropout, embedding, src_steps, tgt_steps,
mode='sq2sq',
max_gradient_norm=5.0, forward_only=False):
self.size = size
self.mode = mode
self.vocab_size = vocab_size
self.label_size = label_size
self.embedding = embedding
self.src_steps = src_steps
self.tgt_steps = tgt_steps
self.batch_size = batch_size
self.num_layers = num_layers
self.keep_prob = 1.0 - dropout
self.learning_rate = tf.Variable(float(learning_rate), trainable=False)
self.learning_rate_decay_op = self.learning_rate.assign(self.learning_rate * learning_rate_decay_factor)
self.global_step = tf.Variable(0, trainable=False)
self.source_tokens = tf.placeholder(tf.int32, shape=[None, self.src_steps], name='srcInput')
self.target_tokens = tf.placeholder(tf.int32, shape=[None, self.tgt_steps], name='targetInput')
self.label_placeholder = tf.placeholder(tf.float32, shape=[None, self.label_size])
self.decoder_state_input, self.decoder_state_output = [], []
self.tgt_encoder_state_input, self.tgt_encoder_state_output = [], []
for i in xrange(num_layers):
self.decoder_state_input.append(tf.placeholder(tf.float32, shape=[None, size]))
self.tgt_encoder_state_input.append(tf.placeholder(tf.float32, shape=[None, size]))
self.setup_embeddings()
self.setup_encoder()
self.setup_decoder()
if mode == 'sq2sq':
self.setup_label_loss()
else:
raise NotImplementedError
params = tf.trainable_variables()
if not forward_only:
opt = tf.train.AdamOptimizer(self.learning_rate)
gradients = tf.gradients(self.losses, params)
clipped_gradients, _ = tf.clip_by_global_norm(gradients, max_gradient_norm)
self.gradient_norm = tf.global_norm(clipped_gradients)
self.param_norm = tf.global_norm(params)
self.updates = opt.apply_gradients(
zip(clipped_gradients, params), global_step=self.global_step)
self.saver = tf.train.Saver(tf.all_variables())
开发者ID:windweller,项目名称:Trident,代码行数:50,代码来源:story_model.py
示例7: initialize
def initialize(self):
if self.summarize:
bs = tf.to_float(tf.shape(self.x)[0])
tf.summary.scalar("model/policy_loss", self.pi_loss / bs)
tf.summary.scalar("model/grad_gnorm", tf.global_norm(self.grads))
tf.summary.scalar("model/var_gnorm", tf.global_norm(self.var_list))
self.summary_op = tf.summary.merge_all()
# TODO(rliaw): Can consider exposing these parameters
self.sess = tf.Session(graph=self.g, config=tf.ConfigProto(
intra_op_parallelism_threads=1, inter_op_parallelism_threads=2,
gpu_options=tf.GPUOptions(allow_growth=True)))
self.variables = ray.experimental.TensorFlowVariables(self.loss,
self.sess)
self.sess.run(tf.global_variables_initializer())
开发者ID:adgirish,项目名称:ray,代码行数:15,代码来源:policy.py
示例8: _summarize_vars_and_grads
def _summarize_vars_and_grads(grads_and_vars):
tf.logging.info('Trainable variables:')
tf.logging.info('-' * 60)
for grad, var in grads_and_vars:
tf.logging.info(var)
def tag(name, v=var):
return v.op.name + '_' + name
# Variable summary
mean = tf.reduce_mean(var)
tf.summary.scalar(tag('mean'), mean)
with tf.name_scope(tag('stddev')):
stddev = tf.sqrt(tf.reduce_mean(tf.square(var - mean)))
tf.summary.scalar(tag('stddev'), stddev)
tf.summary.scalar(tag('max'), tf.reduce_max(var))
tf.summary.scalar(tag('min'), tf.reduce_min(var))
tf.summary.histogram(tag('histogram'), var)
# Gradient summary
if grad is not None:
if isinstance(grad, tf.IndexedSlices):
grad_values = grad.values
else:
grad_values = grad
tf.summary.histogram(tag('gradient'), grad_values)
tf.summary.scalar(tag('gradient_norm'), tf.global_norm([grad_values]))
else:
tf.logging.info('Var %s has no gradient', var.op.name)
开发者ID:hoysasulee,项目名称:models,代码行数:30,代码来源:layers.py
示例9: _add_gradients_summaries
def _add_gradients_summaries(grads_and_vars):
"""Add histogram summaries to gradients.
Note: The summaries are also added to the SUMMARIES collection.
Args:
grads_and_vars: A list of gradient to variable pairs (tuples).
Returns:
The _list_ of the added summaries for grads_and_vars.
"""
summaries = []
for grad, var in grads_and_vars:
if grad is not None:
if isinstance(grad, tf.IndexedSlices):
grad_values = grad.values
else:
grad_values = grad
summaries.append(tf.histogram_summary(var.op.name + ':gradient',
grad_values))
summaries.append(tf.histogram_summary(var.op.name + ':gradient_norm',
tf.global_norm([grad_values])))
else:
tf.logging.info('Var %s has no gradient', var.op.name)
return summaries
开发者ID:Peratham,项目名称:models,代码行数:25,代码来源:model_deploy.py
示例10: optim
def optim(loss, **kwargs):
r"""Applies gradients to variables.
Args:
loss: A 0-D `Tensor` containing the value to minimize.
kwargs:
optim: A name for optimizer. 'MaxProp' (default), 'AdaMax', 'Adam', or 'sgd'.
lr: A Python Scalar (optional). Learning rate. Default is .001.
beta1: A Python Scalar (optional). Default is .9.
beta2: A Python Scalar (optional). Default is .99.
category: A string or string list. Specifies the variables that should be trained (optional).
Only if the name of a trainable variable starts with `category`, it's value is updated.
Default is '', which means all trainable variables are updated.
"""
opt = Opt(kwargs)
# opt += Opt(optim='MaxProp', lr=0.001, beta1=0.9, beta2=0.99, category='')
# default training options
opt += Opt(optim='MaxProp', lr=0.001, beta1=0.9, beta2=0.99, category='')
# select optimizer
# if opt.optim == 'MaxProp':
# optim = tf.sg_optimize.MaxPropOptimizer(learning_rate=opt.lr, beta2=opt.beta2)
# elif opt.optim == 'AdaMax':
# optim = tf.sg_optimize.AdaMaxOptimizer(learning_rate=opt.lr, beta1=opt.beta1, beta2=opt.beta2)
# elif opt.optim == 'Adam':
if opt.optim == 'Adm':
optim = tf.train.AdamOptimizer(learning_rate=opt.lr, beta1=opt.beta1, beta2=opt.beta2)
else:
optim = tf.train.GradientDescentOptimizer(learning_rate=opt.lr)
# get trainable variables
if isinstance(opt.category, (tuple, list)):
var_list = []
for cat in opt.category:
var_list.extend([t for t in tf.trainable_variables() if t.name.startswith(cat)])
else:
var_list = [t for t in tf.trainable_variables() if t.name.startswith(opt.category)]
# calc gradient
gradient = optim.compute_gradients(loss, var_list=var_list)
# add summary
for v, g in zip(var_list, gradient):
# exclude batch normal statics
if 'mean' not in v.name and 'variance' not in v.name \
and 'beta' not in v.name and 'gamma' not in v.name:
prefix = ''
# summary name
name = prefix + ''.join(v.name.split(':')[:-1])
# summary statistics
# noinspection PyBroadException
try:
tf.summary.scalar(name + '/grad', tf.global_norm([g]))
tf.summary.histogram(name + '/grad-h', g)
except:
pass
global_step = tf.Variable(0, name='global_step', trainable=False)
# gradient update op
return optim.apply_gradients(gradient, global_step=global_step), global_step
开发者ID:SiyuanWei,项目名称:tensorflow-101,代码行数:60,代码来源:optimizer.py
示例11: _update_policy_step
def _update_policy_step(self, observ, action, old_mean, old_logstd, advantage, length):
"""Compute the current policy loss and perform a gradient update step.
Args:
observ: Sequences of observations.
action: Sequences of actions.
old_mean: Sequences of action means of the behavioral policy.
old_logstd: Sequences of action log stddevs of the behavioral policy.
advantage: Sequences of advantages.
length: Batch of sequence lengths.
Returns:
Tuple of loss tensor and summary tensor.
"""
network = self._network(observ, length)
loss, summary = self._policy_loss(network.mean, network.logstd, old_mean, old_logstd, action,
advantage, length)
gradients, variables = (zip(*self._policy_optimizer.compute_gradients(loss)))
optimize = self._policy_optimizer.apply_gradients(zip(gradients, variables))
summary = tf.summary.merge([
summary,
tf.summary.scalar('gradient_norm', tf.global_norm(gradients)),
utility.gradient_summaries(zip(gradients, variables), dict(policy=r'.*'))
])
with tf.control_dependencies([optimize]):
return [tf.identity(loss), tf.identity(summary)]
开发者ID:bulletphysics,项目名称:bullet3,代码行数:26,代码来源:algorithm.py
示例12: get_train_op
def get_train_op(loss, params):
"""Generate training operation that updates variables based on loss."""
with tf.variable_scope("get_train_op"):
learning_rate = get_learning_rate(
params.learning_rate, params.hidden_size,
params.learning_rate_warmup_steps)
# Create optimizer. Use LazyAdamOptimizer from TF contrib, which is faster
# than the TF core Adam optimizer.
optimizer = tf.contrib.opt.LazyAdamOptimizer(
learning_rate,
beta1=params.optimizer_adam_beta1,
beta2=params.optimizer_adam_beta2,
epsilon=params.optimizer_adam_epsilon)
# Calculate and apply gradients using LazyAdamOptimizer.
global_step = tf.train.get_global_step()
tvars = tf.trainable_variables()
gradients = optimizer.compute_gradients(
loss, tvars, colocate_gradients_with_ops=True)
train_op = optimizer.apply_gradients(
gradients, global_step=global_step, name="train")
# Save gradient norm to Tensorboard
tf.summary.scalar("global_norm/gradient_norm",
tf.global_norm(list(zip(*gradients))[0]))
return train_op
开发者ID:aelbouchti,项目名称:models,代码行数:28,代码来源:transformer_main.py
示例13: _update_step
def _update_step(self, sequence):
"""Compute the current combined loss and perform a gradient update step.
The sequences must be a dict containing the keys `length` and `sequence`,
where the latter is a tuple containing observations, actions, parameters of
the behavioral policy, rewards, and advantages.
Args:
sequence: Sequences of episodes or chunks of episodes.
Returns:
Tuple of value loss, policy loss, and summary tensor.
"""
observ, action, old_policy_params, reward, advantage = sequence['sequence']
length = sequence['length']
old_policy = self._policy_type(**old_policy_params)
value_loss, value_summary = self._value_loss(observ, reward, length)
network = self._network(observ, length)
policy_loss, policy_summary = self._policy_loss(
old_policy, network.policy, action, advantage, length)
loss = policy_loss + value_loss + network.get('loss', 0)
gradients, variables = (
zip(*self._optimizer.compute_gradients(loss)))
optimize = self._optimizer.apply_gradients(
zip(gradients, variables))
summary = tf.summary.merge([
value_summary, policy_summary,
tf.summary.histogram('network_loss', network.get('loss', 0)),
tf.summary.scalar('gradient_norm', tf.global_norm(gradients)),
utility.gradient_summaries(zip(gradients, variables))])
with tf.control_dependencies([optimize]):
return [tf.identity(x) for x in (value_loss, policy_loss, summary)]
开发者ID:shamanez,项目名称:agents,代码行数:32,代码来源:ppo.py
示例14: _update_network
def _update_network(self, trainer):
self.actions = tf.placeholder(shape=[None], dtype=tf.int32)
self.actions_onehot = tf.one_hot(
self.actions, self.a_dim, dtype=tf.float32)
self.target_v = tf.placeholder(shape=[None], dtype=tf.float32)
self.advantages = tf.placeholder(shape=[None], dtype=tf.float32)
self.outputs = tf.reduce_sum(
self.policy * self.actions_onehot, [1])
# loss
self.value_loss = 0.5 * tf.reduce_sum(tf.square(
self.target_v - tf.reshape(self.value, [-1])))
# higher entropy -> lower loss -> encourage exploration
self.entropy = -tf.reduce_sum(self.policy * tf.log(self.policy))
self.policy_loss = -tf.reduce_sum(
tf.log(self.outputs) * self.advantages)
self.loss = 0.5 * self.value_loss \
+ self.policy_loss - 0.01 * self.entropy
# local gradients
local_vars = tf.get_collection(
tf.GraphKeys.TRAINABLE_VARIABLES, self.scope)
self.gradients = tf.gradients(self.loss, local_vars)
self.var_norms = tf.global_norm(local_vars)
# grads[i] * clip_norm / max(global_norm, clip_norm)
grads, self.grad_norms = tf.clip_by_global_norm(self.gradients, 40.0)
# apply gradients to global network
global_vars = tf.get_collection(
tf.GraphKeys.TRAINABLE_VARIABLES, 'global')
self.apply_grads = trainer.apply_gradients(zip(grads, global_vars))
开发者ID:Funitus,项目名称:reinforce_py,代码行数:35,代码来源:net.py
示例15: __init__
def __init__(self, vocab_size, size, num_layers, max_gradient_norm, batch_size, learning_rate,
learning_rate_decay_factor, dropout, FLAGS, forward_only=False, optimizer="adam"):
self.size = size
self.vocab_size = vocab_size
self.batch_size = batch_size
self.num_layers = num_layers
self.keep_prob_config = 1.0 - dropout
self.learning_rate = tf.Variable(float(learning_rate), trainable=False)
self.learning_rate_decay_op = self.learning_rate.assign(self.learning_rate * learning_rate_decay_factor)
self.global_step = tf.Variable(0, trainable=False)
self.keep_prob = tf.placeholder(tf.float32)
self.source_tokens = tf.placeholder(tf.int32, shape=[None, None])
self.target_tokens = tf.placeholder(tf.int32, shape=[None, None])
self.source_mask = tf.placeholder(tf.int32, shape=[None, None])
self.target_mask = tf.placeholder(tf.int32, shape=[None, None])
self.beam_size = tf.placeholder(tf.int32)
self.target_length = tf.reduce_sum(self.target_mask, reduction_indices=0)
self.FLAGS = FLAGS
self.decoder_state_input, self.decoder_state_output = [], []
for i in xrange(num_layers):
self.decoder_state_input.append(tf.placeholder(tf.float32, shape=[None, size]))
with tf.variable_scope("NLC", initializer=tf.uniform_unit_scaling_initializer(1.0)):
self.setup_embeddings()
self.setup_encoder()
self.setup_decoder()
self.setup_loss()
self.setup_beam()
params = tf.trainable_variables()
if not forward_only:
opt = get_optimizer(optimizer)(self.learning_rate)
gradients = tf.gradients(self.losses, params)
clipped_gradients, _ = tf.clip_by_global_norm(gradients, max_gradient_norm)
# self.gradient_norm = tf.global_norm(clipped_gradients)
self.gradient_norm = tf.global_norm(gradients)
self.param_norm = tf.global_norm(params)
self.updates = opt.apply_gradients(
zip(clipped_gradients, params), global_step=self.global_step)
self.saver = tf.train.Saver(tf.global_variables(), max_to_keep=FLAGS.keep) # write_version=tf.train.SaverDef.V1
开发者ID:windweller,项目名称:nlc,代码行数:46,代码来源:nlc_model.py
示例16: gradient_clip
def gradient_clip(gradients, max_gradient_norm):
"""Clipping gradients of a model."""
clipped_gradients, gradient_norm = tf.clip_by_global_norm(
gradients, max_gradient_norm)
gradient_norm_summary = [tf.summary.scalar("grad_norm", gradient_norm)]
gradient_norm_summary.append(
tf.summary.scalar("clipped_gradient", tf.global_norm(clipped_gradients)))
return clipped_gradients, gradient_norm_summary, gradient_norm
开发者ID:BUPT402,项目名称:nmt,代码行数:9,代码来源:model_helper.py
示例17: __init__
def __init__(self, FLAGS, encoder, decoder, classifier):
self.FLAGS = FLAGS
self.encoder = encoder
self.decoder = decoder
self.classifier = classifier
self.xplaceholder = tf.placeholder(tf.int32,
shape = (None, self.FLAGS.maxSentenceLength))
self.yplaceholder = tf.placeholder(tf.float64, shape = (None,))
# self.maskplaceholder = tf.placeholder(tf.int32, shape = (None,self.FLAGS.maxSentenceLength))
self.maskplaceholder = tf.placeholder(tf.int32, shape = (None,))
self.drop_placeholder = tf.placeholder(tf.float64, shape = ())
self.lr_placeholder = tf.placeholder(tf.float64, shape = ())
self.opplaceholder = tf.placeholder(tf.float64)
with tf.variable_scope("tldr", initializer = tf.contrib.layers.xavier_initializer()):
self.setup_embeddings()
self.setup_system()
self.setup_loss()
params = tf.trainable_variables()
self.globalnorm = 0
self.paramnorm = 0
for param in params:
shp = param.get_shape()
if len(shp) >= 2:
self.paramnorm += tf.nn.l2_loss(param)
opt = tf.train.AdamOptimizer(self.lr_placeholder)
if self.FLAGS.clipGradients == 1:
try:
grads, _ = zip(*opt.compute_gradients(self.loss))
grads, _ = tf.clip_by_global_norm(grads, self.FLAGS.max_gradient_norm)
self.globalnorm = tf.global_norm(grads)
grads_vars = zip(grads, params)
self.updates = opt.apply_gradients(grads_vars)
except AttributeError:
self.updates = None
else:
grads = tf.gradients(self.loss, params)
self.globalnorm = tf.global_norm(grads)
try:
self.updates = opt.minimize(self.loss)
except AttributeError:
self.updates = None
self.saver = tf.train.Saver(keep_checkpoint_every_n_hours = 2, max_to_keep = 0)
开发者ID:andrewquirk,项目名称:extractive-news-summarization,代码行数:44,代码来源:tldr.py
示例18: _create_loss_optimizer
def _create_loss_optimizer(self):
# The loss is composed of two terms:
# 1.) The reconstruction loss (the negative log probability
# of the input under the reconstructed Bernoulli distribution
# induced by the decoder in the data space).
# This can be interpreted as the number of "nats" required
# for reconstructing the input when the activation in latent
# is given.
orig_energies = tf.reshape(self.x, [self.batch_size, -1])
new_energies = tf.reshape(self.x_reconstr_mean, [self.batch_size, -1])
diff = tf.square(tf.sub(orig_energies, new_energies))
diff_norm = tf.div(diff,tf.exp(tf.minimum(20.,self.x_reconstr_log_sigma_sq)))
denom_log = tf.log(2*np.pi) + self.x_reconstr_log_sigma_sq
self.vae_loss_likelihood = tf.reduce_sum(0.5*(diff_norm+denom_log), 1)
# 2.) The latent loss, which is defined as the Kullback Leibler divergence
## between the distribution in latent space induced by the encoder on
# the data and some prior. This acts as a kind of regularizer.
# This can be interpreted as the number of "nats" required
# for transmitting the the latent space distribution given
# the prior.
self.vae_loss_kl = -0.5 * tf.reduce_sum(1 + self.z_log_sigma_sq
- tf.square(self.z_mean)
- tf.exp(tf.minimum(20.,self.z_log_sigma_sq)), 1)
self.cost = tf.reduce_mean(self.vae_loss_likelihood + self.lamb*self.vae_loss_kl) # average over batch
#self.cost = tf.reduce_mean(self.vae_loss_kl + self.vae_loss_l2)
self.t_vars = tf.trainable_variables()
# Use RMSProp optimizer
opt = tf.train.AdamOptimizer(self.learning_rate) #.minimize(self.cost, var_list=self.t_vars)
grads, t_vars = zip(*opt.compute_gradients(self.cost, self.t_vars))
self.gradnorm = tf.global_norm(grads)
grads = tf.cond(
tf.global_norm(grads) > 1e-20,
lambda: tf.clip_by_global_norm(grads, 500.)[0],
lambda: grads)
self.optimizer = opt.apply_gradients(zip(grads,t_vars))
开发者ID:phreeza,项目名称:ml-music,代码行数:42,代码来源:model_vae.py
示例19: __init__
def __init__(self, vocab_size, size, num_layers, max_gradient_norm, batch_size, learning_rate,
learning_rate_decay_factor, dropout, forward_only=False):
self.size = size
self.vocab_size = vocab_size
self.batch_size = batch_size
self.num_layers = num_layers
self.keep_prob = 1.0 - dropout
self.learning_rate = tf.Variable(float(learning_rate), trainable=False)
self.learning_rate_decay_op = self.learning_rate.assign(self.learning_rate * learning_rate_decay_factor)
self.global_step = tf.Variable(0, trainable=False)
self.source_tokens = tf.placeholder(tf.int32, shape=[None, None])
self.target_tokens = tf.placeholder(tf.int32, shape=[None, None])
self.source_mask = tf.placeholder(tf.int32, shape=[None, None])
self.target_mask = tf.placeholder(tf.int32, shape=[None, None])
self.target_length = tf.reduce_sum(self.target_mask, reduction_indices=0)
self.decoder_state_input, self.decoder_state_output = [], []
for i in xrange(num_layers):
self.decoder_state_input.append(tf.placeholder(tf.float32, shape=[None, size]))
self.setup_embeddings()
self.setup_encoder()
self.setup_decoder()
self.setup_loss()
params = tf.trainable_variables()
if not forward_only:
opt = tf.train.AdamOptimizer(self.learning_rate)
gradients = tf.gradients(self.losses, params)
clipped_gradients, _ = tf.clip_by_global_norm(gradients, max_gradient_norm)
# self.gradient_norm = tf.global_norm(clipped_gradients)
self.gradient_norm = tf.global_norm(gradients)
self.param_norm = tf.global_norm(params)
self.updates = opt.apply_gradients(
zip(clipped_gradients, params), global_step=self.global_step)
self.saver = tf.train.Saver(tf.all_variables())
开发者ID:hrishikeshvganu,项目名称:nlc,代码行数:40,代码来源:nlc_model.py
示例20: __init__
def __init__(self, FLAGS, id2word, word2id, emb_matrix):
"""
Initializes the QA model.
Inputs:
FLAGS: the flags passed in from main.py
id2word: dictionary mapping word idx (int) to word (string)
word2id: dictionary mapping word (string) to word idx (int)
emb_matrix: numpy array shape (400002, embedding_size) containing pre-traing GloVe embeddings
"""
print "Initializing the QAModel..."
self.FLAGS = FLAGS
self.id2word = id2word
self.word2id = word2id
# Add all parts of the graph
with tf.variable_scope("QAModel", initializer=tf.contrib.layers.variance_scaling_initializer(factor=1.0, uniform=True)):
self.add_placeholders()
self.add_embedding_layer(emb_matrix)
self.build_graph()
self.add_loss()
# Define trainable parameters, gradient, gradient norm, and clip by gradient norm
params = tf.trainable_variables()
gradients = tf.gradients(self.loss, params)
self.gradient_norm = tf.global_norm(gradients)
clipped_gradients, _ = tf.clip_by_global_norm(gradients, FLAGS.max_gradient_norm)
self.param_norm = tf.global_norm(params)
# Define optimizer and updates
# (updates is what you need to fetch in session.run to do a gradient update)
self.global_step = tf.Variable(0, name="global_step", trainable=False)
opt = tf.train.AdamOptimizer(learning_rate=FLAGS.learning_rate) # you can try other optimizers
self.updates = opt.apply_gradients(zip(clipped_gradients, params), global_step=self.global_step)
# Define savers (for checkpointing) and summaries (for tensorboard)
self.saver = tf.train.Saver(tf.global_variables(), max_to_keep=FLAGS.keep)
self.bestmodel_saver = tf.train.Saver(tf.global_variables(), max_to_keep=1)
self.summaries = tf.summary.merge_all()
开发者ID:trthanhquang,项目名称:QA_SQuAD,代码行数:39,代码来源:qa_model.py
注:本文中的tensorflow.global_norm函数示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论