lstm預(yù)測測試版本

#--coding:utf-8--
#!/bin/env python
#auth:kailkaka
#data:2019-3-3
#############################
import tensorflow as tf
from tensorflow import keras
import numpy as np
import warnings
import pandas as pd
from sklearn import preprocessing
from sklearn.preprocessing import StandardScaler
import matplotlib
matplotlib.use('qt4agg')
#import matplotlib.pyplot as plt
#plt.switch_backend('agg')

創(chuàng)新互聯(lián)專注于企業(yè)營銷型網(wǎng)站、網(wǎng)站重做改版、龍鳳網(wǎng)站定制設(shè)計(jì)、自適應(yīng)品牌網(wǎng)站建設(shè)、H5響應(yīng)式網(wǎng)站商城網(wǎng)站開發(fā)、集團(tuán)公司官網(wǎng)建設(shè)、成都外貿(mào)網(wǎng)站建設(shè)公司、高端網(wǎng)站制作、響應(yīng)式網(wǎng)頁設(shè)計(jì)等建站業(yè)務(wù),價格優(yōu)惠性價比高,為龍鳳等各大城市提供網(wǎng)站開發(fā)制作服務(wù)。

matplotlib.rcParams['font.sans-serif'] = ['SimHei']
matplotlib.rcParams['font.family']='sans-serif'
matplotlib.rcParams['axes.unicode_minus'] = False
import sys
try:
parameter=sys.argv[1]
except:
print "Please Enter parameter..."
exit(1)
warnings.filterwarnings("ignore")
reload(sys)
sys.setdefaultencoding('UTF-8')
class ZcSummary:
def read_csv(self):
with open('config', 'r') as fileline:
list1 = fileline.readlines()
for i in range(0, len(list1)):
if list1[i] !='\n':
list1[i] = list1[i].rstrip('\n')
if list1[i].rstrip(':') == parameter:
print "-----",i
break
try:
datafile=list1[i+1].split("=")[1].replace('\'','').rstrip('\n')
attribute=list1[i+2].split("=")[1]
tag=list1[i+3].split("=")[1]
except:
print "Please check parameter...,system is not support"
exit(2)
v_dataframe = pd.read_csv(datafile,encoding='UTF-8')
#v_dataframe = v_dataframe.reindex(np.random.permutation(v_dataframe.index))
return v_dataframe,attribute,tag
def preprocess_features(self, california_housing_dataframe,attribute,tag):
df = california_housingdataframe
train
= df[0:df.shape[0]-1000]
test_ = df[df.shape[0]-1000:]
predictors = attribute.rstrip('\n').replace('\'','').replace('[','').replace(']','')
predictors=predictors.split(',')
tag=tag.rstrip('\n').replace('\'','').replace('[','').replace(']','')
Xtrain = train[predictors]
ytrain = train[tag]
Xtest = test[predictors]
ytest = test[tag]
num=len(predictors)
test_y_disorder = preprocessing.scale(y_test).reshape(-1, 1)
train_y_disorder = preprocessing.scale(y_train).reshape(-1, 1)
ss_x = preprocessing.StandardScaler()
train_x_disorder = ss_x.fit_transform(X_train)
test_x_disorder = ss_x.transform(X_test)
return train_x_disorder,train_y_disorder,test_x_disorder,test_y_disorder,num
def main(self):
california_housing_dataframe,attribute,tag = self.read_csv()
X,y,X_test,y_test,num=self.preprocess_features(california_housing_dataframe,attribute,tag)
return X,y,X_test,y_test,num
t=ZcSummary()
train_x,train_y,X_test,y_test,num=t.main()

