如何处理张量流中输出lstm的序列形状

时间:2018-06-14 09:17:14

标签: tensorflow sequence lstm shape

我在RNN(lstm)中遇到tensorflow的序列长度问题。 我有关于日志计数,响应时间等的时间序列数据。 数据在几分钟内收集。 问题是,我想用60分钟预测20分钟。 输入数据有4个功能,60 seq_length,想要的输出数据有1个功能,20 seq_length

data_dim = 4
hidden_dim = 2
ouput_dim = 1
seq_length = 60
out_length = 20


X = tf.placeholder(tf.float32, [None, seq_length, data_dim])  # data_dim = 4
Y = tf.placeholder(tf.float32, [None, out_length, output_dim]) # output_dim = 1

cell = tf.contrib.rnn.BasicLSTMCell(num_units=hidden_dim, state_is_tuple=True, activation=tf.tanh)
outputs, _states = tf.nn.dynamic_rnn(cell, X, dtype=tf.float32)
Y_pred = tf.contrib.layers.fully_connected(outputs, output_dim, activation_fn=None)

我的问题是,如何设置输出形状。

输出有[batch_size, seq_length, hidden_dim] ##[batch_size, 60,2]

我认为输出的形状应该像[batch_size, out_length, ouput_dim] ##[batch_size, 20, 1]

一样改变

现在,我该如何处理输出形状 请教我..

我的完整代码在这里

seq_length = 60
data_dim = 4
hidden_dim = 2
output_dim = 1
learning_rate = 0.01
iterations = 500



dataX = []
dataY = []
for i in range(0, len(y) - (20+seq_length)) :
    _x = x[i:i+seq_length]
    _y = y[i+seq_length:i+20+seq_length]
    dataX.append(_x)
    dataY.append(_y)

train_size = int(len(dataY) * 0.7)
test_size = len(dataY) - train_size
trainX, testX = np.array(dataX[0:train_size]), np.array(
    dataX[train_size:len(dataX)])
trainY, testY = np.array(dataY[0:train_size]), np.array(
    dataY[train_size:len(dataY)])

trainY = np.reshape(trainY, (len(trainY), 20,1 ))
testY = np.reshape(testY, (len(testY), 20, 1))

X = tf.placeholder(tf.float32, [None, seq_length, data_dim])
Y = tf.placeholder(tf.float32, [None, 20, 1]) # output_dim

cell = tf.contrib.rnn.BasicLSTMCell(num_units=hidden_dim, state_is_tuple=True, activation=tf.tanh)
outputs, _states = tf.nn.dynamic_rnn(cell, X, dtype=tf.float32)
outputs = tf.reshape(outputs[:,-21:-1,], [tf.shape(outputs)[0], 20, 1])
Y_pred = tf.contrib.layers.fully_connected(outputs, output_dim, activation_fn=None)


# cost/loss
loss = tf.reduce_sum(tf.square(Y_pred - Y))  # sum of the squares
# optimizer
optimizer = tf.train.AdamOptimizer(learning_rate)
train = optimizer.minimize(loss)

targets = tf.placeholder(tf.float32, [None, 20, 1])
predictions = tf.placeholder(tf.float32, [None, 20, 1])
rmse = tf.sqrt(tf.reduce_mean(tf.square(targets - predictions)))



with tf.Session() as sess:
    init = tf.global_variables_initializer()
    sess.run(init)

    # Training step
    for i in range(iterations):
        _, step_loss = sess.run([train, loss], feed_dict={X: trainX, Y: trainY})
        print("[step: {}] loss: {}".format(i, step_loss))

    # Test step
    test_predict = sess.run(Y_pred, feed_dict={X: testX})
    rmse_val = sess.run(rmse, feed_dict={targets: testY, predictions: test_predict})
    print("RMSE: {}".format(rmse_val))

    # Plot predictions
    plt.rcParams["figure.figsize"] = (20,20)
    plt.plot(testY)
    plt.plot(test_predict)
    plt.legend(("testY", "predict"))
    plt.xlabel("Time Period")
    plt.ylabel("TOT_NORM_RESTM")
    plt.show()

0 个答案:

没有答案