简体   繁体   English

如何重塑为 LSTM 制作 3d 输入?

[英]How to reshape to make a 3d input for LSTM?

I have 1002 times of observing the 10 features.我有 1002 次观察 10 个特征。 I have concatenated together for doing preprocessing on the data and now, I need to reshape it to a 3D input data to be used in LSTM.我已经连接在一起对数据进行预处理,现在,我需要将其重塑为要在 LSTM 中使用的 3D 输入数据。 I do not know using pd.df.groupby (['timestep']) is meaningful but I have used a tiny "np.reshape" function but seems does not determine and I got the error.我不知道使用 pd.df.groupby (['timestep'])是否有意义,但我使用了一个很小的"np.reshape"函数,但似乎不确定,我得到了错误。 The code is below:代码如下:

train_dataset = dataset.sample(frac=0.8,random_state=0)
test_dataset = dataset.drop(train_dataset.index)

scaler = MinMaxScaler()
train_x = scaler.fit_transform(train_dataset)
test_x  = scaler.fit_transform(test_dataset)

data_train = train_x.reshape(1002, 1001, 11)
def build_model():
    model = tf.keras.Sequential([
        layers.LSTM(128,activation=tf.nn.relu,input_shape=train_dataset.shape[1:],return_sequences=True, return_state=True),
        layers.LSTM(128,activation=tf.nn.relu,return_sequences=True, return_state=True),
        layers.Dense(1)
    ])
    opt = tf.keras.optimizers.Adam(learning_rate=0.001)
    model.compile(loss='mean_squared_error', optimizer=opt, metrics= ['mean_squared_error','mean_absolute_error'])
    
    return model

and the error that I faced for the Dense layer is below:我在Dense layer遇到的错误如下:

Input 0 of layer lstm_10 is incompatible with the layer: expected ndim=3, found ndim=2. Full shape received: [None, 11]

From the documentation (https://www.tensorflow.org/api_docs/python/tf/keras/layers/LSTM ) we see that the inputs need to have a shape of (batch, timesteps, features).从文档(https://www.tensorflow.org/api_docs/python/tf/keras/layers/LSTM )我们看到输入需要具有(批次、时间步长、特征)的形状。 Not sure what your dataset looks like, but it sounds like each of the observations in your dataset has a shape of (10,).不确定您的数据集是什么样子,但听起来您数据集中的每个观察值的形状都是 (10,)。 In this case, you need to combine the observations along a new dimension, time.在这种情况下,您需要沿着新的维度时间合并观察。 So the overall dataset should have a shape like (observations, timesteps, features).所以整个数据集应该有一个形状(观察、时间步长、特征)。

rawdata = np.random.rand(1002,10)  # generate random dataset
statemem = 50  # past 50 observations are used to make the next prediction
traindata = []
for i in range(len(rawdata)-statemem):
    traindata.append(rawdata[i:i+statemem,:])
traindata = tf.convert_to_tensor(traindata)

I don't specify the training labels here, but remember each index i in traindata is going to correspond to index i+statemem of the labels.我没有在此处指定训练标签,但请记住 traindata 中的每个索引 i 将对应于标签的索引 i+statemem。 The statemem is the past history you use to make the next lstm prediction. statemem 是您用来进行下一个 lstm 预测的过去历史。 You can treat this as a hyperparameter specific to your problem.您可以将其视为特定于您的问题的超参数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM