且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Keras的LSTM层中的4D输入

更新时间:2023-12-03 11:00:34

LSTM层接受形状为(n_sample, n_timesteps, n_features)的3D数组作为输入.由于数据中每个时间步的特征都是一个(15,4)数组,因此您需要先将其展平为长度为60的特征向量,然后将其传递给模型:

LSTM layer accepts a 3D array as input which has a shape of (n_sample, n_timesteps, n_features). Since the features of each timestep in your data is a (15,4) array, you need to first flatten them to a feature vector of length 60 and then pass it to your model:

X_train = X_train.reshape(10000, 20, -1)

# ...
model.add(LSTM(...,input_shape=(20,15*4), ...)) # modify input_shape accordingly

或者,您可以使用包裹在TimeDistributed中的Flatten层作为模型的第一层,以在每个时间步上展平:

Alternatively, you can use a Flatten layer wrapped in a TimeDistributed layer as the first layer of your model to flatten each timestep:

model.add(TimeDistributed(Flatten(input_shape=(15,4))))

此外,请注意,如果每个时间步(即数组(15, 4))都是一个要素图,其元素之间存在局部空间关系(例如图像补丁),则也可以使用

Further, note that if each timestep (i.e. array (15, 4)) is a feature map where there is a local spatial relationship between its elements, say like an image patch, you can also use ConvLSTM2D instead of LSTM layer. Otherwise, flattening the timesteps and using LSTM would be fine.

作为附注:,您只需在模型的第一层上指定input_shape自变量即可.在其他层上进行指定将是多余的,并且将被Keras自动推断出它们的输入形状,因此将被忽略.

As a side note: you only need to specify input_shape argument on the first layer of the model. Specifying it on other layers would be redundant and will be ignored since their input shape is automatically inferred by Keras.