2017-03-14 5 views
0

나는 tensorflow에 대해 새말이 없으며 나는 우리에게 그것을 이해하는 방법을 고심하고 있습니다. 현재 숫자를 식별하기 위해이 코드를 사용하려고 노력하고 있으므로 약간의 수정을 통해 mnist 자습서 (https://www.tensorflow.org/get_started/mnist/pros)에 제공된 코드를 사용했습니다. 나는 내 자신의 소스를 사용했다. mnist에서 제공된 소스와 코드의 일부만 변경하여 다른 크기의 소스로 모델을 만들 수있다. (28x28 및 56x56) 제 1, I는 다음과 같은 모델을 저장 :동시에 여러 tensorflow 모델을 복원하고 사용하십시오.

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")); 

SESS가 tf.InteractiveSession() id_collec '는 28x28'또는 '56x56'이다 beign와 X 입력 imagies위한 자리 인 tf.reduce_mean 결과를 beign tf.matmul 정확도 결과 클래스의 수를 정의 자리 플로트 train_step 대한 자리 = tf.train.AdamOptimizer 결과가 그냥이다 keep_prob Y_ y_conv 모델의 출력 디렉토리를 변경하는 번호 modelDir = 모델 디렉토리가

를 생성 할 위치는 다음 다른 프로그램에서 나는 다음과 같은 모델을 복원

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)) 

나는 하나 개는 괜찮 모델,하지만를로드 할 때 나는 두 가지 모델 (하나 개의 기본을로드 할 때 28x28 이미지 및 56x56 이미지 한베이스) 두 번째 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)]] 

내가 뭘 잘못 했니? 분명히 두 모델은 변수를 사용합니다. 처음에는 컬렉션에 동일한 ID를 사용했기 때문에 그것이 다른 것이 었습니다. 그러나 오류는 복구 자체에서 가져 오기 컬렉션조차도 아닙니다. 어떻게 든 공유하는 두 모델을 피할 수 있도록 somekind의 범위를 만드는 방법이 있다고 들었지만 나는 그 작업을 이해하지 못했습니다.

웹에 대한 답을 찾을 때 많은 정보를 찾았지만 tensorflow에 익숙하지 않아서 이러한 정보를 내 코드에 적용하지 못했습니다. 아이디어가 있으십니까?

Ps : 필자는 필자가 필 요한 경우 나중에 필자가 나중에 필자의 경우 교육을 계속하거나 sess.run을 시작하기 위해 필 요할 경우이 값을 구속 상태로 두었습니다.

답변

1

좋아, 내가 그래프 전체 그래프를 정의 내 함수를 호출하기 전에

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(...)