梯度如何通过tf.py_func传递

时间:2017-01-08 17:06:15

标签: python machine-learning tensorflow neural-network

这是在张量流中更快的R-CNN实现 proposal_layerimplement by python

我很好奇渐变是否可以通过tf.py_func
权重和偏见不断变化 所以我认为渐变成功了

然后我做了一个小测试

import tensorflow as tf
import numpy as np

def addone(x):
    # print type(x)
    return x + 1

def pyfunc_test():
    # create data
    x_data = tf.placeholder(dtype=tf.float32, shape=[None])
    y_data = tf.placeholder(dtype=tf.float32, shape=[None])

    w = tf.Variable(tf.constant([0.5]))
    b = tf.Variable(tf.zeros([1]))

    y1 = tf.mul(w, x_data, name='y1')
    y2 = tf.py_func(addone, [y1], tf.float32)
    y = tf.add(y2, b)

    loss = tf.reduce_mean(tf.square(y - y_data))
    optimizer = tf.train.GradientDescentOptimizer(0.5)
    train = optimizer.minimize(loss)

    with tf.Session() as sess:
        sess.run(tf.global_variables_initializer())

        for step in xrange(201):
            ran = np.random.rand(115).astype(np.float32)
            ans = ran * 1.5 + 3
            dic = {x_data: ran, y_data: ans}
            tt, yy, yy1= sess.run([train, y1, y2], feed_dict=dic)
            if step % 20 == 0:
                print 'step {}'.format(step)
                print '{}, {}'.format(w.eval(), b.eval())

        test = sess.run(y, feed_dict={x_data:[1]})
        print 'test = {}'.format(test)


if __name__ == '__main__':
    pyfunc_test()

变量b不断变化,但w在初始化后保留值,永不改变

sess.run(tf.gradients(loss, b), feed_dict=dic)获得价值
sess.run(tf.gradients(loss, w), feed_dict=dic)获取{TypeError}Fetch argument None has invalid type <type 'NoneType'>

我知道有些问题建议使用tf.RegisterGradientgradient_override_map
但我无法在更快的rcnn回购中找到这些(链接在帖子顶部)
我做错了什么或遗漏了某些东西,以便w冻结

1 个答案:

答案 0 :(得分:6)

py_func的渐变为None(只需检查ops.get_gradient_function(y2.op))。 @harpone的这个gist显示了如何为py_func使用渐变覆盖映射。

此处修改了您的示例以使用该配方

import numpy as np
import tensorflow as tf

def addone(x):
    # print(type(x)
    return x + 1

def addone_grad(op, grad):
    x = op.inputs[0]
    return x

from tensorflow.python.framework import ops
import numpy as np

# Define custom py_func which takes also a grad op as argument:
def py_func(func, inp, Tout, stateful=True, name=None, grad=None):

    # Need to generate a unique name to avoid duplicates:
    rnd_name = 'PyFuncGrad' + str(np.random.randint(0, 1E+8))

    tf.RegisterGradient(rnd_name)(grad)  # see _MySquareGrad for grad example
    g = tf.get_default_graph()
    with g.gradient_override_map({"PyFunc": rnd_name}):
        return tf.py_func(func, inp, Tout, stateful=stateful, name=name)

def pyfunc_test():

    # create data
    x_data = tf.placeholder(dtype=tf.float32, shape=[None])
    y_data = tf.placeholder(dtype=tf.float32, shape=[None])

    w = tf.Variable(tf.constant([0.5]))
    b = tf.Variable(tf.zeros([1]))

    y1 = tf.mul(w, x_data, name='y1')
    y2 = py_func(addone, [y1], [tf.float32], grad=addone_grad)[0]
    y = tf.add(y2, b)

    loss = tf.reduce_mean(tf.square(y - y_data))
    optimizer = tf.train.GradientDescentOptimizer(0.01)
    train = optimizer.minimize(loss)

    print("Pyfunc grad", ops.get_gradient_function(y2.op))
    with tf.Session() as sess:
        sess.run(tf.global_variables_initializer())

        for step in range(10):
            #            ran = np.random.rand(115).astype(np.float32)
            ran = np.ones((115)).astype(np.float32)
            ans = ran * 1.5 + 3
            dic = {x_data: ran, y_data: ans}
            tt, yy, yy1= sess.run([train, y1, y2], feed_dict=dic)
            if step % 1 == 0:
                print('step {}'.format(step))
                print('{}, {}'.format(w.eval(), b.eval()))

        test = sess.run(y, feed_dict={x_data:[1]})
        print('test = {}'.format(test))


if __name__ == '__main__':
    pyfunc_test()