python - Keras:约束链接输入和输出

标签 python tensorflow constraints keras

有没有一种方法可以对序列的预测施加约束?

比如说,如果我的建模如下:

model = Sequential()
model.add(LSTM(150, input_shape=(n_timesteps_in, n_features)))
model.add(RepeatVector(n_timesteps_in))
model.add(LSTM(150, return_sequences=True))
model.add(TimeDistributed(Dense(n_features, activation='linear')))
model.compile(loss='mean_squared_error', optimizer='adam', metrics=['acc'])

我能以某种方式捕获 model.pred(x) <= x 的约束吗?

docs表明我们可以为网络权重添加约束。但是,他们没有提到如何映射输入和输出之间的关系或约束。

最佳答案

从未听说过....但是您可以通过多种方式使用函数式 API 模型和自定义函数自行实现它。

下面有一个可能的答案,但首先,这真的是最好的做法吗??

如果你正在尝试创建一个自动编码器,你不应该关心限制输出。否则,您的模型不会真正学到很多东西。

也许最好的做法是先将输入标准化(在 -1 和 +1 之间),然后在最后使用 tanh 激活函数。

保留输入的功能 API 模型:

inputTensor = Input(n_timesteps_in, n_features)

out = LSTM(150, input_shape=)(inputTensor)
out = RepeatVector(n_timesteps_in)(out) #this line sounds funny in your model...
out = LSTM(150, return_sequences=True)(out)
out = TimeDistributed(Dense(n_features))(out)
out = Activation(chooseOneActivation)(out)

out = Lambda(chooseACustomFunction)([out,inputTensor])

model = Model(inputTensor,out)
model.compile(...)

自定义限制选项

有无数种方法可以做到这一点,这里有一些示例可能是您需要的,也可能不是您需要的。但是你可以自由地开发任何类似的东西。

以下选项将单个输出限制为各自的单个输入。但是您可能更愿意使用限制在最大输入范围内的所有输出。

如果是这样,请使用下面的代码:maxInput = max(originalInput, axis=1, keepdims=True)

1 - 一个简单的拉伸(stretch)“tanh”:

您可以通过使用 tanh(范围从 -1 到 +1)并将其乘以输入来简单地定义上限和下限。

使用 Activation('tanh') 层,以及 Lambda 层中的以下自定义函数:

import keras.backend as K

def stretchedTanh(x):

    originalOutput = x[0]
    originalInput = x[1]

    return K.abs(originalInput) * originalOutput

我不完全确定这是一个健康的选择。如果想法是创建一个自动编码器,这个模型将很容易找到一个解决方案,输出所有 tanh 激活尽可能接近 1,而无需真正查看输入。

2 - 修改后的'relu'

首先,您可以根据输入简单地剪辑您的输出,更改relu 激活。在上面的模型中使用 Activation('relu')(out),并在 Lambda 层中使用以下自定义函数:

def modifiedRelu(x):
    negativeOutput = (-1) * x[0] #ranging from -infinite to 0
    originalInput = x[1] 

    #ranging from -infinite to originalInput
    return negativeOutput + originalInput #needs the same shape between input and output

当一切都超过限制并且反向传播无法返回时,这可能会产生不利影响。 ('relu' 可能会发生的问题)。

3 - 半线性,半修正 tanh

在这种情况下,您不需要Activation层,或者您可以将其用作'linear'

import keras.backend as K

def halfTanh(x):

    originalOutput = x[0]
    originalInput = x[1]  #assuming all inputs are positive

    #find the positive outputs and get a tensor with 1's at their positions
    positiveOutputs = K.greater(originalOuptut,0)
    positiveOutputs = K.cast(positiveOutputs,K.floatx())

    #now the 1's are at the negative positions
    negativeOutputs = 1 - positiveOutputs

    tanhOutputs = K.tanh(originalOutput) #function limited to -1 or +1
    tanhOutputs = originalInput * sigmoidOutputs #raises the limit from 1 to originalInput

    #use the conditions above to select between the negative and the positive side
    return positiveOutputs * tanhOutputs + negativeOutputs * originalOutputs

关于python - Keras:约束链接输入和输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47848606/

相关文章:

python - 使用 python 包 bokeh 绘制直方图时,如何控制图中使用的颜色?

Python - 转换错误

python - NameError:名称 'KEYDOWN' 未定义

python - socket.recvmsg 忽略 ancbufsize、辅助数据

ios - 如何使用 Objective-C 在约束条件下绘图

ios - 在 swift 中使用约束的 UICollectionView 单元格固定宽度和动态高度

python - 为什么 Python 中的 CNN 比 Matlab 中的表现差很多?

python - 加载 keras 模型时产生的警告

numpy - 数据集中模式的随机化

android-layout - 这个 View 没有约束,它只有设计时的位置,所以它会跳到 (0,0) 除非你添加约束