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


Python meta_graph.import_scoped_meta_graph方法代码示例

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


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

示例1: _testScopedImportWithQueue

# 需要导入模块: from tensorflow.python.framework import meta_graph [as 别名]
# 或者: from tensorflow.python.framework.meta_graph import import_scoped_meta_graph [as 别名]
def _testScopedImportWithQueue(self, test_dir, exported_filename,
                                 new_exported_filename):
    graph = tf.Graph()
    meta_graph.import_scoped_meta_graph(
        os.path.join(test_dir, exported_filename),
        graph=graph,
        import_scope="new_queue1")
    graph.as_graph_element("new_queue1/dequeue:0")
    graph.as_graph_element("new_queue1/close")
    with graph.as_default():
      new_meta_graph, _ = meta_graph.export_scoped_meta_graph(
          filename=os.path.join(test_dir, new_exported_filename),
          graph=graph, export_scope="new_queue1")

    return new_meta_graph

  # Verifies that we can export the subgraph containing a FIFOQueue under
  # "queue1" and import it into "new_queue1" in a new graph. 
开发者ID:tobegit3hub,项目名称:deep_image_model,代码行数:20,代码来源:meta_graph_test.py

示例2: testPotentialCycle

# 需要导入模块: from tensorflow.python.framework import meta_graph [as 别名]
# 或者: from tensorflow.python.framework.meta_graph import import_scoped_meta_graph [as 别名]
def testPotentialCycle(self):
    graph1 = tf.Graph()
    with graph1.as_default():
      a = tf.constant(1.0, shape=[2, 2])
      b = tf.constant(2.0, shape=[2, 2])
      matmul = tf.matmul(a, b)
      with tf.name_scope("hidden1"):
        c = tf.nn.relu(matmul)
        d = tf.constant(3.0, shape=[2, 2])
        matmul = tf.matmul(c, d)

    orig_meta_graph, _ = meta_graph.export_scoped_meta_graph(
        export_scope="hidden1", graph=graph1)

    graph2 = tf.Graph()
    with graph2.as_default():
      with self.assertRaisesRegexp(ValueError, "Graph contains unbound inputs"):
        meta_graph.import_scoped_meta_graph(
            orig_meta_graph, import_scope="new_hidden1")

      meta_graph.import_scoped_meta_graph(
          orig_meta_graph, import_scope="new_hidden1",
          input_map={"$unbound_inputs_MatMul": tf.constant(4.0, shape=[2, 2])}) 
开发者ID:tobegit3hub,项目名称:deep_image_model,代码行数:25,代码来源:meta_graph_test.py

示例3: testExportNestedNames

# 需要导入模块: from tensorflow.python.framework import meta_graph [as 别名]
# 或者: from tensorflow.python.framework.meta_graph import import_scoped_meta_graph [as 别名]
def testExportNestedNames(self):
    graph1 = tf.Graph()
    with graph1.as_default():
      with tf.name_scope("hidden1/hidden2/hidden3"):
        images = tf.constant(1.0, tf.float32, shape=[3, 2], name="images")
        weights1 = tf.Variable([[1.0, 2.0, 3.0], [4.0, 5.0, 6.0]],
                               name="weights")
        biases1 = tf.Variable([0.1] * 3, name="biases")
        tf.nn.relu(tf.matmul(images, weights1) + biases1, name="relu")

    orig_meta_graph, var_list = meta_graph.export_scoped_meta_graph(
        export_scope="hidden1/hidden2", graph=graph1)
    var_names = [v.name for _, v in var_list.items()]
    self.assertEqual(["hidden3/biases:0", "hidden3/weights:0"],
                     sorted(var_list.keys()))
    self.assertEqual(["hidden1/hidden2/hidden3/biases:0",
                      "hidden1/hidden2/hidden3/weights:0"],
                     sorted(var_names))
    for node in orig_meta_graph.graph_def.node:
      self.assertTrue(node.name.startswith("hidden3"))

    graph2 = tf.Graph()
    new_var_list = meta_graph.import_scoped_meta_graph(
        orig_meta_graph, import_scope="new_hidden1/new_hidden2",
        graph=graph2)
    self.assertEqual(["hidden3/biases:0", "hidden3/weights:0"],
                     sorted(new_var_list.keys()))
    new_var_names = [v.name for _, v in new_var_list.items()]
    self.assertEqual(["new_hidden1/new_hidden2/hidden3/biases:0",
                      "new_hidden1/new_hidden2/hidden3/weights:0"],
                     sorted(new_var_names))

    nodes = ["new_hidden1/new_hidden2/hidden3/biases/Assign",
             "new_hidden1/new_hidden2/hidden3/weights/Assign"]
    expected = [b"loc:@new_hidden1/new_hidden2/hidden3/biases",
                b"loc:@new_hidden1/new_hidden2/hidden3/weights"]
    for n, e in zip(nodes, expected):
      self.assertEqual([e], graph2.get_operation_by_name(n).get_attr("_class")) 
