在Keras中设置LSTM和CuDNNLSTM的参数

2024-05-18 10:08:48 发布

您现在位置:Python中文网/ 问答频道 /正文

我开始学习Keras,对LSTM有些困惑。我不知道输入参数是什么,比如括号(n)中的第一个参数和input_shape

我的数据集是数字的,它有30列,29列是特征,1列是输出(1和0)

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

基于此,参数在我的图层中应该是什么样子

第一:

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

第二:

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')) 

如果我使用例如CuDNNLSTM,这些参数是否相同


Tags: testaddinput参数modelreturntrainactivation
1条回答
网友
1楼 · 发布于 2024-05-18 10:08:48
x_train shape (19192, 29)
y_train shape (19192,)
x_test shape (4799, 29)
y_test shape (4799,)

如果您有熊猫数据帧,请将它们转换为numpy数组

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

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

首先,您需要重塑数据

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)

现在,LSTM的尺寸通常为:

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.

因此,第三个1我们添加了一个维度来对应特征

LSTM输入形状将为(29,1)(29=时间步长,1=每个时间序列的特征数量(同样为简单起见,您可以将其视为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')) 

观察,我们为第一层添加了return_srquence=True,但对于第二层LSTM,我们没有。原因是LSTM需要3D数据(批次、时间、特征),而Dense需要2D数据(批次、特征),当我们看到return_srquence=True时,我们将3D数据发送到下一层进行Dense,而不是发送2D数据

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')) 

相关问题 更多 >