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
Python Fetch参数None的类型无效<;类别';非类型'&燃气轮机;何时调用tf.gradients_Python_Tensorflow - Fatal编程技术网

Python Fetch参数None的类型无效<;类别';非类型'&燃气轮机;何时调用tf.gradients

Python Fetch参数None的类型无效<;类别';非类型'&燃气轮机;何时调用tf.gradients,python,tensorflow,Python,Tensorflow,我正在创建一个蒙特卡罗模拟,通过tensorflow为期权定价。当我试图调用第二个梯度来计算gamma时,它抛出了一个错误: Fetch argument None has invalid type <class 'NoneType'> 一切正常。但当我打电话时: pricer = pricerobj.tfmcPricer(TFMonteCarloSimulatorType.GBM,enable_greeks = True) price = pricer(100,110,2.0,

我正在创建一个蒙特卡罗模拟,通过tensorflow为期权定价。当我试图调用第二个梯度来计算gamma时,它抛出了一个错误:

Fetch argument None has invalid type <class 'NoneType'>
一切正常。但当我打电话时:

pricer = pricerobj.tfmcPricer(TFMonteCarloSimulatorType.GBM,enable_greeks = True)

price = pricer(100,110,2.0,0.2,0.03,0,0,100000,365 * 2)
它将抛出一个错误:

Fetch argument None has invalid type <class 'NoneType'>
有人能帮我吗

请参阅下面我的代码:


    class TFMonteCarloSimulator(object):    
        def __init__(self, SimulatorType):
            if not isinstance(SimulatorType, TFMonteCarloSimulatorType):
                raise Exception("Please use defined Simulator Type from class TFMonteCarloSimulatorType")
            self.simulatorType = SimulatorType
        def _GBMSimulation(self):
            spot = tf.placeholder(tf.float32)
            strike = tf.placeholder(tf.float32)
            dt = tf.placeholder(tf.float32)
            vol = tf.placeholder(tf.float32)
            timeToMaturity = tf.placeholder(tf.float32)
            dw = tf.placeholder(tf.float32)
            miu = tf.placeholder(tf.float32)
            div = tf.placeholder(tf.float32)
            pricer = spot * tf.cumprod(tf.exp((miu - div - (vol ** 2) / 2) * dt + vol * tf.sqrt(dt) * dw), axis=1)
            return (spot, strike, dt, vol, miu, div, dw, pricer,timeToMaturity)
        def createSimulator(self):
            if self.simulatorType == TFMonteCarloSimulatorType.GBM:
                (spot, strike, dt, vol, miu, div, dw, pricer,timeToMaturity) = self._GBMSimulation()
                def simulator(S, K, TTM, volatility, drift, dividend, seed, Nsimulation, Ndays):
                    if seed != 0:
                        np.random.seed(seed)
                    rndMatrix = np.random.randn(Nsimulation, Ndays)

                    with tf.Session() as sess:
                        timeDelta = TTM / Ndays
                        res = sess.run(pricer,{
                            spot: S,
                            strike: K,
                            miu:drift,
                            div:dividend,
                            vol:volatility,
                            dt:timeDelta,
                            timeToMaturity:TTM,
                            dw:rndMatrix                    
                        })
                        return res
                return simulator
            else:
                return None



    class OptionPricer(TFMonteCarloSimulator):
        def __init__(self):
            pass

        def blstfPricer(self, enable_greeks = True):
            spot = tf.placeholder(tf.float32)
            strike = tf.placeholder(tf.float32)
            dt = tf.placeholder(tf.float32)
            vol = tf.placeholder(tf.float32)
            ir = tf.placeholder(tf.float32)
            div = tf.placeholder(tf.float32)
            ndf = tfp.distributions.Normal(0,1).cdf
            d1 = (tf.log(spot/strike) + (ir + (vol ** 2)/2) * dt) / (vol * tf.sqrt(dt))
            d2 = d1 - vol * tf.sqrt(dt)
            price = spot * tf.exp(-div * dt) * ndf(d1) - strike * tf.exp(-ir * dt) * ndf(d2)
            targets = [price]
            if enable_greeks:
                greeks = tf.gradients(price, [spot, vol, ir, dt])
                gamma = tf.gradients(ys = greeks[0],xs = [spot])
                targets += [greeks, gamma]
            def execute(S,K, TTM, imVol, IR, Div):
                with tf.Session() as sess:
                    res = sess.run(targets,
                                  {spot:S,
                                   strike:K,
                                   ir:IR,
                                   vol:imVol,
                                   div:Div,
                                   dt:TTM
                                  })
                    return res
            return execute

        def tfmcPricer(self, simType,enable_greeks = True):
            if simType == TFMonteCarloSimulatorType.GBM:
                (spot, strike, dt, vol, miu, div, dw, pricer, timeToMaturity) = super(OptionPricer, self)._GBMSimulation()
                payoff = tf.maximum(pricer[:,-1] - strike, 0)
                price = tf.exp(-miu * timeToMaturity) * tf.reduce_mean(payoff)
                targets = [price]
                if enable_greeks:
                    greeks = tf.gradients(price, [spot, vol, miu,timeToMaturity])
                    gamma = tf.gradients(greeks[0],[spot])
                    targets += [greeks]
                    targets += [gamma]
                def execute(S, K, TTM, volatility, drift, dividend,seed, Nsimulation,Ndays):
                    if seed != 0:
                        np.random.seed(seed)
                    rndMatrix = np.random.randn(Nsimulation, Ndays)
                    with tf.Session() as sess:
                        timedelta = TTM / Ndays 
                        res = sess.run(targets,
                                          {
                                            timeToMaturity:TTM,
                                            spot:S,
                                            strike:K,
                                            dt:timedelta,
                                            vol:volatility,
                                            miu:drift, 
                                            div:dividend,
                                            dw:rndMatrix
                                          }

                                      )
                        return res
                return execute
            else:
                return None







原因是计算中存在一些不可微运算,因此梯度最终为
None
。原则上,我在你的代码中看不到任何不可微的东西,但是你正在尝试计算梯度的梯度。这在原则上是可能的,但是第一个梯度可能在其计算中使用一些不可微的操作,在这种情况下,第二个梯度将不可计算(注意,当我在这里说“不可微”时,我的意思是“在TF中没有定义的梯度”)。
gamma = tf.gradients(greeks[0],[spot])

    class TFMonteCarloSimulator(object):    
        def __init__(self, SimulatorType):
            if not isinstance(SimulatorType, TFMonteCarloSimulatorType):
                raise Exception("Please use defined Simulator Type from class TFMonteCarloSimulatorType")
            self.simulatorType = SimulatorType
        def _GBMSimulation(self):
            spot = tf.placeholder(tf.float32)
            strike = tf.placeholder(tf.float32)
            dt = tf.placeholder(tf.float32)
            vol = tf.placeholder(tf.float32)
            timeToMaturity = tf.placeholder(tf.float32)
            dw = tf.placeholder(tf.float32)
            miu = tf.placeholder(tf.float32)
            div = tf.placeholder(tf.float32)
            pricer = spot * tf.cumprod(tf.exp((miu - div - (vol ** 2) / 2) * dt + vol * tf.sqrt(dt) * dw), axis=1)
            return (spot, strike, dt, vol, miu, div, dw, pricer,timeToMaturity)
        def createSimulator(self):
            if self.simulatorType == TFMonteCarloSimulatorType.GBM:
                (spot, strike, dt, vol, miu, div, dw, pricer,timeToMaturity) = self._GBMSimulation()
                def simulator(S, K, TTM, volatility, drift, dividend, seed, Nsimulation, Ndays):
                    if seed != 0:
                        np.random.seed(seed)
                    rndMatrix = np.random.randn(Nsimulation, Ndays)

                    with tf.Session() as sess:
                        timeDelta = TTM / Ndays
                        res = sess.run(pricer,{
                            spot: S,
                            strike: K,
                            miu:drift,
                            div:dividend,
                            vol:volatility,
                            dt:timeDelta,
                            timeToMaturity:TTM,
                            dw:rndMatrix                    
                        })
                        return res
                return simulator
            else:
                return None



    class OptionPricer(TFMonteCarloSimulator):
        def __init__(self):
            pass

        def blstfPricer(self, enable_greeks = True):
            spot = tf.placeholder(tf.float32)
            strike = tf.placeholder(tf.float32)
            dt = tf.placeholder(tf.float32)
            vol = tf.placeholder(tf.float32)
            ir = tf.placeholder(tf.float32)
            div = tf.placeholder(tf.float32)
            ndf = tfp.distributions.Normal(0,1).cdf
            d1 = (tf.log(spot/strike) + (ir + (vol ** 2)/2) * dt) / (vol * tf.sqrt(dt))
            d2 = d1 - vol * tf.sqrt(dt)
            price = spot * tf.exp(-div * dt) * ndf(d1) - strike * tf.exp(-ir * dt) * ndf(d2)
            targets = [price]
            if enable_greeks:
                greeks = tf.gradients(price, [spot, vol, ir, dt])
                gamma = tf.gradients(ys = greeks[0],xs = [spot])
                targets += [greeks, gamma]
            def execute(S,K, TTM, imVol, IR, Div):
                with tf.Session() as sess:
                    res = sess.run(targets,
                                  {spot:S,
                                   strike:K,
                                   ir:IR,
                                   vol:imVol,
                                   div:Div,
                                   dt:TTM
                                  })
                    return res
            return execute

        def tfmcPricer(self, simType,enable_greeks = True):
            if simType == TFMonteCarloSimulatorType.GBM:
                (spot, strike, dt, vol, miu, div, dw, pricer, timeToMaturity) = super(OptionPricer, self)._GBMSimulation()
                payoff = tf.maximum(pricer[:,-1] - strike, 0)
                price = tf.exp(-miu * timeToMaturity) * tf.reduce_mean(payoff)
                targets = [price]
                if enable_greeks:
                    greeks = tf.gradients(price, [spot, vol, miu,timeToMaturity])
                    gamma = tf.gradients(greeks[0],[spot])
                    targets += [greeks]
                    targets += [gamma]
                def execute(S, K, TTM, volatility, drift, dividend,seed, Nsimulation,Ndays):
                    if seed != 0:
                        np.random.seed(seed)
                    rndMatrix = np.random.randn(Nsimulation, Ndays)
                    with tf.Session() as sess:
                        timedelta = TTM / Ndays 
                        res = sess.run(targets,
                                          {
                                            timeToMaturity:TTM,
                                            spot:S,
                                            strike:K,
                                            dt:timedelta,
                                            vol:volatility,
                                            miu:drift, 
                                            div:dividend,
                                            dw:rndMatrix
                                          }

                                      )
                        return res
                return execute
            else:
                return None