本文整理汇总了Python中tensorflow.initialize_variables方法的典型用法代码示例。如果您正苦于以下问题:Python tensorflow.initialize_variables方法的具体用法?Python tensorflow.initialize_variables怎么用?Python tensorflow.initialize_variables使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类tensorflow
的用法示例。
在下文中一共展示了tensorflow.initialize_variables方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: initialize_interdependent_variables
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import initialize_variables [as 别名]
def initialize_interdependent_variables(session, vars_list, feed_dict):
"""Initialize a list of variables one at a time, which is useful if
initialization of some variables depends on initialization of the others.
"""
vars_left = vars_list
while len(vars_left) > 0:
new_vars_left = []
for v in vars_left:
try:
# If using an older version of TensorFlow, uncomment the line
# below and comment out the line after it.
#session.run(tf.initialize_variables([v]), feed_dict)
session.run(tf.variables_initializer([v]), feed_dict)
except tf.errors.FailedPreconditionError:
new_vars_left.append(v)
if len(new_vars_left) >= len(vars_left):
# This can happend if the variables all depend on each other, or more likely if there's
# another variable outside of the list, that still needs to be initialized. This could be
# detected here, but life's finite.
raise Exception("Cycle in variable dependencies, or extenrnal precondition unsatisfied.")
else:
vars_left = new_vars_left
示例2: test_scipy_lbfgsb
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import initialize_variables [as 别名]
def test_scipy_lbfgsb():
sess = tf.Session()
x = tf.Variable(np.float64(2), name='x')
sess.run(tf.initialize_variables([x]))
optimizer = ScipyLBFGSBOptimizer(verbose=True, session=sess)
# With gradient
results = optimizer.minimize([x], x**2, [2 * x])
assert results.success
# Without gradient
results = optimizer.minimize([x], x**2)
assert results.success
# Test callback
def callback(xs):
pass
optimizer = ScipyLBFGSBOptimizer(verbose=True, session=sess, callback=callback)
assert optimizer.minimize([x], x**2).success
@raises(ValueError)
def test_illegal_parameter_as_variable1():
optimizer.minimize([42], x**2)
test_illegal_parameter_as_variable1()
@raises(ValueError)
def test_illegal_parameter_as_variable2():
optimizer.minimize(42, x**2)
test_illegal_parameter_as_variable2()
示例3: test_migrad
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import initialize_variables [as 别名]
def test_migrad():
sess = tf.Session()
x = tf.Variable(np.float64(2), name='x')
sess.run(tf.initialize_variables([x]))
optimizer = MigradOptimizer(session=sess)
# With gradient
results = optimizer.minimize([x], x**2, [2 * x])
assert results.success
# Without gradient
results = optimizer.minimize([x], x**2)
assert results.success
@raises(ValueError)
def test_illegal_parameter_as_variable1():
optimizer.minimize([42], x**2)
test_illegal_parameter_as_variable1()
@raises(ValueError)
def test_illegal_parameter_as_variable2():
optimizer.minimize(42, x**2)
test_illegal_parameter_as_variable2()
示例4: typeBasedColdEmbExp
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import initialize_variables [as 别名]
def typeBasedColdEmbExp(self, ckptName="FigerModel-20001"):
''' Train cold embeddings using wiki desc loss
'''
saver = tf.train.Saver(var_list=tf.all_variables())
print("Loading Model ... ")
if ckptName == None:
print("Given CKPT Name")
sys.exit()
else:
load_status = self.fm.loadSpecificCKPT(
saver=saver, checkpoint_dir=self.fm.checkpoint_dir,
ckptName=ckptName, attrs=self.fm._attrs)
if not load_status:
print("No model to load. Exiting")
sys.exit(0)
self._makeDescLossGraph()
self.fm.sess.run(tf.initialize_variables(self.allcoldvars))
self._trainColdEmbFromTypes(epochsToTrain=5)
self.runEval()
##############################################################################
示例5: typeAndWikiDescBasedColdEmbExp
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import initialize_variables [as 别名]
def typeAndWikiDescBasedColdEmbExp(self, ckptName="FigerModel-20001"):
''' Train cold embeddings using wiki desc loss
'''
saver = tf.train.Saver(var_list=tf.all_variables())
print("Loading Model ... ")
if ckptName == None:
print("Given CKPT Name")
sys.exit()
else:
load_status = self.fm.loadSpecificCKPT(
saver=saver, checkpoint_dir=self.fm.checkpoint_dir,
ckptName=ckptName, attrs=self.fm._attrs)
if not load_status:
print("No model to load. Exiting")
sys.exit(0)
self._makeDescLossGraph()
self.fm.sess.run(tf.initialize_variables(self.allcoldvars))
self._trainColdEmbFromTypesAndDesc(epochsToTrain=5)
self.runEval()
# EVALUATION FOR COLD START WHEN INITIALIZING COLD EMB FROM WIKI DESC ENCODING
示例6: testInitializeFromValue
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import initialize_variables [as 别名]
def testInitializeFromValue(self):
with self.test_session() as sess:
init = tf.constant(0.1)
w = tf.get_variable("v", initializer=init)
sess.run(tf.initialize_variables([w]))
self.assertAllClose(w.eval(), 0.1)
with self.assertRaisesRegexp(ValueError, "shape"):
# We disallow explicit shape specification when initializer is constant.
tf.get_variable("u", [1], initializer=init)
with tf.variable_scope("foo", initializer=init):
# Constant initializer can be passed through scopes if needed.
v = tf.get_variable("v")
sess.run(tf.initialize_variables([v]))
self.assertAllClose(v.eval(), 0.1)
# Check that non-float32 initializer creates a non-float32 variable.
init = tf.constant(1, dtype=tf.int32)
t = tf.get_variable("t", initializer=init)
self.assertEqual(t.dtype.base_dtype, tf.int32)
# Raise error if `initializer` dtype and `dtype` are not identical.
with self.assertRaisesRegexp(ValueError, "don't match"):
tf.get_variable("s", initializer=init, dtype=tf.float64)
示例7: _test_streaming_sparse_precision_at_top_k
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import initialize_variables [as 别名]
def _test_streaming_sparse_precision_at_top_k(self,
top_k_predictions,
labels,
expected,
class_id=None,
weights=None):
with tf.Graph().as_default() as g, self.test_session(g):
if weights is not None:
weights = tf.constant(weights, tf.float32)
metric, update = metrics.streaming_sparse_precision_at_top_k(
top_k_predictions=tf.constant(top_k_predictions, tf.int32),
labels=labels, class_id=class_id, weights=weights)
# Fails without initialized vars.
self.assertRaises(tf.OpError, metric.eval)
self.assertRaises(tf.OpError, update.eval)
tf.initialize_variables(tf.local_variables()).run()
# Run per-step op and assert expected values.
if math.isnan(expected):
self.assertTrue(math.isnan(update.eval()))
self.assertTrue(math.isnan(metric.eval()))
else:
self.assertEqual(expected, update.eval())
self.assertEqual(expected, metric.eval())
示例8: _test_streaming_sparse_average_precision_at_k
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import initialize_variables [as 别名]
def _test_streaming_sparse_average_precision_at_k(
self, predictions, labels, k, expected, weights=None):
with tf.Graph().as_default() as g, self.test_session(g):
if weights is not None:
weights = tf.constant(weights, tf.float32)
predictions = tf.constant(predictions, tf.float32)
metric, update = metrics.streaming_sparse_average_precision_at_k(
predictions, labels, k, weights=weights)
# Fails without initialized vars.
self.assertRaises(tf.OpError, metric.eval)
self.assertRaises(tf.OpError, update.eval)
local_variables = tf.local_variables()
tf.initialize_variables(local_variables).run()
# Run per-step op and assert expected values.
if math.isnan(expected):
_assert_nan(self, update.eval())
_assert_nan(self, metric.eval())
else:
self.assertAlmostEqual(expected, update.eval())
self.assertAlmostEqual(expected, metric.eval())
示例9: load_params
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import initialize_variables [as 别名]
def load_params(sess, filename, checkpoint, init_all = True):
params = tf.trainable_variables()
filename = filename + '_' + str(checkpoint)
f = open(filename + '.pkl', 'r')
param_dict = cPickle.load(f)
print 'param loaded', len(param_dict)
f.close()
ops = []
for v in params:
if v.name in param_dict.keys():
ops.append(tf.assign(v, param_dict[v.name]))
sess.run(ops)
# init uninitialised params
if init_all:
all_var = tf.all_variables()
var = [v for v in all_var if v not in params]
sess.run(tf.initialize_variables(var))
print 'loaded parameters from ' + filename + '.pkl'
示例10: init_and_reload
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import initialize_variables [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!!')
示例11: init
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import initialize_variables [as 别名]
def init(self):
self._sess.run(tf.initialize_variables(self._var_list))
self._init = True
示例12: _initialize_variables
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import initialize_variables [as 别名]
def _initialize_variables(self):
uninitialized_var_names = [bytes.decode(var) for var in self._sess.run(tf.report_uninitialized_variables())]
uninitialized_vars = [var for var in tf.global_variables() if var.name.split(':')[0] in uninitialized_var_names]
self._sess.run(tf.initialize_variables(uninitialized_vars))
示例13: guarantee_initialized_variables
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import initialize_variables [as 别名]
def guarantee_initialized_variables(self, session, list_of_variables=None):
if list_of_variables is None:
list_of_variables = tf.all_variables()
uninitialized_variables = list(tf.get_variable(name) for name in
session.run(tf.report_uninitialized_variables(list_of_variables)))
session.run(tf.initialize_variables(uninitialized_variables))
return uninitialized_variables
示例14: __setitem__
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import initialize_variables [as 别名]
def __setitem__(self, index, value):
for use_gpu in [False, True]:
with self.test.test_session(use_gpu=use_gpu) as sess:
var = tf.Variable(self.x)
sess.run(tf.initialize_variables([var]))
val = sess.run(var[index].assign(
tf.constant(
value, dtype=self.tensor_type)))
valnp = np.copy(self.x_np)
valnp[index] = np.array(value)
self.test.assertAllEqual(val, valnp)
示例15: testVarScopeInitializer
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import initialize_variables [as 别名]
def testVarScopeInitializer(self):
with self.test_session() as sess:
init = tf.constant_initializer(0.3)
with tf.variable_scope("tower") as tower:
with tf.variable_scope("foo", initializer=init):
v = tf.get_variable("v", [])
sess.run(tf.initialize_variables([v]))
self.assertAllClose(v.eval(), 0.3)
with tf.variable_scope(tower, initializer=init):
w = tf.get_variable("w", [])
sess.run(tf.initialize_variables([w]))
self.assertAllClose(w.eval(), 0.3)