BATCH_START = 0 # 建立 batch data 時候的 index
TIME_STEPS = 10 # backpropagation through time 的 time_steps
BATCH_SIZE = 30
INPUT_SIZE = num # sin 數(shù)據(jù)輸入 size
OUTPUT_SIZE = 1 # cos 數(shù)據(jù)輸出 size
CELL_SIZE = 10 # RNN 的 hidden unit size
LR = 0.006 # learning rate
def get_batch_boston():
global train_x, train_y,BATCH_START, TIME_STEPS
x_part1 = train_x[BATCH_START : BATCH_START+TIME_STEPSBATCH_SIZE]
y_part1 = train_y[BATCH_START : BATCH_START+TIME_STEPS
BATCH_SIZE]
#print(u'時間段=', BATCH_START, BATCH_START + TIME_STEPS BATCH_SIZE)
seq =x_part1.reshape((BATCH_SIZE, TIME_STEPS ,INPUT_SIZE))
res =y_part1.reshape((BATCH_SIZE, TIME_STEPS ,1))
BATCH_START += TIME_STEPS
#returned seq, res and xs: shape (batch, step, input)
#np.newaxis 用來增加一個維度 變?yōu)槿齻€維度,第三個維度將用來存上一批樣本的狀態(tài)
return [seq , res ]
def get_batch():
global BATCH_START, TIME_STEPS
#xs shape (50batch, 20steps)
xs = np.arange(BATCH_START, BATCH_START+TIME_STEPS
BATCH_SIZE).reshape((BATCH_SIZE, TIME_STEPS)) / (10*np.pi)
print('xs.shape=',xs.shape)
seq = np.sin(xs)
res = np.cos(xs)
BATCH_START += TIME_STEPS
#import matplotlib.pyplot as plt
#plt.plot(xs[0, :], res[0, :], 'r', xs[0, :], seq[0, :], 'b--')
#plt.show()
#print(u'增加維度前:',seq.shape)
#print( seq[:2])
#print(u'增加維度后:',seq[:, :, np.newaxis].shape)
#print(seq[:2])
#returned seq, res and xs: shape (batch, step, input)
#np.newaxis 用來增加一個維度 變?yōu)槿齻€維度,第三個維度將用來存上一批樣本的狀態(tài)
return [seq[:, :, np.newaxis], res[:, :, np.newaxis], xs]
class LSTMRNN(object):
def init(self, n_steps, input_size, output_size, cell_size, batch_size):
'''
:param n_steps: 每批數(shù)據(jù)總包含多少時間刻度
:param input_size: 輸入數(shù)據(jù)的維度
:param output_size: 輸出數(shù)據(jù)的維度 如果是類似價格曲線的話,應(yīng)該為1
:param cell_size: cell的大小
:param batch_size: 每批次訓(xùn)練數(shù)據(jù)的數(shù)量
'''
self.n_steps = n_steps
self.input_size = input_size
self.output_size = output_size
self.cell_size = cell_size
self.batch_size = batch_size
with tf.name_scope('inputs'):
self.xs = tf.placeholder(tf.float32, [None, n_steps, input_size], name='xs') #xs 有三個維度
self.ys = tf.placeholder(tf.float32, [None, n_steps, output_size], name='ys') #ys 有三個維度
with tf.variable_scope('in_hidden'):
self.add_input_layer()
with tf.variable_scope('LSTM_cell'):
self.add_cell()
with tf.variable_scope('out_hidden'):
self.add_output_layer()
with tf.name_scope('cost'):
self.compute_cost()
with tf.name_scope('train'):
self.train_op = tf.train.AdamOptimizer(LR).minimize(self.cost)
#增加一個輸入層
def add_input_layer(self,):

l_in_x:(batch*n_step, in_size),相當(dāng)于把這個批次的樣本串到一個長度1000的時間線上,每批次50個樣本,每個樣本20個時刻

    l_in_x = tf.reshape(self.xs, [-1, self.input_size], name='2_2D')  #-1 表示任意行數(shù)
    # Ws (in_size, cell_size)
    Ws_in = self._weight_variable([self.input_size, self.cell_size])
    # bs (cell_size, )
    bs_in = self._bias_variable([self.cell_size,])
    # l_in_y = (batch * n_steps, cell_size)
    with tf.name_scope('Wx_plus_b'):
        l_in_y = tf.matmul(l_in_x, Ws_in) + bs_in
    # reshape l_in_y ==> (batch, n_steps, cell_size)
    self.l_in_y = tf.reshape(l_in_y, [-1, self.n_steps, self.cell_size], name='2_3D')
#多時刻的狀態(tài)疊加層
def add_cell(self):
    lstm_cell = tf.nn.rnn_cell.BasicLSTMCell(self.cell_size, forget_bias=1.0, state_is_tuple=True)
    with tf.name_scope('initial_state'):
        self.cell_init_state = lstm_cell.zero_state(self.batch_size, dtype=tf.float32)
    #time_major=False 表示時間主線不是第一列batch
    self.cell_outputs, self.cell_final_state = tf.nn.dynamic_rnn(
        lstm_cell, self.l_in_y, initial_state=self.cell_init_state, time_major=False)
# 增加一個輸出層
def add_output_layer(self):
    # shape = (batch * steps, cell_size)
    l_out_x = tf.reshape(self.cell_outputs, [-1, self.cell_size], name='2_2D')
    Ws_out = self._weight_variable([self.cell_size, self.output_size])
    bs_out = self._bias_variable([self.output_size, ])
    # shape = (batch * steps, output_size)
    with tf.name_scope('Wx_plus_b'):
        self.pred = tf.matmul(l_out_x, Ws_out) + bs_out #預(yù)測結(jié)果
