• 大小: 8.73MB
    文件类型: .zip
    金币: 1
    下载: 0 次
    发布日期: 2023-10-06
  • 语言: 其他
  • 标签:

资源简介

LSTM多变量预测,本程序利用TensorFlow构建一个简易LSTM模型,内含对多个变量因子,以及本程序的运行环境。

资源截图

代码片段和文件信息

import warnings
from sklearn import preprocessing
from sklearn.utils import shuffle
import tensorflow as tf
import numpy as np
import pandas as pd
from sklearn.model_selection import train_test_split KFold

warnings.filterwarnings(‘ignore‘)
dataset = pd.read_csv(r‘PCA.csv‘)

BATCH_START = 0  # 建立 batch data 时候的 index
TIME_STEPS = 10  # backpropagation through time 的 time_steps
BATCH_SIZE = 10
INPUT_SIZE = 9  # sin 数据输入 size
OUTPUT_SIZE = 1  # cos 数据输出 size
CELL_SIZE = 10  # RNN 的 hidden unit size
LR = 0.006  # learning rate


class LSTMRNN(object):
    def __init__(self n_steps input_size output_size cell_size batch_size):
        ‘‘‘
        :param n_steps: 每批数据总包含多少时间刻度
        :param input_size: 输入数据的维度
        :param output_size: 输出数据的维度
        :param cell_size: cell的大小
        :param batch_size: 每批次训练数据的数量
        ‘‘‘
        self.n_steps = n_steps
        self.input_size = input_size
        self.output_size = output_size
        self.cell_size = cell_size
        self.batch_size = batch_size
        with tf.name_scope(‘inputs‘):
            self.xs = tf.placeholder(tf.float32 [None n_steps input_size] name=‘xs‘)  # xs 有三个维度
            self.ys = tf.placeholder(tf.float32 [None n_steps output_size] name=‘ys‘)  # ys 有三个维度
        with tf.variable_scope(‘in_hidden‘):
            self.add_input_layer()
        with tf.variable_scope(‘LSTM_cell‘):
            self.add_cell()
        with tf.variable_scope(‘out_hidden‘):
            self.add_output_layer()
        with tf.name_scope(‘cost‘):
            self.compute_cost()
        with tf.name_scope(‘train‘):
            self.train_op = tf.train.AdamOptimizer(LR).minimize(self.cost)
        with tf.name_scope(‘std‘):
            self.standard_var()

    # 增加一个输入层
    def add_input_layer(self ):
        l_in_x = tf.reshape(self.xs [-1 self.input_size] name=‘2_2D‘)  # -1 表示任意行数
        Ws_in = self._weight_variable([self.input_size self.cell_size])
        bs_in = self._bias_variable([self.cell_size ])
        with tf.name_scope(‘Wx_plus_b‘):
            l_in_y = tf.matmul(l_in_x Ws_in) + bs_in
        self.l_in_y = tf.reshape(l_in_y [-1 self.n_steps self.cell_size] name=‘2_3D‘)

    # 多时刻的状态叠加层
    def add_cell(self):
        # lstm_cell = tf.nn.rnn_cell.BasicLSTMCell(self.cell_size forget_bias=1.0 state_is_tuple=True)
        # lstm_cell = tf.nn.rnn_cell.BasicLSTMCell(self.cell_size forget_bias=1.0 state_is_tuple=True
        #                                                  activation=tf.nn.relu)
        # lstm_cell = tf.nn.rnn_cell.BasicLSTMCell(self.cell_size forget_bias=1.0 state_is_tuple=True
        #                                          activation=tf.nn.softsign)
        # lstm_cell = tf.nn.rnn_cell.BasicLSTMCell(self.cell_size forget_bias=1.0 state_is_tuple=True
        #                                          activation=tf.nn.softplus)
        # lstm_cell = tf.nn.rnn_cell.BasicLSTMCell(self.cell_size forget_bias=1.0 state_is_tuple=True
    

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----
     目录           0  2019-09-24 07:29  LSTM\
     目录           0  2019-09-24 07:33  LSTM\LSTM\
     目录           0  2019-05-25 18:55  LSTM\LSTM\.idea\
     文件         592  2019-05-24 09:45  LSTM\LSTM\.idea\LSTM.iml
     文件         309  2019-05-24 09:45  LSTM\LSTM\.idea\misc.xml
     文件         267  2019-05-23 17:38  LSTM\LSTM\.idea\modules.xml
     文件        8887  2019-05-25 18:55  LSTM\LSTM\.idea\workspace.xml
     目录           0  2019-05-24 10:49  LSTM\LSTM\logs\
     文件     1999747  2019-05-23 20:49  LSTM\LSTM\logs\events.out.tfevents.1558615324.MEMPHIS
     文件      304713  2019-05-23 21:14  LSTM\LSTM\logs\events.out.tfevents.1558617243.MEMPHIS
     文件      297103  2019-05-23 21:20  LSTM\LSTM\logs\events.out.tfevents.1558617599.MEMPHIS
     文件      297645  2019-05-23 21:31  LSTM\LSTM\logs\events.out.tfevents.1558618300.MEMPHIS
     文件      297645  2019-05-23 21:32  LSTM\LSTM\logs\events.out.tfevents.1558618322.MEMPHIS
     文件      290639  2019-05-23 21:35  LSTM\LSTM\logs\events.out.tfevents.1558618550.MEMPHIS
     文件      313713  2019-05-23 22:16  LSTM\LSTM\logs\events.out.tfevents.1558620975.MEMPHIS
     文件      313543  2019-05-23 22:21  LSTM\LSTM\logs\events.out.tfevents.1558621259.MEMPHIS
     文件     2498563  2019-05-23 22:59  LSTM\LSTM\logs\events.out.tfevents.1558623095.MEMPHIS
     文件     2498563  2019-05-23 23:14  LSTM\LSTM\logs\events.out.tfevents.1558623920.MEMPHIS
     文件     2498688  2019-05-23 23:27  LSTM\LSTM\logs\events.out.tfevents.1558624612.MEMPHIS
     文件     2498688  2019-05-23 23:42  LSTM\LSTM\logs\events.out.tfevents.1558625371.MEMPHIS
     文件     2498688  2019-05-23 23:42  LSTM\LSTM\logs\events.out.tfevents.1558625372.MEMPHIS
     文件     2507384  2019-05-23 23:42  LSTM\LSTM\logs\events.out.tfevents.1558625376.MEMPHIS
     文件     2507384  2019-05-24 00:06  LSTM\LSTM\logs\events.out.tfevents.1558626851.MEMPHIS
     文件     2507384  2019-05-24 00:21  LSTM\LSTM\logs\events.out.tfevents.1558627757.MEMPHIS
     文件      311239  2019-05-24 10:27  LSTM\LSTM\logs\events.out.tfevents.1558664809.MEMPHIS
     文件      311239  2019-05-24 10:27  LSTM\LSTM\logs\events.out.tfevents.1558664861.MEMPHIS
     文件      311294  2019-05-24 10:29  LSTM\LSTM\logs\events.out.tfevents.1558664944.MEMPHIS
     文件     1169624  2019-05-24 10:32  LSTM\LSTM\logs\events.out.tfevents.1558665031.MEMPHIS
     文件     1169624  2019-05-24 10:52  LSTM\LSTM\logs\events.out.tfevents.1558666127.MEMPHIS
     文件     1169624  2019-05-24 10:52  LSTM\LSTM\logs\events.out.tfevents.1558666151.MEMPHIS
     文件     1169624  2019-05-24 10:53  LSTM\LSTM\logs\events.out.tfevents.1558666172.MEMPHIS
............此处省略2个文件信息

评论

共有 条评论