我正在尝试使用介于两者之间的LSTM层来实现降噪自动编码器。该体系结构如下。

FC layer -> FC layer -> LSTM cell -> FC layer -> FC layer.

我无法理解我的输入维度应如何实现此体系结构?

我尝试了以下代码
batch_size = 1
model = Sequential()
model.add(Dense(5, input_shape=(1,)))
model.add(Dense(10))
model.add(LSTM(32))
model.add(Dropout(0.3))
model.add(Dense(5))
model.add(Dense(1))
model.compile(loss='mean_squared_error', optimizer='adam')
model.fit(trainX, trainY, nb_epoch=100, batch_size=batch_size, verbose=2)

我的trainX是[650,20,1]矢量。它是仅具有一个功能的时间序列数据。

我收到以下错误
ValueError                                Traceback (most recent call last)
<ipython-input-20-1248a33f6518> in <module>()
      3 model.add(Dense(5, input_shape=(1,)))
      4 model.add(Dense(10))
----> 5 model.add(LSTM(32))
      6 model.add(Dropout(0.3))
      7 model.add(Dense(5))

/usr/local/lib/python2.7/dist-packages/keras/models.pyc in add(self, layer)
    330                  output_shapes=[self.outputs[0]._keras_shape])
    331         else:
--> 332             output_tensor = layer(self.outputs[0])
    333             if isinstance(output_tensor, list):
    334                 raise TypeError('All layers in a Sequential model '

/usr/local/lib/python2.7/dist-packages/keras/engine/topology.pyc in __call__(self, x, mask)
    527             # Raise exceptions in case the input is not compatible
    528             # with the input_spec specified in the layer constructor.
--> 529             self.assert_input_compatibility(x)
    530
    531             # Collect input shapes to build layer.

/usr/local/lib/python2.7/dist-packages/keras/engine/topology.pyc in assert_input_compatibility(self, input)
    467                                          self.name + ': expected ndim=' +
    468                                          str(spec.ndim) + ', found ndim=' +
--> 469                                          str(K.ndim(x)))
    470             if spec.dtype is not None:
    471                 if K.dtype(x) != spec.dtype:

ValueError: Input 0 is incompatible with layer lstm_10: expected ndim=3, found ndim=2

最佳答案

密集层可以将序列作为输入,它将在每个向量(最后一维)上应用相同的密集层。例子 :

您有一个二维张量输入,表示序列(timesteps, dim_features),如果您通过new_dim输出将密集层应用于该层,则该层之后的张量将是一个新序列(timesteps, new_dim)
如果您有一个可以作为文档的3D张量(n_lines, n_words, embedding_dim),具有n_lines行,每行n_words单词和每个单词的embedding_dim维度,则在其上应用new_dim输出为其施加密集层将获得一个具有(n_lines, n_words, new_dim)形状的新文档张量(3D)

您可以使用Dense()层see here输入和输出的尺寸输入和输出。

关于neural-network - 在keras或Tensorflow中的LSTM层之前添加密集层?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/42714688/

10-12 19:40