本文整理汇总了Python中tensorflow.global_variables方法的典型用法代码示例。如果您正苦于以下问题:Python tensorflow.global_variables方法的具体用法?Python tensorflow.global_variables怎么用?Python tensorflow.global_variables使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类tensorflow
的用法示例。
在下文中一共展示了tensorflow.global_variables方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: to_darknet
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import global_variables [as 别名]
def to_darknet(self):
darknet_ckpt = self.darknet
with self.graph.as_default() as g:
for var in tf.global_variables():
name = var.name.split(':')[0]
var_name = name.split('-')
l_idx = int(var_name[0])
w_sig = var_name[1].split('/')[-1]
l = darknet_ckpt.layers[l_idx]
l.w[w_sig] = var.eval(self.sess)
for layer in darknet_ckpt.layers:
for ph in layer.h:
layer.h[ph] = None
return darknet_ckpt
示例2: init_uninited_vars
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import global_variables [as 别名]
def init_uninited_vars(vars=None):
if vars is None: vars = tf.global_variables()
test_vars = []; test_ops = []
with tf.control_dependencies(None): # ignore surrounding control_dependencies
for var in vars:
assert is_tf_expression(var)
try:
tf.get_default_graph().get_tensor_by_name(var.name.replace(':0', '/IsVariableInitialized:0'))
except KeyError:
# Op does not exist => variable may be uninitialized.
test_vars.append(var)
with absolute_name_scope(var.name.split(':')[0]):
test_ops.append(tf.is_variable_initialized(var))
init_vars = [var for var, inited in zip(test_vars, run(test_ops)) if not inited]
run([var.initializer for var in init_vars])
#----------------------------------------------------------------------------
# Set the values of given tf.Variables.
# Equivalent to the following, but more efficient and does not bloat the tf graph:
# tfutil.run([tf.assign(var, value) for var, value in var_to_value_dict.items()]
示例3: initialize_uninitialized_global_variables
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import global_variables [as 别名]
def initialize_uninitialized_global_variables(sess):
"""
Only initializes the variables of a TensorFlow session that were not
already initialized.
:param sess: the TensorFlow session
:return:
"""
# List all global variables
global_vars = tf.global_variables()
# Find initialized status for all variables
is_var_init = [tf.is_variable_initialized(var) for var in global_vars]
is_initialized = sess.run(is_var_init)
# List all variables that were not initialized previously
not_initialized_vars = [var for (var, init) in
zip(global_vars, is_initialized) if not init]
# Initialize all uninitialized variables found, if any
if len(not_initialized_vars):
sess.run(tf.variables_initializer(not_initialized_vars))
示例4: define_saver
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import global_variables [as 别名]
def define_saver(exclude=None):
"""Create a saver for the variables we want to checkpoint.
Args:
exclude: List of regexes to match variable names to exclude.
Returns:
Saver object.
"""
variables = []
exclude = exclude or []
exclude = [re.compile(regex) for regex in exclude]
for variable in tf.global_variables():
if any(regex.match(variable.name) for regex in exclude):
continue
variables.append(variable)
saver = tf.train.Saver(variables, keep_checkpoint_every_n_hours=5)
return saver
示例5: _start_session
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import global_variables [as 别名]
def _start_session(self):
"""
Starts the Tensorflow Session
:return: None
"""
self.sess.run(tf.global_variables_initializer())
# initialize the saver node
# print tf.GraphKeys.GLOBAL_VARIABLES
self.saver = tf.train.Saver(tf.global_variables())
# get the latest checkpoint
last_checkpoint_path = self.checkpointer.get_last_checkpoint()
if last_checkpoint_path is not None:
print 'Previous saved tensorflow objects found... Extracting...'
# restore the tensorflow variables
self.saver.restore(self.sess, last_checkpoint_path)
print 'Extraction Complete. Moving Forward....'
示例6: testVarNames
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import global_variables [as 别名]
def testVarNames(self):
with tf.Graph().as_default():
model, features = get_model(
mode=tf.estimator.ModeKeys.PREDICT,
model_cls=transformer.TransformerScorer)
_ = model.infer(features)
scorer_vars = [v.name for v in tf.global_variables()]
with tf.Graph().as_default():
model, features = get_model(
mode=tf.estimator.ModeKeys.EVAL,
model_cls=transformer.TransformerScorer)
_ = model(features)
scorer_eval_vars = [v.name for v in tf.global_variables()]
with tf.Graph().as_default():
model, features = get_model(
mode=tf.estimator.ModeKeys.EVAL,
model_cls=transformer.Transformer)
_ = model(features)
transformer_vars = [v.name for v in tf.global_variables()]
self.assertEqual(sorted(scorer_vars), sorted(transformer_vars))
self.assertEqual(sorted(scorer_eval_vars), sorted(transformer_vars))
示例7: underlying_variable
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import global_variables [as 别名]
def underlying_variable(t):
"""Find the underlying tf.Variable object.
Args:
t: a Tensor
Returns:
tf.Variable.
"""
t = underlying_variable_ref(t)
assert t is not None
# make sure that the graph has a variable index and that it is up-to-date
if not hasattr(tf.get_default_graph(), "var_index"):
tf.get_default_graph().var_index = {}
var_index = tf.get_default_graph().var_index
for v in tf.global_variables()[len(var_index):]:
var_index[v.name] = v
return var_index[t.name]
示例8: main
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import global_variables [as 别名]
def main(tiny):
if tiny:
model = YOLOv3_tiny(n_classes=80,
iou_threshold=0.5,
confidence_threshold=0.5)
else:
model = YOLOv3(n_classes=80,
iou_threshold=0.5,
confidence_threshold=0.5)
inputs = tf.placeholder(tf.float32, [1, 416, 416, 3])
model(inputs)
model_vars = tf.global_variables(scope=model.scope)
if tiny:
assign_ops = load_weights_tiny(model_vars, './weights/yolov3-tiny.weights')
else:
assign_ops = load_weights(model_vars, './weights/yolov3.weights')
saver = tf.train.Saver(tf.global_variables(scope=model.scope))
with tf.Session() as sess:
save_path = './weights/model-tiny.ckpt' if tiny else './weights/model.ckpt'
sess.run(assign_ops)
saver.save(sess, save_path)
print("Model Saved at \"" + save_path + "\"")
示例9: convert_to_coverage_model
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import global_variables [as 别名]
def convert_to_coverage_model(self):
"""Load non-coverage checkpoint, add initialized extra variables for coverage, and save as new checkpoint"""
tf.logging.info("converting non-coverage model to coverage model..")
# initialize an entire coverage model from scratch
sess = tf.Session(config=util.get_config())
print("initializing everything...")
sess.run(tf.global_variables_initializer())
# load all non-coverage weights from checkpoint
saver = tf.train.Saver([v for v in tf.global_variables() if "coverage" not in v.name and "Adagrad" not in v.name])
print("restoring non-coverage variables...")
curr_ckpt = util.load_ckpt(saver, sess)
print("restored.")
# save this model and quit
new_fname = curr_ckpt + '_cov_init'
print("saving model to %s..." % (new_fname))
new_saver = tf.train.Saver() # this one will save all variables that now exist
new_saver.save(sess, new_fname)
print("saved.")
exit()
示例10: convert_to_reinforce_model
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import global_variables [as 别名]
def convert_to_reinforce_model(self):
"""Load non-reinforce checkpoint, add initialized extra variables for reinforce, and save as new checkpoint"""
tf.logging.info("converting non-reinforce model to reinforce model..")
# initialize an entire reinforce model from scratch
sess = tf.Session(config=util.get_config())
print("initializing everything...")
sess.run(tf.global_variables_initializer())
# load all non-reinforce weights from checkpoint
saver = tf.train.Saver([v for v in tf.global_variables() if "reinforce" not in v.name and "Adagrad" not in v.name])
print("restoring non-reinforce variables...")
curr_ckpt = util.load_ckpt(saver, sess)
print("restored.")
# save this model and quit
new_fname = curr_ckpt + '_rl_init'
print("saving model to %s..." % (new_fname))
new_saver = tf.train.Saver() # this one will save all variables that now exist
new_saver.save(sess, new_fname)
print("saved.")
exit()
示例11: _init_pos_model
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import global_variables [as 别名]
def _init_pos_model(self, session):
"""Create POS Tagger model and initialize with random or load parameters in session."""
# initilize config
config_dict = load_config(self.model_config_path)
config = get_config(config_dict, self.name)
config.batch_size = 1
config.num_steps = 1 # iterator one token per time
model_var_scope = get_model_var_scope(self.var_scope, self.name)
print ("NOTICE: Input POS Model Var Scope Name '%s'" % model_var_scope)
# Check if self.model already exist
if self.model is None:
with tf.variable_scope(model_var_scope, tf.AUTO_REUSE):
self.model = pos_model.POSTagger(is_training=False, config=config) # save object after is_training
# Load Specific .data* ckpt file
if len(glob.glob(self.ckpt_path + '.data*')) > 0: # file exist with pattern: 'pos.ckpt.data*'
print("NOTICE: Loading model parameters from %s" % self.ckpt_path)
all_vars = tf.global_variables()
model_vars = [k for k in all_vars if model_var_scope in k.name.split("/")]
tf.train.Saver(model_vars).restore(session, self.ckpt_path)
else:
print("NOTICE: Model not found, Try to run method: deepnlp.download(module='pos', name='%s')" % self.name)
print("NOTICE: Created with fresh parameters.")
session.run(tf.global_variables_initializer())
示例12: _checkpoint_var_search
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import global_variables [as 别名]
def _checkpoint_var_search(self, checkpoint_path):
reader = tf.train.NewCheckpointReader(checkpoint_path)
saved_shapes = reader.get_variable_to_shape_map()
model_names = tf.model_variables() # Used by tf.slim layers
if not len(tf.model_variables()):
model_names = tf.global_variables() # Fallback when slim is not used
model_names = set([v.name.split(':')[0] for v in model_names])
checkpoint_names = set(saved_shapes.keys())
found_names = model_names & checkpoint_names
missing_names = model_names - checkpoint_names
shape_conflicts = set()
restored = []
with tf.variable_scope('', reuse=True):
for name in found_names:
# print(tf.global_variables())
# print(name, name in model_names, name in checkpoint_names)
var = tf.get_variable(name)
var_shape = var.get_shape().as_list()
if var_shape == saved_shapes[name]:
restored.append(var)
else:
shape_conflicts.add(name)
found_names -= shape_conflicts
return (restored, sorted(found_names),
sorted(missing_names), sorted(shape_conflicts))
示例13: setSavers
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import global_variables [as 别名]
def setSavers(model):
saver = tf.train.Saver(max_to_keep = config.weightsToKeep)
subsetSaver = None
if config.saveSubset:
isRelevant = lambda var: any(s in var.name for s in config.varSubset)
relevantVars = [var for var in tf.global_variables() if isRelevant(var)]
subsetSaver = tf.train.Saver(relevantVars, max_to_keep = config.weightsToKeep, allow_empty = True)
emaSaver = None
if config.useEMA:
emaSaver = tf.train.Saver(model.emaDict, max_to_keep = config.weightsToKeep)
return {
"saver": saver,
"subsetSaver": subsetSaver,
"emaSaver": emaSaver
}
################################### restore / initialize weights ##################################
# Restores weights of specified / last epoch if on restore mod.
# Otherwise, initializes weights.
示例14: restore_from_classification_checkpoint_fn
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import global_variables [as 别名]
def restore_from_classification_checkpoint_fn(self, feature_extractor_scope):
"""Returns a map of variables to load from a foreign checkpoint.
Note that this overrides the default implementation in
ssd_meta_arch.SSDFeatureExtractor which does not work for PNASNet
checkpoints.
Args:
feature_extractor_scope: A scope name for the first stage feature
extractor.
Returns:
A dict mapping variable names (to load from a checkpoint) to variables in
the model graph.
"""
variables_to_restore = {}
for variable in tf.global_variables():
if variable.op.name.startswith(feature_extractor_scope):
var_name = variable.op.name.replace(feature_extractor_scope + '/', '')
var_name += '/ExponentialMovingAverage'
variables_to_restore[var_name] = variable
return variables_to_restore
示例15: restore_from_classification_checkpoint_fn
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import global_variables [as 别名]
def restore_from_classification_checkpoint_fn(self, feature_extractor_scope):
"""Returns a map of variables to load from a foreign checkpoint.
Args:
feature_extractor_scope: A scope name for the feature extractor.
Returns:
A dict mapping variable names (to load from a checkpoint) to variables in
the model graph.
"""
variables_to_restore = {}
for variable in tf.global_variables():
var_name = variable.op.name
if var_name.startswith(feature_extractor_scope + '/'):
var_name = var_name.replace(feature_extractor_scope + '/', '')
variables_to_restore[var_name] = variable
return variables_to_restore