DL之LSTM:基於 tensorflow 框架利用 LSTM 演算法對氣溫資料集訓練並回歸預測

語言: CN / TW / HK

輸出結果


資料集

tensorboard 視覺化

iter: 0 loss: 0.010328549

iter: 500 loss: 0.0044991444

iter: 1000 loss: 0.003714567

iter: 1500 loss: 0.0033356838

iter: 2000 loss: 0.003116763

iter: 2500 loss: 0.0029606873

iter: 3000 loss: 0.0028696475

iter: 3500 loss: 0.0026985144

iter: 4000 loss: 0.0025833827

iter: 4500 loss: 0.0024938423

iter: 5000 loss: 0.0024183288

iter: 5500 loss: 0.0023511213

iter: 6000 loss: 0.0022882319

iter: 6500 loss: 0.0022265154

iter: 7000 loss: 0.002163515

iter: 7500 loss: 0.0020974649

iter: 8000 loss: 0.0020275544

iter: 8500 loss: 0.0019528335

iter: 9000 loss: 0.0018700107

iter: 9500 loss: 0.0017752206

iter: 10000 loss: 0.0016714178

iter: 10500 loss: 0.0015757289

iter: 11000 loss: 0.0015021019

iter: 11500 loss: 0.0014435991

iter: 12000 loss: 0.0013950231

iter: 12500 loss: 0.0013551206

iter: 13000 loss: 0.0013215576

iter: 13500 loss: 0.0012917771

iter: 14000 loss: 0.0012640483

iter: 14500 loss: 0.0012376485

iter: 15000 loss: 0.0012124979

iter: 15500 loss: 0.0011886061

iter: 16000 loss: 0.0011660281

iter: 16500 loss: 0.0011447266

iter: 17000 loss: 0.0011244208

iter: 17500 loss: 0.001104528

iter: 18000 loss: 0.0010844271

iter: 18500 loss: 0.0010633252

iter: 19000 loss: 0.0010399523

iter: 19500 loss: 0.001011961

iter: 20000 loss: 0.00097585854

iter: 20500 loss: 0.00093142985

iter: 21000 loss: 0.00089110696

iter: 21500 loss: 0.00086476567

iter: 22000 loss: 0.00084816053

iter: 22500 loss: 0.0008364689

iter: 23000 loss: 0.00082719745

iter: 23500 loss: 0.000819149

iter: 24000 loss: 0.00081174297

iter: 24500 loss: 0.00080478605

iter: 25000 loss: 0.0007982892

iter: 25500 loss: 0.00079225213

iter: 26000 loss: 0.0007866463

iter: 26500 loss: 0.0007813923

iter: 27000 loss: 0.00077644055

iter: 27500 loss: 0.00077167765

iter: 28000 loss: 0.00076701824

iter: 28500 loss: 0.0007624052

iter: 29000 loss: 0.00075781584

iter: 29500 loss: 0.00075323426

iter: 30000 loss: 0.0007487352

iter: 30500 loss: 0.00074437447

iter: 31000 loss: 0.000740188

iter: 31500 loss: 0.00073620223

iter: 32000 loss: 0.0007323837

iter: 32500 loss: 0.00072883896

iter: 33000 loss: 0.0007253971

iter: 33500 loss: 0.0007672859

iter: 34000 loss: 0.00074850733

iter: 34500 loss: 0.0007547441

iter: 35000 loss: 0.00075676554

iter: 35500 loss: 0.00075801736

iter: 36000 loss: 0.00075870997

iter: 36500 loss: 0.0007588421

iter: 37000 loss: 0.0007584684

iter: 37500 loss: 0.00075732305

iter: 38000 loss: 0.0007555771

iter: 38500 loss: 0.00075331994

iter: 39000 loss: 0.0007502647

iter: 39500 loss: 0.00074706867
複製程式碼

核心程式碼

DL 之 LSTM:基於 tensorflow 框架利用 LSTM 演算法對氣溫資料集訓練並預測

def LSTM(X):  
    batch_size=tf.shape(X)[0]
    time_step=tf.shape(X)[1]
    w_in=weights['in']
    b_in=biases['in']  
    input=tf.reshape(X,[-1,input_size])  
 
    input_rnn=tf.matmul(input,w_in)+b_in
    input_rnn=tf.reshape(input_rnn,[-1,time_step,rnn_unit])  
 
    cell=tf.contrib.rnn.BasicLSTMCell(rnn_unit)
    #cell=tf.contrib.rnn.core_rnn_cell.BasicLSTMCell(rnn_unit)
    init_state=cell.zero_state(batch_size,dtype=tf.float32)
    output_rnn,final_states=tf.nn.dynamic_rnn(cell, input_rnn,initial_state=init_state, dtype=tf.float32)  
 
    output=tf.reshape(output_rnn,[-1,rnn_unit]) 
    w_out=weights['out']
    b_out=biases['out']
    pred=tf.matmul(output,w_out)+b_out
    return pred,final_states
複製程式碼
「其他文章」