然后,您可以通过传递 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_matrixmodel.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_matrixmodel.add(Conv2D(..., kernel_initializer=my_init)) # replace '...' with your argsThat 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中可能有不可训练的层吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 08-14 05:21