1

나는 두 모델 foobar을 가지고 있습니다. bar이 미리 계산되어로드되었다고 가정합니다. foo의 비용 함수를 다음 코드에서 대략적으로 스케치 한 것으로 정의하고 싶습니다. 실제로는 자동 인코딩입니다. 이것은 수학적으로 이해할 수 없도록 제 문제를 재현 할 수있는 최소한의 예입니다. 내가 스크립트 (TF 버전 1.0)를 실행하면variable_scope가 옵티마이 저가 '변수가 존재하지 않습니다.'원인

import tensorflow as tf 

def foo(X): 
     with tf.variable_scope("foo"): 
       A = tf.get_variable("A",shape=[1]) 
     return tf.add(X,A) 

def bar(X): 
     with tf.variable_scope("bar"): 
       B = tf.get_variable("B",shape=[1]) 
     return tf.multiply(X,B) 

X = tf.placeholder("float") 

X_prime = foo(X) 

Y = bar(X) 
tf.get_variable_scope().reuse_variables() 
Y_prime = bar(X_prime) 


#foo(X) is manipulated with some other terms, but the point is foo is called again 
cost = foo(X) + tf.pow(Y-Y_prime,2) 

optimizer = tf.train.AdamOptimizer(learning_rate=0.01).minimize(cost) 

, 나는 다음과 같은 오류가 발생합니다 :

ValueError: Variable foo/A/Adam/ does not exist, or was not created with tf.get_variable(). Did you mean to set reuse=None in VarScope? 

그러나,이 GradientDescentOptimizer으로 발생하지 않습니다. 모든 설명과 포인터가 인정 될 것이다.

+0

누군가 도와 줄 수 있습니까? – user3813674

+0

문제가 해결 되었습니까? 필자가 생각하기에,''tf.get_variable_scope(). reuse_variables()''를 전역 적으로 설정하면, 그 라인 다음의 모든 변수는 기존 변수를 찾을 것입니다. 그러한 변수가 없으면 tensorflow는 해당 오류를 프롬프트합니다. 모멘텀 (Momentum), 아담 (Adam)과 같은 최적화 도구는 비용을 "최소화"하려고 할 때 과거 그라데이션을 저장하는 변수를 만들어야합니다. 이 문제를 해결할 수있는 방법은 전역 적이 아닌 함수에 대한 매개 변수를 추가하여''variable_scope ("foo", reuse = reuse)''를 국지적으로 설정할 수 있다는 것입니다. – Seven

답변

0

귀하의 ValueError는 variable_scope.reuse == True 내에 새 변수를 작성하면 발생합니다.

변수는 그래프에서 각 학습 가능한 변수의 운동량을 저장하기 위해 Adam의 최소화 함수를 호출 할 때 Adam에 의해 만들어집니다.

재사용을 True로 설정 했으므로 기본 variable_scope.reuse == True입니다. 재사용 상태는 True로 설정하면 영원히 False로 되돌릴 수 없습니다. 그런 다음 Adam은 state reuse == True로 변수를 생성하여 오류가 발생합니다.

이 솔루션은 variable_scope.reuse는, 디폴트 scope.reuse 여전히 거짓 True 인 = 설정하고 Adam.minimize이 작동 할 때 다음과 같이 그래프의 기본 범위에서 하위 범위를 추가하는 것입니다

import tensorflow as tf 
def foo(X): 
     with tf.variable_scope("foo"): 
       A = tf.get_variable("A",shape=[1]) 
     return tf.add(X,A) 

def bar(X): 
     with tf.variable_scope("bar"): 
       B = tf.get_variable("B",shape=[1]) 
     return tf.multiply(X,B) 

X = tf.placeholder("float") 

with tf.variable_scope("for_reuse_scope"): 
    X_prime = foo(X) 
    Y = bar(X) 
    tf.get_variable_scope().reuse_variables() 
    Y_prime = bar(X_prime) 


#foo(X) is manipulated with some other terms, but the point is foo is called again 
cost = foo(X) + tf.pow(Y-Y_prime,2) 

optimizer = tf.train.AdamOptimizer(learning_rate=0.01).minimize(cost) 
+0

당신은 넣어해야합니다 를 tf.variable_scope와 (tf.get_variable_scope()) 당신의 장치를 통해 실행 루프 앞에 ... 지금은 그렇게 : 를 tf.variable_scope와 (tf.get_variable_scope을 ()) : for xrange (FLAGS.num_gpus) : with tf.device ('/ gpu : % d'% i) : – BenJLI

관련 문제