Basic
tf.while_loop
def while_loop(cond, body, loop_vars):
...
while cond(loop_vars):
loop_vars = body(loop_vars)
...
loop_vars
参数先传入 cond
判断条件是否成立,如果 cond(loop_vars)
位 True
,则把 loop_vars
参数传入 body
执行操作并用 body
操作后的返回值更新 loop_vars
参数,然后再次进入循环,直到不满足条件。
Graph
API
get tf.Operation from Graph
# session is tf.Session
# returns tf.Operation(in tensorflow/python/framework/ops.py)
session.graph.get_operations()
get_tensor_by_name
import tensorflow as tf
with tf.variable_scope("generate"):
with tf.variable_scope("resnet_stack"):
bias = tf.Variable(0.0,name="bias")
for tv in tf.trainable_variables():
print (tv.name)
b = tf.get_default_graph().get_tensor_by_name("generate/resnet_stack/bias:0")
with tf.Session() as sess:
tf.global_variables_initializer().run()
print(sess.run(b))
Model
analysis
tf.contrib.tfprof.model_analyzer.print_model_analysis()