本文整理汇总了Python中tensorflow.get_collection方法的典型用法代码示例。如果您正苦于以下问题:Python tensorflow.get_collection方法的具体用法?Python tensorflow.get_collection怎么用?Python tensorflow.get_collection使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类tensorflow
的用法示例。
在下文中一共展示了tensorflow.get_collection方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: get_params
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import get_collection [as 别名]
def get_params(self):
"""
Provides access to the model's parameters.
:return: A list of all Variables defining the model parameters.
"""
# Catch eager execution and assert function overload.
try:
if tf.executing_eagerly():
raise NotImplementedError("For Eager execution - get_params "
"must be overridden.")
except AttributeError:
pass
# For Graoh based execution
scope_vars = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES,
self.scope)
return scope_vars
示例2: testCreateLogisticClassifier
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import get_collection [as 别名]
def testCreateLogisticClassifier(self):
g = tf.Graph()
with g.as_default():
tf.set_random_seed(0)
tf_inputs = tf.constant(self._inputs, dtype=tf.float32)
tf_labels = tf.constant(self._labels, dtype=tf.float32)
model_fn = LogisticClassifier
clone_args = (tf_inputs, tf_labels)
deploy_config = model_deploy.DeploymentConfig(num_clones=1)
self.assertEqual(slim.get_variables(), [])
clones = model_deploy.create_clones(deploy_config, model_fn, clone_args)
clone = clones[0]
self.assertEqual(len(slim.get_variables()), 2)
for v in slim.get_variables():
self.assertDeviceEqual(v.device, 'CPU:0')
self.assertDeviceEqual(v.value().device, 'CPU:0')
self.assertEqual(clone.outputs.op.name,
'LogisticClassifier/fully_connected/Sigmoid')
self.assertEqual(clone.scope, '')
self.assertDeviceEqual(clone.device, 'GPU:0')
self.assertEqual(len(slim.losses.get_losses()), 1)
update_ops = tf.get_collection(tf.GraphKeys.UPDATE_OPS)
self.assertEqual(update_ops, [])
示例3: testCreateSingleclone
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import get_collection [as 别名]
def testCreateSingleclone(self):
g = tf.Graph()
with g.as_default():
tf.set_random_seed(0)
tf_inputs = tf.constant(self._inputs, dtype=tf.float32)
tf_labels = tf.constant(self._labels, dtype=tf.float32)
model_fn = BatchNormClassifier
clone_args = (tf_inputs, tf_labels)
deploy_config = model_deploy.DeploymentConfig(num_clones=1)
self.assertEqual(slim.get_variables(), [])
clones = model_deploy.create_clones(deploy_config, model_fn, clone_args)
clone = clones[0]
self.assertEqual(len(slim.get_variables()), 5)
for v in slim.get_variables():
self.assertDeviceEqual(v.device, 'CPU:0')
self.assertDeviceEqual(v.value().device, 'CPU:0')
self.assertEqual(clone.outputs.op.name,
'BatchNormClassifier/fully_connected/Sigmoid')
self.assertEqual(clone.scope, '')
self.assertDeviceEqual(clone.device, 'GPU:0')
self.assertEqual(len(slim.losses.get_losses()), 1)
update_ops = tf.get_collection(tf.GraphKeys.UPDATE_OPS)
self.assertEqual(len(update_ops), 2)
示例4: _get_variables_to_train
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import get_collection [as 别名]
def _get_variables_to_train():
"""Returns a list of variables to train.
Returns:
A list of variables to train by the optimizer.
"""
if FLAGS.trainable_scopes is None:
return tf.trainable_variables()
else:
scopes = [scope.strip() for scope in FLAGS.trainable_scopes.split(',')]
variables_to_train = []
for scope in scopes:
variables = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES, scope)
variables_to_train.extend(variables)
return variables_to_train
示例5: _add_loss_summaries
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import get_collection [as 别名]
def _add_loss_summaries(total_loss):
"""Add summaries for losses in CIFAR-10 model.
Generates moving average for all losses and associated summaries for
visualizing the performance of the network.
Args:
total_loss: Total loss from loss().
Returns:
loss_averages_op: op for generating moving averages of losses.
"""
# Compute the moving average of all individual losses and the total loss.
loss_averages = tf.train.ExponentialMovingAverage(0.9, name='avg')
losses = tf.get_collection('losses')
loss_averages_op = loss_averages.apply(losses + [total_loss])
# Attach a scalar summary to all individual losses and the total loss; do the
# same for the averaged version of the losses.
for l in losses + [total_loss]:
# Name each loss as '(raw)' and name the moving average version of the loss
# as the original loss name.
tf.summary.scalar(l.op.name + ' (raw)', l)
tf.summary.scalar(l.op.name, loss_averages.average(l))
return loss_averages_op
示例6: add_variable
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import get_collection [as 别名]
def add_variable(var, restore=True):
"""Adds a variable to the MODEL_VARIABLES collection.
Optionally it will add the variable to the VARIABLES_TO_RESTORE collection.
Args:
var: a variable.
restore: whether the variable should be added to the
VARIABLES_TO_RESTORE collection.
"""
collections = [MODEL_VARIABLES]
if restore:
collections.append(VARIABLES_TO_RESTORE)
for collection in collections:
if var not in tf.get_collection(collection):
tf.add_to_collection(collection, var)
示例7: get_unique_variable
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import get_collection [as 别名]
def get_unique_variable(name):
"""Gets the variable uniquely identified by that name.
Args:
name: a name that uniquely identifies the variable.
Returns:
a tensorflow variable.
Raises:
ValueError: if no variable uniquely identified by the name exists.
"""
candidates = tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES, name)
if not candidates:
raise ValueError('Couldnt find variable %s' % name)
for candidate in candidates:
if candidate.op.name == name:
return candidate
raise ValueError('Variable %s does not uniquely identify a variable', name)
示例8: testTotalLossWithoutRegularization
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import get_collection [as 别名]
def testTotalLossWithoutRegularization(self):
batch_size = 5
height, width = 299, 299
num_classes = 1001
with self.test_session():
inputs = tf.random_uniform((batch_size, height, width, 3))
dense_labels = tf.random_uniform((batch_size, num_classes))
with slim.arg_scope([slim.ops.conv2d, slim.ops.fc], weight_decay=0):
logits, end_points = slim.inception.inception_v3(
inputs,
num_classes=num_classes)
# Cross entropy loss for the main softmax prediction.
slim.losses.cross_entropy_loss(logits,
dense_labels,
label_smoothing=0.1,
weight=1.0)
# Cross entropy loss for the auxiliary softmax head.
slim.losses.cross_entropy_loss(end_points['aux_logits'],
dense_labels,
label_smoothing=0.1,
weight=0.4,
scope='aux_loss')
losses = tf.get_collection(slim.losses.LOSSES_COLLECTION)
self.assertEqual(len(losses), 2)
示例9: build_graph
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import get_collection [as 别名]
def build_graph(self,state,global_step):
'''
Builds the computation graph for the critic
Inputs:
states: tf placeholder inputs to network
'''
self.global_step = global_step
self.outputs = [state]
with tf.variable_scope(self.scope, reuse=self.reuse):
for i in range(1,len(self.units)-1):
layer = tf.layers.dense(self.outputs[i-1], self.units[i], tf.nn.relu, trainable=self.trainable)
self.outputs.append(layer)
layer = tf.layers.dense(self.outputs[-1], self.units[-1], trainable=self.trainable)
self.outputs.append(layer)
self.params = tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES, scope=self.scope)
示例10: __call__
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import get_collection [as 别名]
def __call__(self, input):
with tf.variable_scope(self.name, reuse=self._reuse):
if not self._reuse:
print('\033[93m'+self.name+'\033[0m')
_ = input
num_channel = [32, 64, 128, 256, 256, 512]
num_layer = np.ceil(np.log2(min(_.shape.as_list()[1:3]))).astype(np.int)
for i in range(num_layer):
ch = num_channel[i] if i < len(num_channel) else 512
_ = conv2d(_, ch, self._is_train, info=not self._reuse,
norm=self._norm_type, name='conv{}'.format(i+1))
_ = conv2d(_, int(num_channel[i]/4), self._is_train, k=1, s=1,
info=not self._reuse, norm='None', name='conv{}'.format(i+2))
_ = conv2d(_, self._num_class+1, self._is_train, k=1, s=1, info=not self._reuse,
activation_fn=None, norm='None',
name='conv{}'.format(i+3))
_ = tf.squeeze(_)
if not self._reuse:
log.info('discriminator output {}'.format(_.shape.as_list()))
self._reuse = True
self.var_list = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES, self.name)
return tf.nn.sigmoid(_), _
示例11: scope_vars
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import get_collection [as 别名]
def scope_vars(scope, trainable_only=False):
"""
Get variables inside a scope
The scope can be specified as a string
Parameters
----------
scope: str or VariableScope
scope in which the variables reside.
trainable_only: bool
whether or not to return only the variables that were marked as trainable.
Returns
-------
vars: [tf.Variable]
list of variables in `scope`.
"""
return tf.get_collection(
tf.GraphKeys.TRAINABLE_VARIABLES if trainable_only else tf.GraphKeys.GLOBAL_VARIABLES,
scope=scope if isinstance(scope, str) else scope.name
)
示例12: __init__
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import get_collection [as 别名]
def __init__(self, ob_dim, ac_dim): #pylint: disable=W0613
X = tf.placeholder(tf.float32, shape=[None, ob_dim*2+ac_dim*2+2]) # batch of observations
vtarg_n = tf.placeholder(tf.float32, shape=[None], name='vtarg')
wd_dict = {}
h1 = tf.nn.elu(dense(X, 64, "h1", weight_init=U.normc_initializer(1.0), bias_init=0, weight_loss_dict=wd_dict))
h2 = tf.nn.elu(dense(h1, 64, "h2", weight_init=U.normc_initializer(1.0), bias_init=0, weight_loss_dict=wd_dict))
vpred_n = dense(h2, 1, "hfinal", weight_init=U.normc_initializer(1.0), bias_init=0, weight_loss_dict=wd_dict)[:,0]
sample_vpred_n = vpred_n + tf.random_normal(tf.shape(vpred_n))
wd_loss = tf.get_collection("vf_losses", None)
loss = tf.reduce_mean(tf.square(vpred_n - vtarg_n)) + tf.add_n(wd_loss)
loss_sampled = tf.reduce_mean(tf.square(vpred_n - tf.stop_gradient(sample_vpred_n)))
self._predict = U.function([X], vpred_n)
optim = kfac.KfacOptimizer(learning_rate=0.001, cold_lr=0.001*(1-0.9), momentum=0.9, \
clip_kl=0.3, epsilon=0.1, stats_decay=0.95, \
async=1, kfac_update=2, cold_iter=50, \
weight_decay_dict=wd_dict, max_grad_norm=None)
vf_var_list = []
for var in tf.trainable_variables():
if "vf" in var.name:
vf_var_list.append(var)
update_op, self.q_runner = optim.minimize(loss, loss_sampled, var_list=vf_var_list)
self.do_update = U.function([X, vtarg_n], update_op) #pylint: disable=E1101
U.initialize() # Initialize uninitialized TF variables
示例13: get_trainable_variables
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import get_collection [as 别名]
def get_trainable_variables():
scopes = [scope.strip() for scope in TRAINABLE_SCOPES.split(',')]
variables_to_train = []
for scope in scopes:
variables = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES, scope)
variables_to_train.extend(variables)
return variables_to_train
示例14: build_optim
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import get_collection [as 别名]
def build_optim(self):
# Update moving_mean and moving_variance for batch normalization layers
update_ops = tf.get_collection(tf.GraphKeys.UPDATE_OPS)
with tf.control_dependencies(update_ops):
with tf.name_scope('reinforce'):
# Actor learning rate
self.lr1 = tf.train.exponential_decay(self.lr1_start, self.global_step, self.lr1_decay_step,self.lr1_decay_rate, staircase=False, name="learning_rate1")
# Optimizer
self.opt1 = tf.train.AdamOptimizer(learning_rate=self.lr1,beta1=0.9,beta2=0.99, epsilon=0.0000001)
# Discounted reward
self.reward_baseline = tf.stop_gradient(self.reward - self.critic.predictions) # [Batch size, 1]
variable_summaries('reward_baseline',self.reward_baseline, with_max_min = True)
# Loss
self.loss1 = tf.reduce_mean(self.reward_baseline*self.log_softmax,0)
tf.summary.scalar('loss1', self.loss1)
# Minimize step
gvs = self.opt1.compute_gradients(self.loss1)
capped_gvs = [(tf.clip_by_norm(grad, 1.), var) for grad, var in gvs if grad is not None] # L2 clip
self.train_step1 = self.opt1.apply_gradients(capped_gvs, global_step=self.global_step)
with tf.name_scope('state_value'):
# Critic learning rate
self.lr2 = tf.train.exponential_decay(self.lr2_start, self.global_step2, self.lr2_decay_step,self.lr2_decay_rate, staircase=False, name="learning_rate1")
# Optimizer
self.opt2 = tf.train.AdamOptimizer(learning_rate=self.lr2,beta1=0.9,beta2=0.99, epsilon=0.0000001)
# Loss
self.loss2 = tf.losses.mean_squared_error(self.reward, self.critic.predictions, weights = 1.0)
tf.summary.scalar('loss2', self.loss1)
# Minimize step
gvs2 = self.opt2.compute_gradients(self.loss2)
capped_gvs2 = [(tf.clip_by_norm(grad, 1.), var) for grad, var in gvs2 if grad is not None] # L2 clip
self.train_step2 = self.opt1.apply_gradients(capped_gvs2, global_step=self.global_step2)
示例15: build_optim
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import get_collection [as 别名]
def build_optim(self):
# Update moving_mean and moving_variance for batch normalization layers
update_ops = tf.get_collection(tf.GraphKeys.UPDATE_OPS)
with tf.control_dependencies(update_ops):
with tf.name_scope('baseline'):
# Update baseline
reward_mean, reward_var = tf.nn.moments(self.reward,axes=[0])
self.base_op = tf.assign(self.avg_baseline, self.alpha*self.avg_baseline+(1.0-self.alpha)*reward_mean)
tf.summary.scalar('average baseline',self.avg_baseline)
with tf.name_scope('reinforce'):
# Actor learning rate
self.lr1 = tf.train.exponential_decay(self.lr1_start, self.global_step, self.lr1_decay_step,self.lr1_decay_rate, staircase=False, name="learning_rate1")
# Optimizer
self.opt1 = tf.train.AdamOptimizer(learning_rate=self.lr1,beta1=0.9,beta2=0.99, epsilon=0.0000001)
# Discounted reward
self.reward_baseline = tf.stop_gradient(self.reward - self.avg_baseline - self.critic.predictions) # [Batch size, 1]
variable_summaries('reward_baseline',self.reward_baseline, with_max_min = True)
# Loss
self.loss1 = tf.reduce_mean(self.reward_baseline*self.log_softmax,0)
tf.summary.scalar('loss1', self.loss1)
# Minimize step
gvs = self.opt1.compute_gradients(self.loss1)
capped_gvs = [(tf.clip_by_norm(grad, 1.), var) for grad, var in gvs if grad is not None] # L2 clip
self.train_step1 = self.opt1.apply_gradients(capped_gvs, global_step=self.global_step)
with tf.name_scope('state_value'):
# Critic learning rate
self.lr2 = tf.train.exponential_decay(self.lr2_start, self.global_step2, self.lr2_decay_step,self.lr2_decay_rate, staircase=False, name="learning_rate1")
# Optimizer
self.opt2 = tf.train.AdamOptimizer(learning_rate=self.lr2,beta1=0.9,beta2=0.99, epsilon=0.0000001)
# Loss
weights_ = 1.0 #weights_ = tf.exp(self.log_softmax-tf.reduce_max(self.log_softmax)) # probs / max_prob
self.loss2 = tf.losses.mean_squared_error(self.reward - self.avg_baseline, self.critic.predictions, weights = weights_)
tf.summary.scalar('loss2', self.loss1)
# Minimize step
gvs2 = self.opt2.compute_gradients(self.loss2)
capped_gvs2 = [(tf.clip_by_norm(grad, 1.), var) for grad, var in gvs2 if grad is not None] # L2 clip
self.train_step2 = self.opt1.apply_gradients(capped_gvs2, global_step=self.global_step2)