Answers.Body: <p>In most modern neural network frameworks, the update rules for training can be selectively applied to some parameters and not others. </p>
  
  <p>How to do that is dependent on the framework. Some will have the concept of "freezing" a layer, preventing parameters in it being updated. <a href="https://keras.io/getting-started/faq/#how-can-i-freeze-keras-layers" rel="nofollow noreferrer">Keras does this for example.</a> Others will do the opposite and expect you to provide a list of trainable parameters - these typically come with helpers that will list all parameters in a neural network, so you would need to add some kind of filter after collecting that data to exclude your pre-trained layer. <a href="https://gist.github.com/L0SG/2f6d81e4ad119c4f798ab81fa8d62d3f#file-freeze_example-py-L76" rel="nofollow noreferrer">PyTorch does this (although the linked example is slightly more complex in that it stops calculating gradients too)</a>.</p>
  
  <p>If your framework of choice does not allow you to select and isolate layers in the training process, then you still have a couple of options:</p>
  
  <ul>
  <li><p>You could store a copy of layer parameters that you want to keep and force your learning network to re-load these parameters after each mini-batch. This does depend on you having a method that can selectively set parameters.</p></li>
  <li><p>If your pre-trained layers are the first ones, immediately next to the input, then instead of including them in your learning network model, you can pre-process all your training data with just the fixed layers (build a model using only those layers), save the output and use that as an alternative input for the learning layers (build a second model with only the learning layers). Later, once training is complete, you can build a combined neural network out of the fixed layers and the learning layers.</p></li>
  </ul>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-01-14T15:52:58.320Z
Answers.LastActivityDate: 2020-01-14T16:01:56.587Z
Answers.LastEditDate: 2020-01-14T16:01:56.587Z
Answers.UserDisplayName: Neil Slater
Body: <p>I'm a newbie in Convolutional Neural Networks. I have found out that kernels in convolutional layers are usually learned while training.</p>
  <p>Suppose I have a kernel that is very good to extract the features that I want to extract. In that case, I don't want the kernels to be learnable. So, how can I make the kernels non-learnable and set them manually?</p>
  <p>Maybe, in that case, I have to use something different from a CNN.</p>
  
Tags: <convolutional-neural-networks><keras><image-processing><convolution><filters>
Title: How can I make the kernels non-learnable and set them manually?
UserDisplayName: VansFannel
ThreadText: # How can I make the kernels non-learnable and set them manually?
  
  (Asked by: username_0 on 2020-01-14)
  
  I'm a newbie in Convolutional Neural Networks. I have found out that kernels in convolutional layers are usually learned while training.
  
  Suppose I have a kernel that is very good to extract the features that I want to extract. In that case, I don't want the kernels to be learnable. So, how can I make the kernels non-learnable and set them manually?
  
  Maybe, in that case, I have to use something different from a CNN.
  
  **Tags:** convolutional-neural-networks, keras, image-processing, convolution, filters
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2020-01-14. Score: 2 | Accepted answer
  
  In most modern neural network frameworks, the update rules for training can be selectively applied to some parameters and not others.
  
  How to do that is dependent on the framework. Some will have the concept of "freezing" a layer, preventing parameters in it being updated. [Keras does this for example.](https://keras.io/getting-started/faq/#how-can-i-freeze-keras-layers) Others will do the opposite and expect you to provide a list of trainable parameters - these typically come with helpers that will list all parameters in a neural network, so you would need to add some kind of filter after collecting that data to exclude your pre-trained layer. [PyTorch does this (although the linked example is slightly more complex in that it stops calculating gradients too)](https://gist.github.com/L0SG/2f6d81e4ad119c4f798ab81fa8d62d3f#file-freeze_example-py-L76).
  
  If your framework of choice does not allow you to select and isolate layers in the training process, then you still have a couple of options:
  
  * You could store a copy of layer parameters that you want to keep and force your learning network to re-load these parameters after each mini-batch. This does depend on you having a method that can selectively set parameters.
  * If your pre-trained layers are the first ones, immediately next to the input, then instead of including them in your learning network model, you can pre-process all your training data with just the fixed layers (build a model using only those layers), save the output and use that as an alternative input for the learning layers (build a second model with only the learning layers). Later, once training is complete, you can build a combined neural network out of the fixed layers and the learning layers.
  
  
  ---
  
  