理论基础:
RNN(Recurrent Neural Network) --- shortage: can't remember the memory long time ago.
LSTM(Long Short Term Memory) ---- 衍生算法:attention, transformer.
相比较而言:
LSTM多了一个标识为c(carry)的单元,可以理解为传送带。
传送带上的状态信息由遗忘门和输入门控制。
遗忘门:通过结合输入和激活函数,产出一个值(值大于0.5则输出1,否则输出0)------从而控制该段记忆是否被记住。
输入门:
输出门:
LSTM有多种变形。
利用LSTM生成文本:
滑动窗口概念:
序列(样本)长度,步长,一个样本后边紧跟的一个字符就是该样本的标签。a,b,c d
知识点*
- dropout: dropout是指在深度学习网络的训练过程中,对于神经网络单元,按照一定的概率将其暂时从网络中丢弃。注意是暂时,对于随机梯度下降来说,由于是随机丢弃,故而每一个mini-batch都在训练不同的网络。
- Batch_size, epoch, iteration这三的概念,看这一链接足矣神经网络中Epoch、Iteration、Batchsize相关理解和说明_Microstrong-CSDN博客_epoch
利用LSTM进行股票价格预测流程:
- import neccesary packages
- read and see the data structure(make 'date' to be the index)
- split the data into train set and test set
- normalization of the trainset (to fit the model)
- within the train set, do 滑动窗口(set sequence as 60), then得到的训练样本(包含训练数据和标签)个数为训练集数据总数-60.(一开始将其储存在列表,滑动结束后将储存在列表中的train set(包含训练数据和标签)转换成nparray的类型
- use numpy to reshape the train set in to a 3 dimensional array.(数据准备完毕)
- Model building(3 LSTM layers and 1 Dense layer) 前两层LSTM return整个序列,后两层不
- Model compiling(epoch=20同一批数据训练20次, batch_size=32每一批数据取32个训练数据作为样本)
- 模型训练完后,提取测试集的数据,归一化
- Predict后要inverse transform转置回去
- plot