解决LSTM

时间:2019-09-04 18:14:12

标签: python-3.x tensorflow machine-learning lstm simulation

基于https://github.com/mgroncki/DataScienceNotebooks/blob/master/DeepHedging/DeepHedging_Part1.ipynb和他的RNN网络的工作,我正在尝试扩展此价差期权模型。价差期权的收益基本上在时间T处为max(Underlying1-Underlying2-Strike; 0)。

在扩展和更改此代码时,我确实遇到了一些整形问题。

我记下了每个张量,变量等的所有形状,但我不明白这一点。另外,我尝试将其数量减少,以逐步观察整个过程,而没有任何积极的回报。

这是基于Matthias Github代码的LSTM / RNN代码:

class LSTM(object):
    def __init__(self, time_steps, batch_size, features, nodes = [62,46,46,2], name='model'):
        tf.reset_default_graph()
        self.batch_size = batch_size
        self.S_t_input = tf.placeholder(tf.float32, [time_steps, batch_size, features])
        self.K = tf.placeholder(tf.float32, batch_size)
        self.alpha = tf.placeholder(tf.float32)

        S_T_0 = self.S_t_input[-1,:,0]
        S_T_1 = self.S_t_input[-1,:,1]

        # Returns from Underlying 1 and Underlying 2
        dS_0 = self.S_t_input[1:, :, 0] - self.S_t_input[0:-1, :, 0]
        dS_1 = self.S_t_input[1:, :, 1] - self.S_t_input[0:-1, :, 1]
        #dS = tf.reshape(dS, (time_steps, batch_size))

        #Prepare S_t for the use in the RNN remove the last time step (at T the portfolio is zero)
        #S_t = tf.unstack(self.S_t_input[:-1, :,:], axis=0)
        # S_t wird zu einer Liste bestehend aus Tensoren mit Shape

        # Build the LSTM (Multi Zellen) !! Output in nodes = 1 !!!
        lstm = tf.contrib.rnn.MultiRNNCell([tf.contrib.rnn.LSTMCell(n) for n in nodes])

        # Output == Stragegy 
        self.strategy, state = tf.nn.dynamic_rnn(lstm, self.S_t_input[:-1,:,:], dtype=tf.float32)

        #self.strategy = tf.reshape(self.strategy, (time_steps-1, batch_size, features))

        # Optionvalue of the Spread option
        self.option = tf.maximum(S_T_0-S_T_1-self.K, 0)

        # Maybe Wrong but lets focus on the Shapes
        self.Hedging_PnL = - self.option + tf.reduce_sum(dS_0*self.strategy[:,:,0], axis=0) + tf.reduce_sum(dS_1*self.strategy[:,:,1], axis=0)

        print(tf.shape(self.option))
        print(tf.shape(self.Hedging_PnL))

        self.Hedging_PnL_Paths = - self.option + dS_0*self.strategy[:,:,0] + dS_1*self.strategy[:,:,1]

        # Calculate the CVaR for a given confidence level alpha
        # Take the 1-alpha largest losses (top 1-alpha negative PnLs) and calculate the mean
        CVaR, idx = tf.nn.top_k(-self.Hedging_PnL, tf.cast((1-self.alpha)*batch_size, tf.int32))

        #CVaR = tf.reduce_mean(CVaR)
        CVaR = tf.math.reduce_sum(CVaR)

        self.train = tf.train.AdamOptimizer().minimize(CVaR)
        self.saver = tf.train.Saver()
        self.modelname = name
        print('--- DONE ---')

Python将建立​​模型,但是当我运行trainroutine时,我从Python中得到了Errors。训练程序与Matthias完全相同

def _execute_graph_batchwise(self, paths, strikes, riskaversion, sess, epochs=1, train_flag=False):
        sample_size = paths.shape[1]
        batch_size=self.batch_size
        idx = np.arange(sample_size)
        start = dt.datetime.now()
        for epoch in range(epochs):
            # Save the hedging Pnl for each batch      
            pnls = []
            strategies = []
            if train_flag:
                np.random.shuffle(idx)
            for i in range(int(sample_size/batch_size)):
                indices = idx[i*batch_size : (i+1)*batch_size]
                batch = paths[:,indices,:]
                if train_flag:
                    _, pnl, strategy = sess.run([self.train, self.Hedging_PnL, self.strategy], {self.S_t_input: batch,
                                                                                          self.K : strikes[indices],
                                                                                          self.alpha: riskaversion})
                else:
                    pnl, strategy = sess.run([self.Hedging_PnL, self.strategy], {self.S_t_input: batch,
                                                                                self.K : strikes[indices],
                                                                                self.alpha: riskaversion})
                pnls.append(pnl)
                strategies.append(strategy)
            #Calculate the option prive given the risk aversion level alpha
            CVaR = np.mean(-np.sort(np.concatenate(pnls))[:int((1-riskaversion)*sample_size)])
            if train_flag:
                if epoch % 10 == 0:
                    print('Time elapsed:', dt.datetime.now()-start)
                    print('Epoch', epoch, 'CVaR', CVaR)
                    self.saver.save(sess, r"/Users/floriankonig/LSTM/%s/model.ckpt" % self.modelname)
        self.saver.save(sess, r"/Users/floriankonig/LSTM/%s/model.ckpt" % self.modelname)
        return CVaR, np.concatenate(pnls), np.concatenate(strategies,axis=1)

    def training(self, paths, strikes, riskaversion, epochs, session, init=True):
        if init:
            session.run(tf.global_variables_initializer())
        self._execute_graph_batchwise(paths, strikes, riskaversion, session, epochs, train_flag=True)

输入数据是通过蒙特卡罗模拟生成的,并且具有形状(时间步长,批处理大小,特征)。

我想计算CVaR,但是出现以下错误。

  

除非所有指定的输入大小都不为零,否则Reshape无法推断出空张量的缺失输入大小

0 个答案:

没有答案