如何强制LSTM学习单调序列?

时间:2018-04-27 14:47:33

标签: keras constraints lstm forecasting

我使用LSTM和Keras预测一组序列。这是我的基本模型:

    <ol>
    <ng-container *ngFor="let path of listOfPaths">
      <li>
        <p>{{path}}</p>
    </li>
    </ng-container>
  </ol>

我确信序列从0开始并且是单调的(不是递减的)。 我尝试使用Maximum()图层

inputs = Input(shape=(1,seq_dim))       #seq_dim = 2
# shape = (timesteps, featdim) = (1,2) since my input sequences are pair of values
# I want to predict the sequence of the fist values in the pairs

se = LSTM(lstm_size)(inputs)   

out = Dense(1)(se)   # I want to forecast one value
model = Model(inputs=inputs, outputs=out)

这里的模型

max_out = Maximum()([output_seq,input_seq])

然而,在编译模型时会出现错误:

inputs = Input(shape=(1,seq_dim))       
# shape = (timesteps, featdim) = (1,2) since my input sequences are pair of values
# I want to predict the sequence of the fist values in the pairs

se = LSTM(lstm_size)(inputs)   

out = Dense(1)(se)   # I want to forecast one value
# max between the output and the previous value of the sequence (current input)
max_out = Maximum()([out,inputs[:,:,0]]) 
model = Model(inputs=inputs, outputs=max_out)

我也尝试使用Lambda图层,但它会引发同样的错误。

"AttributeError: 'Tensor' object has no attribute '_keras_history'"

如何将此约束添加到我的模型中?是否有可能在架构定义(我正在尝试做)或编辑损失函数? 提前谢谢

1 个答案:

答案 0 :(得分:1)

试试这个 max_out = Lambda( lambda oi: K_BACKEND.maximum( oi[0], oi[1][:,:,0], axis=-1)),output_shape=lambda oi : oi[0] )([out,inputs])