通过return_sequences = True在Keras中进行简单的LSTM训练

时间:2018-09-05 15:47:37

标签: python keras lstm rnn mnist

我正在尝试使用return_sequences在Keras中训练基于LSTM的简单RNN模型。正如我想要的所有时间步输出。这是我正在使用的代码:

template<class...Ts>
void f(Ts...ts) {
  ( (void)(g(ts)), ... );
}

但是出现以下错误:

model = Sequential()
model.add(LSTM(10, return_sequences = True, input_shape = (8, 8)))
model.add(Activation('softmax'))

model.compile(loss = 'categorical_crossentropy', optimizer = adam, metrics = ['accuracy'])
model.fit(X_train, y_train, batch_size = 1, epochs = 1000, verbose = 1)

但是当我从以下位置更改行时,相同的代码可以正常工作:

model.fit(X_train, y_train, batch_size = 1, epochs = 1000, verbose = 1)
File "C:\Program Files\Anaconda3\lib\site-packages\keras\models.py", line 1002, in fit
validation_steps=validation_steps)
File "C:\Program Files\Anaconda3\lib\site-packages\keras\engine\training.py", line 1630, in fit
batch_size=batch_size)
File "C:\Program Files\Anaconda3\lib\site-packages\keras\engine\training.py", line 1480, in _standardize_user_data
exception_prefix='target')
File "C:\Program Files\Anaconda3\lib\site-packages\keras\engine\training.py", line 113, in _standardize_input_data
'with shape ' + str(data_shape))
ValueError: Error when checking target: expected activation_1 to have 3 dimensions, but got array with shape (1437, 10)

收件人:

model.add(LSTM(10, return_sequences = True, input_shape = (8, 8)))

我假设我将不得不更改Activation('softmax')图层定义,但不确定如何更改。有人可以帮我解决这个问题吗?

TIA

1 个答案:

答案 0 :(得分:0)

我在代码中看到两个主要问题。

首先,您尝试使用softmax激活来获取LSTM单元的输出吗?与return_sequences=True一样,输出形状将为[batch_size, time_steps, 10]。您可能需要使用TimeDistributed层,以确保激活层可以将时间分布的输出作为TimeDistributed(Activation('softmax'))接受。

接下来,您的y_train的形状为[1437,10],显然没有时间分布。要使用标签来优化输出形状为[batch_size,time_steps,10]的模型,您的标签必须具有相同的形状。因此,如果所有时间步骤都重复相同的标签,我将按如下所示更改您的代码,

# Making y_train in to time distributed format, i.e. [1437,8,10]
y_train = y_train.reshape(-1,1,10)
y_train = np.repeat(y_train,8,axis=1)

model = Sequential()
model.add(LSTM(10, return_sequences = True, input_shape = (8, 8)))

# Making sure Activation layer is TimeDistributed
model.add(TimeDistributed(Activation('softmax')))

model.compile(loss = 'categorical_crossentropy', optimizer = Adam(), metrics = ['accuracy'])
model.fit(X_train, y_train, batch_size = 1, epochs = 10, verbose = 1)