简体   繁体   中英

Setting parameters on LSTM and CuDNNLSTM in Keras

I started to learn Keras and I came to some confusion with LSTM. I do not get what are the input parameters such as the first parameter that goes into brackets (n) and input_shape .

My dataset is numeric, it has 30 columns, 29 are features and 1 is output (1 and 0).

DataFrame shape (23991, 30)
x_train shape (19192, 29)
y_train shape (19192,)
x_test shape (4799, 29)
y_test shape (4799,)

Based on that, how should parameters look in my layers?

First:

model = Sequential()
model.add(LSTM((?), input_shape = ?, return_sequences = ?, activation = ?))
model.add(Dropout(0.01))
model.add(Dense(1, activation='sigmoid')) 

Second:

model = Sequential()
model.add(LSTM((?), input_shape = ?, return_sequences = ?, activation = ?))
model.add(LSTM((?), input_shape = ?, return_sequences = ?, activation = ?))
model.add(Dropout(0.01))
model.add(Dense(1, activation='sigmoid')) 

Are these parameters the same if I use for example CuDNNLSTM ?

x_train shape (19192, 29)
y_train shape (19192,)
x_test shape (4799, 29)
y_test shape (4799,)

If you have pandas dataframe, convert them to numpy array.

x_train = x_train.to_numpy()
y_train = y_train.to_numpy()

x_test = x_test.to_numpy()
y_test = y_test.to_numpy()

First you need to reshape the data.

x_train = x_train.reshape(19192, 29, 1)
y_train = y_train.reshape(19192,1)
x_test = x_test.reshape(4799, 29, 1)
y_test = y_test.reshape(4799,1)

Now, usually for LSTM dimensions are:

0 - Samples. One sequence is one sample. A batch is comprised of one or more samples.
1 - Time Steps. One time step is one point of observation in the sample.
2 - Features. One feature is one observation at a time step.

So, the third 1 we add a dimension to correspond for features.

LSTM input shape will be (29,1) (29 = time steps, 1 = number of features per time sequence (also for simplicity, you can think of it like number of channels as in CNN)

model = Sequential()
model.add(LSTM(units = 10, input_shape = (29,1), return_sequences = False)) # keep other parameters default if you're not sure
model.add(Dropout(0.01))
model.add(Dense(1, activation='sigmoid')) 

Observe, we add return_srquence = True for the first layer but for second LSTM layer we don't. The reason is LSTM needs 3D data (batch, time, features) but Dense needs 2D data (batch, features), when we see return_sequences = True, we send 3D data to next layer for Dense, we instead send 2D data.

model = Sequential()
model.add(LSTM(units = 10, input_shape = (29,1), return_sequences = True))
model.add(LSTM(units = 10, return_sequences = False))
model.add(Dropout(0.01))
model.add(Dense(1, activation='sigmoid')) 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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