开发者ID:tobegit3hub,项目名称:deep_image_model,代码行数:40,代码来源:meta_graph_test.py

示例4: import_and_run_meta_graph

# 需要导入模块: from tensorflow.python.framework import meta_graph [as 别名]
# 或者: from tensorflow.python.framework.meta_graph import import_scoped_meta_graph [as 别名]
def import_and_run_meta_graph(
    meta_graph_def,
    result_pattern,
    feed_dict_fn,
    log_dir_fn,
    finish_session_fn=None):
  # TODO(adamb) Carefully find the asset map and replace any asset py funcs appropriately with input_map.
  with create_session() as sess:
    try:
      meta_graph.import_scoped_meta_graph(
        meta_graph_def,
        input_map=None,
      )
    except KeyError as e:
      nodes = [n.name for n in tf.get_default_graph().as_graph_def().node]
      nodes.sort()
      eprint('error, but got nodes', nodes)
      raise e

    # NOTE(adamb) Could also store files to copy out in assets_collection
    js_py_func_data_tensor = None
    try:
      js_py_func_data_tensor = sess.graph.get_tensor_by_name("py_funcs_json:0")
    except KeyError as e:
      pass

    if js_py_func_data_tensor is not None:
      js_py_func_data = js_py_func_data_tensor.eval().decode('utf-8')
      py_func_data = json.loads(js_py_func_data)
      # eprint('loaded py_func_data', py_func_data)
      py_importer = graph_ffi.PythonImporter()
      py_importer.restore_py_funcs(script_ops._py_funcs, py_func_data)

    try:
      return run_session(sess, result_pattern, feed_dict_fn(), log_dir_fn, finish_session_fn=finish_session_fn)
    finally:
      sess.close() 
开发者ID:tensorlang,项目名称:tensorlang,代码行数:39,代码来源:graph_execution.py

示例5: testNoVariables

# 需要导入模块: from tensorflow.python.framework import meta_graph [as 别名]
# 或者: from tensorflow.python.framework.meta_graph import import_scoped_meta_graph [as 别名]
def testNoVariables(self):
    test_dir = _TestDir("no_variables")
    filename = os.path.join(test_dir, "metafile")

    input_feed_value = -10  # Arbitrary input value for feed_dict.

    orig_graph = tf.Graph()
    with self.test_session(graph=orig_graph) as sess:
      # Create a minimal graph with zero variables.
      input_tensor = tf.placeholder(tf.float32, shape=[], name="input")
      offset = tf.constant(42, dtype=tf.float32, name="offset")
      output_tensor = tf.add(input_tensor, offset, name="add_offset")

      # Add input and output tensors to graph collections.
      tf.add_to_collection("input_tensor", input_tensor)
      tf.add_to_collection("output_tensor", output_tensor)

      output_value = sess.run(output_tensor, {input_tensor: input_feed_value})
      self.assertEqual(output_value, 32)

      # Generates MetaGraphDef.
      meta_graph_def, var_list = meta_graph.export_scoped_meta_graph(
          filename=filename,
          graph_def=tf.get_default_graph().as_graph_def(add_shapes=True),
          collection_list=["input_tensor", "output_tensor"],
          saver_def=None)
      self.assertEqual({}, var_list)

    # Create a clean graph and import the MetaGraphDef nodes.
    new_graph = tf.Graph()
    with self.test_session(graph=new_graph) as sess:
      # Import the previously export meta graph.
      meta_graph.import_scoped_meta_graph(filename)

      # Re-exports the current graph state for comparison to the original.
      new_meta_graph_def, _ = meta_graph.export_scoped_meta_graph(
          filename + "_new")
      self.assertProtoEquals(meta_graph_def, new_meta_graph_def)

      # Ensures that we can still get a reference to our graph collections.
      new_input_tensor = tf.get_collection("input_tensor")[0]
      new_output_tensor = tf.get_collection("output_tensor")[0]
      # Verifies that the new graph computes the same result as the original.
      new_output_value = sess.run(
          new_output_tensor, {new_input_tensor: input_feed_value})
      self.assertEqual(new_output_value, output_value) 
