2017-02-22 47 views
1

我有形状(38307, 26)的数据帧与时间戳指数:饲喂时间序列数据转换成Tensorflow为LSTM分类培训

我想实现一个LSTM分类,但我努力把它送入数据流

我试图喂最终阵列形状的 '(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

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

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

+0

我一定错过了,我已经添加它在编辑。 – SerialDev