* refactor toc * refactor toc * Change to pydata-sphinx-theme and update packages requirement list for ReadtheDocs * Remove customized css for old theme * Add index page to each top bar section and limit dropdown maximum to be 4 * Use js to change 'More' to 'Libraries' * Add custom.css to conf.py for further css changes * Add BigDL logo and search bar * refactor toc * refactor toc and add overview * refactor toc and add overview * refactor toc and add overview * refactor get started * add paper and video section * add videos * add grid columns in landing page * add document roadmap to index * reapply search bar and github icon commit * reorg orca and chronos sections * Test: weaken ads by js * update: change left attrbute * update: add comments * update: change opacity to 0.7 * Remove useless theme template override for old theme * Add sidebar releases component in the home page * Remove sidebar search and restore top nav search button * Add BigDL handouts * Add back to homepage button to pages except from the home page * Update releases contents & styles in left sidebar * Add version badge to the top bar * Test: weaken ads by js * update: add comments * remove landing page contents * rfix chronos install * refactor install * refactor chronos section titles * refactor nano index * change chronos landing * revise chronos landing page * add document navigator to nano landing page * revise install landing page * Improve css of versions in sidebar * Make handouts image pointing to a page in new tab * add win guide to install * add dliib installation * revise title bar * rename index files * add index page for user guide * add dllib and orca API * update user guide landing page * refactor side bar * Remove extra style configuration of card components & make different card usage consistent * Remove extra styles for Nano how-to guides * Remove extra styles for Chronos how-to guides * Remove dark mode for now * Update index page description * Add decision tree for choosing BigDL libraries in index page * add dllib models api, revise core layers formats * Change primary & info color in light mode * Restyle card components * Restructure Chronos landing page * Update card style * Update BigDL library selection decision tree * Fix failed Chronos tutorials filter * refactor PPML documents * refactor and add friesian documents * add friesian arch diagram * update landing pages and fill key features guide index page * Restyle link card component * Style video frames in PPML sections * Adjust Nano landing page * put api docs to the last in index for convinience * Make badge horizontal padding smaller & small changes * Change the second letter of all header titles to be small capitalizd * Small changes on Chronos index page * Revise decision tree to make it smaller * Update: try to change the position of ads. * Bugfix: deleted nonexist file config * Update: update ad JS/CSS/config * Update: change ad. * Update: delete my template and change files. * Update: change chronos installation table color. * Update: change table font color to --pst-color-primary-text * Remove old contents in landing page sidebar * Restyle badge for usage in card footer again * Add quicklinks template on landing page sidebar * add quick links * Add scala logo * move tf, pytorch out of the link * change orca key features cards * fix typo * fix a mistake in wording * Restyle badge for card footer * Update decision tree * Remove useless html templates * add more api docs and update tutorials in dllib * update chronos install using new style * merge changes in nano doc from master * fix quickstart links in sidebar quicklinks * Make tables responsive * Fix overflow in api doc * Fix list indents problems in [User guide] section * Further fixes to nested bullets contents in [User Guide] section * Fix strange title in Nano 5-min doc * Fix list indent problems in [DLlib] section * Fix misnumbered list problems and other small fixes for [Chronos] section * Fix list indent problems and other small fixes for [Friesian] section * Fix list indent problem and other small fixes for [PPML] section * Fix list indent problem for developer guide * Fix list indent problem for [Cluster Serving] section * fix dllib links * Fix wrong relative link in section landing page Co-authored-by: Yuwen Hu <yuwen.hu@intel.com> Co-authored-by: Juntao Luo <1072087358@qq.com>
108 lines
3.2 KiB
Markdown
108 lines
3.2 KiB
Markdown
# Model Freeze
|
|
|
|
To "freeze" a model means to exclude some layers of model from training.
|
|
|
|
```scala
|
|
model.freeze("layer1", "layer2")
|
|
model.unFreeze("layer1", "layer2")
|
|
```
|
|
* The model can be "freezed" by calling ```freeze()```. If a model is freezed,
|
|
its parameters(weight/bias, if exists) are not changed in training process.
|
|
If model names are passed, then layers that match the given names will be freezed.
|
|
* The whole model can be "unFreezed" by calling ```unFreeze()```.
|
|
If model names are provided, then layers that match the given names will be unFreezed.
|
|
* stop the input gradient of layers that match the given names. Their input gradient are not computed.
|
|
And they will not contributed to the input gradient computation of layers that depend on them.
|
|
|
|
Original model without "freeze"
|
|
```scala
|
|
val reshape = Reshape(Array(4)).inputs()
|
|
val fc1 = Linear(4, 2).setName("fc1").inputs()
|
|
val fc2 = Linear(4, 2).setName("fc2").inputs(reshape)
|
|
val cadd_1 = CAddTable().setName("cadd").inputs(fc1, fc2)
|
|
val output1_1 = ReLU().inputs(cadd_1)
|
|
val output2_1 = Threshold(10.0).inputs(cadd_1)
|
|
|
|
val model = Graph(Array(reshape, fc1), Array(output1_1, output2_1))
|
|
|
|
val input = T(Tensor(T(0.1f, 0.2f, -0.3f, -0.4f)),
|
|
Tensor(T(0.5f, 0.4f, -0.2f, -0.1f)))
|
|
val gradOutput = T(Tensor(T(1.0f, 2.0f)), Tensor(T(3.0f, 4.0f)))
|
|
|
|
fc1.element.getParameters()._1.apply1(_ => 1.0f)
|
|
fc2.element.getParameters()._1.apply1(_ => 2.0f)
|
|
model.zeroGradParameters()
|
|
println("output1: \n", model.forward(input))
|
|
model.backward(input, gradOutput)
|
|
model.updateParameters(1)
|
|
println("fc2 weight \n", fc2.element.parameters()._1(0))
|
|
```
|
|
```
|
|
(output1:
|
|
, {
|
|
2: 0.0
|
|
0.0
|
|
[com.intel.analytics.bigdl.tensor.DenseTensor of size 2]
|
|
1: 2.8
|
|
2.8
|
|
[com.intel.analytics.bigdl.tensor.DenseTensor of size 2]
|
|
})
|
|
(fc2 weight
|
|
,1.9 1.8 2.3 2.4
|
|
1.8 1.6 2.6 2.8
|
|
[com.intel.analytics.bigdl.tensor.DenseTensor of size 2x4])
|
|
```
|
|
|
|
"Freeze" ```fc2```, the parameters of ```fc2``` is not changed.
|
|
```scala
|
|
fc1.element.getParameters()._1.apply1(_ => 1.0f)
|
|
fc2.element.getParameters()._1.apply1(_ => 2.0f)
|
|
model.zeroGradParameters()
|
|
model.freeze("fc2")
|
|
println("output2: \n", model.forward(input))
|
|
model.backward(input, gradOutput)
|
|
model.updateParameters(1)
|
|
println("fc2 weight \n", fc2.element.parameters()._1(0))
|
|
```
|
|
|
|
```
|
|
(output2:
|
|
, {
|
|
2: 0.0
|
|
0.0
|
|
[com.intel.analytics.bigdl.tensor.DenseTensor of size 2]
|
|
1: 2.8
|
|
2.8
|
|
[com.intel.analytics.bigdl.tensor.DenseTensor of size 2]
|
|
})
|
|
(fc2 weight
|
|
,2.0 2.0 2.0 2.0
|
|
2.0 2.0 2.0 2.0
|
|
[com.intel.analytics.bigdl.tensor.DenseTensor of size 2x4])
|
|
```
|
|
"unFreeze" ```fc2```, the parameters of ```fc2``` will be updated.
|
|
```scala
|
|
fc1.element.getParameters()._1.apply1(_ => 1.0f)
|
|
fc2.element.getParameters()._1.apply1(_ => 2.0f)
|
|
model.zeroGradParameters()
|
|
model.unFreeze()
|
|
println("output3: \n", model.forward(input))
|
|
model.backward(input, gradOutput)
|
|
model.updateParameters(1)
|
|
println("fc2 weight \n", fc2.element.parameters()._1(0))
|
|
```
|
|
```
|
|
(output3:
|
|
, {
|
|
2: 0.0
|
|
0.0
|
|
[com.intel.analytics.bigdl.tensor.DenseTensor of size 2]
|
|
1: 2.8
|
|
2.8
|
|
[com.intel.analytics.bigdl.tensor.DenseTensor of size 2]
|
|
})
|
|
(fc2 weight
|
|
,1.9 1.8 2.3 2.4
|
|
1.8 1.6 2.6 2.8
|
|
[com.intel.analytics.bigdl.tensor.DenseTensor of size 2x4])
|
|
```
|