Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/tensorflow/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Numpy TensorFlow:如何在不使用eval的情况下使用自定义渐变实现python功能?_Numpy_Tensorflow - Fatal编程技术网

Numpy TensorFlow:如何在不使用eval的情况下使用自定义渐变实现python功能?

Numpy TensorFlow:如何在不使用eval的情况下使用自定义渐变实现python功能?,numpy,tensorflow,Numpy,Tensorflow,我正在尝试使用tf.py_func在python中编写一些自定义TensorFlow函数,其中我希望在python中计算结果和梯度。我正在使用渐变覆盖贴图技巧,例如从和 然而,当正向函数得到一个numpy数组作为输入时,梯度函数得到张量。这是一个问题,取决于调用函数的时间,因为可能没有默认会话,和/或可能没有包含所有必需值的提要,例如,在tf.train优化器中 如何在向前和向后函数都获取并返回numpy数组的情况下执行py_func 示例代码: import tensorflow as tf

我正在尝试使用tf.py_func在python中编写一些自定义TensorFlow函数,其中我希望在python中计算结果和梯度。我正在使用渐变覆盖贴图技巧,例如从和

然而,当正向函数得到一个numpy数组作为输入时,梯度函数得到张量。这是一个问题,取决于调用函数的时间,因为可能没有默认会话,和/或可能没有包含所有必需值的提要,例如,在tf.train优化器中

如何在向前和向后函数都获取并返回numpy数组的情况下执行py_func

示例代码:

import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt

def sin_func(x):
    return np.sin(x)

def sin_grad_func(op, grad):
    x = op.inputs[0].eval()
    grad = grad.eval() # <--- this is what I'd like to avoid
    output_grad = np.cos(x) * grad
    return tf.convert_to_tensor(output_grad)

def py_func(func, inp, Tout, stateful=True, name=None, grad_func=None):
    grad_name = 'PyFuncGrad_' + str(np.random.randint(0, 1E+8))
    tf.RegisterGradient(grad_name)(grad_func)
    g = tf.get_default_graph()
    with g.gradient_override_map({"PyFunc": grad_name}):
        return tf.py_func(func, inp, Tout, stateful=stateful, name=name)

with tf.Session() as sess:
    np_x = np.linspace(0, np.pi, num=1000, dtype=np.float32)
    x = tf.constant(np_x)
    y = py_func(sin_func,
                [x],
                [tf.float32],
                name='np_sin',
                grad_func=sin_grad_func)
    y = y[0]
    gr = tf.gradients(y, [x])
    tf.global_variables_initializer().run()
    plt.plot(y.eval())
    plt.plot(gr[0].eval())

如果要在渐变函数中包含任意Python代码,最简单的解决方案是在sin_grad_func中创建另一个tf.py_func:

def sin_grad_func_impl(x, grad):
    return np.cos(x) * grad

def sin_grad_func(op, grad):
    return tf.py_func(sin_grad_func_impl, [x, grad], grad.dtype)