TensorFlow-1 入门

2018-06-28  本文已影响0人  danyue_smile

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

[图片上传失败...(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

4. 变量共享

上一篇下一篇

猜你喜欢

热点阅读