关于RNN或LSTM中的输入和输出数据,我有一个问题。一个RNN需要一个三维向量作为表单的输入(Batch_size,sequence_length_input,features_input)和一个三维输出向量(Batch_size,sequence_length_output,features_output).
我知道features_input和features_output不必有相同的数字,而Batch_size必须是输入和输出相等的。但是sequence_length_input和sequence_length_output的中间部分呢?他们必须是一样的吗?至少在我的例子中(使用Keras和Tensorflow),如果它们不一样,我总是会得到一个错误。因此,我想知道我是否在代码中有一个bug,或者这通常是不可能的。
因此,例如,我可以使用数据X_train =( 1000,100,10)和输出Y_train = (1000,20,3)作为训练的输入,这样我就可以为每个1000个itmes (Batch_size)从一个具有100个时间步长(sequence_length_input)的10维(features_input)时间序列映射到一个具有20个时间步长(sequence_length_output)的三维(features_output)时间序列。
Update:下面是我的用于时间序列预测的RNN代码,只有当输入steps_backward的sequence_length等于输出steps_forward的sequence_length时才能工作,否则它会抛出ValueError:
ValueError: Dimensions must be equal, but are 192 and 96 for '{{node mean_squared_error/SquaredDifference}} = SquaredDifference[T=DT_FLOAT](sequential_5/time_distributed_5/Reshape_1, IteratorGetNext:1)' with input shapes: [?,192,1], [?,96,1].在代码中,我使用96个过去的时间步骤(或2*96=192时间步骤)来预测未来的96个时间步骤。当过去和将来的时间步数相等(相等的sequence_length)时,一切都正常。否则(不相等的sequence_length)我得到ValueError。
代码:
#Import modules
import pandas as pd
import numpy as np
import tensorflow as tf
from sklearn.preprocessing import StandardScaler
from tensorflow import keras
# Define the parameters of the RNN and the training
epochs = 1
batch_size = 50
steps_backwards = 2 * 96
steps_forward = 96
split_fraction_trainingData = 0.70
split_fraction_validatinData = 0.90
randomSeedNumber = 50
#Read dataset
df = pd.read_csv('C:/Users/User1/Desktop/TestData.csv', sep=';', header=0, low_memory=False, infer_datetime_format=True, parse_dates={'datetime':[0]}, index_col=['datetime'])
# standardize data
data = df.values
indexWithYLabelsInData = 0
data_X = data[:, 0:3]
data_Y = data[:, indexWithYLabelsInData].reshape(-1, 1)
scaler_standardized_X = StandardScaler()
data_X = scaler_standardized_X.fit_transform(data_X)
data_X = pd.DataFrame(data_X)
scaler_standardized_Y = StandardScaler()
data_Y = scaler_standardized_Y.fit_transform(data_Y)
data_Y = pd.DataFrame(data_Y)
# Prepare the input data for the RNN
series_reshaped_X = np.array([data_X[i:i + (steps_backwards+steps_forward)].copy() for i in range(len(data) - (steps_backwards+steps_forward))])
series_reshaped_Y = np.array([data_Y[i:i + (steps_backwards+steps_forward)].copy() for i in range(len(data) - (steps_backwards+steps_forward))])
timeslot_x_train_end = int(len(series_reshaped_X)* split_fraction_trainingData)
timeslot_x_valid_end = int(len(series_reshaped_X)* split_fraction_validatinData)
X_train = series_reshaped_X[:timeslot_x_train_end, :steps_backwards]
X_valid = series_reshaped_X[timeslot_x_train_end:timeslot_x_valid_end, :steps_backwards]
X_test = series_reshaped_X[timeslot_x_valid_end:, :steps_backwards]
Y_train = series_reshaped_Y[:timeslot_x_train_end, steps_backwards:]
Y_valid = series_reshaped_Y[timeslot_x_train_end:timeslot_x_valid_end, steps_backwards:]
Y_test = series_reshaped_Y[timeslot_x_valid_end:, steps_backwards:]
# Build the model and train it
np.random.seed(randomSeedNumber)
tf.random.set_seed(randomSeedNumber)
model = keras.models.Sequential([
keras.layers.SimpleRNN(10, return_sequences=True, input_shape=[None, 3]),
keras.layers.SimpleRNN(10, return_sequences=True),
keras.layers.TimeDistributed(keras.layers.Dense(1))
])
model.compile(loss="mean_squared_error", optimizer="adam")
history = model.fit(X_train, Y_train, epochs=epochs, batch_size=batch_size, validation_data=(X_valid, Y_valid))
#Predict the test data
Y_pred = model.predict(X_test)下面是一些测试数据https://filetransfer.io/data-package/ufbzh09o#link
提醒:代码和数据提供了一个最小的可重复示例。也许您可以看看它,因为在这段代码中,输入和输出数据的sequence_length必须相等,否则我会出错。不巧的是,我还没有弄清楚为什么会发生这个问题。
发布于 2022-05-24 17:06:11
我也遇到过同样的问题。我的输入数据形状为512,10,3,输出数据为512,20,1,这意味着最后的10次时间步骤数据用于预测未来的20次步骤。当我试图在PyTorch中实现它时,问题与您出现的问题相同。最后,我只是使用LSTM的最后一个状态重复20次,并输入到下一个完全连接的层。然而,我不能在经典的反向传播(只是由完全连接的层组成)神经网络。
https://stackoverflow.com/questions/71065782
复制相似问题