时序预测一直是比较重要的研究问题,在统计学中我们有各种的模型来解决时间序列问题,但是最近几年比较火的深度学习中也有能解决时序预测问题的方法,另外在深度学习领域中时序预测算法可以解决自然语言问题等。

在网上找到了    tensorflow 中   RNN    和    LSTM   算法预测  sin  曲线的代码,效果不错。

LSTM:

  1. #encoding:UTF-8
  2.  
  3. import random
  4. import numpy as np
  5. import tensorflow as tf
  6. from tensorflow.contrib.rnn.python.ops import core_rnn
  7. from tensorflow.contrib.rnn.python.ops import core_rnn_cell
  8.  
  9. def build_data(n):
  10. xs = []
  11. ys = []
  12. for i in range(0, 2000):
  13. k = random.uniform(1, 50)
  14.  
  15. x = [[np.sin(k + j)] for j in range(0, n)]
  16. y = [np.sin(k + n)]
  17.  
  18. # x[i] = sin(k + i) (i = 0, 1, ..., n-1)
  19. # y[i] = sin(k + n)
  20. xs.append(x)
  21. ys.append(y)
  22.  
  23. train_x = np.array(xs[0: 1500])
  24. train_y = np.array(ys[0: 1500])
  25. test_x = np.array(xs[1500:])
  26. test_y = np.array(ys[1500:])
  27. return (train_x, train_y, test_x, test_y)
  28.  
  29. length = 10
  30. time_step_size = length
  31. vector_size = 1
  32. batch_size = 10
  33. test_size = 10
  34.  
  35. # build data
  36. (train_x, train_y, test_x, test_y) = build_data(length)
  37. print(train_x.shape, train_y.shape, test_x.shape, test_y.shape)
  38.  
  39. X = tf.placeholder("float", [None, length, vector_size])
  40. Y = tf.placeholder("float", [None, 1])
  41.  
  42. # get lstm_size and output predicted value
  43. W = tf.Variable(tf.random_normal([10, 1], stddev=0.01))
  44. B = tf.Variable(tf.random_normal([1], stddev=0.01))
  45.  
  46. def seq_predict_model(X, w, b, time_step_size, vector_size):
  47. # input X shape: [batch_size, time_step_size, vector_size]
  48. # transpose X to [time_step_size, batch_size, vector_size]
  49. X = tf.transpose(X, [1, 0, 2])
  50.  
  51. # reshape X to [time_step_size * batch_size, vector_size]
  52. X = tf.reshape(X, [-1, vector_size])
  53.  
  54. # split X, array[time_step_size], shape: [batch_size, vector_size]
  55. X = tf.split(X, time_step_size, 0)
  56.  
  57. # LSTM model with state_size = 10
  58. cell = core_rnn_cell.BasicLSTMCell(num_units=10,
  59. forget_bias=1.0,
  60. state_is_tuple=True)
  61. outputs, _states = core_rnn.static_rnn(cell, X, dtype=tf.float32)
  62.  
  63. # Linear activation
  64. return tf.matmul(outputs[-1], w) + b, cell.state_size
  65.  
  66. pred_y, _ = seq_predict_model(X, W, B, time_step_size, vector_size)
  67. loss = tf.square(tf.subtract(Y, pred_y))
  68. train_op = tf.train.GradientDescentOptimizer(0.001).minimize(loss)
  69.  
  70. with tf.Session() as sess:
  71. tf.global_variables_initializer().run()
  72.  
  73. # train
  74. for i in range(50):
  75. # train
  76. for end in range(batch_size, len(train_x), batch_size):
  77. begin = end - batch_size
  78. x_value = train_x[begin: end]
  79. y_value = train_y[begin: end]
  80. sess.run(train_op, feed_dict={X: x_value, Y: y_value})
  81.  
  82. # randomly select validation set from test set
  83. test_indices = np.arange(len(test_x))
  84. np.random.shuffle(test_indices)
  85. test_indices = test_indices[0: test_size]
  86. x_value = test_x[test_indices]
  87. y_value = test_y[test_indices]
  88.  
  89. # eval in validation set
  90. val_loss = np.mean(sess.run(loss,
  91. feed_dict={X: x_value, Y: y_value}))
  92. print('Run %s' % i, val_loss)
  93.  
  94. for b in range(0, len(test_x), test_size):
  95. x_value = test_x[b: b + test_size]
  96. y_value = test_y[b: b + test_size]
  97. pred = sess.run(pred_y, feed_dict={X: x_value})
  98. for i in range(len(pred)):
  99. print(pred[i], y_value[i], pred[i] - y_value[i])