开发者ID:tobegit3hub,项目名称:deep_image_model,代码行数:48,代码来源:meta_graph_test.py

示例6: testClearDevices

# 需要导入模块: from tensorflow.python.framework import meta_graph [as 别名]
# 或者: from tensorflow.python.framework.meta_graph import import_scoped_meta_graph [as 别名]
def testClearDevices(self):
    graph1 = tf.Graph()
    with graph1.as_default():
      with tf.device("/device:CPU:0"):
        a = tf.Variable(tf.constant(1.0, shape=[2, 2]), name="a")
      with tf.device("/job:ps/replica:0/task:0/gpu:0"):
        b = tf.Variable(tf.constant(2.0, shape=[2, 2]), name="b")
      with tf.device("/job:localhost/replica:0/task:0/cpu:0"):
        tf.matmul(a, b, name="matmul")

    self.assertEqual("/device:CPU:0", str(graph1.as_graph_element("a").device))
    self.assertEqual("/job:ps/replica:0/task:0/device:GPU:0",
                     str(graph1.as_graph_element("b").device))
    self.assertEqual("/job:localhost/replica:0/task:0/device:CPU:0",
                     str(graph1.as_graph_element("matmul").device))

    # Verifies that devices are cleared on export.
    orig_meta_graph, _ = meta_graph.export_scoped_meta_graph(
        graph=graph1, clear_devices=True)

    graph2 = tf.Graph()
    with graph2.as_default():
      meta_graph.import_scoped_meta_graph(orig_meta_graph, clear_devices=False)

    self.assertEqual("", str(graph2.as_graph_element("a").device))
    self.assertEqual("", str(graph2.as_graph_element("b").device))
    self.assertEqual("", str(graph2.as_graph_element("matmul").device))

    # Verifies that devices are cleared on export when passing in graph_def.
    orig_meta_graph, _ = meta_graph.export_scoped_meta_graph(
        graph_def=graph1.as_graph_def(), clear_devices=True)

    graph2 = tf.Graph()
    with graph2.as_default():
      meta_graph.import_scoped_meta_graph(orig_meta_graph, clear_devices=False)

    self.assertEqual("", str(graph2.as_graph_element("a").device))
    self.assertEqual("", str(graph2.as_graph_element("b").device))
    self.assertEqual("", str(graph2.as_graph_element("matmul").device))

    # Verifies that devices are cleared on import.
    orig_meta_graph, _ = meta_graph.export_scoped_meta_graph(
        graph=graph1, clear_devices=False)

    graph2 = tf.Graph()
    with graph2.as_default():
      meta_graph.import_scoped_meta_graph(orig_meta_graph, clear_devices=True)

    self.assertEqual("", str(graph2.as_graph_element("a").device))
    self.assertEqual("", str(graph2.as_graph_element("b").device))
    self.assertEqual("", str(graph2.as_graph_element("matmul").device)) 
开发者ID:tobegit3hub,项目名称:deep_image_model,代码行数:53,代码来源:meta_graph_test.py


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