图层Max_pooling1d_1不支持遮罩

时间:2019-05-13 12:58:23

标签: keras lstm max-pooling

我试图用return_sequences=True建立一个LSTM模型,这就是我想获取LSTM单元的所有输出,但是当我使用函数Masking()对顺序数据进行预处理时,发生了错误。任何申请都是感激的:)

这是我建立LSTM模型的代码,

# package importing
from keras.models import Sequential
from keras.layers import Input, Dense, LSTM, Masking, MaxPooling1D
from keras.preprocessing.sequence import pad_sequences
from keras import losses
from keras import metrics
from keras.utils import to_categorical

# hyper-argument setting
n_steps = 3
msk_value = 0
n_features = 3
n_hidden = 10

# dataset
x_train = [[[2,3,4],[4,5,6],[6,7,8]],[[2,3,4],[5,6,7]],[[5,6,7]]] # sequential data set
y_train = [1, 1, 0] # label set

# padding msk_values(zeros) to the unequal sequences
x_train = pad_sequences(x_train, maxlen=n_steps, dtype='float32', padding='post', truncating='post', value=msk_value)
x_train = x_train.reshape((-1, n_steps, n_features))
y_train = to_categorical(y_train)

model = Sequential()
model.add(Masking(mask_value=msk_value, input_shape=(n_steps, n_features)))
model.add(LSTM(n_hidden, activation='tanh', return_sequences=True)) 
model.add(MaxPooling1D(pool_size=2, stride=n_steps))  
model.add(Dense(2, activation='softmax'))

model.compile(optimizer='adam', 
    loss=losses.categorical_crossentropy,  
    metrics=[metrics.binary_accuracy])  

这是错误消息,

Using TensorFlow backend.
D:\Users\LEE\Desktop\test.py:248: UserWarning: Update your `MaxPooling1D` call to the Keras 2 API: `MaxPooling1D(pool_size=2, strides=3)`
  model.add(MaxPooling1D(pool_size=2, stride=n_steps))
Traceback (most recent call last):
  File "D:\Users\LEE\Desktop\test.py", line 256, in <module>
    problem_1()
  File "D:\Users\LEE\Desktop\test.py", line 248, in problem_1
    model.add(MaxPooling1D(pool_size=2, stride=n_steps))  
  File "E:\Software\Anaconda2\lib\site-packages\keras\engine\sequential.py", line 181, in add
    output_tensor = layer(self.outputs[0])
  File "E:\Software\Anaconda2\lib\site-packages\keras\engine\base_layer.py", line 458, in __call__
    output_mask = self.compute_mask(inputs, previous_mask)
  File "E:\Software\Anaconda2\lib\site-packages\keras\engine\base_layer.py", line 616, in compute_mask
    str(mask))
TypeError: Layer max_pooling1d_1 does not support masking, but was passed an input_mask: Tensor("masking_1/Any_1:0", shape=(?, 3), dtype=bool)
[Finished in 3.7s with exit code 1]

0 个答案:

没有答案