RNN :

  1. import random
  2.  
  3. import numpy as np
  4. import tensorflow as tf
  5. from tensorflow.contrib.rnn.python.ops import core_rnn
  6. from tensorflow.contrib.rnn.python.ops import core_rnn_cell
  7.  
  8. def build_data(n):
  9. xs = []
  10. ys = []
  11. for i in range(0, 2000):
  12. k = random.uniform(1, 50)
  13.  
  14. x = [[np.sin(k + j)] for j in range(0, n)]
  15. y = [np.sin(k + n)]
  16.  
  17. # x[i] = sin(k + i) (i = 0, 1, ..., n-1)
  18. # y[i] = sin(k + n)
  19. xs.append(x)
  20. ys.append(y)
  21.  
  22. train_x = np.array(xs[0: 1500])
  23. train_y = np.array(ys[0: 1500])
  24. test_x = np.array(xs[1500:])
  25. test_y = np.array(ys[1500:])
  26. return (train_x, train_y, test_x, test_y)
  27.  
  28. length = 10
  29. time_step_size = length
  30. vector_size = 1
  31. batch_size = 10
  32. test_size = 10
  33.  
  34. # build data
  35. (train_x, train_y, test_x, test_y) = build_data(length)
  36. print(train_x.shape, train_y.shape, test_x.shape, test_y.shape)
  37.  
  38. X = tf.placeholder("float", [None, length, vector_size])
  39. Y = tf.placeholder("float", [None, 1])
  40.  
  41. # get lstm_size and output predicted value
  42. W = tf.Variable(tf.random_normal([10, 1], stddev=0.01))
  43. B = tf.Variable(tf.random_normal([1], stddev=0.01))
  44.  
  45. def seq_predict_model(X, w, b, time_step_size, vector_size):
  46. # input X shape: [batch_size, time_step_size, vector_size]
  47. # transpose X to [time_step_size, batch_size, vector_size]
  48. X = tf.transpose(X, [1, 0, 2])
  49. # reshape X to [time_step_size * batch_size, vector_size]
  50. X = tf.reshape(X, [-1, vector_size])
  51. # split X, array[time_step_size], shape: [batch_size, vector_size]
  52. X = tf.split(X, time_step_size, 0)
  53.  
  54. cell = core_rnn_cell.BasicRNNCell(num_units=10)
  55. initial_state = tf.zeros([batch_size, cell.state_size])
  56. outputs, _states = core_rnn.static_rnn(cell, X, initial_state=initial_state)
  57.  
  58. # Linear activation
  59. return tf.matmul(outputs[-1], w) + b, cell.state_size
  60.  
  61. pred_y, _ = seq_predict_model(X, W, B, time_step_size, vector_size)
  62. loss = tf.square(tf.subtract(Y, pred_y))
  63. train_op = tf.train.GradientDescentOptimizer(0.001).minimize(loss)
  64.  
  65. with tf.Session() as sess:
  66. tf.global_variables_initializer().run()
  67.  
  68. # train
  69. for i in range(50):
  70. # train
  71. for end in range(batch_size, len(train_x), batch_size):
  72. begin = end - batch_size
  73. x_value = train_x[begin: end]
  74. y_value = train_y[begin: end]
  75. sess.run(train_op, feed_dict={X: x_value, Y: y_value})
  76.  
  77. # randomly select validation set from test set
  78. test_indices = np.arange(len(test_x))
  79. np.random.shuffle(test_indices)
  80. test_indices = test_indices[0: test_size]
  81. x_value = test_x[test_indices]
  82. y_value = test_y[test_indices]
  83.  
  84. # eval in validation set
  85. val_loss = np.mean(sess.run(loss,
  86. feed_dict={X: x_value, Y: y_value}))
  87. print('Run %s' % i, val_loss)
  88.  
  89. for b in range(0, len(test_x), test_size):
  90. x_value = test_x[b: b + test_size]
  91. y_value = test_y[b: b + test_size]
  92. pred = sess.run(pred_y, feed_dict={X: x_value})
  93. for i in range(len(pred)):
  94. print(pred[i], y_value[i], pred[i] - y_value[i])

-------------------------------------------------------------------

