tensorflow学习笔记:sess.run(tf.global_variables_initializer()) 做了什么?

一步步看源代码:(代码在后面)

  • global_variables_initializer 返回一个用来初始化 计算图中 所有global variableop
    • 这个op 到底是啥,还不清楚。
    • 函数中调用了 variable_initializer()global_variables()
  • global_variables() 返回一个 Variable list ,里面保存的是 gloabal variables
  • variable_initializer()Variable list 中的所有 Variable 取出来,将其 variable.initializer 属性做成一个 op group
  • 然后看 Variable 类的源码可以发现, variable.initializer 就是一个 assign op

所以: sess.run(tf.global_variables_initializer()) 就是 run了 所有global Variableassign op,这就是初始化参数的本来面目。

def global_variables_initializer():
  """Returns an Op that initializes global variables.
  Returns:
    An Op that initializes global variables in the graph.
  """
  return variables_initializer(global_variables())

def global_variables():
  """Returns global variables.
  Returns:
    A list of `Variable` objects.
  """
  return ops.get_collection(ops.GraphKeys.GLOBAL_VARIABLES)

def variables_initializer(var_list, name="init"):
  """Returns an Op that initializes a list of variables.
  Args:
    var_list: List of `Variable` objects to initialize.
    name: Optional name for the returned operation.

  Returns:
    An Op that run the initializers of all the specified variables.
  """
  if var_list:
    return control_flow_ops.group(*[v.initializer for v in var_list], name=name)
  return control_flow_ops.no_op(name=name)
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
class Variable(object):
    def _init_from_args(self, ...):
        self._initializer_op = state_ops.assign(
            self._variable, self._initial_value,
            validate_shape=validate_shape).op
    @property
    def initializer(self):
        """The initializer operation for this variable."""
        return self._initializer_op
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
					<link href="https://csdnimg.cn/release/phoenix/mdeditor/markdown_views-8cccb36679.css" rel="stylesheet">
            </div>
								
				<script>
					(function(){
						function setArticleH(btnReadmore,posi){
							var winH = $(window).height();
							var articleBox = $("div.article_content");
							var artH = articleBox.height();
							if(artH > winH*posi){
								articleBox.css({
									'height':winH*posi+'px',
									'overflow':'hidden'
								})
								btnReadmore.click(function(){
									articleBox.removeAttr("style");
									$(this).parent().remove();
								})
							}else{
								btnReadmore.parent().remove();
							}
						}
						var btnReadmore = $("#btn-readmore");
						if(btnReadmore.length>0){
							if(currentUserName){
								setArticleH(btnReadmore,3);
							}else{
								setArticleH(btnReadmore,1.2);
							}
						}
					})()
				</script>
				</article>
GitHub 加速计划 / te / tensorflow
184.55 K
74.12 K
下载
一个面向所有人的开源机器学习框架
最近提交(Master分支:2 个月前 )
a49e66f2 PiperOrigin-RevId: 663726708 2 个月前
91dac11a This test overrides disabled_backends, dropping the default value in the process. PiperOrigin-RevId: 663711155 2 个月前
Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