将图层的一半过滤器设置为不可训练的keras/tensorflow [英] Set half of the filters of a layer as not trainable keras/tensorflow

查看:89
本文介绍了将图层的一半过滤器设置为不可训练的keras/tensorflow的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试训练建议的模型研究论文中,我将卷积层的一半过滤器设置为Gabor过滤器,其余为随机权重,这些权重默认情况下已初始化.通常,如果必须将图层设置为不可训练,则将trainable属性设置为False.但是在这里,我只需要冻结一层过滤器的一半,而我不知道该怎么做.任何帮助将非常感激.我在Tensorflow后端使用Keras.

I'm trying to train a model suggested by this research paper where I set half of the filters of a convolution layer to Gabor filters and the rest are random weights which are initialized by default. Normally, if I have to set a layer as not trainable, I set the trainable attribute as False. But here I have to freeze only half of the filters of a layer and I have no idea how to do so. Any help would be really appreciated. I'm using Keras with Tensorflow backend.

推荐答案

如何使两个卷积层获得相同的输入和(几乎)相同的参数?因此,其中之一是在初始化时可训练的随机权重,而另一层则无法使用gabor滤波器训练.

How about making two convolutional layers which are getting the same input and (nearly) the same parameters? So one of them is trainable wir random weights at initialization and the other layer is non trainable with the gabor filters.

然后,您可以将两层的输出合并在一起,看起来就像是来自一个卷积网络的输出.

You could then merge the outputs of the two layers together in a way that it looks like it's the output from one convolutional network.

这是一个演示示例(您需要使用 Keras功能API ):

Here is an example for demonstration (you need to use Keras functional API):

n_filters = 32

my_input = Input(shape=...)
conv_freezed = Conv2D(n_filters/2, (3,3), ...)
conv_trainable = Conv2D(n_filters/2, (3,3), ...)

conv_freezed_out = conv_freezed(my_input)
conv_trainable_out = conv_trainable(my_input)
conv_out = concatenate([conv_freezed_out, conv_trainable_out])

# set weights and freeze the layer
conv_freezed.set_weights(...)
conv_freezed.trainable = False

这篇关于将图层的一半过滤器设置为不可训练的keras/tensorflow的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