大規(guī)模時間序列的分析預(yù)測與實踐_第1頁
大規(guī)模時間序列的分析預(yù)測與實踐_第2頁
大規(guī)模時間序列的分析預(yù)測與實踐_第3頁
大規(guī)模時間序列的分析預(yù)測與實踐_第4頁
大規(guī)模時間序列的分析預(yù)測與實踐_第5頁
已閱讀5頁,還剩79頁未讀, 繼續(xù)免費閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報或認(rèn)領(lǐng)

文檔簡介

1、技術(shù)創(chuàng)新,引領(lǐng)變革規(guī)模時間序列的分析、預(yù)測與實踐理論篇背景介紹探索性數(shù)據(jù)分析 經(jīng)典時序預(yù)測法神經(jīng)絡(luò)時序預(yù)測法蘇寧時序預(yù)測與異常檢測平臺Tensorflow基礎(chǔ) 實踐環(huán)境傳統(tǒng)時序分析 DeepAR算法全景實現(xiàn) Demo演實踐篇主要內(nèi)容理論篇理解什么是時序數(shù)據(jù)分析。理解常用的探索性數(shù)據(jù)分析方法。經(jīng)典時序預(yù)測方法(指數(shù)平滑、線性回歸法、概率預(yù)測模型等)神經(jīng)網(wǎng)絡(luò)時序預(yù)測方法(DeepAR、MQCNN、MQRNN和集成學(xué)習(xí)等)蘇寧時序預(yù)測與異常檢測平臺的能力、架構(gòu)和產(chǎn)品介紹。培訓(xùn)目標(biāo)主要內(nèi)容理論篇背景介紹探索性數(shù)據(jù)分析 經(jīng)典時序預(yù)測法神經(jīng)絡(luò)時序預(yù)測法蘇寧時序預(yù)測與異常檢測平臺Tensorflow基礎(chǔ) 實

2、踐環(huán)境傳統(tǒng)時序分析 DeepAR算法全景實現(xiàn) Demo演實踐篇時間序列(time series)是一組按照時間發(fā)生先后順序進(jìn)行排列的數(shù)據(jù)點序列。通常一組時間序列的時間間隔為一恒定值(如1秒, 5分鐘,12小時,7天,1年),因此時間序列可以作為離散時間數(shù)據(jù)進(jìn)行分析處理。時間序列廣泛應(yīng)用于數(shù)理統(tǒng)計、信號處理、 模式識別、計量經(jīng)濟學(xué)、數(shù)學(xué)金融、天氣預(yù)報、地震預(yù)測、腦電圖、控制工程、航空學(xué)、通信工程以及絕大多數(shù)涉及到時間數(shù)據(jù)測量的應(yīng)用科學(xué)與工程學(xué)。-摘自維基百科Prediction is very difficult, especially about the future. NielsBohr理論

3、篇-背景介紹平穩(wěn)性(nonstationarity,不平穩(wěn)性,穩(wěn)定性): 即時間序列的方差無法呈現(xiàn)出一個長期趨勢并最終趨于一個常數(shù)或是一個線 性函數(shù)。波動幅度隨時間變化(Timevarying Volatility): 即一個時間序列變量的方差隨時間的變化而變化。業(yè)務(wù)背景(Operational 預(yù)測)參考文獻(xiàn): Tim Januschowski. (2017). Forecasting at Amazon Problems, Methods and Systems理論篇-背景介紹時序預(yù)測用來回答這樣的問題:有多少個?多少收入?什么時候會發(fā)生? 這種行為正常嗎?基于過去的歷史數(shù)據(jù),預(yù)測未來的可

