本文整理匯總了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