本文整理汇总了Python中tensorflow.is_variable_initialized方法的典型用法代码示例。如果您正苦于以下问题:Python tensorflow.is_variable_initialized方法的具体用法?Python tensorflow.is_variable_initialized怎么用?Python tensorflow.is_variable_initialized使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类tensorflow
的用法示例。
在下文中一共展示了tensorflow.is_variable_initialized方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: init_uninited_vars
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import is_variable_initialized [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()]
示例2: _create_autosummary_var
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import is_variable_initialized [as 别名]
def _create_autosummary_var(name, value_expr):
assert not _autosummary_finalized
v = tf.cast(value_expr, tf.float32)
if v.shape.ndims is 0:
v = [v, np.float32(1.0)]
elif v.shape.ndims is 1:
v = [tf.reduce_sum(v), tf.cast(tf.shape(v)[0], tf.float32)]
else:
v = [tf.reduce_sum(v), tf.reduce_prod(tf.cast(tf.shape(v), tf.float32))]
v = tf.cond(tf.is_finite(v[0]), lambda: tf.stack(v), lambda: tf.zeros(2))
with tf.control_dependencies(None):
var = tf.Variable(tf.zeros(2)) # [numerator, denominator]
update_op = tf.cond(tf.is_variable_initialized(var), lambda: tf.assign_add(var, v), lambda: tf.assign(var, v))
if name in _autosummary_vars:
_autosummary_vars[name].append(var)
else:
_autosummary_vars[name] = [var]
return update_op
#----------------------------------------------------------------------------
# Call filewriter.add_summary() with all summaries in the default graph,
# automatically finalizing and merging them on the first call.
示例3: initialize_uninitialized_global_variables
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import is_variable_initialized [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: testPrepareSessionDidNotInitLocalVariable
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import is_variable_initialized [as 别名]
def testPrepareSessionDidNotInitLocalVariable(self):
with tf.Graph().as_default():
v = tf.Variable(1, name="v")
w = tf.Variable(
v,
trainable=False,
collections=[tf.GraphKeys.LOCAL_VARIABLES],
name="w")
with self.test_session():
self.assertEqual(False, tf.is_variable_initialized(v).eval())
self.assertEqual(False, tf.is_variable_initialized(w).eval())
sm2 = tf.train.SessionManager(
ready_op=tf.report_uninitialized_variables())
with self.assertRaisesRegexp(RuntimeError,
"Init operations did not make model ready"):
sm2.prepare_session("", init_op=v.initializer)
示例5: testPrepareSessionWithReadyNotReadyForLocal
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import is_variable_initialized [as 别名]
def testPrepareSessionWithReadyNotReadyForLocal(self):
with tf.Graph().as_default():
v = tf.Variable(1, name="v")
w = tf.Variable(
v,
trainable=False,
collections=[tf.GraphKeys.LOCAL_VARIABLES],
name="w")
with self.test_session():
self.assertEqual(False, tf.is_variable_initialized(v).eval())
self.assertEqual(False, tf.is_variable_initialized(w).eval())
sm2 = tf.train.SessionManager(
ready_op=tf.report_uninitialized_variables(),
ready_for_local_init_op=tf.report_uninitialized_variables(
tf.all_variables()),
local_init_op=w.initializer)
with self.assertRaisesRegexp(
RuntimeError,
"Init operations did not make model ready for local_init"):
sm2.prepare_session("", init_op=None)
示例6: init_and_reload
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import is_variable_initialized [as 别名]
def init_and_reload(self):
##########
# this function is only used for the gan training with reload
##########
params = [param for param in tf.trainable_variables() if 'generate' in param.name]
#params = [param for param in tf.all_variables()]
if not self.sess.run(tf.is_variable_initialized(params[0])):
#init_op = tf.initialize_variables(params)
init_op = tf.global_variables_initializer() ## this is important here to initialize_all_variables()
self.sess.run(init_op)
saver = tf.train.Saver(params)
self.saver=saver
if self.gen_reload: ##here must be true
print('reloading params from %s '% self.saveto)
self.saver.restore(self.sess, './'+self.saveto)
print('reloading params done')
else:
print('error, reload must be true!!')
示例7: initialize_uninitialized
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import is_variable_initialized [as 别名]
def initialize_uninitialized(sess):
"""
Function to initialize only uninitialized variables in a session graph
Parameters
----------
sess : tf.Session()
"""
global_vars = tf.global_variables()
is_not_initialized = sess.run(
[tf.is_variable_initialized(var) for var in global_vars])
not_initialized_vars = [v for (v, f) in zip(
global_vars, is_not_initialized) if not f]
if not_initialized_vars:
sess.run(tf.variables_initializer(not_initialized_vars))
示例8: initialize_uninitialized_variables
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import is_variable_initialized [as 别名]
def initialize_uninitialized_variables(sess):
"""
Only initialize the weights that have not yet been initialized by other
means, such as importing a metagraph and a checkpoint. It's useful when
extending an existing model.
"""
uninit_vars = []
uninit_tensors = []
for var in tf.global_variables():
uninit_vars.append(var)
uninit_tensors.append(tf.is_variable_initialized(var))
uninit_bools = sess.run(uninit_tensors)
uninit = zip(uninit_bools, uninit_vars)
uninit = [var for init, var in uninit if not init]
sess.run(tf.variables_initializer(uninit))
#-------------------------------------------------------------------------------
示例9: initialize_uninitialized
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import is_variable_initialized [as 别名]
def initialize_uninitialized(self, ):
"""Only initializes the variables of a TensorFlow session that were not
already initialized.
"""
# List all global variables.
sess = self.sess
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 previously initialized.
not_initialized_vars = [var for (var, init) in
zip(global_vars, is_initialized) if not init]
for v in not_initialized_vars:
print('[!] not init: {}'.format(v.name))
# Initialize all uninitialized variables found, if any.
if len(not_initialized_vars):
sess.run(tf.variables_initializer(not_initialized_vars))
示例10: initialize_uninitialized_variables
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import is_variable_initialized [as 别名]
def initialize_uninitialized_variables(variables=None):
if variables is None:
variables = tf.global_variables()
if not variables:
return
session = tensorflow_session()
is_not_initialized = session.run([
tf.is_variable_initialized(var) for var in variables])
not_initialized_vars = [
v for (v, f) in zip(variables, is_not_initialized) if not f]
if len(not_initialized_vars):
session.run(tf.variables_initializer(not_initialized_vars))
示例11: initialize_uninitialized_global_variables
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import is_variable_initialized [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))
示例12: initialize_uninitialized
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import is_variable_initialized [as 别名]
def initialize_uninitialized(sess):
global_vars = tf.global_variables()
is_not_initialized = sess.run([tf.is_variable_initialized(var) for var in global_vars])
not_initialized_vars = [v for (v, f) in zip(global_vars, is_not_initialized) if not f]
if len(not_initialized_vars):
sess.run(tf.variables_initializer(not_initialized_vars))
示例13: init_uninitialized_vars
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import is_variable_initialized [as 别名]
def init_uninitialized_vars(target_vars: List[tf.Variable] = None) -> None:
"""Initialize all tf.Variables that have not already been initialized.
Equivalent to the following, but more efficient and does not bloat the tf graph:
tf.variables_initializer(tf.report_uninitialized_variables()).run()
"""
assert_tf_initialized()
if target_vars is None:
target_vars = tf.global_variables()
test_vars = []
test_ops = []
with tf.control_dependencies(None): # ignore surrounding control_dependencies
for var in target_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])
示例14: initialize_uninitialized
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import is_variable_initialized [as 别名]
def initialize_uninitialized(sess):
global_vars = tf.global_variables()
is_not_initialized = sess.run([tf.is_variable_initialized(var) for var in global_vars])
not_initialized_vars = [v for (v,f) in zip(global_vars, is_not_initialized) if not f]
print [str(i.name) for i in not_initialized_vars]
示例15: testIsVariableInitialized
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import is_variable_initialized [as 别名]
def testIsVariableInitialized(self):
for use_gpu in [True, False]:
with self.test_session(use_gpu=use_gpu):
v0 = state_ops.variable_op([1, 2], tf.float32)
self.assertEqual(False, tf.is_variable_initialized(v0).eval())
tf.assign(v0, [[2.0, 3.0]]).eval()
self.assertEqual(True, tf.is_variable_initialized(v0).eval())