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

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

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

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

  • sin

 

  • sin and cos on the same time

 

  • x*sin(x)

 

模型的建立

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

 

 

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

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

 

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

 

我们首先定义超参数

 

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

 

预测sin函数

 

测试集

 

  • real sin function

 

一起预测sin和cos函数

 

测试集

 

  • predicted sin-cos function

 

x*sin

 

  • 真实 x*sin 

 

  • 预测 x*sin 

 

model loss

 

 

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 TAG tensorflow-0.11.0 Setup

(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
免责声明:信息仅供参考,不构成投资及交易建议。投资者据此操作,风险自担。
如果觉得文章对你有用,请随意赞赏收藏
相关推荐
相关下载
登录后评论
Copyright © 2019 宽客在线