그것은에 관련이 크게 다른 결과를 생성합니다.조각
# define gradient of a python function
def py_func_with_grad(func, inp, Tout, stateful=True, name=None, grad=None):
num = []
for i in range(100):
num.append(str(np.random.randint(0,10)))
rnd_name = 'PyFuncGrad' + ''.join(num)
tf.RegisterGradient(rnd_name)(grad)
g = tf.get_default_graph()
with g.gradient_override_map({"PyFunc": rnd_name}):
return tf.py_func(func, inp, Tout, stateful=stateful, name=name)
다음 코드 스 니펫이 포함 된 신경 네트워크가 있는데, 여기에는 숫자가 같은 5 개의 행 (한 번 사용합니다)이 있습니다. 그들은 크게 다른 결과를냅니다. 아무도 단서가 있는지 궁금해. 감사!!
예를 들어, x를 TF 변수 (s_final)로 바꾸는 것만으로 (1)과 (2)에서 이와 같은 차이가 발생할 수 있습니다. 나는 수치 적으로 같기 때문에 어떤 차이도 있어서는 안된다고 생각했다.
s_final은 Tensorflow 비 훈련 가능 변수입니다.
def _idenity_func(x,s):
return s
def _dummy_grad(op,grad):
return grad*0,grad*0
assign_op_s_final_2 = s_final.assign(x)
with tf.control_dependencies([assign_op_s_final_2]):
x = tf.identity(x)
x = tf.stop_gradient(x)
# the three following lines should be numerically identical. since s_final has been assigned the value of x. but...
# (1) use the following line, the network does not learn AT ALL!!
x_revised = py_func_with_grad(_idenity_func, [x, s_final], [tf.float32], name=name, grad=lambda op,grad: _dummy_grad(op,grad))
# (2) use the following line, the network learns, even if x does not need any gradient (since there is tf.stop_gradient)
# x_revised = py_func_with_grad(_idenity_func, [x, x], [tf.float32], name=name, grad=lambda op,grad: _dummy_grad(op,grad))
# (3) use the following line, the network learns as well as (2)
# x_revised = tf.stop_gradient(x)
# (4) use the following line, the network learns, but seems not as well as (2)
# x_revised = tf.stop_gradient(s_final)
# (5) use the following line, the network does not learn AT ALL!!
# x_revised = py_func_with_grad(_idenity_func, [x, tf.stop_gradient(s_final)], [tf.float32], name=name, grad=lambda op,grad: _dummy_grad(op,grad))
코드 제공 (필요 tensorflow 0.12.1 하이퍼 망 분류기의 구현 tensorflow 버전> = 1을 지원하지 않기 때문에 버전> = 1을 사용하지 않습니다.) :
https://www.dropbox.com/s/58khyqdy3mtnri7/tensorflow_clean_ver01.zip?dl=0
위의 줄은 우리가 제공하는 코드에 있습니다. 모델을 변경하고 모델을 실행하여 차이점을 확인하십시오. 코드에 대한 질문을 알려주십시오.
당신은 tensorflow 0.12.1 임시 폴더에 설치할 수 있습니다 : 당신이 제공하는 코드를 실행하면 다음 경로가 추가됩니다
export TF_BINARY_URL=https://storage.googleapis.com/tensorflow/linux/gpu/tensorflow_gpu-0.12.1-cp27-none-linux_x86_64.whl
pip install --target=$HOME/tensorflow_versions/tf-0.12.1 --upgrade $TF_BINARY_URL
. 이 방법을 사용하여 여러 버전의 Tensorflow를 컴퓨터에 설치합니다.
모든 의견을 감사드립니다! – DataHungry
자체 테스트를 쉽게 할 수 있도록 실제 예제를 제공 할 수 있습니까? 그렇지 않으면 너무 많은 일입니다. –
@ onurgüngör 코드가 제공됩니다 – DataHungry