4、能需求。通過考慮諸如人口、天氣等外部因素來預(yù)測未來的多面方法。參考文獻(xiàn): https:/demand-forecasting-solution-offering業(yè)務(wù)背景(需求預(yù)測)理論篇-背景介紹理論篇背景介紹探索性數(shù)據(jù)分析 經(jīng)典時序預(yù)測法神經(jīng)絡(luò)時序預(yù)測法蘇寧時序預(yù)測與異常檢測平臺Tensorflow基礎(chǔ) 實踐環(huán)境傳統(tǒng)時序分析 DeepAR算法全景實現(xiàn) Demo演實踐篇主要內(nèi)容探索性數(shù)據(jù)分析(Exploratory Data Analysis, EDA)通過系列段(如可視化、假設(shè)檢驗等)形 成數(shù)據(jù)的整體概貌,為后續(xù)選擇算法進(jìn)建模預(yù)測提供參考。主要的EDA段包括:數(shù)據(jù)可視化( run seque

5、nce plot、distplot、boxplot)正態(tài)性檢驗( jarque-bera檢驗、Shapiro-Wilk檢驗)平穩(wěn)性檢驗( adfuller 檢驗)時間序列分解(seasonal decompose)(trend + seasonality + residual)周期性分析突變點檢測理論篇-探索性數(shù)據(jù)分析DistplotBox PlotRun Sequence Plot理論篇-探索性數(shù)據(jù)分析業(yè)務(wù)背景(需求預(yù)測)檢驗結(jié)果Window表示滑動平均的窗口大小,后面的值代表adf平穩(wěn)性的p值。P值越小,代表穩(wěn)定性越好。一般當(dāng)p Tensor(Add:0, shape=(), dtype=

6、int32)(Not 8)創(chuàng)建1個會話,給會話定義1個名字,便于后續(xù)調(diào)在會話中運計算圖,得到輸出結(jié)果import tensorflow as tf a = tf.add(3, 5)sess = tf.Session()with tf.Session() as sess:print(sess.run(a) sess.close() 8會話import tensorflow as tf a = tf.add(3, 5)print(a)實踐篇-TensorFlow基礎(chǔ)常量存儲在計算圖中當(dāng)常量很大時計算圖的加載將很慢tf.constant( value,dtype=None, shape=None,

7、name=Const, verify_shape=False )tf.zeros(shape, dtype=tf.float32, name=None)tf.zeros_like(input_tensor, dtype=None, name=None, optimize=True)tf.ones(shape, dtype=tf.float32, name=None)import tensorflow as tfa = tf.constant(2, 2, name=a)b = tf.constant(0, 1, 2, 3, name=b)常量、操作符實踐篇-TensorFlow基礎(chǔ)Initial

8、izer是一種運算,需要在session中執(zhí)行with tf.Session() as sess:sess.run(tf.global_variables_initializer()變量# create variables with tf.Variable s = tf.Variable(2, name=scalar)m = tf.Variable(0, 1, 2, 3, name=matrix)W = tf.Variable(tf.zeros(784,10)# create variables with tf.get_variables = tf.get_variable(scalar, i

9、nitializer=tf.constant(2)m = tf.get_variable(matrix, initializer=tf.constant(0, 1, 2, 3)W = tf.get_variable(big_matrix, shape=(784, 10), initializer=tf.zeros_initializer()with tf.Session() as sess:print(sess.run(W) FailedPreconditionError: Attempting to use uninitialized value Variable變量在運行前需要初始化運算最

10、簡單的方式是對所有變量一次性進(jìn)行初始化實踐篇-TensorFlow基礎(chǔ)定義計算圖時不需要知道計算的數(shù)據(jù)f(x, y) = 2 * x + y,x, y是實際值的placeholders在執(zhí)行計算時,把實際的值提供給placeholderstf.placeholder(dtype, shape=None, name=None)# create a placeholder for a vector of 3 elements, type tf.float32a = tf.placeholder(tf.float32, shape=3)b = tf.constant(5, 5, 5, tf.floa

11、t32)# use the placeholder as you would a constant or a variablec = a + b # short for tf.add(a, b)with tf.Session() as sess:print(sess.run(c, feed_dict=a: 1, 2, 3)# the tensor a is the key, not the string a # 6, 7, 8容易使用,但性能較低實踐篇-TensorFlow基礎(chǔ)PlaceholderSession對loss依賴的所有trainable變量進(jìn)更新tf.train.Gradient

12、DescentOptimizer tf.train.AdagradOptimizer tf.train.MomentumOptimizer tf.train.AdamOptimizer tf.train.FtrlOptimizer tf.train.RMSPropOptimizer.Optimizeroptimizer = tf.train.GradientDescentOptimizer(learning_rate=0.001).minimize(loss)_, l = sess.run(optimizer, loss, feed_dict=X: x, Y:y)實踐篇-TensorFlow基

13、礎(chǔ)計算圖定義好后,會話運前創(chuàng)建summary writerGo to terminal, run:$ python3 yourprogram.py$ tensorboard -logdir=./graphs -port 6006Then open your browser and go to: http:/localhost:6006/b = tf.constant(3) x = tf.add(a, b)writer = tf.summary.FileWriter(./graphs, tf.get_default_graph()with tf.Session() as sess:# write

14、r = tf.summary.FileWriter(./graphs, sess.graph)print(sess.run(x)writer.close() # close the writer when youre done using itTensorBoardimport tensorflow as tf a = tf.constant(2)實踐篇-TensorFlow基礎(chǔ)使TensorFlow進(jìn)線性回歸Phase 1: 定義計算圖Step 1: 讀取數(shù)據(jù)Step 2: 為輸入數(shù)據(jù)和標(biāo)注創(chuàng)建placeholders Step 3: 創(chuàng)建weight and biasStep 4: Inf

15、erenceY_predicted = w * X + bStep 5: 定義 loss functionloss = tf.square(Y - Y_predicted, name=loss)實踐篇-TensorFlow基礎(chǔ)X: 出生率 Y: 平均壽命 190 個國家Inference: Y_predicted = w * X + bMean squared error: E(y - y_predicted)2目標(biāo):找到X和Y的線性關(guān)系,用X來預(yù)測Y實踐篇-TensorFlow基礎(chǔ)使TensorFlow進(jìn)線性回歸Step 6: 創(chuàng)建 optimizeropt = tf.train.Gradi

16、entDescentOptimizer(learning_rate=0.001) optimizer = opt.minimize(loss)Phase 2: 訓(xùn)練模型Step 1: 初始化變量 Step 2: 運行 optimizer(use a feed_dict to feed data into X and Y placeholders)實踐篇-TensorFlow基礎(chǔ)使TensorFlow進(jìn)線性回歸理論篇背景介紹探索性數(shù)據(jù)分析 經(jīng)典時序預(yù)測法神經(jīng)絡(luò)時序預(yù)測法蘇寧時序預(yù)測與異常檢測平臺Tensorflow基礎(chǔ) 實踐環(huán)境傳統(tǒng)時序分析 DeepAR算法全景實現(xiàn) Demo演實踐篇主要內(nèi)容實踐

17、篇-實踐環(huán)境AnacondaAnaconda is an open-source Python distribution that is particularly popular for data analysis and scientific computingAvailable for Windows, Mac OS X and LinuxManage libraries, dependencies, and environments with CondaIncludes many popular packages: NumPy, SciPy, Matplotlib, Pandas, IP

18、ython, Cython1、創(chuàng)建新環(huán)境:2、激活環(huán)境conda create -name Linux 或 macOS:source activate Windows:activate 在指定環(huán)境中安裝包:conda install -name 在當(dāng)前環(huán)境中安裝包:conda install 3、安裝包理論篇背景介紹探索性數(shù)據(jù)分析 經(jīng)典時序預(yù)測法神經(jīng)絡(luò)時序預(yù)測法蘇寧時序預(yù)測與異常檢測平臺Tensorflow基礎(chǔ) 實踐環(huán)境傳統(tǒng)時序分析 DeepAR算法全景實現(xiàn) Demo演實踐篇主要內(nèi)容ipi1i2i pz 1 (zz . z)實踐篇-傳統(tǒng)時序分析移動平均法1、讀取數(shù)據(jù)df=pd.read_csv

19、(data_path) # 讀取csv數(shù)據(jù)# 將時間字符串轉(zhuǎn)換為datetime,格式為%Y-%m-%d %H:%M:%S dftime=pd.to_datetime(dftime,format=%Y-%m-%d %H:%M:%S)df.set_index(time, inplace=True) # 設(shè)置time列為index train_len=int(df.shape0*0.8)train_df=df:train_len # 切分為訓(xùn)練集 test_df=dftrain_len: # 切分為測試集2、移動平均y_hat_avg = test_df.copy() # 復(fù)制測試集 # 移動平均

20、預(yù)測,滑動窗口長度為100y_hat_avgmoving_avg_forecast = train_dfvalue.rolling(window = 100).mean().iloc-1 plt.figure(figsize=(12, 8) #繪圖plt.plot(y_hat_avg.index, y_hat_avgmoving_avg_forecast, label=Moving Average Forecast)1T hT 2z= z (1 )z (1 )2 zTT 1 . (1 )T zfrom statsmodels.tsa.api import SimpleExpSmoothing

21、#導(dǎo)入包 y_hat_avg = test_df.copy()# 構(gòu)建簡單指數(shù)平滑模型,平滑系數(shù)0.6fit = SimpleExpSmoothing(np.asarray(train_dfvalue).fit(smoothing_level=0.6, optimized=False) y_hat_avgSES = fit.forecast(test_df.shape0) # 預(yù)測train_df.loc:,sms_fitted= fit.fittedvalues # 繪圖plt.figure(figsize=(12, 8)plt.plot(train_df.index, train_dfva

22、lue, label=Train) plt.plot(train_df.index, train_dfsms_fitted, label=fitted)plt.plot(test_df.index, test_dfvalue, label=Test) plt.plot(y_hat_avg.index, y_hat_avgSES, label=SES) plt.show()簡單指數(shù)平滑法實踐篇-傳統(tǒng)時序分析泛化指數(shù)平滑法(Holt-Winters)1、Level, Trend, Seasonalfrom statsmodels.tsa.api import ExponentialSmoothin

23、g # 導(dǎo)入包y_hat_avg = test_df.copy()# 構(gòu)建泛化指數(shù)平滑模型,周期為1440fit = ExponentialSmoothing(np.asarray(train_dfvalue), seasonal_periods=1440, trend=add, seasonal=add, ).fit() y_hat_avgHolt_Winter = fit.forecast(test_df.shape0) # 預(yù)測train_df.loc:,holt_winter_fitted= fit.fittedvalues # 繪圖plt.figure(figsize=(12, 8)

24、plt.plot(train_df.index, train_dfholt_winter_fitted, label=fitted)plt.plot(y_hat_avg.index, y_hat_avgHolt_Winter, label=Holt_Winter) plt.show()實踐篇-傳統(tǒng)時序分析理論篇背景介紹探索性數(shù)據(jù)分析 經(jīng)典時序預(yù)測法神經(jīng)絡(luò)時序預(yù)測法蘇寧時序預(yù)測與異常檢測平臺Tensorflow基礎(chǔ) 實踐環(huán)境傳統(tǒng)時序分析 DeepAR算法全景實現(xiàn) Demo演實踐篇主要內(nèi)容時間序列數(shù)量603時間序列粒度1 min時間序列類型count/rate時間序列長度30天實踐篇-DeepAR

25、算法全景實現(xiàn)實踐標(biāo)1、使用Anaconda及Jupyter Notebook進(jìn)行數(shù)據(jù)分析及模型構(gòu)建 2、理解DeepAR算法的實現(xiàn)內(nèi)容3、了解工業(yè)中深度學(xué)習(xí)模型部署流程數(shù)據(jù)集蘇寧生產(chǎn)環(huán)境中業(yè)務(wù)相關(guān)數(shù)據(jù),包括count和rate數(shù)據(jù)原始時間序列生成缺失植處理周期性、平穩(wěn)性檢測生成包含特征的時間序列數(shù)據(jù)將數(shù)據(jù)分割為訓(xùn)練集合驗證 集,生成tfrecord文件數(shù)據(jù)預(yù)處理DeepAR訓(xùn)練、推理流程One hot 變換Embedding 層Enboder層Decoder層仿射變換層Loss計算Optimizer模型保存模型轉(zhuǎn)換拉取或加載 tensorflow/運行鏡像,啟動serving服 務(wù)模型訓(xùn)練模型

26、部署通過RESTful API發(fā)起 推理請求獲取推理結(jié)果在線推理總體流程實踐篇-DeepAR算法全景實現(xiàn)原始時間序列成:1、原始數(shù)據(jù)存儲在ClickHouse中,需將ClickHouse中的數(shù)據(jù)按指定時間粒度、維度聚合后取出,形成原始時間序列2、本次實踐直接使用從ClickHouse中取出并轉(zhuǎn)為csv的原始時間序列數(shù)據(jù)缺失值處理:1、原始時間序列存在部分時間點值缺失的情況,需要通過填充,將原始時間序列補全為完整時間序列# count/rate數(shù)據(jù)都為非負(fù)數(shù),過濾負(fù)數(shù),并對時間序列進(jìn)行升序排序 dfdfvalue = 0ds, value.sort_values(by=ds)# 將時間列轉(zhuǎn)換為d

27、atetime格式,并設(shè)置為index # ds列為具體時間 df.set_index(ds,inplace=True)dfds=pd.to_datetime(dfds,format=%Y-%m-%d %H:%M:%S) # 使用resample和interpolate對時間序列進(jìn)行前項填充 df=df.resample(T). interpolate(method=pad, downcast =infer)實踐篇-DeepAR算法全景實現(xiàn)數(shù)據(jù)預(yù)處理2、用acf進(jìn)行周期性檢測,相同周期的個數(shù)大于acf局部最大值個數(shù)的period_threshold認(rèn)為是周期性的acf_values=acf(d

28、fvalue,nlags=df.shape0-1) #計算acf值 #檢測acf局部極大值indexloc_max_index = signal.argrelextrema(acf_values, comparator=np.greater, order=3)數(shù)據(jù)預(yù)處理周期性、平穩(wěn)性檢測:1、用adfuller進(jìn)行平穩(wěn)性檢測,p0.05認(rèn)為是平穩(wěn)數(shù)據(jù)df=df.resample(60min).mean() #對原始時間序列進(jìn)行重采樣,聚合為小時序列 adfuller_result = adfuller(dfvalue, autolag=AIC) #用adfuller進(jìn)行平穩(wěn)性檢測 if adf

29、uller_result10.05: #進(jìn)行平穩(wěn)性判斷,p0.05認(rèn)為是平穩(wěn)數(shù)據(jù)return True實踐篇-DeepAR算法全景實現(xiàn)數(shù)據(jù)預(yù)處理特征提取:1、提取時間相關(guān)的特征,如分鐘、小時、星期、日、embedding id ,時間序列類別等特征dfdate = df.index # date列為時間,為datetime格式 dfminute = dfdate.dt.minute # 獲取分鐘特征 dfhour = dfdate.dt.hour # 獲取小時特征 dfdayofweek = dfdate.dt.dayofweek # 獲取星期特征 dfdayofmonth = dfdate.

30、dt.day # 獲取日特征dfembedding_id = np.ones(df.shape0) * embedding_id # 獲取embedding id dfcategory=np.ones(df.shape0)* category # 獲取周期性、平穩(wěn)性類別特征dfX = df.shift(periods=1, fill_value=0)self.value_label # X為上一時間點的值,作為encoder輸入dfy = dfself.value_label # y為當(dāng)前時間點的值,用于計算loss # 獲取需要的列,得到最終包含特征的時間序列DataFrame df_fea

31、tures = dfX, minute, hour, dayofweek, dayofmonth, embedding_id, category,y實踐篇-DeepAR算法全景實現(xiàn)數(shù)據(jù)預(yù)處理數(shù)據(jù)分割及tfrecord文件生成:1、數(shù)據(jù)分割data_len=df.shape0 #時間序列總長度 training_len=int(data_len*0.9) #訓(xùn)練序列長度 validating_len=int(data_len*0.1) #驗證序列長度 df_training = df:training_len #訓(xùn)練序列DataFrame df_validating = dftraining_l

32、en: #驗證序列DataFrame2、生成3維Tensor -1,data_len, time_series_dim data_len = encoder_len + decoder_lenstep_list = range(data_len, df.shape0, lag_step) #獲取切割的index listdata_np = np.zeros(len(step_list) + 1, data_len, self.time_series_dim) #構(gòu)建初始3維 tensorfor index, i in enumerate(step_list):data_npindex, :,

33、: = np.array(dfi - data_len:i).reshape(data_len, self.time_series_dim) data_np-1, :, : = np.array(df-data_len:).reshape(data_len, self.time_series_dim)實踐篇-DeepAR算法全景實現(xiàn)數(shù)據(jù)預(yù)處理One hot變換1、將特征向量轉(zhuǎn)換為one hot 向量# minute_feature:分鐘特征,batch_size,seq_len , minute_one_hot batch_size,seq_len,60 minute_one_hot = tf

34、.one_hot(tf.reshape(minute_feature, -1, seq_len), 60)# hour_feature:小時特征,batch_size,seq_len, hour_one_hot batch_size,seq_len,24 hour_one_hot = tf.one_hot(tf.reshape(hour_feature, -1, seq_len), 24)# dayofweek_feature:星期特征,batch_size,seq_lendayofweek_one_hot = tf.one_hot(tf.reshape(dayofweek_feature,

35、-1, seq_len), 7) # dayofweek_one_hot batch_size,seq_len,7dayofmonth_one_hot = tf.one_hot(tf.reshape(dayofmonth_feature, -1, seq_len), 31)實踐篇-DeepAR算法全景實現(xiàn)模型訓(xùn)練Embedding層:1、將序列id進(jìn)行Embedding,并和特征向量拼接成最終輸入向量# 創(chuàng)建Embedding矩陣,embedding_id_nums, embedding_sizeid_embedding_matrix = tf.get_variable(shape=self.

36、embedding_id_nums, self.id_embedding_size,initializer=tf.initializers.random_uniform(-1.0, 1.0), dtype=tf.float32,name=EM_id)# 利用embedding_lookup查詢對應(yīng)id 的Embedding向量encoder_id_embedding = tf.nn.embedding_lookup(id_embedding_matrix, encoder_embedding_id) # 將one hot特征向量,embedding向量拼接為最終輸入向量 batch_size,

37、encoder_len,dim encoder_feature = tf.concat(encoder_feature_one_hot,encoder_category_embedding,encoder_id_embedding,-1)實踐篇-DeepAR算法全景實現(xiàn)模型訓(xùn)練Encoder層:1、構(gòu)建多層LSTM encoder # 構(gòu)建多層LSTM單元stacked_rnn = for i in range(self.layer_num):lstm = tf.contrib.cudnn_rnn.CudnnCompatibleLSTMCell(self.hidden_size) drop =

38、 tf.nn.rnn_cell.DropoutWrapper(lstm, output_keep_prob=keep_prob) stacked_rnn.append(drop)cells= tf.contrib.rnn.MultiRNNCell(stacked_rnn)# 通過dynamic_rnn構(gòu)建多層LSTM網(wǎng)絡(luò)with tf.variable_scope(encoder, reuse=tf.AUTO_REUSE):_, encoder_state = tf.nn.dynamic_rnn(cell=cells, inputs=encoder_input,sequence_length=

39、encoder_seq_len,dtype=tf.float32)實踐篇-DeepAR算法全景實現(xiàn)模型訓(xùn)練Decoder 層:1、訓(xùn)練過程中的Decoder和Encoder類似,只是Decoder的初始狀態(tài)為Encoder的最終狀態(tài),initial_state= encoder_state 2、驗證或推理過程中的Decoder LSTM下一個時間點的輸入是上一個時間點分布的采樣# 利用TensorArray存儲Decoder 仿射輸出的mu,sigma mu_ta = tf.TensorArray(dtype=tf.float32, size=decoder_len)sigma_alpha_t

40、a = tf.TensorArray(dtype=tf.float32, size=decoder_len) # 定義循環(huán)結(jié)束條件函數(shù)def cond_fn(time, prev_state, prev_output, decoder_scale_factor, mu_ta, sigma_alpha_ta):return time decoder_len # 定義循環(huán)函數(shù)def loop_fn(time, prev_state, prev_output, decoder_scale_factor, mu_ta, sigma_alpha_ta): decoder_outputs, decoder

41、_state = self.decoder(next_input, decoder_seq_len, prev_state,keep_prob)mu, sigma_alpha = self.create_likelihood_parameters(decoder_outputs, decoder_scale_factor, 1)實踐篇-DeepAR算法全景實現(xiàn)模型訓(xùn)練Decoder 層:mu_ta = mu_ta.write(time, mu)sigma_alpha_ta = sigma_alpha_ta.write(time, sigma_alpha) # 對預(yù)測的概率分布進(jìn)行采樣,得到下一

42、個時間點的輸入值sample_data=tf.random.normal(batch_size,mean=tf.reshape(mu,-1),stddev=tf.reshape(sigma,-1),dtype=tf.float32)# 進(jìn)行scale還原為原始狀態(tài)向量sample_data_scale = tf.math.divide(tf.reshape(sample_data, batch_size, 1,1),decoder_scale_factor)return time + 1, decoder_state, sample_data_scale, decoder_scale_fact

43、or, mu_ta, sigma_alpha_taloop_init = tf.constant(0, dtype=32),encoder_state,decoder_x_input_scale, decoder_scale_factor,mu_ta,sigma_alpha_ta# 利用while_loop進(jìn)行循環(huán)_, _, _, _, mu_output, sigma_alpha_output = tf.while_loop(cond_fn, loop_fn, loop_init)實踐篇-DeepAR算法全景實現(xiàn)模型訓(xùn)練仿射變換層:Decoder輸出的狀態(tài)向量經(jīng)仿射轉(zhuǎn)換為高斯分布

44、的均值、標(biāo)準(zhǔn)差參數(shù)向量with tf.variable_scope(gaussian_parameters, reuse=tf.AUTO_REUSE): # 定義W b變量W_mu = tf.get_variable(shape=self.hidden_size, 1, dtype=tf.float32, name=W_mu) b_mu = tf.get_variable(shape=1, dtype=tf.float32, name=b_mu)# 將W b變量進(jìn)行維度擴展,decoder_outputs:batch_size,decoder_len,hidden_size W_mu_expa

45、nd = tf.tile(tf.expand_dims(W_mu, 0), batch_size, 1, 1)b_mu_expand = tf.tile(tf.expand_dims(b_mu, 0), batch_size, 1) # 仿射計算mu = tf.reshape(tf.matmul(decoder_outputs, W_mu_expand), batch_size, -1) + b_mu_expand # 進(jìn)行scale ,得到真實的參數(shù)mu_scale = tf.math.multiply(mu, tf.reshape(decoder_scale_factor, -1, dec

46、oder_len)實踐篇-DeepAR算法全景實現(xiàn)222Gl (z|, )= 2/ 21/ 2exp z-0.5 * tf.math.divide(tf.math.square(y_true - mu),tf.math.square(sigma) + 1e-6模型訓(xùn)練Loss計算:1、根據(jù)似然公式,計算loss值,一般使用log似然tf.math.reduce_mean(0.5 * tf.math.log(tf.math.square(sigma) +optimizer:# 利用AdamOptimizer進(jìn)行優(yōu)化optimizer = tf.train.AdamOptimizer(learning_rate=self.lr * hvd.size() optimizer = hvd.DistributedOptimizer(optimizer)grads_and_vars = pute_gradients(loss_op)capped_gvs = (tf.clip_by_value(grad, -threshold, threshold), var) for grad, var ingrads_and_vars # 防止梯度爆炸train_op = optimizer.apply_gradients(capped_gvs, global_step=global_s

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論