使用 RNN 的 Keras 损失函数中的梯度

Gradients in Keras loss function with RNNs

我有一个简单的测试LSTM模型:

inputs = Input(shape=(k, m))
layer1 = LSTM(128, activation='relu', return_sequences=True)(inputs)
layer2 = LSTM(128, activation='relu')(layer1)
predictions = Dense(1, activation='linear')(layer2)
model = Model(inputs=inputs, outputs=predictions)

以及使用输出梯度和输入的自定义损失函数:

def custom_loss(model, input_tensor):
    def loss(y_true, y_pred):
        grads = K.gradients(model.output, model.input)[0] 
        loss_f = losses.mean_squared_error(y_true, y_pred) + K.exp(-K.sum(grads))
        return loss_f

return loss

模型训练失败并出现错误"Second-order gradient for while loops not supported":

model.compile(optimizer='adam', loss=custom_loss(model_reg, inputs_reg), metrics=['mean_absolute_error'])
model_reg.fit(x_train, y_train, batch_size=32, epochs=20, verbose=1, validation_data=(x_val, y_val)) 

-----
....
      159 
      160   if op_ctxt.grad_state:
-->   161     raise TypeError("Second-order gradient for while loops not supported.")
      162 
      163   if isinstance(grad, ops.Tensor):

TypeError: Second-order gradient for while loops not supported.

为什么TF要在这里计算二阶梯度?它应该只是第一顺序。

相同的损失函数适用于非 RNN 模型。

设置展开 属性 帮助解决了问题:

layer1 = LSTM(128, activation='relu', return_sequences=True, unroll=True)(inputs)
layer2 = LSTM(128, activation='relu', unroll=True)(layer1)