Demo entry 6714272

LSTM

   

Submitted by anonymous on Feb 12, 2018 at 19:31
Language: Python 3. Code size: 6.5 kB.

xtmp = srcDf[1].iloc[:, 6:10].values
ytmp = srcDf[1].iloc[:, 1:5].values

# define the constants
rnn_unit = 10       # hidden layer units
input_size = 4
output_size = 4
lr = 0.0006         # learning rate
tf.reset_default_graph()

# input layer, output layer weight, bias
weights = { 'in': tf.Variable(tf.random_normal([input_size, rnn_unit])),
            'out': tf.Variable(tf.random_normal([rnn_unit, 1]))  }
biases = { 'in': tf.Variable(tf.constant(0.1, shape=[rnn_unit,])),
           'out': tf.Variable(tf.constant(0.1, shape=[1, ]))  }

def get_data(batch_size=10, time_step=5, train_begin=0, train_end=1000):
    batch_index = []

    scaler_for_x = MinMaxScaler(feature_range=(0, 1))  # minmax zoom by column
    scaler_for_y = MinMaxScaler(feature_range=(0, 1))
    scaled_x_data = scaler_for_x.fit_transform(xtmp)
    scaled_y_data = scaler_for_y.fit_transform(ytmp)

    #print(scaled_y_data)

    label_train = scaled_y_data[train_begin:train_end]
    label_test = scaled_y_data[train_end:]
    normalized_train_data = scaled_x_data[train_begin:train_end]
    normalized_test_data = scaled_x_data[train_begin:]

    #print(label_train)

    train_x, train_y = [], []   # primary definition of x, y in training set
    for i in range(len(normalized_train_data) - time_step):
        if i % batch_size==0:
            batch_index.append(i)
        x = normalized_train_data[i:i+time_step, :4]
        y = label_train[i:i+time_step]
        train_x.append(x.tolist())
        train_y.append(y.tolist())
        #print(y.tolist())
    batch_index.append((len(normalized_train_data) - time_step))

    size = (len(normalized_test_data) + time_step - 1)//time_step  # count of sample
    test_x, test_y=[], []
    for i in range(size-1):
        x = normalized_test_data[i * time_step:(i+1) * time_step, :4]
        y = label_test[i * time_step:(i+1) * time_step]
        test_x.append(x.tolist())
        test_y.extend(y.tolist())
    test_x.append((normalized_test_data[(i+1) * time_step:, :4]).tolist())
    test_y.extend((label_test[(i+1) * time_step:]).tolist())

    return batch_index, train_x, train_y, test_x, test_y, scaler_for_y

# definition of recurrent neural network
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])  #turn tensor to 3D-Array as the input of hidden layer
    input_rnn = tf.matmul(input, w_in) + b_in
    input_rnn = tf.reshape(input_rnn, [-1, time_step, rnn_unit])  # turn tensor to 3D-Array as the input of lstm cell
    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]) # for input layer
    w_out = weights['out']
    b_out = biases['out']
    pred = tf.matmul(output,w_out) + b_out
    return pred, final_states

# training model
def train_lstm(batch_size = 10, time_step = 5, train_begin = 0, train_end = 1000):
    X = tf.placeholder(tf.float32, shape = [None, time_step, input_size])
    Y = tf.placeholder(tf.float32, shape = [None, time_step, output_size])
    batch_index, train_x, train_y, test_x, test_y, scaler_for_y = get_data(batch_size, time_step, train_begin, train_end)
    pred,_ = lstm(X)
    # cost function
    loss = tf.reduce_mean(tf.square(tf.reshape(pred, [-1])-tf.reshape(Y, [-1])))
    train_op = tf.train.AdamOptimizer(lr).minimize(loss)
    with tf.Session() as sess:
        sess.run(tf.global_variables_initializer())
        # repeat training 10000 times
        iter_time = 10000
        for i in range(iter_time):
            for step in range(len(batch_index) - 1):
                _, loss_=sess.run([train_op,loss], feed_dict={X: train_x[batch_index[step]: batch_index[step+1]],
                                                              Y: train_y[batch_index[step]: batch_index[step+1]]})
            if i % 100 == 0:
                print('iter:', i, 'loss:', loss_)
        # predict
        test_predict = []
        for step in range(len(test_x)):
            prob = sess.run(pred, feed_dict = {X: [test_x[step]]})
            predict = prob.reshape((-1))
            test_predict.extend(predict)

        test_predict = scaler_for_y.inverse_transform([test_predict])
        test_y = scaler_for_y.inverse_transform([test_y])
        rmse = np.sqrt(mean_squared_error(test_predict,test_y))
        mae = mean_absolute_error(y_pred = test_predict, y_true = test_y)
        print ('mae:', mae, '   rmse:', rmse)
    return test_predict

test_predict = train_lstm(batch_size=10, time_step=5, train_begin=0, train_end=1000)

# visualization

plt.figure(1, figsize=(8, 6), dpi=600)

# figure(1,1)
plt.subplot(221)
plt.plot([x for x in range(1977, 2010)], y1)
plt.plot([x for x in range(1977, 2052)], [t for t in [x for x in p1][0]])
plt.grid(True)
plt.xlabel('Year')
plt.ylabel('Nonrenewable Consumption')
plt.legend(('origin', 'prediction'),
           shadow=True, loc='best')
my_x_ticks = np.arange(1980, 2060, 10)
plt.xticks(my_x_ticks)

# figure(1,2)
plt.subplot(222)
plt.plot([x for x in range(1977, 2010)], y2)
plt.plot([x for x in range(1977, 2052)], [t for t in [x for x in p2][0]])
plt.grid(True)
plt.xlabel('Year')
plt.ylabel('Renewable Consumption')
plt.legend(('origin', 'prediction'),
           shadow=True, loc='best')
my_x_ticks = np.arange(1980, 2060, 10)
plt.xticks(my_x_ticks)


# figure(2,1)
plt.subplot(223)
plt.plot([x for x in range(1977, 2010)], y3)
plt.plot([x for x in range(1977, 2052)], [t for t in [x for x in p3][0]])
plt.grid(True)
plt.xlabel('Year')
plt.ylabel('Nonrenewable Production')
plt.legend(('origin', 'prediction'),
           shadow=True, loc='best')
my_x_ticks = np.arange(1980, 2060, 10)
plt.xticks(my_x_ticks)

# figure(2,2)
plt.subplot(224)
plt.plot([x for x in range(1977, 2010)], y4)
plt.plot([x for x in range(1977, 2052)], [t for t in [x for x in p4][0]])
plt.grid(True)
plt.xlabel('Year')
plt.ylabel('Renewable Production')

plt.legend(('origin', 'prediction'),
           shadow=True, loc='best')
my_x_ticks = np.arange(1980, 2060, 10)
plt.xticks(my_x_ticks)

plt.tight_layout()

plt.show()

This snippet took 0.02 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).