本文整理汇总了Python中tensorflow.contrib.graph_editor.get_backward_walk_ops方法的典型用法代码示例。如果您正苦于以下问题:Python graph_editor.get_backward_walk_ops方法的具体用法?Python graph_editor.get_backward_walk_ops怎么用?Python graph_editor.get_backward_walk_ops使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类tensorflow.contrib.graph_editor
的用法示例。
在下文中一共展示了graph_editor.get_backward_walk_ops方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: dependency_of_targets
# 需要导入模块: from tensorflow.contrib import graph_editor [as 别名]
# 或者: from tensorflow.contrib.graph_editor import get_backward_walk_ops [as 别名]
def dependency_of_targets(targets, op):
"""
Check that op is in the subgraph induced by the dependencies of targets.
The result is memoized.
This is useful if some SessionRunHooks should be run only together with certain ops.
Args:
targets: a tuple of ops or tensors. The targets to find dependencies of.
op (tf.Operation or tf.Tensor):
Returns:
bool
"""
# TODO tensorarray? sparsetensor?
if isinstance(op, tf.Tensor):
op = op.op
assert isinstance(op, tf.Operation), op
from tensorflow.contrib.graph_editor import get_backward_walk_ops
# alternative implementation can use graph_util.extract_sub_graph
dependent_ops = get_backward_walk_ops(targets, control_inputs=True)
return op in dependent_ops
示例2: fast_backward_ops
# 需要导入模块: from tensorflow.contrib import graph_editor [as 别名]
# 或者: from tensorflow.contrib.graph_editor import get_backward_walk_ops [as 别名]
def fast_backward_ops(within_ops, seed_ops, stop_at_ts):
bwd_ops = set(ge.get_backward_walk_ops(seed_ops, stop_at_ts=stop_at_ts))
ops = bwd_ops.intersection(within_ops).difference(
[t.op for t in stop_at_ts])
return list(ops)
示例3: fast_backward_ops
# 需要导入模块: from tensorflow.contrib import graph_editor [as 别名]
# 或者: from tensorflow.contrib.graph_editor import get_backward_walk_ops [as 别名]
def fast_backward_ops(within_ops, seed_ops, stop_at_ts):
bwd_ops = set(ge.get_backward_walk_ops(seed_ops, stop_at_ts=stop_at_ts))
ops = bwd_ops.intersection(within_ops).difference([t.op for t in stop_at_ts])
return list(ops)
示例4: export_subgraph
# 需要导入模块: from tensorflow.contrib import graph_editor [as 别名]
# 或者: from tensorflow.contrib.graph_editor import get_backward_walk_ops [as 别名]
def export_subgraph(checkpoint, output_tensors, saveto):
"""
For the current graph, export the subgraph connected to output_tensors to a new graph_def file
:param checkpoint: path to checkpoint
:param output_tensors: output tensor names
:param saveto: path to save graph_def file to
:return:
"""
saver = tf.train.import_meta_graph(checkpoint + '.meta', clear_devices=True)
graph = tf.get_default_graph()
if isinstance(output_tensors, str):
output_tensors = [graph.get_tensor_by_name(output_tensors)]
else:
assert all([isinstance(out, str) for out in output_tensors])
output_tensors = [graph.get_tensor_by_name(out) for out in output_tensors]
def _var_ops(var_op): # get operations one step ahead of variable ops: read/assign/etc.
return [var_op.name] + [op.name for t in var_op.outputs for op in t.consumers()]
keep_op_names = [out.op.name for out in output_tensors]
var_ops = list({op for out in output_tensors for op in ge.get_backward_walk_ops(out) if op.type == 'VariableV2'})
keep_op_names += [opname for op in var_ops for opname in _var_ops(op)]
keep_op_names = [opname for opname in keep_op_names if 'save/' not in opname and 'save_' not in opname]
graph_def = tf.graph_util.extract_sub_graph(graph.as_graph_def(), keep_op_names)
with tf.Session() as sess:
saver.restore(sess, checkpoint)
new_graph_def = tf.graph_util.convert_variables_to_constants(
sess, graph_def, [out.op.name for out in output_tensors])
tf.reset_default_graph()
tf.train.export_meta_graph(saveto, graph_def=new_graph_def, clear_devices=True)
示例5: create_session
# 需要导入模块: from tensorflow.contrib import graph_editor [as 别名]
# 或者: from tensorflow.contrib.graph_editor import get_backward_walk_ops [as 别名]
def create_session(self):
sess = tf.Session(target=self.target, config=self.config)
def blocking_op(x):
"""
Whether an op is possibly blocking.
"""
if x.op_def is not None and not x.op_def.is_stateful:
return False
if "Dequeue" in x.type or "Enqueue" in x.type:
return True
if "Unstage" in x.type:
return True
if x.type in ["ZMQPull"]:
return True
return False
def run(op):
if not is_tfv2():
from tensorflow.contrib.graph_editor import get_backward_walk_ops
deps = get_backward_walk_ops(op, control_inputs=True)
for dep_op in deps:
if blocking_op(dep_op):
logger.warn(
"Initializer '{}' depends on a blocking op '{}'. "
"This initializer is likely to hang!".format(
op.name, dep_op.name))
sess.run(op)
run(tf.global_variables_initializer())
run(tf.local_variables_initializer())
run(tf.tables_initializer())
return sess