将时间序列数据输入Tensorflow进行LSTM分类器培训

时间:2017-02-22 09:37:00

标签: python numpy machine-learning tensorflow neural-network

我有一个形状为(38307, 26)的数据框,时间戳为索引:

我正在尝试实施LSTM分类器,但我很难将其提供给DataFlow

我想要喂食的最终数组是'(X_train =(38307,25),y_train =(38307,2))'

我已添加代码

# Parametres
learning_rate = 0.001
training_epochs = 100
batch_size = 128
display_step = 10

# Network Parameters
n_input = 25    # features= 25
n_steps = 28    # timesteps
n_hidden = 128  # hidden layer num of features
n_classes = 2  # Binary classification

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

# TF Weights
weights = {
    'out': tf.Variable(tf.random_normal([n_hidden, n_classes]))
}
biases = {
    'out': tf.Variable(tf.random_normal([n_classes]))
}

pred = RNN(x, weights, biases)

# Initialize the variables
init = tf.global_variables_initializer()

# Launch the graph
with tf.Session() as sess:
    sess.run(init)
    step = 1
    # Keep training until reach max iterations
    for epoch in range(training_epochs):
        avg_cost = 0
        total_batch = int(len(X_train)/batch_size)
        X_batches = np.array_split(X_train, total_batch)
        Y_batches = np.array_split(y_train, total_batch)
        #Loop over all batches
        for i in range(total_batch):
            batch_x, batch_y = X_batches[i], Y_batches[i]
            # batch_y.shape = (batch_y.shape[0]), 1)
            # Run optimization op (backprop) and cost op(to get loss value)
            _, c = sess.run([optimizer, cost], feed_dict={x: batch_x,
                                                          y: batch_y})

            # Compute average loss
            avg_cost += c / total_batch
        #Display logs per epoch step
        if epoch % display_step == 0:
            print(("Epoch:", '%04d' % (epoch + 1), "cost=", "{:.9f}".format(avg_cost)))
    print('Optimization finished')

    # Store session for analysis with TensorBoard
    writer = tf.summary.FileWriter("/tmp/test", sess.graph)

    #Test model
    print("Accuracy:", accuracy.eval({x: X_test, y: y_test}))
    global result
    result = tf.argmax(pred, 1).eval({x: X_test, y: y_test})

编辑RNN功能:

def RNN(x, weights, biases):
    # Prepare data shape to match 'rnn' function requirements
    # Current data input shape: (batch_size, n_steps, n_input)
    # Required Shape: 'n_steps' tensors list of shape (batch size, n_input)

    # Permuting batch_size and n_steps
    x = tf.transpose(x, [1, 0, 2])
    # Reshaping to (n_steps*batch_size, n_input)
    x = tf.reshape(x, [-1, n_input])
    # Split to get a list of 'n_steps' tensors of shape (batch_size, n_input)
    x = tf.split(0, n_steps, x)
    # x = tf.split(x, n_steps, 0) # Syntax change this version 

    # LSTM tensorflow using rnn from tensorflow.contrib
    lstm_cell = rnn_cell.BasicLSTMCell(n_hidden, forget_bias=1.0)

    # Get  LSTM cell output
    outputs, states = rnn.rnn(lstm_cell, x, dtype=tf.float32)

    # Linear activation, using rnn inner loop last output
    return tf.matmul(outputs[-1], weights['out']) + biases['out']

1 个答案:

答案 0 :(得分:1)

不幸的是,代码中最重要的部分隐藏在RNN功能中。

一些提示可以帮助你:我猜你正在尝试建立一个动态的RNN ......(这是正确的吗?)在这种情况下,我看到的一个常见错误是人们混淆时间主要和批量主要设置这些RNN。换句话说,您输入的数据是[批次,时间,变量]还是[时间,批次,变量]。 有关这方面的更多信息,请访问:https://github.com/tensorflow/tensorflow/blob/master/tensorflow/g3doc/api_docs/python/functions_and_classes/shard8/tf.nn.dynamic_rnn.md