keras中使用LSTM实现一对多和多对多

版权声明:著作权归作者所有,任何形式的转载都请联系作者获得授权并注明出处。 https://blog.csdn.net/ChaoFeiLi/article/details/89319410
  • 一、前言

 1、TimeDistributed和Dense的使用
下面代码是keras里面给出的解释:


        # as the first layer in a model
        model = Sequential()
        model.add(TimeDistributed(Dense(8), input_shape=(10, 16)))
        # now model.output_shape == (None, 10, 8)

从上述代码中可以发现,TimeDistributed和Dense一起配合使用,主要应用于一对多,多对多的情况。

input_shape = (10,16),表示步长是10,每一步的维度为16,(即:每一个数据的属性长度为16))

首先使用TimeDistributed(Dense(8),input_shape = (10,16))把每一步的维度为16变成8,不改变步长的大小

若该层的批输入形状然后(50, 10, 16),则这一层之后的输出为(50, 10, 8)

2、RepeatVector的使用

这个是keras官网给出的解释

```python
        model = Sequential()
        model.add(Dense(32, input_dim=32))
        # now: model.output_shape == (None, 32)
        # note: `None` is the batch dimension

        model.add(RepeatVector(3))
        # now: model.output_shape == (None, 3, 32)
    ```

解释:如果输入的形状为(None,32),经过添加RepeatVector(3)层之后,输出变为(None,3,32),RepeatVector不改变我们的步长,改变我们的每一步的维数(即:属性长度)

  • 二、一对多

from keras.models import Sequential
from keras.layers import LSTM,Dense,TimeDistributed,RepeatVector

batch_size = 50
n_in = 1
vector = 10
n_out = 5
model = Sequential()
model.add(LSTM(150, batch_input_shape=(batch_size, n_in, vector), stateful=True))
model.add(RepeatVector(n_out))
model.add(LSTM(150, return_sequences=True, stateful=True))
model.add(TimeDistributed(Dense(vector, activation='softmax')))
print(model.summary())

解释:输入的数据结构为(50,1,10),输出的数据结构为(50,5,10),实现了一对多

  • 三、多对多

3.1、多对多之输入与输出的序列长度相同

from keras.models import Sequential
from keras.layers import LSTM, Dense, TimeDistributed, RepeatVector

batch_size = 50
in_steps = 10
in_vector = 10
out_vector = 100
model = Sequential()
model.add(LSTM(150, batch_input_shape=(batch_size, in_steps, in_vector), stateful=True, return_sequences=True))
model.add(TimeDistributed(Dense(out_vector, activation='softmax')))
print(model.summary())

解释:输入的数据结构为(50,10,10),输出的数据结构为(50,10,100),实现了多对多中的输入与输入序列长度相同

3.2、多对多之输入与输出的序列长度不同

from keras.models import Sequential
from keras.layers import LSTM, Dense, TimeDistributed, RepeatVector

batch_size = 50
in_steps = 10
in_vector = 10
out_vector = 100
model = Sequential()
model.add(LSTM(150, batch_input_shape=(batch_size, in_steps, in_vector), stateful=True))
model.add(RepeatVector(20))
model.add(TimeDistributed(Dense(out_vector, activation='softmax')))
print(model.summary())

解释:输入的数据结构为(50,10,10),输出的数据结构为(50,20,100),实现了多对多中的输入与输入序列长度不同

部分代码解释:

model.add(LSTM(150, batch_input_shape=(batch_size, in_steps, in_vector), stateful=True))
model.add(RepeatVector(20))

在LSTM后面添加RepeatVector层的时候,必须保障没有在LSTM中没有return_sequences=True,因为RepeatVector需要的是二维输入,而不是三维

model.add(LSTM(150, batch_input_shape=(batch_size, in_steps, in_vector), stateful=True,return_sequences=True))
model.add(TimeDistributed(Dense(out_vector, activation='softmax')))

在LSTM后面添加TimeDistributed层的时候,必须保障在LSTM中有return_sequences=True,因为TimeDistributed需要的是三维输入。

小贴士:

RepeatVector主要作用于步长

TimeDistributed主要作用于每一步向量长度(即:属性大小)

猜你喜欢

转载自blog.csdn.net/ChaoFeiLi/article/details/89319410
今日推荐