Keras:在特定层之后停止渐变

标签 keras

假设您有Keras NN模型,如何在特定层之后的反向传播中停止梯度?

即,如果我们有一个带有两个输出的模型:

input_layer = Input(shape=(10,10,3))

x = Convolution2D(...)(input_layer)
x = Activation('relu')(x)

x = Flatten()(x)

x_1 = Dense(64)(x)
x_1 = Dense(32)(x_1)
x_1 = Dense(2)(x_1)

x_2 = Dense(64)(x)
x_2 = Dense(32)(x_2)
x_2 = Dense(2)(x_2)

model = Model(inputs=input_layer, outputs=[x_1, x_2])

如何在x_1层之后停止输出x_1 = Dense(64)(x)的梯度,以免在卷积层的权重更新中不计入它?

根据Stopping Gradient back prop through a particular layer in keras中的答案,我会在x_1密集层之前添加一个lambda层,但是我不确定:

x_1 = Dense(64)(x)
x_1_stop_grad = Lambda(lambda x: K.stop_gradient(x))(x_1)
x_1 = Dense(32)(x_1)
x_1 = Dense(2)(x_1)

我是否必须在第一个密集x_1层的之前添加lamtda层或在之后添加

最佳答案

由于渐变是通过网络反向流动的,因此您需要在不应该​​到达渐变的图层之后直接添加渐变停止图层。

IE。

# weights in x should not be updated by gradients from x_1
x = Convolution2D(...)(input_layer) 
x_1_stop_grad = Lambda(lambda x: K.stop_gradient(x))(x)
x_1 = Dense(64)(x_1_stop_grad)
x_1 = Dense(32)(x_1)
...

关于Keras:在特定层之后停止渐变,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50931591/

相关文章:

keras - 在 Keras 中嵌入层的偏置权重

neural-network - 值错误 : Input 0 is incompatible with layer dense_6 in keras

python - 如何对长度不等的事件和时间序列生成进行建模

python - ValueError : Input 0 of layer sequential is incompatible with the layer: : expected min_ndim=4, 发现 ndim=3。收到的完整形状 : [8, 28, 28]

keras:在模型中加载保存的模型权重以进行评估

python - 使用 tensorflow.keras.backend.shape 时,'Tensor' 对象没有属性 'is_initialized'

python - 如何使用 keras image_ocr 示例预测 OCR 的结果?

python - Keras 中的深度自动编码器将一个维度转换为另一个维度

python - 神经网络模型精度超低

python - 让 Keras/Tensorflow 输出 OneHotCategorical,但操作没有梯度