简体   繁体   English

如何为 LSTM keras 重塑 X_train 和 y_train

[英]How to reshape X_train and y_train for LSTM keras

I have the following:我有以下内容:

X_train.shape
(2730, 10)

y_train.shape
(2730)

I want to train LSTM model with keras, but I'm not sure how to reshape the input.我想用 keras 训练 LSTM model,但我不确定如何重塑输入。

I have added this LSTM layer我添加了这个 LSTM 层

time_steps = 30
input_dim = 10 # number of features
...
self.model.add(LSTM(self.hidden_dim, input_shape=(time_steps, self.input_dim), return_sequences=True))
...

The input_shape doesn't match my input. input_shape 与我的输入不匹配。 How should I reshape my X_train?我应该如何重塑我的 X_train? Do I also have to reshape the y_train?我是否还必须重塑 y_train?

How should I reshape my X_train?我应该如何重塑我的 X_train?

The simplest option would be to add a timesteps dimension to your data to make it compatible with an LSTM :最简单的选择是为您的数据添加一个timesteps维度,以使其与LSTM兼容:

import tensorflow as tf

samples = 5
features = 10
data = tf.random.normal((samples, features))
time_series_data = tf.expand_dims(data, axis=1) # add timesteps dimension
tf.print('Data -->', tf.shape(data), 'Time series data', tf.shape(time_series_data))
# Data --> [5 10] Time series data [5 1 10]

However, you want to use 30 timesteps for each feature leading to the shape (samples, 30, 10) .但是,您希望对导致形状(samples, 30, 10)的每个特征使用 30 个timesteps So, what you can use is the RepeatVector layer as part of your model or tf.repeat .因此,您可以使用RepeatVector层作为 model 或tf.repeat的一部分。 Here is an example with the RepeatVector layer:这是一个带有RepeatVector层的示例:

model = tf.keras.Sequential()
model.add(tf.keras.layers.Dense(10, input_shape=(features,)))
model.add(tf.keras.layers.RepeatVector(30))
model.add(tf.keras.layers.LSTM(32))
model.add(tf.keras.layers.Dense(1, activation='sigmoid'))
model.build((1, 10))
tf.print(model.summary())
Model: "sequential_01"
_________________________________________________________________
 Layer (type)                Output Shape              Param #   
=================================================================
 dense_24 (Dense)            (None, 10)                110       
                                                                 
 repeat_vector_1 (RepeatVect  (None, 30, 10)           0         
 or)                                                             
                                                                 
 lstm_3 (LSTM)               (None, 32)                5504      
                                                                 
 dense_25 (Dense)            (None, 1)                 33        
                                                                 
=================================================================
Total params: 5,647
Trainable params: 5,647
Non-trainable params: 0
_________________________________________________________________
None

You could also first map the 10 features to a 300-dimensional output and then reshape the output to fit into an LSTM :您也可以首先将 map 10 个特征转换为 300 维 output,然后重塑 output 以适应LSTM

model = tf.keras.Sequential()
model.add(tf.keras.layers.Dense(300, input_shape=(features,)))
model.add(tf.keras.layers.Reshape((30, 10)))
model.add(tf.keras.layers.LSTM(32))
model.add(tf.keras.layers.Dense(1, activation='sigmoid'))
Model: "sequential_02"
_________________________________________________________________
 Layer (type)                Output Shape              Param #   
=================================================================
 dense_26 (Dense)            (None, 300)               3300      
                                                                 
 reshape (Reshape)           (None, 30, 10)            0         
                                                                 
 lstm_4 (LSTM)               (None, 32)                5504      
                                                                 
 dense_27 (Dense)            (None, 1)                 33        
                                                                 
=================================================================
Total params: 8,837
Trainable params: 8,837
Non-trainable params: 0
_________________________________________________________________
None

To the question:对于这个问题:

Do I also have to reshape the y_train?我是否还必须重塑 y_train?

It depends on what you want.这取决于你想要什么。 If you only have a simple classification task, as I have assumed in the examples, then you do not need to change y_train.如果您只有一个简单的分类任务,就像我在示例中假设的那样,那么您不需要更改 y_train。

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

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