Tensorflow 循环神经网络 基本 RNN 和 LSTM 网络 拟合、预测sin曲线的更多相关文章

  1. 深度学习项目——基于循环神经网络(RNN)的智能聊天机器人系统

    基于循环神经网络(RNN)的智能聊天机器人系统 本设计研究智能聊天机器人技术,基于循环神经网络构建了一套智能聊天机器人系统,系统将由以下几个部分构成:制作问答聊天数据集.RNN神经网络搭建.seq2s ...

  2. 大话循环神经网络(RNN)

      在上一篇文章中,介绍了 卷积神经网络(CNN)的算法原理,CNN在图像识别中有着强大.广泛的应用,但有一些场景用CNN却无法得到有效地解决,例如: 语音识别,要按顺序处理每一帧的声音信息,有些结果 ...

  3. Coursera Deep Learning笔记 序列模型(一)循环序列模型[RNN GRU LSTM]

    参考1 参考2 参考3 1. 为什么选择序列模型 序列模型能够应用在许多领域,例如: 语音识别 音乐发生器 情感分类 DNA序列分析 机器翻译 视频动作识别 命名实体识别 这些序列模型都可以称作使用标 ...

  4. 【学习笔记】循环神经网络(RNN)

    前言 多方寻找视频于博客.学习笔记,依然不能完全熟悉RNN,因此决定还是回到书本(<神经网络与深度学习>第六章),一点点把啃下来,因为这一章对于整个NLP学习十分重要,我想打好基础. 当然 ...

  5. TensorFlow(十一):递归神经网络(RNN与LSTM)

    RNN RNN(Recurrent Neural Networks,循环神经网络)不仅会学习当前时刻的信息,也会依赖之前的序列信息.由于其特殊的网络模型结构解决了信息保存的问题.所以RNN对处理时间序 ...

  6. CNN(卷积神经网络)、RNN(循环神经网络)、DNN,LSTM

    http://cs231n.github.io/neural-networks-1 https://arxiv.org/pdf/1603.07285.pdf https://adeshpande3.g ...

  7. 深度学习之循环神经网络(RNN)

    循环神经网络(Recurrent Neural Network,RNN)是一类具有短期记忆能力的神经网络,适合用于处理视频.语音.文本等与时序相关的问题.在循环神经网络中,神经元不但可以接收其他神经元 ...

  8. TensorFlow——循环神经网络基本结构

    1.导入依赖包,初始化一些常量 import collections import numpy as np import tensorflow as tf TRAIN_DATA = "./d ...

  9. [深度学习]理解RNN, GRU, LSTM 网络

    Recurrent Neural Networks(RNN) 人类并不是每时每刻都从一片空白的大脑开始他们的思考.在你阅读这篇文章时候,你都是基于自己已经拥有的对先前所见词的理解来推断当前词的真实含义 ...

随机推荐

  1. Centos7.3云服务器上安装Nginx、MySQL、JDK、Tomcat环境

    安装的软件路径建议放到/usr/local目录下 Tomcat 首先从最简单的Tomcat开始,进入到Apache的官网:http://www.apache.org,下载合适的版本来装,一般建议8.0 ...

  2. Spring -11 -单例设计模式 -懒汉式/饿汉式(idea默认的)

    单例设计模式 作用: 在应用程序有保证最多只能有一个实例. 好处: 2.1 提升运行效率. 2.2 实现数据共享. 案例:application 对象 懒汉式 3.1 对象只有被调用时才去创建. 3. ...

  3. 零基础python教程-用Python设计你的第一个小游戏

    学以致用,既然学习了python就要让它来实现我们想做的东西,这次咱就用python来做个简单小游戏,在实践中不断成长. 1.游戏代码: 输入数字,来猜测原作者心中所想的数字,猜中夸你,猜不中不夸你, ...

  4. 01.Vue前端路由学习目标

    路由的基本概念与原理 vue-router的基本使用 vue-router嵌套路由 vue-router动态路由匹配 vue-router命名路由 vue-router编程式导航 基于vue-rout ...

  5. vscode——设置自动保存

    前言 懒就一个字 步骤 打开设置第一项就是,选择焦点改变自动保存即可

  6. Java单个对象内存布局.md

    我们在如何获取一个Java对象所占内存大小的文章中写了一个获取Java对象所占内存大小的工具类(ObjectSizeFetcher),那么接下来,我们使用这个工具类来看一下Java中各种类型的对象所占 ...

  7. Dubbo源码分析(1):Spring集成Dubbo

    spring与dubbo事件 类图

  8. b、B、KB、MB、GB 的关系?

    1. 8bit (位) = 1Byte (字节) 2.1024Byte (字节 ) = 1KB 3.1024KB = 1MB 4.1024MB = 1GB 5.1024GB = 1TB

  9. c语言冒泡排序算法

    案例一: #include <stdio.h> int main(void){ int a[5]; printf("please input sort number:" ...

  10. 洛谷 P1083 借教室 题解

    P1083 借教室 题目描述 在大学期间,经常需要租借教室.大到院系举办活动,小到学习小组自习讨论,都需要向学校申请借教室.教室的大小功能不同,借教室人的身份不同,借教室的手续也不一样. 面对海量租借 ...