2016-10-03 1 views
0

深い学習プロジェクトのためにグラフからの出力を別のグラフへの入力として使用し、2つのグラフのすべての変数を最適化する必要があります。各グラフの入力はプレースホルダーです。原因その時点で指摘したように私も取得していますエラーにTensorflow: How to replace a node in a calculation graph?インポートされた2つのグラフで作成されたグラフを最適化するTensorflow

残念ながら、問題が解決されなかった。

私の問題は、ここで議論したものと非常によく似ています。

with tf.Graph().as_default() as g_combined: 
    x = tf.placeholder(tf.float32, shape=[3,3], name="input_matrix") 

    # Import gdef_1, which performs f(x). 
    # "input:0" and "output:0" are the names of tensors in gdef_1. 
    y, = tf.import_graph_def(gdef_1, input_map={"input:0": x}, 
          return_elements=["output:0"]) 

    # Import gdef_2, which performs g(y) 
    z, = tf.import_graph_def(gdef_2, input_map={"input:0": y}, 
          return_elements=["output:0"]) 

    cost = tf.reduce_sum(tf.square(z-x)) 
    variables = [op.outputs[0] for op in tf.get_default_graph().get_operations() if op.type == "Variable"] 
    print (variables) 
    optimizer = tf.train.AdamOptimizer(0.01).minimize(cost,var_list = variables) 

これはで提案ソリューションです:私はその後、私は別のグラフに2つのグラフをインポートする。これは、実際には些細な計算に

with tf.Graph().as_default() as g_1: 
    input_1 = tf.placeholder(tf.float32, shape=[3,3], name="input") 
    weight1 = tf.Variable(tf.truncated_normal([3, 3], stddev=0.1), name="weight") 
    y = tf.matmul(input_1,weight1) 
    # NOTE: using identity to get a known name for the output tensor. 
    output1 = tf.identity(y, name="output") 

gdef_1 = g_1.as_graph_def() 

with tf.Graph().as_default() as g_2: # NOTE: g_2 not g_1  
    input_2 = tf.placeholder(tf.float32, shape=[3,3], name="input") 
    weight2 = tf.Variable(tf.truncated_normal([3, 3], stddev=0.1), name="weight") 
    z = tf.matmul(input_2, weight2) 
    output2 = tf.identity(z, name="output") 

gdef_2 = g_2.as_graph_def() 

を行うサンプルプログラムです

私の場合を投稿します以前にリンクされた質問ですが、次のエラーが表示されます。

TypeError: Argument is not a tf.Variable: Tensor("import/weight:0", dtype=float32_ref) 

変数variablesは含まれています

[<tf.Tensor 'import/weight:0' shape=<unknown> dtype=float32_ref>, <tf.Tensor 'import_1/weight:0' shape=<unknown> dtype=float32_ref>] 

誰もがそれを動作させる方法を知っていますか?または、プレースホルダにフィードするために中間結果が必要であると考えて、構造全体を最適化する方法は?

は、これを行うための最善の方法は、最初にあなたがしたいグラフの労働組合からのすべてのノードが含まれているグラフを構築するが、グラフを交換することではありません非常に

+0

以前にリンクされた質問で提案されているのとまったく同じコードを使用している場合は、結果を再現できますか?もしそうなら、あなたはあなたのバージョンと実際のバージョンを比較して、エラーの原因を特定するのに役立ちますか? –

+0

私は以前のリンクと同じ結果を得ました。そのバージョンは動作しません(まさに私のものです)。 – Cramer

答えて

0

をありがとう、そして独立して訓練する部分グラフと共同モデル。次に、個々の変数を保存して読み込むことで、共同で事を最適化できるはずです。

関連する問題