TensorFlow:整合神经网络的输出

TensorFlow: integrate output of neural network

我有一个将两个参数作为输入的神经网络:

t = tf.placeholder(tf.float32, [None, 1])
x = tf.placeholder(tf.float32, [None, 1])

在我的损失函数中,我需要对 t 的输出进行积分,但我想不出这样做的方法,因为 TensorFlow 中唯一可用的数值积分函数 tf.contrib.integrate.odeint_fixed ,不能将张量作为函数,因为它不能被调用:

通话

t = tf.constant(np.linspace(0.0,1.0,100), dtype = tf.float64 )

integ = tf.contrib.integrate.odeint_fixed(model.output, 
                                          0.0, 
                                          t, 
                                          method = "rk4")

输出

...

<ipython-input-5-c79e79b75391> in loss(model, t, x)
     24                                                 0.0,
     25                                                 t,
---> 26                                                 method = "rk4")

...

TypeError: 'Tensor' object is not callable

更何况我也不知道如何在这个计算中处理x,它应该是固定的。

tf.contrib.integrate.odeint_fixed 似乎用于积分常微分方程 (ODE)。但是,如果我理解正确的话,您想要近似模型输出的定积分,我们称之为 y,在 t.

处采样

为此,您可以使用 trapezoidal rule, for which you find a possible implementation in tensorflows AUC function。在您的情况下,它可能看起来像:

from tensorflow.python.ops import math_ops

def trapezoidal_integral_approx(t, y):
    return math_ops.reduce_sum(
            math_ops.multiply(t[:-1] - t[1:],
                              (y[:-1] + y[1:]) / 2.), 
            name='trapezoidal_integral_approx')

其中 y 将是您的模型的输出。