栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

tf.global_variables_initializer()在后台做什么?

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

tf.global_variables_initializer()在后台做什么?

TensorFlow
API的一节非常短

tf.global_variables_initializer
。它只是提到:

这只是的快捷方式

variable_initializer(global_variables())

将其跟踪到

tf.variables_initializer
,我们可以看到此函数的用法如下:

tf.variables_initializer(var_list, name='init')

这意味着我们正在implitcitly传递

tf.global_variables
作为
var_list
tf.variables_initializer
。如果在调用之前没有定义任何变量
tf.global_variables_initializer
var_list
则本质上为空。下面的代码说明了这一点:

import tensorflow as tfwith tf.Graph().as_default():  # Nothing is printed  for v in tf.global_variables():    print v  init_op = tf.global_variables_initializer()  a = tf.Variable(0)  b = tf.Variable(0)  c = tf.Variable(0)  # 3 Variables are printed here  for v in tf.global_variables():    print v  with tf.Session() as sess:    sess.run(init_op)    print sess.run(a)

输出的3个变量是这样的:

<tf.Variable 'Variable:0' shape=() dtype=int32_ref><tf.Variable 'Variable_1:0' shape=() dtype=int32_ref><tf.Variable 'Variable_2:0' shape=() dtype=int32_ref>

像上面那样运行上面的代码会导致错误:

Attempting to use uninitialized value

交换位置

init_op
a b c

  a = tf.Variable(0)  b = tf.Variable(0)  c = tf.Variable(0)  init_op = tf.global_variables_initializer()

将使其工作。



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/637135.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号