當前位置: 首頁>>代碼示例>>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;未經允許,請勿轉載。