问题:1:构成的网络结构?
2:输入数据大小?
https://yq.aliyun.com/articles/118726?spm=5176.100239.blogcont122848.14.zwhUbk
import tensorflow as tf import pandas as pd import numpy as np import os import matplotlib import matplotlib.pyplot as plt import random #matplotlib inline import tensorflow as tf import shutil import tensorflow.contrib.learn as tflearn import tensorflow.contrib.layers as tflayers from tensorflow.contrib.learn.python.learn import learn_runner import tensorflow.contrib.metrics as metrics import tensorflow.contrib.rnn as rnn random.seed(111) rng=pd.date_range(start='2000',periods=209,freq='M') """pd.date_range 函数:生成日期范围 start:开始日期 periods:日期范围内日期的个数,periods*freq 等于日期范围的长度 freq:每多少天或其他明确时间频率的方式,默认为D,即1天 M为一个月 """ ts=pd.Series(np.random.uniform(-10,10,size=len(rng)).cumsum()) ts.plot(c='b',title='Example Time Series') #颜色蓝色 plt.show() ts.head(10) #使用head查看前几行数据(默认是前5行),不过你可以指定前几行 TS=np.array(ts) num_periods=20 #观测值 f_horizon=1 #forecast horizon 预测一步 x_data=TS[:(len(TS)-len(TS)%num_periods)] #len(TS)=209 x_batches=x_data.reshape(-1,20,1) y_data=TS[1:(len(TS)-(len(TS)%num_periods))+f_horizon] y_batches=y_data.reshape(-1,20,1) print("x_data shape%s"%len(TS)) print("x_batchesis%s"%len(x_batches)) #10 print(x_batches.shape) # (10, 20, 1) print("x_batches[0:2]is %s"%x_batches[0:2]) print("y_batches[0:1]is%s"%y_batches[0:1]) print(y_batches.shape) #(10, 20, 1) ##??不懂 测试数据是怎么取的 为什么 def test_data(series,forecast,num_periods): test_x_setup=TS[-(num_periods+forecast):] testX=test_x_setup[:num_periods].reshape(-1,20,1) testY=TS[-(num_periods):].reshape(-1,20,1) return testX,testY X_test,Y_test=test_data(TS,f_horizon,num_periods) print(X_test.shape) print(X_test) tf.reset_default_graph() #清除默认图的堆栈,并设置全局图为默认图 num_periods=20 inputs=1 hidden=100 output=1 X=tf.placeholder(tf.float32,[None,num_periods,inputs]) y=tf.placeholder(tf.float32,[None,num_periods,output]) basic_cell=tf.contrib.rnn.BasicRNNCell(num_units=hidden,activation=tf.nn.relu) rnn_output,states=tf.nn.dynamic_rnn(basic_cell,X,dtype=tf.float32) learning_rate=0.001 "tf.layers.dense:全连接层 操作 outputs = activation(inputs.kernel + bias) " stacked_rnn_output=tf.reshape(rnn_output,[-1,hidden]) #转为二维数据 stacked_outputs=tf.layers.dense(stacked_rnn_output,output) outputs=tf.reshape(stacked_outputs,[-1,num_periods,output]) #? loss=tf.reduce_sum(tf.square(outputs-y)) optimizer=tf.train.AdamOptimizer(learning_rate=learning_rate) training_op=optimizer.minimize(loss) init=tf.global_variables_initializer() #################################### epochs=1000 with tf.Session() as sess: init.run() for ep in range(epochs): sess.run(training_op,feed_dict={X:x_batches,y:y_batches}) if ep %100 ==0: mse=loss.eval(feed_dict={X:x_batches,y:y_batches}) print(ep,"\TMSE:",mse) y_pred=sess.run(outputs,feed_dict={X:X_test}) print(y_pred) plt.title("Forecast vs Actual",fontsize=14) plt.plot(pd.Series(np.ravel(Y_test)),"bo",markersize=10,label="Actual") #plt.plot(pd.Series(np.ravel(Y_test)),"w*",markersize=10) plt.plot(pd.Series(np.ravel(y_pred)),"r.",markersize=10,label="Forecast") plt.legend(loc="upper left") plt.xlabel("Time Periods") plt.show()