在Keras中可能有不可训练的层吗? [英] Is it possible to have non-trainable layer in Keras?

查看:172
本文介绍了在Keras中可能有不可训练的层吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想计算诸如模糊或重采样之类的常数卷积,并希望它在训练过程中永远不变.

我可以将卷积核初始化为常数并将其排除在Keras训练之外吗?

更具体地说,我不想将其用于文档中声明的目的.我想以这种方式实现残差网络:一个分支执行正常的可训练卷积,而并行分支执行一些常数,例如求平均值.

解决方案

您应该能够将trainable = False参数传递给图层定义,或者在创建图层后设置layer.trainable = False属性.在后一种情况下,您需要根据事实进行编译.请参见此处的常见问题解答.

然后,您可以通过传递 kernel_initializer = initializer参数来为图层设置恒定权重.可以在此处找到有关初始化程序的更多信息.如果您已经在某处定义了权重矩阵,那么我认为您需要定义一个自定义的初始化程序,以将权重设置为所需的值.该链接在底部显示了如何定义自定义初始化程序.假设您已定义my_constant_weight_matrix,则可以使用以下简单的方法:

def my_init(shape, dtype=None):
    # Note it must take arguments 'shape' and 'dtype'.
    return my_constant_weight_matrix
model.add(Conv2D(..., kernel_initializer=my_init))  # replace '...' with your args

那是说,我尚未验证,当我进行Google搜索时,我看到很多错误报告弹出,提示层冻结无法正常工作.不过值得一试.

I would like to calculate constant convolution like blurring or resampling and want it never change during training.

Can I initialize convolution kernel to constant and exclude it from training in Keras?

More specifically, I don't want to use this for purposes declared in the doc. I want to implement residual network this way: one branch does normal trainable convolution, while parallel branch does something constant, like averaging.

解决方案

You should be able to pass a trainable = False argument to your layer definition, or set the layer.trainable = False property after creating your layer. In the latter case you need to compile after the fact. See the FAQ here.

You can then set constant weights for the layer by passing a kernel_initializer = initializer argument. More information on initializers can be found here. If you have a weight matrix already defined somewhere, I think you will need to define a custom initializer that sets the weights to your desired values. The link shows how to define custom initializers at the bottom. Something as simple as the following might work, assuming you have my_constant_weight_matrix defined:

def my_init(shape, dtype=None):
    # Note it must take arguments 'shape' and 'dtype'.
    return my_constant_weight_matrix
model.add(Conv2D(..., kernel_initializer=my_init))  # replace '...' with your args

That said, I have not verified, and when I did a Google search I saw a lot of bug reports pop up about layer freezing not working correctly. Worth a shot though.

这篇关于在Keras中可能有不可训练的层吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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