我试图在张量流中使用LSTM来进行空气质量预测 . 数据来自https://archive.ics.uci.edu/ml/datasets/Beijing+PM2.5+Data .

它是一个8输入和8输出型号 .

在我训练模型并进行预测之后,答案总是相同的 .

[  1.07586029e+02   1.60341334e+00   1.03546591e+01   1.01711316e+03
   1.08991003e+00   2.57353954e+01  -6.55468106e-02   1.81939721e-01]
[  1.07586029e+02   1.60341334e+00   1.03546591e+01   1.01711316e+03
   1.08991003e+00   2.57353954e+01  -6.55468106e-02   1.81939721e-01]
[  1.07586029e+02   1.60341334e+00   1.03546591e+01   1.01711316e+03
   1.08991003e+00   2.57353954e+01  -6.55468106e-02   1.81939721e-01]
[  1.07586029e+02   1.60341334e+00   1.03546591e+01   1.01711316e+03
   1.08991003e+00   2.57353954e+01  -6.55468106e-02   1.81939721e-01]

在每个[]是每天的空气质量,它应该是不同的 . 即使我更改输入数据也没有什么不同 . 再训练剂量不起作用 . 我感到很困惑 . 请告诉我它为什么会发生以及如何解决它 . 非常感谢!

# -*- coding: utf-8 -*-
"""
Created on Wed Jun  6 10:56:52 2018

@author: Administrator
"""

import tensorflow as tf
import pandas as pd
import numpy as np
import csv

tf.reset_default_graph()

rnn_unit = 50       #the amount of hidden lstm units
batch_size = 72     #the amount of data trained every time
input_size = 8      #size of input
output_size = 8     #size of output
lr = 0.006           #learn rate
train_x, train_y = [], []   #

f = open('C:\\Users\\Administrator\\Desktop\\BJair\\BjAirDat3.csv',encoding='UTF-8')
df = pd.read_csv(f) #read the csv file
#get data, use the data between 2010 ans 1013 for train,the data of 2014 as exam

weatherdata = df.iloc[0:39312 ,5:13]  #weather with 7 items, not including PM2.5, for train
#pm25data = df.iloc[0+batch_size:39312+batch_size ,5:6]  #pm2.5 data, for train
weathertest = df.iloc[39312:, 5:13]    #weatherdata with 7 items, not including PM2.5, for exam
#pm25test = df.iloc[39312: 5:6]  #pm2.5 data, for exam

#train_x is a tensor which [?,batch_size,input_size]
#train_y is a tensor which [?,batch_size,output_size]
i = 0
while i < len(weatherdata):
    x = weatherdata[i:i+batch_size].values  #conver weatherdata to a tensor
    #y = pm25data[i:i+batch_size].values     #the same with the pm25data
    train_x.append(x.tolist())  #push them into train_x ans train_y
    #train_y.append(y.tolist())
    i += batch_size

#placeholder
X = tf.placeholder(tf.float32, [None, batch_size, input_size])    #a placeholder as the input tensor
Y = tf.placeholder(tf.float32, [None, batch_size, output_size])   #the lable

#initialize the weights and biases [7,50] [50,1] 
weights = {
            'in':tf.Variable(tf.random_normal([input_size, rnn_unit], seed = 1)),
            'out':tf.Variable(tf.random_normal([rnn_unit, output_size], seed = 2))
        }

biases = {
            'in':tf.Variable(tf.random_normal([batch_size, rnn_unit], seed = 5)),
            'out':tf.Variable(tf.random_normal([batch_size, output_size], seed = 8))
        }

def lstm(batch):
    w_in = weights['in']
    b_in = biases['in']
    w_out = weights['out']
    b_out = biases['out']
    #convert the tensor(X Accepts value from outside function) to a 2-dimensional tensor, [?*7]
    input_ = tf.reshape(X, [-1, input_size])  
    #make matrix multiplication between input_ and w_in, then add b_in
    input_rnn = tf.matmul(input_, w_in) + b_in   
    #convert input_rnn to a 3-dimensional tensor as the input of BaicSTMCell
    input_rnn = tf.reshape(input_rnn, [-1, batch, rnn_unit])
    #BasicLSTMCell cell,the amount of rnn_unit
    cell = tf.nn.rnn_cell.BasicLSTMCell(rnn_unit)
    #initial cell,batch_size is equal with the input parameter BATCH
    init_state = cell.zero_state(batch, dtype = tf.float32)
    #outputs is a tensor of shape [batch_size, max_time, cell_state_size]
    #final_state is a tensor of shape [batch_size, cell_state_size]
    #Create a Cell
    #time_major = True ==> Tensorshape [max_time, batch_size, ...] something goes wrong when FALSE
    output_rnn, final_state = tf.nn.dynamic_rnn(cell, input_rnn, initial_state = init_state, dtype = tf.float32, time_major = True)   
    #convert the output tensor to a 2-dimensional tensor, then calculate the output
    output = tf.reshape(output_rnn, [-1, rnn_unit])    
    #make matrix multiplication between output and w_out, then add b_out
    pred = tf.matmul(output, w_out) + b_out
    return pred,final_state


def train_lstm():
    print('start train lstm') 
    print(len(train_x))
    global batch_size
    pred,_ = lstm(batch_size)
    #calculate the loss, use the sum of variance between PRED and Y
    ##
    ##loss need to be improved
    ##
    loss = tf.reduce_sum(tf.square(tf.reshape(pred, [-1]) - tf.reshape(Y, [-1])))  
    #use lr as the learn rate, to make the loss minimize
    train_op = tf.train.AdamOptimizer(lr).minimize(loss)   
    #save the model
    saver = tf.train.Saver(tf.global_variables())

    with tf.Session() as sess:
        sess.run(tf.global_variables_initializer())
        for i in range(1000):
            step = 0
            start = 0
            end = start + 1
            while(end < len(train_x)-1):
                loss_ = sess.run([train_op, loss], feed_dict = {X:train_x[start:end], Y:train_x[start+1:end+1]})
                start += 1
                end += 1
                if step%100 == 0:
                    print('round: ' , i , '  step: ' , step, '  loss : ' , loss_)
                if step % 1000 == 0:
                    saver.save(sess, "C:\\Users\\Administrator\\Desktop\\modell\\model.ckpt")
                    print('save model')`enter code here`
                step += 1

#train_lstm()

def predection():
    print('start predection')

    #test_x, test_y = [], []

    x_t = weathertest[100:172].values
    #y_t = pm25test[i:i+72].values     
    #test_x.append(x_t.tolist())  
    #test_y.append(y_t.tolist())

    prev_seq = x_t

    pred,_ = lstm(72)

    saver = tf.train.Saver(tf.global_variables())

    with tf.Session() as sess:

        saver.restore(sess, "C:\\Users\\Administrator\\Desktop\\modell\\model.ckpt")

        #print(test_x)

        predict = []

        for i in range(30):
            next_seq = sess.run(pred, feed_dict = {X:[prev_seq]})
            #print(prev_seq[0])
            #if i == 16:
            #print(next_seq[-1])
            predict.append(next_seq[-1])
            prev_seq = np.vstack((prev_seq[1:], next_seq[-1]))
            print(predict[-1])

predection()