美文网首页
TensorFlow-1 入门

TensorFlow-1 入门

作者: danyue_smile | 来源:发表于2018-06-28 16:02 被阅读0次

1. Graph is Template; Session is the actual realization;

**Computation Paths** 

import tensorflow as tf

two_node = tf.constant(2)

three_node = tf.constant(3)

sum_node = two_node + three_node ## equivalent to tf.add(two_node, three_node)

input_placeholder = tf.placeholder(tf.int32)

sess = tf.Session()

print sess.run(input_placeholder, feed_dict={input_placeholder: 2})

如上,分别展示了 tf 中的 常量,占位符的应用;需要注意的是:

In general, sess.run() calls tend to be one of the biggest TensorFlow bottlenecks, so the fewer times you call it, the better. Whenever possible, return multiple items in a single sess.run() call instead of making multiple calls.

也就是,sess.run() 不应该多次调用,应尽可能地在单次调用时,返回所需要的多个目标

2. 上面介绍了 常量,占位符 ,常用的还有 变量


import tensorflow as tf

count_variable = tf.get_variable("count", []) # []给定标量,[3,8] 给定 3*8 矩阵

zero_node = tf.constant(0.)

assign_node = tf.assign(count_variable, zero_node)

sess = tf.Session()

sess.run(assign_node)

print sess.run(count_variable)

输出:

0

  • tf.assign(target, value) 将值分配到目标变量上

  • side effects 指对 graph 中节点的影响

  • 节点间的依赖性

[图片上传失败...(image-ed562b-1530172910213)]

3. 关于初始化


import tensorflow as tf

const_init_node = tf.constant_initializer(0.)

count_variable = tf.get_variable("count", [], initializer=const_init_node)

init = tf.global_variables_initializer()

sess = tf.Session()

sess.run(init)

print sess.run(count_variable)

输出:

0

  • tf.global_variables_initializer() 对 graph 中的 tf.initializer 进行初始化,
    并在 sess 运行时,被实现

4. 变量共享

相关文章

网友评论

      本文标题:TensorFlow-1 入门

      本文链接:https://www.haomeiwen.com/subject/dzmgyftx.html