在keras或Tensorflow中的LSTM层之前添加密集层? [英] Add dense layer before LSTM layer in keras or Tensorflow?

查看:539
本文介绍了在keras或Tensorflow中的LSTM层之前添加密集层?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to implement a denoising autoencoder with an LSTM layer in between. The architecture goes following.

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

我无法理解实现该体系结构的输入维度是什么?

I am unable to understand how my input dimension should be to implement this architecture?

我尝试了以下代码

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]矢量.这是一个只有一个功能的时间序列数据.

My trainX is [650,20,1] vector. It is a time series data in with only one feature.

我遇到以下错误

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

推荐答案

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

The dense layer can take sequences as input and it will apply the same dense layer on every vector (last dimension). Example :

您有一个表示序列(timesteps, dim_features)的2D张量输入,如果您通过new_dim输出对其施加密集层,则该层之后的张量将是一个新序列(timesteps, new_dim)

You have a 2D tensor input that represents a sequence (timesteps, dim_features), if you apply a dense layer to it with new_dim outputs, the tensor that you will have after the layer will be a new sequence (timesteps, new_dim)

如果您有一个3D张量(n_lines, n_words, embedding_dim)可以作为文档,具有n_lines行,每行n_words个单词和每个单词embedding_dim尺寸,则使用new_dim输出对其施加密集层将得到您将使用形状为(n_lines, n_words, new_dim)

If you have a 3D tensor (n_lines, n_words, embedding_dim) that can be a document, with n_lines lines, n_words words per lines and embedding_dim dimensions for each word, applying a dense layer to it with new_dim outputs will get you a new doc tensor (3D) with shape (n_lines, n_words, new_dim)

您可以在此处查看您可以通过Dense()层获取和获取的尺寸输入和输出.

You can see here the dimensions input and output that you can feed and get with the Dense() layer.

这篇关于在keras或Tensorflow中的LSTM层之前添加密集层?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