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