2017-04-18 69 views
1

它涉及到: tensorflow modify variables in py_func (and its grad func)件的数字是相同的代码产生截然不同的结果

我定义自己的运算及其TensorFlow使用此功能梯度。我有一个神经网络,其中包含以下代码片段,其中我有5个数字相同的行(我使用其中之一)。他们产生截然不同的结果。我想知道有没有人有任何线索。谢谢!!

例如,它很奇怪,在(1)和(2)中,用TF变量(s_final)代替x可以产生这样的差异。我认为,因为它们在数字上是相同的,所以不应该有任何区别。

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不能与版本> = 1工作,因为超级网络的实现不支持tensorflow版本> = 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版本。

+0

任何意见是赞赏! – DataHungry

+0

您能否提供一个有效的示例,以便我们可以轻松进行自己的测试?否则,它是太多的工作。 –

+0

@onurgüngör提供的代码 – DataHungry

回答

0

在我的实验中正常工作:我添加了使用x_revised的代码,并查看了涉及其他变量的渐变值。错误必须在未发布的代码中。

+0

提供代码 – DataHungry

0

我的猜测是分配没有真正执行。请注意,你只是建立图表,没有任何执行(不像pytorch)...

相关问题