LSTM实现对sin函数的预测

 LSTM实现对sin函数的预测

import tensorflow as tf
import numpy as np
from matplotlib import pyplot as plt
hidden_size=30
num_layer=2
time_stept=10
training_stept=10000
batch_size=32
train_example=10000
test_example=1000
sample_gap=0.01
def generate_data(seq):#对数据进行处理
           X=[]
           y=[]
           for i in range(len(seq-time_stept)-time_stept):
                      X.append([seq[i:i+time_stept]])
                      y.append([seq[i+time_stept]])
           return np.array(X),np.array(y)
def lstm_model(X,y,is_training):
           cell=tf.nn.rnn_cell.MultiRNNCell([tf.nn.rnn_cell.BasicLSTMCell(hidden_size) for _ in range(num_layer)])
           outputs,_=tf.nn.dynamic_rnn(cell,X,dtype=tf.float32)
           output=outputs[:,-1,:]
           predictions=tf.contrib.layers.fully_connected(output,1,activation_fn=None)
           if not is_training:
                      return predictions,None,None
           loss=tf.losses.mean_squared_error(labels=y,predictions=predictions)
           train_op=tf.contrib.layers.optimize_loss(loss,tf.train.get_global_step(),optimizer='Adagrad',learning_rate=0.1)
           return predictions,loss,train_op
def train(sess,train_X,train_y):
           ds=tf.data.Dataset.from_tensor_slices((train_X,train_y))
           ds=ds.repeat().shuffle(1000).batch(batch_size)
           X,y=ds.make_one_shot_iterator().get_next()
           with tf.variable_scope('model'):
                      predictions,loss,train_op=lstm_model(X,y,True)
           sess.run(tf.global_variables_initializer())
           for i in range(training_stept):
                      _,l=sess.run([train_op,loss])
                      if i%100==0:
                                 print(i,  l)

def test(sess,test_X,test_y):
           ds=tf.data.Dataset.from_tensor_slices((test_X,test_y))
           ds=ds.batch(1)
           X,y=ds.make_one_shot_iterator().get_next()
           with tf.variable_scope('model',reuse=True):
                      prediction,_,_=lstm_model(X,[0.0],False)
                      predictions=[]
                      lab=[]
                      for i in range(test_example):
                             p,m=sess.run([prediction,y])
                             predictions.append(p)
                             lab.append(m)
                      predictions=np.array(predictions).squeeze()
                      lab=np.array(lab).squeeze()
                      rmse=np.sqrt(((predictions-lab)**2).mean(axis=0))
                      print('error:', rmse)
           plt.figure()
           plt.plot(lab,label='real_sin')
           plt.plot(predictions,label='prediction')
           
           plt.legend()
           plt.show()



test_start=(train_example+time_stept)*sample_gap
test_end=test_start+(train_example+time_stept)*sample_gap
train_X,train_y=generate_data(np.sin(np.linspace(-test_start,test_start,train_example+time_stept,dtype=np.float32)))
test_X,test_y=generate_data(np.sin(np.linspace(test_start,test_end,train_example+time_stept,dtype=np.float32)))
with tf.Session() as sess:
           train(sess,train_X,train_y)
           test(sess,test_X,test_y)

运行结果图:

LSTM实现对sin函数的预测_第1张图片

对运行结果截取部分图:

LSTM实现对sin函数的预测_第2张图片

 

你可能感兴趣的:(算法)