tensorflow 波士顿房价预测LSTM/RNN tensorboard 完整代码

在tensorflow里RNN才是做回归计算的正规军,其中LSTM更是让人工智能有了记忆,如果cnn最适合做的是图像识别,那么LSTM就是视频识别。网上的教程多是用正余弦数据在做预测,输入输出都是一维,我这用波士顿房价,输入是13个特征!

注意与前面两个模型不同的是,没有用train_test_split把训练数据分割,而是用的时序数据。

代码中注释比较少,不明白的可以看周莫烦的视频!

https://morvanzhou.github.io/tutorials/machine-learning/tensorflow/5-09-RNN3/

  1. # 参考https://morvanzhou.github.io/tutorials/machine-learning/tensorflow/5-09-RNN3/
  2. from sklearn.datasets import load_boston
  3. from sklearn import preprocessing
  4. import tensorflow as tf
  5. import numpy as np
  6. # 波士顿房价数据
  7. boston = load_boston()
  8. x = boston.data
  9. y = boston.target
  10. print( '波士顿数据X:',x.shape) # (506, 13)
  11. # print(x[::100])
  12. print( '波士顿房价Y:',y.shape)
  13. # print(y[::100])
  14. # 数据标准化
  15. ss_x = preprocessing.StandardScaler()
  16. train_x = ss_x.fit_transform(x)
  17. ss_y = preprocessing.StandardScaler()
  18. train_y = ss_y.fit_transform(y.reshape( -1, 1))
  19. BATCH_START = 0 # 建立 batch data 时候的 index
  20. TIME_STEPS = 10 # backpropagation through time 的 time_steps
  21. BATCH_SIZE = 30
  22. INPUT_SIZE = 13 # sin 数据输入 size
  23. OUTPUT_SIZE = 1 # cos 数据输出 size
  24. CELL_SIZE = 10 # RNN 的 hidden unit size
  25. LR = 0.006 # learning rate
  26. def get_batch_boston():
  27. global train_x, train_y,BATCH_START, TIME_STEPS
  28. x_part1 = train_x[BATCH_START : BATCH_START+TIME_STEPS*BATCH_SIZE]
  29. y_part1 = train_y[BATCH_START : BATCH_START+TIME_STEPS*BATCH_SIZE]
  30. print( '时间段=', BATCH_START, BATCH_START + TIME_STEPS * BATCH_SIZE)
  31. seq =x_part1.reshape((BATCH_SIZE, TIME_STEPS ,INPUT_SIZE))
  32. res =y_part1.reshape((BATCH_SIZE, TIME_STEPS , 1))
  33. BATCH_START += TIME_STEPS
  34. # returned seq, res and xs: shape (batch, step, input)
  35. #np.newaxis 用来增加一个维度 变为三个维度,第三个维度将用来存上一批样本的状态
  36. return [seq , res ]
  37. def get_batch():
  38. global BATCH_START, TIME_STEPS
  39. # xs shape (50batch, 20steps)
  40. xs = np.arange(BATCH_START, BATCH_START+TIME_STEPS*BATCH_SIZE).reshape((BATCH_SIZE, TIME_STEPS)) / ( 10*np.pi)
  41. print( 'xs.shape=',xs.shape)
  42. seq = np.sin(xs)
  43. res = np.cos(xs)
  44. BATCH_START += TIME_STEPS
  45. # import matplotlib.pyplot as plt
  46. # plt.plot(xs[0, :], res[0, :], 'r', xs[0, :], seq[0, :], 'b--')
  47. # plt.show()
  48. print( '增加维度前:',seq.shape)
  49. print( seq[: 2])
  50. print( '增加维度后:',seq[:, :, np.newaxis].shape)
  51. print(seq[: 2])
  52. # returned seq, res and xs: shape (batch, step, input)
  53. #np.newaxis 用来增加一个维度 变为三个维度,第三个维度将用来存上一批样本的状态
  54. return [seq[:, :, np.newaxis], res[:, :, np.newaxis], xs]
  55. class LSTMRNN(object):
  56. def __init__(self, n_steps, input_size, output_size, cell_size, batch_size):
  57. '''
  58. :param n_steps: 每批数据总包含多少时间刻度
  59. :param input_size: 输入数据的维度
  60. :param output_size: 输出数据的维度 如果是类似价格曲线的话,应该为1
  61. :param cell_size: cell的大小
  62. :param batch_size: 每批次训练数据的数量
  63. '''
  64. self.n_steps = n_steps
  65. self.input_size = input_size
  66. self.output_size = output_size
  67. self.cell_size = cell_size
  68. self.batch_size = batch_size
  69. with tf.name_scope( 'inputs'):
  70. self.xs = tf.placeholder(tf.float32, [ None, n_steps, input_size], name= 'xs') #xs 有三个维度
  71. self.ys = tf.placeholder(tf.float32, [ None, n_steps, output_size], name= 'ys') #ys 有三个维度
  72. with tf.variable_scope( 'in_hidden'):
  73. self.add_input_layer()
  74. with tf.variable_scope( 'LSTM_cell'):
  75. self.add_cell()
  76. with tf.variable_scope( 'out_hidden'):
  77. self.add_output_layer()
  78. with tf.name_scope( 'cost'):
  79. self.compute_cost()
  80. with tf.name_scope( 'train'):
  81. self.train_op = tf.train.AdamOptimizer(LR).minimize(self.cost)
  82. #增加一个输入层
  83. def add_input_layer(self,):
  84. # l_in_x:(batch*n_step, in_size),相当于把这个批次的样本串到一个长度1000的时间线上,每批次50个样本,每个样本20个时刻
  85. l_in_x = tf.reshape(self.xs, [ -1, self.input_size], name= '2_2D') #-1 表示任意行数
  86. # Ws (in_size, cell_size)
  87. Ws_in = self._weight_variable([self.input_size, self.cell_size])
  88. # bs (cell_size, )
  89. bs_in = self._bias_variable([self.cell_size,])
  90. # l_in_y = (batch * n_steps, cell_size)
  91. with tf.name_scope( 'Wx_plus_b'):
  92. l_in_y = tf.matmul(l_in_x, Ws_in) + bs_in
  93. # reshape l_in_y ==> (batch, n_steps, cell_size)
  94. self.l_in_y = tf.reshape(l_in_y, [ -1, self.n_steps, self.cell_size], name= '2_3D')
  95. #多时刻的状态叠加层
  96. def add_cell(self):
  97. lstm_cell = tf.nn.rnn_cell.BasicLSTMCell(self.cell_size, forget_bias= 1.0, state_is_tuple= True)
  98. with tf.name_scope( 'initial_state'):
  99. self.cell_init_state = lstm_cell.zero_state(self.batch_size, dtype=tf.float32)
  100. #time_major=False 表示时间主线不是第一列batch
  101. self.cell_outputs, self.cell_final_state = tf.nn.dynamic_rnn(
  102. lstm_cell, self.l_in_y, initial_state=self.cell_init_state, time_major= False)
  103. # 增加一个输出层
  104. def add_output_layer(self):
  105. # shape = (batch * steps, cell_size)
  106. l_out_x = tf.reshape(self.cell_outputs, [ -1, self.cell_size], name= '2_2D')
  107. Ws_out = self._weight_variable([self.cell_size, self.output_size])
  108. bs_out = self._bias_variable([self.output_size, ])
  109. # shape = (batch * steps, output_size)
  110. with tf.name_scope( 'Wx_plus_b'):
  111. self.pred = tf.matmul(l_out_x, Ws_out) + bs_out #预测结果
  112. def compute_cost(self):
  113. losses = tf.nn.seq2seq.sequence_loss_by_example(
  114. [tf.reshape(self.pred, [ -1], name= 'reshape_pred')],
  115. [tf.reshape(self.ys, [ -1], name= 'reshape_target')],
  116. [tf.ones([self.batch_size * self.n_steps], dtype=tf.float32)],
  117. average_across_timesteps= True,
  118. softmax_loss_function=self.ms_error,
  119. name= 'losses'
  120. )
  121. with tf.name_scope( 'average_cost'):
  122. self.cost = tf.div(
  123. tf.reduce_sum(losses, name= 'losses_sum'),
  124. self.batch_size,
  125. name= 'average_cost')
  126. tf.scalar_summary( 'cost', self.cost)
  127. def ms_error(self, y_pre, y_target):
  128. return tf.square(tf.sub(y_pre, y_target))
  129. def _weight_variable(self, shape, name='weights'):
  130. initializer = tf.random_normal_initializer(mean= 0., stddev= 1.,)
  131. return tf.get_variable(shape=shape, initializer=initializer, name=name)
  132. def _bias_variable(self, shape, name='biases'):
  133. initializer = tf.constant_initializer( 0.1)
  134. return tf.get_variable(name=name, shape=shape, initializer=initializer)
  135. if __name__ == '__main__':
  136. seq, res = get_batch_boston()
  137. model = LSTMRNN(TIME_STEPS, INPUT_SIZE, OUTPUT_SIZE, CELL_SIZE, BATCH_SIZE)
  138. sess = tf.Session()
  139. merged = tf.merge_all_summaries()
  140. writer = tf.train.SummaryWriter( "logs", sess.graph)
  141. # tf.initialize_all_variables() no long valid from
  142. # 2017-03-02 if using tensorflow >= 0.12
  143. sess.run(tf.global_variables_initializer())
  144. # relocate to the local dir and run this line to view it on Chrome (http://0.0.0.0:6006/):
  145. # $ tensorboard --logdir='logs'
  146. for j in range( 200): #训练200次
  147. pred_res= None
  148. for i in range( 20): #把整个数据分为20个时间段
  149. seq, res = get_batch_boston()
  150. if i == 0:
  151. feed_dict = {
  152. model.xs: seq,
  153. model.ys: res,
  154. # create initial state
  155. }
  156. else:
  157. feed_dict = {
  158. model.xs: seq,
  159. model.ys: res,
  160. model.cell_init_state: state # use last state as the initial state for this run
  161. }
  162. _, cost, state, pred = sess.run(
  163. [model.train_op, model.cost, model.cell_final_state, model.pred],
  164. feed_dict=feed_dict)
  165. pred_res=pred
  166. result = sess.run(merged, feed_dict)
  167. writer.add_summary(result, i)
  168. print( '{0} cost: '.format(j ), round(cost, 4))
  169. BATCH_START= 0 #从头再来一遍
  170. # 画图
  171. print( "结果:",pred_res.shape)
  172. #与最后一次训练所用的数据保持一致
  173. train_y = train_y[ 190: 490]
  174. print( '实际',train_y.flatten().shape)
  175. r_size=BATCH_SIZE * TIME_STEPS
  176. ###画图###########################################################################
  177. import matplotlib.pyplot as plt
  178. fig = plt.figure(figsize=( 20, 3)) # dpi参数指定绘图对象的分辨率,即每英寸多少个像素,缺省值为80
  179. axes = fig.add_subplot( 1, 1, 1)
  180. #为了方便看,只显示了后100行数据
  181. line1,=axes.plot(range( 100), pred.flatten()[ -100:] , 'b--',label= 'rnn计算结果')
  182. #line2,=axes.plot(range(len(gbr_pridict)), gbr_pridict, 'r--',label='优选参数')
  183. line3,=axes.plot(range( 100), train_y.flatten()[ - 100:], 'r',label= '实际')
  184. axes.grid()
  185. fig.tight_layout()
  186. #plt.legend(handles=[line1, line2,line3])
  187. plt.legend(handles=[line1, line3])
  188. plt.title( '递归神经网络')
  189. plt.show()

本文原址:http://blog.csdn.net/baixiaozhe/article/details/54410313
lstm输入和输出都是时序数据,是尊重时间的,和上两篇用的交叉数据集是不一样的,所以 结果是这样的:

猜你喜欢

转载自blog.csdn.net/liangjiubujiu/article/details/80987054