我试图模拟需要进行明确整合的东西 . 代码如下所示:

import tensorflow as tf

from numpy import pi, inf
from tensorflow import log, sqrt, exp, pow
from scipy.integrate import quad  # for integration

def risk_neutral_pdf(phi, a, S, K, r, sigma, Mt, p_dict):

    phii = tf.complex(0., phi)

    A = tf.cast(0., tf.complex64)
    B = tf.cast(0., tf.complex64)

    p_dict['gamma'] = p_dict['gamma'] + p_dict['lamda'] + .5
    p_dict['lamda'] = -.5

    for t in range(Mt-1, -1, -1):
        temp = 1. - 2. * p_dict['alpha'] * B
        A = A + (phii + a) * r + p_dict['omega'] * B - .5 * log(temp)
        B = B * p_dict['beta'] + (phii + a) * (p_dict['lamda'] + p_dict['gamma']) - \
            .5 * p_dict['gamma']**2. + (.5*((phii + a) - p_dict['gamma'])**2. / temp)

    return tf.real(S**a * (S/K)**phii * exp(A + B * sigma**2.) / phii)

p_dict={'lamda': 0.205, 'omega': 5.02e-6, 'beta': 0.589, 'gamma': 421.39, 'alpha': 1.32e-6}

S = 100.
K = 100.
r = 0.
Mt = 0
sq_ht = sqrt(.15**2/252.)
sigma = sq_ht

P1 = tf.py_func(lambda z: quad(risk_neutral_pdf, z, inf, args=(1., S, K, r, sigma, Mt, p_dict))[0],
                [0.], tf.float64)

with tf.Session() as sess:
    res = sess.run(P1)
    print(res)

结果返回"InvalidArgumentError (see above for traceback): ValueError: Tensor(" pow:0 ", shape=(), dtype=float32) must be from the same graph as Tensor(" Cast_2:0 ", shape=(), dtype=complex64)."但是,无论我如何更改代码或引用"ValueError: Tensor A must be from the same graph as Tensor B"中的解决方案,它都不起作用 . 我想知道在将 tf.reset_default_graph() 放在最顶层时是否做错了,或者代码是否需要进行一些更改 .

谢谢 . (Tensroflow版本:1.6.0)

Update: 我发现 sigma 变量在传入risk_neutral_pdf函数之前已经是sqrt,并且在返回时被驱动,这是不必要的 . 因此在将返回值修改为 return tf.real(S**a * (S/K)**phii * exp(A + B * sigma) / phii) 并将sq_ht修改为 .15**2/252. 之后 . 错误更改为 "TypeError: a float is required" ,我认为这是由 quadTensor 引起的 . 有什么想法要解决?

非常感谢 .