跟随大神Jason Brownlee, Ph.D学习Deep Learning.《How to Reshape Input Data for Long Short-Term Memory Networks in Keras》原文地址点击打开链接
Tips for LSTM Input:
- The LSTM input layer must be 3D.
- The meaning of the 3 input dimensions are: samples, time steps, and features.
- The LSTM input layer is defined by the input_shape argument on the first hidden layer.
- The input_shape argument takes a tuple of two values that define the number of time steps and features.
- The number of samples is assumed to be 1 or more.
- The reshape() function on NumPy arrays can be used to reshape your 1D or 2D data to be 3D.
- The reshape() function takes a tuple as an argument that defines the new shape.
问:训练和测试序列的不同样本的长度可能不同,怎么处理?
答:"A way I use often is to pad all sequences to the same length and use a masking layer on the front end to ignore masked time steps." "Mask layer supports masking for input data with a variable number of time steps. To introduce masks to your data, use an Embedding layer with the mask_zero
parameter set to True
."
Mask示例:
model = Sequential()
model.add(Masking(mask_value=0., input_shape=(timesteps, features)))
model.add(LSTM(32))
问:Embedding layer怎么支持变长time steps?
You could also use a model that does not specify the input length, for example:
https://machinelearningmastery.com/develop-encoder-decoder-model-sequence-sequence-prediction-keras/