gpt4 book ai didi

python - 梯度如何通过 tf.py_func

转载 作者:行者123 更新时间:2023-11-28 20:03:22 26 4
gpt4 key购买 nike

这是在 tensorflow 中实现的 Faster R-CNN。
proposal_layerimplement通过 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)获取值(value)
sess.run(tf.gradients(loss, w), feed_dict=dic)得到 {TypeError}Fetch argument None has invalid type <type 'NoneType'>

我知道一些问题建议使用tf.RegisterGradientgradient_override_map
但我无法在更快的 rcnn 存储库中找到这些(帖子顶部的链接)
我是做错了什么还是遗漏了什么w卡住

最佳答案

py_func 的梯度为None(只需检查 ops.get_gradient_function(y2.op))。有这个 gist @harpone 展示了如何为 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()

关于python - 梯度如何通过 tf.py_func,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41535347/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com