当前位置: 首页>>代码示例>>Python>>正文


Python tf_optimizer.OptimizeGraph方法代码示例

本文整理汇总了Python中tensorflow.python.grappler.tf_optimizer.OptimizeGraph方法的典型用法代码示例。如果您正苦于以下问题:Python tf_optimizer.OptimizeGraph方法的具体用法?Python tf_optimizer.OptimizeGraph怎么用?Python tf_optimizer.OptimizeGraph使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在tensorflow.python.grappler.tf_optimizer的用法示例。


在下文中一共展示了tf_optimizer.OptimizeGraph方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: auto_parallel

# 需要导入模块: from tensorflow.python.grappler import tf_optimizer [as 别名]
# 或者: from tensorflow.python.grappler.tf_optimizer import OptimizeGraph [as 别名]
def auto_parallel(metagraph, model):
  from tensorflow.python.grappler import tf_optimizer
  rewriter_config = rewriter_config_pb2.RewriterConfig()
  rewriter_config.optimizers.append("autoparallel")
  rewriter_config.auto_parallel.enable = True
  rewriter_config.auto_parallel.num_replicas = FLAGS.num_gpus
  optimized_graph = tf_optimizer.OptimizeGraph(rewriter_config, metagraph)
  metagraph.graph_def.CopyFrom(optimized_graph)
  UpdateCollection(metagraph, model) 
开发者ID:rky0930,项目名称:yolo_v2,代码行数:11,代码来源:util.py

示例2: optimize_graph_spec

# 需要导入模块: from tensorflow.python.grappler import tf_optimizer [as 别名]
# 或者: from tensorflow.python.grappler.tf_optimizer import OptimizeGraph [as 别名]
def optimize_graph_spec(graph_spec_obj, config_proto):
  """Applies Grappler with given options to a `graph_spec.GraphSpec`.

  For more information on Grappler, see
  https://www.tensorflow.org/guide/graph_optimization

  Args:
    graph_spec_obj: Instance of `graph_spec.GraphSpec` representing the
      TensorFlow computation to optimize.
    config_proto: Instance of `tf.compat.v1.ConfigProto` specifying optimization
      options for Grappler.

  Returns:
    An instance of `graph_spec_obj` which has been passed through Grappler and
    optimized if possible.
  """
  meta_graph_def = graph_spec_obj.to_meta_graph_def()

  try:
    # Grappler raises if it fails to find feeds and fetches, but can handle
    # *some* no-arg graphs, so we try/except here.
    optimized_graph_def = tf_optimizer.OptimizeGraph(config_proto,
                                                     meta_graph_def)
  except ValueError as error:
    logging.info(
        'Grappler has raised the error %s; falling back to using '
        'non-optimized graph.', error)
    optimized_graph_def = graph_spec_obj.graph_def

  return graph_spec.GraphSpec(
      optimized_graph_def,
      init_op=graph_spec_obj.init_op,
      in_names=graph_spec_obj.in_names,
      out_names=graph_spec_obj.out_names) 
开发者ID:tensorflow,项目名称:federated,代码行数:36,代码来源:graph_optimizations.py

示例3: _run_tf_optimizer

# 需要导入模块: from tensorflow.python.grappler import tf_optimizer [as 别名]
# 或者: from tensorflow.python.grappler.tf_optimizer import OptimizeGraph [as 别名]
def _run_tf_optimizer(config: ConfigProto,
                      graph: tf.Graph,
                      signature_def: SignatureDef) -> GraphDef:
    """Run the TF optimizer ("grappler") on a graph"""
    graph_def = graph.as_graph_def()
    meta_graph = export_meta_graph(graph_def=graph_def, graph=graph)
    meta_graph.signature_def['not_used_key'].CopyFrom(signature_def)
    return tf_optimizer.OptimizeGraph(config, meta_graph) 
开发者ID:patlevin,项目名称:tfjs-to-tf,代码行数:10,代码来源:optimization.py

示例4: _run_inline_graph_optimization

# 需要导入模块: from tensorflow.python.grappler import tf_optimizer [as 别名]
# 或者: from tensorflow.python.grappler.tf_optimizer import OptimizeGraph [as 别名]
def _run_inline_graph_optimization(func, lower_control_flow):
  """Apply function inline optimization to the graph.

  Returns the GraphDef after Grappler's function inlining optimization is
  applied. This optimization does not work on models with control flow.

  Args:
    func: ConcreteFunction.
    lower_control_flow: Boolean indicating whether or not to lower control flow
      ops such as If and While. (default True)

  Returns:
    GraphDef
  """
  graph_def = func.graph.as_graph_def()
  if not lower_control_flow:
    graph_def = disable_lower_using_switch_merge(graph_def)

  # In some cases, a secondary implementation of the function (e.g. for GPU) is
  # written to the "api_implements" attribute. (e.g. `tf.keras.layers.LSTM` in
  # TF2 produces a CuDNN-based RNN for GPU).
  # This function suppose to inline all functions calls, but "api_implements"
  # prevents this from happening. Removing the attribute solves the problem.
  # To learn more about "api_implements", see:
  #   tensorflow/core/grappler/optimizers/implementation_selector.h
  for function in graph_def.library.function:
    if "api_implements" in function.attr:
      del function.attr["api_implements"]

  meta_graph = export_meta_graph(graph_def=graph_def, graph=func.graph)

  # Clear the initializer_name for the variables collections, since they are not
  # needed after saved to saved_model.
  for name in [
      "variables", "model_variables", "trainable_variables", "local_variables"
  ]:
    raw_list = []
    for raw in meta_graph.collection_def["variables"].bytes_list.value:
      variable = variable_pb2.VariableDef()
      variable.ParseFromString(raw)
      variable.ClearField("initializer_name")
      raw_list.append(variable.SerializeToString())
    meta_graph.collection_def[name].bytes_list.value[:] = raw_list

  # Add a collection 'train_op' so that Grappler knows the outputs.
  fetch_collection = meta_graph_pb2.CollectionDef()
  for array in func.inputs + func.outputs:
    fetch_collection.node_list.value.append(array.name)
  meta_graph.collection_def["train_op"].CopyFrom(fetch_collection)

  # Initialize RewriterConfig with everything disabled except function inlining.
  config = config_pb2.ConfigProto()
  rewrite_options = config.graph_options.rewrite_options
  rewrite_options.min_graph_nodes = -1  # do not skip small graphs
  rewrite_options.optimizers.append("function")
  return tf_optimizer.OptimizeGraph(config, meta_graph) 
开发者ID:onnx,项目名称:keras-onnx,代码行数:58,代码来源:_graph_cvt.py


注:本文中的tensorflow.python.grappler.tf_optimizer.OptimizeGraph方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。