다음 코드 (tf v1.12.0)를 실행하면 6.0 (x-> mul-> ident), 7.0 (x-> mul-> add-> ident) 또는 9.0 (x-> add-> mul -> ident).
누군가 ops의 실행 순서가 tf.control_dependencies에 의해 제어되지 않는 이유를 설명해 주시겠습니까? 나는 적어도 add_op이 컨트롤 컨텍스트 내의 어떤 것도 고려되기 전에 실행될 것이라고 생각합니다.
tf.reset_default_graph()
x=tf.Variable(2.0)
add_op = tf.assign_add(x, 1)
mul_op = tf.assign(x, 3*x)
with tf.control_dependencies([add_op]):
out_op = tf.identity(mul_op)
init_op = tf.global_variables_initializer()
with tf.Session() as sess:
sess.run(init_op)
print(sess.run([out_op]))
감사!
에 mul_op
의존하지 않기 때문 입니다 add_op
. 오히려는 (명시 적 입력으로) 및 제어 종속성 으로 out_op
모두 의존합니다. TensorFlow 1.x (및 컨텍스트 내 TensorFlow 2.x ) 에서 Python 의 작업 순서 는 TensorFlow 런타임 의 작업 순서에 영향을주지 않습니다 .mul_op
add_op
tf.Graph
위의 예에서 결정적 동작을 강제하기 위해 몇 가지 옵션이 있습니다.
mul_op
내부 tf.control_dependencies
를 구성하십시오 add_op
.add_op = tf.assign_add(x, 1)
with tf.control_dependencies([add_op]):
mul_op = tf.assign(x, 3 * x)
mul_op
첨가 (출력 받아 add_op
입력 등 참조).add_op = tf.assign_add(x, 1)
mul_op = tf.assign(x, 3 * add_op)
out_op
하고 add_op
명시 적으로.x=tf.Variable(2.0)
add_op = tf.assign_add(x, 1)
mul_op = tf.assign(x, 3*x)
out_op = tf.identity(mul_op)
init_op = tf.global_variables_initializer()
with tf.Session() as sess:
sess.run(init_op)
sess.run(add_op)
print(sess.run(out_op))
이들은 항상을 반환 9.0
합니다.
정말 깊이 파고 들어 그래프의 종속성이 무엇인지 확인 하려면 다음을 시도해 볼 수 있습니다.
tf.get_default_graph().as_graph_def()
input
그래프의 각 노드에 대한 값이 무엇인지 확인하십시오 .
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다