def compute_cost(self):
    losses =  tf.contrib.legacy_seq2seq.sequence_loss_by_example(
        [tf.reshape(self.pred, [-1], name='reshape_pred')],
        [tf.reshape(self.ys, [-1], name='reshape_target')],
        [tf.ones([self.batch_size * self.n_steps], dtype=tf.float32)],
        average_across_timesteps=True,
        softmax_loss_function=self.ms_error,
        name='losses'
    )
    with tf.name_scope('average_cost'):
        self.cost = tf.div(
            tf.reduce_sum(losses, name='losses_sum'),
            self.batch_size,
            name='average_cost')
        tf.summary.scalar('cost', self.cost)
def ms_error(self, labels, logits): #參數(shù)可能是因?yàn)?tf.contrib.legacy_seq2seq.sequence_loss_by_example參數(shù)的
    return tf.square(tf.subtract(labels,logits))
def _weight_variable(self, shape, name='weights'):
    initializer = tf.random_normal_initializer(mean=0., stddev=1.,)
    return tf.get_variable(shape=shape, initializer=initializer, name=name)
def _bias_variable(self, shape, name='biases'):
    initializer = tf.constant_initializer(0.1)
    return tf.get_variable(name=name, shape=shape, initializer=initializer)

if name== 'main':
seq, res = get_batch_boston()
model = LSTMRNN(TIME_STEPS, INPUT_SIZE, OUTPUT_SIZE, CELL_SIZE, BATCH_SIZE)
sess = tf.Session()
merged = tf.summary.merge_all()
writer = tf.summary.FileWriter("houseprice", sess.graph)

tf.initialize_all_variables() no long valid from

# 2017-03-02 if using tensorflow >= 0.12
sess.run(tf.global_variables_initializer())
# relocate to the local dir and run this line to view it on Chrome (http://0.0.0.0:6006/):
# $ tensorboard --logdir='logs'
for j in range(1000):#訓(xùn)練200次
    pred_res=None
    for i in range(20):#把整個數(shù)據(jù)分為20個時間段
        seq, res = get_batch_boston()
        if i == 0:
            feed_dict = {
                    model.xs: seq,
                    model.ys: res,
                    # create initial state
            }
        else:
            feed_dict = {
                model.xs: seq,
                model.ys: res,
                model.cell_init_state: state    # use last state as the initial state for this run
            }
        _, cost, state, pred = sess.run(
            [model.train_op, model.cost, model.cell_final_state, model.pred],
            feed_dict=feed_dict)
        pred_res=pred
        result = sess.run(merged, feed_dict)
        writer.add_summary(result, i)
    print('{0} loss= '.format(j ), round(cost, 4))
    BATCH_START=0 #從頭再來一遍
# 畫圖
#print(u"結(jié)果:",pred_res.shape)
#與最后一次訓(xùn)練所用的數(shù)據(jù)保持一致
train_y = train_y[190:490]
#print(u'實(shí)際',train_y.flatten().shape)
r_size=BATCH_SIZE * TIME_STEPS
###畫圖###########################################################################
import matplotlib.pyplot as plt
fig = plt.figure(figsize=(20, 3))  # dpi參數(shù)指定繪圖對象的分辨率,即每英寸多少個像素,缺省值為80
axes = fig.add_subplot(1, 1, 1)
#為了方便看,只顯示了后100行數(shù)據(jù)
line1,=axes.plot(range(100), pred.flatten()[-100:] , 'b--',label='rnn計(jì)算結(jié)果')
#line2,=axes.plot(range(len(gbr_pridict)), gbr_pridict, 'r--',label='優(yōu)選參數(shù)')
line3,=axes.plot(range(100), train_y.flatten()[ - 100:], 'r',label='實(shí)際')
axes.grid()
fig.tight_layout()
#plt.legend(handles=[line1, line2,line3])
plt.legend(handles=[line1,  line3])
plt.title(u'遞歸神經(jīng)網(wǎng)絡(luò)')
plt.savefig('houseprice.png')
plt.show()

標(biāo)題名稱:lstm預(yù)測測試版本
轉(zhuǎn)載源于:http://muchs.cn/article30/gphopo.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供云服務(wù)器、網(wǎng)站策劃定制網(wǎng)站、App開發(fā)、企業(yè)網(wǎng)站制作、全網(wǎng)營銷推廣

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

網(wǎng)站優(yōu)化排名