python - 使用 MLP 和 Tensorflow 预测时间序列值

标签 python machine-learning neural-network tensorflow

我在尝试建立多层感知器神经网络来使用 Tensorflow 预测时间序列的下一个值时遇到了困难。

我从文件中读取时间序列,将其分成三个数组,并使用这些数组来训练、测试和验证网络。不幸的是,我的网络对我提供的每个输入的响应都是 0.9999。

下图显示了我期望网络产生的值,请注意,它们的范围从 2.8 到 4.2

This is the real values my network is supposed to output. Note that it goes from 2.8 to 4.2

现在,这些是我的网络预测的值。虽然它们看起来都一样,但实际上都是 0.9999...(小数点后第九位有些差异)。

These are the values my network predicts

import csv
import numpy as np

from statsmodels.tsa.tsatools import lagmat
import tensorflow as tf

# Data split (values represent percentage)
perc_train = 0.5
perc_test = 0.4
perc_eval = 0.1

# Parameters
learning_rate = 10 ** -3
min_step_size_train = 10 ** -5
training_epochs = 250
display_step = 1

# Network Parameters
n_input = 15 
n_classes = 1 
n_hidden = (n_input + n_classes) / 2 


def get_nn_sets(pmX, pmY):
    '''
    Splits data into three subsets
    '''
    trainningIndex = int(len(pmX) * perc_train)
    validationIndex = int(len(pmX) * perc_test) + trainningIndex

    pmXFit = pmX[:trainningIndex, :]
    pmYFit = pmY[:trainningIndex]

    pmXTest = pmX[trainningIndex:validationIndex, :]
    pmYTest = pmY[trainningIndex:validationIndex]

    pmxEvaluate = pmX[validationIndex:, :]
    pmYEvaluate = pmY[validationIndex:]

    return pmXFit, pmYFit, pmXTest, pmYTest, pmxEvaluate, pmYEvaluate


def read_dollar_file(clip_first = 4000):
    '''
    Reads the CSV file containing the dollar value for Brazilian real during the years
    -----
    RETURNS:
        A matrix with the file contents
    '''

    str_vals = []
    with open('dolar.csv', 'rb') as csvfile:
        spamreader = csv.reader(csvfile, delimiter=',')
        for row in spamreader:
            # retrieving the first column of the file (the dollar value)
            str_vals.append(row[1]) 

    # removing title
    str_vals = str_vals[1:]
    # removing the empty strings (sunday and holidays have no values)
    y = filter(None, str_vals) 
    # converting from string to float values
    y = np.array(y).astype(np.float) 

    # checking if initial elements should be discarded
    if (clip_first > 0):
        y = y[clip_first:]
    return y

 # Create model
def get_multilayer_perceptron(x):
    # Store layers weight & bias

    weights = {
        'h1': tf.Variable(tf.random_normal([n_input, n_hidden], dtype=tf.float64)),
        'out': tf.Variable(tf.random_normal([n_hidden, n_classes], dtype=tf.float64))
    }
    biases = {
        'b1': tf.Variable(tf.random_normal([n_hidden], dtype=tf.float64)),
        'out': tf.Variable(tf.random_normal([n_classes], dtype=tf.float64))
    }
    # Hidden layer with relu activation
    layer_1 = tf.add(tf.matmul(x, weights['h1']), biases['b1'])
    layer_1 = tf.nn.relu(layer_1)
    # Output layer with tanh activation
    out_layer = tf.matmul(layer_1, weights['out']) + biases['out']
    out_layer = tf.nn.tanh(out_layer)
    return out_layer

def run_mlp(inp, outp):

    pmXFit, pmYFit, pmXTest, pmYTest, pmXEvaluate, pmYEvaluate = get_nn_sets(inp, outp)

    # tf Graph input
    x = tf.placeholder("float64", [None, n_input])
    y = tf.placeholder("float64", [None, n_classes])

    # Construct model
    pred = get_multilayer_perceptron(x)    

    # Define loss and optimizer
    cost = tf.nn.l2_loss(tf.sub(pred, y))        
    optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate).minimize(cost)

    # Initializing the variables
    init = tf.initialize_all_variables()

    # Launch the graph
    with tf.Session() as sess:
        sess.run(init)

        # Training cycle
        last_cost = min_step_size_train + 1
        for epoch in range(training_epochs):

            # Trainning data
            for i in range(len(pmXFit)):
                batch_x = np.reshape(pmXFit[i,:], (1, n_input))
                batch_y = np.reshape(pmYFit[i], (1, n_classes))

                # Run optimization
                sess.run(optimizer, feed_dict={x: batch_x, y: batch_y})


            # Calculating data error
            c = 0.0
            for i in range(len(pmXTest)):
                batch_x = np.reshape(pmXTest[i,:], (1, n_input))
                batch_y = np.reshape(pmYTest[i], (1, n_classes))

                # Run Cost function                
                c += sess.run(cost, feed_dict={x: batch_x, y: batch_y})

            c /= len(pmXTest)
            # Display logs per epoch step
            if epoch % display_step == 0:
                print("Epoch:", '%04d' % (epoch+1), "cost=", \
                    "{:.30f}".format(c))            

            if abs(c - last_cost) < min_step_size_train:
                break
            last_cost = c

        nn_predictions = np.array([])          
        for i in range(len(pmXEvaluate)):
            batch_x = np.reshape(pmXEvaluate[i,:], (1, n_input))
            nn_predictions = np.append(nn_predictions, sess.run(pred, feed_dict={x: batch_x})[0])

        print("Optimization Finished!")

    nn_predictions.flatten()
    return [pmYEvaluate, nn_predictions]

inp = lagmat(read_dollar_file(), n_input, trim='both')
outp = inp[1:, 0]
inp = inp[:-1]

real_value, predicted_value = run_mlp(inp, outp)

我也尝试了不同的成本函数,但没有成功。我知道我可能错过了一些非常愚蠢的东西,所以我非常感谢您的帮助。

谢谢。

最佳答案

从您的代码中:

out_layer = tf.nn.tanh(out_layer)

tanh 只能输出(-1.0, 1.0)之间的值,删除这行会使其做得更好。

关于python - 使用 MLP 和 Tensorflow 预测时间序列值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40646783/

相关文章:

machine-learning - 什么是多头模型?模型中的 'head' 到底是什么?

android - Google App Engine 上跨 GET 和 POST 的 Beaker Session?

c++ - ai junkie 神经网络教程 - 不明白

python - 为什么我的 plt.savefig 不起作用?

machine-learning - 处理 scikit-learn 中不平衡测试集的最佳方法

swift - 神经网络如何在无法直接控制的外部条件下从测试输出中学习

machine-learning - Scikit learn 多层神经网络

mobile - 移动设备中的深度学习人脸检测

python - 使用 easy_install 安装 mechanize

python - Keras 和 TensorBoard - AttributeError : 'Sequential' object has no attribute '_get_distribution_strategy'