2017-03-14 13 views
0

テンソルフローが新しく静かで、私はそれをどのように理解するのか苦労しています。私は現在番号を識別するためにそれを使用しようとしていますので、いくつかの変更を加えてmnistチュートリアル(https://www.tensorflow.org/get_started/mnist/pros)で提供しているコードを使用しました。私は自分のソースを使用しましたが、mnistで与えられたソースとコードの一部を変更して、サイズの異なるソースでモデルを作成することができます。 (28x28および56×56) ザ、私は次のようにモデルを保存:複数のテンソルフローモデルを同時に復元して使用する

def save_progression(sess, id_collec, x, y_conv, y_, accuracy, keep_prob, train_step, i, modelDir): 
    saver = tf.train.Saver() 
    print(modelDir) 
    modelNamePrefix=os.path.join(modelDir, "step%s" % str(i)) 
    if (os.path.isdir(modelNamePrefix) == False): 
    os.makedirs(modelNamePrefix) 
    if (len(tf.get_collection(id_collec)) > 0): 
    tf.get_collection_ref(id_collec)[0] = x 
    tf.get_collection_ref(id_collec)[1] = y_conv 
    tf.get_collection_ref(id_collec)[2] = y_ 
    tf.get_collection_ref(id_collec)[3] = accuracy 
    tf.get_collection_ref(id_collec)[4] = keep_prob 
    tf.get_collection_ref(id_collec)[5] = train_step 
    else: 
    tf.add_to_collection(id_collec, x) 
    tf.add_to_collection(id_collec, y_conv) 
    tf.add_to_collection(id_collec, y_) 
    tf.add_to_collection(id_collec, accuracy) 
    tf.add_to_collection(id_collec, keep_prob) 
    tf.add_to_collection(id_collec, train_step) 
    saver.save(sess, os.path.join(modelNamePrefix, "myModel")); 

のSESはtf.InteractiveSession() id_collecが '28x28' または '56×56' であるのbeignと X入力imagiesのプレースホルダであります tf.reduce_meanの結果をbeign tf.matmul 精度の結果がクラス の数を定義されたプレースホルダがフロート train_stepのプレースホルダ= tf.train.AdamOptimizer の結果は、私はちょうどあるkeep_prob Y_ y_convモデルのアウトディレクトリを変更する番号 modelDir =モデルディレクトリが

を作成する場所は、その後、別のプログラムの中で、私は次のようにモデルを復元:私は1つのモデルをロードすると

self._sess = tf.Session() 
print("import meta graph %s.meta" % (os.path.join(modelDir, modelName))) 
saver = tf.train.import_meta_graph("%s.meta" % (os.path.join(modelDir, modelName))) 
print("restoring %s" % (os.path.join(modelDir, modelName))) 
saver.restore(self._sess, "%s" % (os.path.join(modelDir, modelName))); 
self._placeHolder_x, self._predictNode, _, _, self._placeHolder_keep_prob, _ = tf.get_collection('%dx%d' % (dim, dim)) 

それは大丈夫だが、私は二つの異なるモデル(1台のロード時28x28イメージと56x56イメージの1ベース)2番目のtf.restoreでエラーが発生しました。

[...] 
tensorflow.python.framework.errors_impl.InvalidArgumentError: Assign requires shapes of both tensors to match. lhs shape= [3136,1024] rhs shape= [5,5,64,128] [[Node: save/Assign_14 = Assign[T=DT_FLOAT, 
_class=["loc:@Variable_4"], use_locking=true, validate_shape=true, _device="/job:localhost/replica:0/task:0/cpu:0"](Variable_4/Adam_1, save/RestoreV2_14)]] 

Caused by op u'save/Assign_14' 
[...] 
InvalidArgumentError (see above for traceback): Assign requires shapes of both tensors to match. lhs shape= [3136,1024] rhs shape= [5,5,64,128]  [[Node: save/Assign_14 = Assign[T=DT_FLOAT, _class=["loc:@Variable_4"], use_locking=true, validate_shape=true, 
_device="/job:localhost/replica:0/task:0/cpu:0"](Variable_4/Adam_1, save/RestoreV2_14)]] 

どうすればよいですか?明らかに、2つのモデルはいくつかの変数や何かを使用します。 私はコレクションに同じIDを使用していたので、最初は違っていました。しかし、エラーは復元自体でもget collectionではありません。 何とか何かのスコープを作る方法があると聞いて、2つのモデルを一緒に共有するのを避けることができますが、私はその仕事の仕方を理解していません。

私は多くの情報を発見しましたが、テンソルフローを初めて知りましたが、私はそれらの情報を自分のコードに適用できませんでした。

Ps:もし私がそれらを必要とするならば、もし私が2つを望むならば後日トレーニングを続行するか、sess.runを起動するかのどちらかで、これらの値を黙読に入れます。

答えて

1

[OK]を、私は、グラフ全体のグラフを定義し、私の関数の呼び出し前に

dim = int(sys.argv[5]) 
with tf.variable_scope('%dx%d' % (dim, dim)): 

を追加し、解決策を見つけたと私は同様にグラフを復元する前に、同じ行を追加し、それが

をクラッシュすることなく実行します
0

両方のモデルを同じグラフにリストアしている可能性があります。モデルごとに個別のグラフを初期化することができます:

graph1 = tf.Graph() 
graph2 = tf.Graph() 

with tf.Session(graph = graph1) as sess1: 
    saver.restore(.....) 
with tf.Session(graph = graph2) as sess2: 
    saver.restore(...) 
関連する問題