使用CNN(LSTM架构)进行序列预测基于TensorFlow

Posted 量化投资与机器学习

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了使用CNN(LSTM架构)进行序列预测基于TensorFlow相关的知识,希望对你有一定的参考价值。


编辑部

关键字全网搜索最新排名

『量化投资』:排名第一

『量       化』:排名第一

『机器学习』:排名第四


我们会再接再厉


作者 / mouradmourafiq 

翻译 / 编辑部翻译组

来源 / https://github.com/mouradmourafiq


前言

这篇推文抛砖引玉的介绍如何使用循环神经网络逼近一系列向量,特别的是,将使用LSTM架构。


根据先前的观察预测一系列实数。 传统的神经网络架构不能做到这一点,这就是为什么要复制神经网络来解决这个问题,因为它们允许存储以前的信息来预测将来的事件。


在这个例子中,将尝试预测一些功能:


  • sin

使用CNN(LSTM架构)进行序列预测基于TensorFlow

  • sin and cos on the same time

使用CNN(LSTM架构)进行序列预测基于TensorFlow

  • x*sin(x)

使用CNN(LSTM架构)进行序列预测基于TensorFlow


模型的建立

首先建立模型,lstm_model,该模型是不同时间步骤的堆叠lstm单元的列表,后面是一个密集层。

使用CNN(LSTM架构)进行序列预测基于TensorFlow

因此,我们的模型期望一个维度对应的数据((batch size, time_steps of the first lstm cell, num_features in our data)


接下来,我们以模型可以接受的方式准备数据。

使用CNN(LSTM架构)进行序列预测基于TensorFlow

这将创建一个数据,这将允许我们的模型查看time_steps在过去的次数,以进行预测。 所以如果我们的第一个单元格是10个time_steps单元格,那么对于我们想做的每个预测,我们需要为单元格提供10个历史数据点。 y值应该对应于我们想要预测的数据的第十个值。


我们首先定义超参数

使用CNN(LSTM架构)进行序列预测基于TensorFlow


现在我们可以根据我们的模型创建一个回归函数

使用CNN(LSTM架构)进行序列预测基于TensorFlow


预测sin函数

使用CNN(LSTM架构)进行序列预测基于TensorFlow

测试集

使用CNN(LSTM架构)进行序列预测基于TensorFlow

  • real sin function

使用CNN(LSTM架构)进行序列预测基于TensorFlow


一起预测sin和cos函数

使用CNN(LSTM架构)进行序列预测基于TensorFlow

测试集

使用CNN(LSTM架构)进行序列预测基于TensorFlow

  • predicted sin-cos function

使用CNN(LSTM架构)进行序列预测基于TensorFlow

x*sin

使用CNN(LSTM架构)进行序列预测基于TensorFlow

使用CNN(LSTM架构)进行序列预测基于TensorFlow

  • 真实 x*sin 

使用CNN(LSTM架构)进行序列预测基于TensorFlow


  • 预测 x*sin 

使用CNN(LSTM架构)进行序列预测基于TensorFlow

model loss

使用CNN(LSTM架构)进行序列预测基于TensorFlow



文档说明


Install and Run

Create a Virtual Environment

It is reccomended that you create a virtualenv for the setup since this example is highly dependant on the versions set in the requirements file.

$ virtualenv ~/python/ltsm $ source ~/python/ltsm/bin/activate (ltsm) $

Install Requirements

This example depends on tensorflow-0.11.0 to work. You will first need to install the requirements. You will need the appropriate version of tensorflow for your platform, this example is for mac. For more details goto 

(ltsm) $ pip install -U https://storage.googleapis.com/tensorflow/mac/cpu/tensorflow-0.11.0-py3-none-any.whl (ltsm) $ pip install -r ./requirements.txt

Running on Jupyter

Three Jupyter notebooks are provided as examples on how to use lstm for predicting shapes. They will be available when you start up Jupyter in the project dir.

(ltsm) $ jupyter notebook


- END -


关注者


110000+


我们每天都在进步


以上是关于使用CNN(LSTM架构)进行序列预测基于TensorFlow的主要内容,如果未能解决你的问题,请参考以下文章

时间序列预测基于matlab CNN优化LSTM时间序列预测含Matlab源码 2003期

[时间序列预测]基于BPRNNLSTMCNN-LSTM算法多特征(多影响因素)用电负荷预测[保姆级手把手教学]

CNN+LSTM+Attention实现时间序列预测

多层多输入的CNN-LSTM时间序列回归预测(卷积神经网络-长短期记忆网络)——附代码

在 keras 中使用 CNN-LSTM 模型进行序列到序列分类

基于LSTM深层神经网络的时间序列预测