GeezerVison is showing github.com — Add set_trainable() method to freeze layers (#346) · Randl/tiny-cnn@a1c6ab0
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: docs/how_tos/How-Tos.md +11Lines changed: 11 additions & 0 deletions - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
@@ -174,6 +174,17 @@ nn.train<cross_entropy>(optimizer, train_images, train_labels, 50, 20, [](){}, | 174 | 174 |
| 175 | 175 | ### train unbalanced data | 176 | 176 |
| | 177 | +
| | 178 | +## "freeze" layers | | 179 | +
| | 180 | +You can use ```layer::set_trainable``` to exclude a layer from updating its weights. | | 181 | +
| | 182 | +```cpp | | 183 | +network<sequential> net = make_mlp({10,20,10}); | | 184 | +
| | 185 | +net[1]->set_trainable(false); // freeze 2nd layer | | 186 | +``` | | 187 | + | 177 | 188 | ## use/evaluate trained model | 178 | 189 | ### predict a value | 179 | 190 |
| |
Collapse file Copy file name to clipboard Collapse file Copy file name to clipboardExpand all lines: test/test_network.h +40Lines changed: 40 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -548,4 +548,44 @@ TEST(network, read_write) | 548 | 548 | } | 549 | 549 | } | 550 | 550 |
| | 551 | +TEST(network, trainable) { | | 552 | + auto net = make_mlp<sigmoid>({ 2,3,2,1 }); // fc(2,3) - fc(3,2) - fc(2,1) | | 553 | + | | 554 | + // trainable=false, or "freeze" 2nd layer fc(3,2) | | 555 | + net[1]->set_trainable(false); | | 556 | + | | 557 | + vec_t w0 = { 0,1,2,3,4,5 }; | | 558 | + vec_t w1 = { 6,7,8,9,8,7 }; | | 559 | + vec_t w2 = { 6,5 }; | | 560 | + | | 561 | + *net[0]->weights()[0] = { 0,1,2,3,4,5 }; | | 562 | + *net[1]->weights()[0] = { 6,7,8,9,8,7 }; | | 563 | + *net[2]->weights()[0] = { 6,5 }; | | 564 | + | | 565 | + adam a; | | 566 | + | | 567 | + net.init_weight(); | | 568 | + | | 569 | + auto w0_standby = *net[0]->weights()[0]; | | 570 | + auto w1_standby = *net[1]->weights()[0]; | | 571 | + auto w2_standby = *net[2]->weights()[0]; | | 572 | + | | 573 | + EXPECT_NE(w0, w0_standby); | | 574 | + EXPECT_EQ(w1, w1_standby); | | 575 | + EXPECT_NE(w2, w2_standby); | | 576 | + | | 577 | + std::vector<vec_t> data{ {1,0}, {0,2} }; | | 578 | + std::vector<vec_t> out{ {2}, {1} }; | | 579 | + | | 580 | + net.fit<mse>(a, data, out, 1, 1); | | 581 | + | | 582 | + auto w0_after_update = *net[0]->weights()[0]; | | 583 | + auto w1_after_update = *net[1]->weights()[0]; | | 584 | + auto w2_after_update = *net[2]->weights()[0]; | | 585 | + | | 586 | + EXPECT_NE(w0, w0_after_update); | | 587 | + EXPECT_EQ(w1, w1_after_update); | | 588 | + EXPECT_NE(w2, w2_after_update); | | 589 | +} | | 590 | + | 551 | 591 | } // namespace tiny-dnn |
Collapse file Copy file name to clipboardExpand all lines: tiny_dnn/layers/layer.h +11-1Lines changed: 11 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -82,6 +82,7 @@ class layer : public node { | 82 | 82 | out_type_(out_type) { | 83 | 83 | weight_init_ = std::make_shared<weight_init::xavier>(); | 84 | 84 | bias_init_ = std::make_shared<weight_init::constant>(); | | 85 | + trainable_ = true; | 85 | 86 | } | 86 | 87 |
| 87 | 88 | layer(const layer&) = default; | @@ -267,6 +268,9 @@ class layer : public node { | 267 | 268 |
| 268 | 269 | std::vector<vector_type> out_types() const { return out_type_; } | 269 | 270 |
| | 271 | + void set_trainable(bool trainable) { trainable_ = trainable; } | | 272 | + | | 273 | + bool trainable() const { return trainable_; } | 270 | 274 |
| 271 | 275 | /** | 272 | 276 | * return output value range | @@ -496,6 +500,11 @@ class layer : public node { | 496 | 500 | } | 497 | 501 |
| 498 | 502 | void init_weight() { | | 503 | + if (!trainable_) { | | 504 | + initialized_ = true; | | 505 | + return; | | 506 | + } | | 507 | + | 499 | 508 | for (cnn_size_t i = 0; i < in_channels_; i++) { | 500 | 509 | switch (in_type_[i]) { | 501 | 510 | case vector_type::weight: | @@ -522,7 +531,7 @@ class layer : public node { | 522 | 531 | void update_weight(optimizer *o, cnn_size_t batch_size) { | 523 | 532 | float_t rcp_batch_size = float_t(1) / float_t(batch_size); | 524 | 533 | for (size_t i = 0; i < in_type_.size(); i++) { | 525 | | - if (is_trainable_weight(in_type_[i])) { | | 534 | + if (trainable() && is_trainable_weight(in_type_[i])) { | 526 | 535 | vec_t diff; | 527 | 536 | vec_t& target = *get_weight_data(i); | 528 | 537 |
| @@ -600,6 +609,7 @@ class layer : public node { | 600 | 609 | Device* device_ptr_ = nullptr; | 601 | 610 |
| 602 | 611 | private: | | 612 | + bool trainable_; | 603 | 613 | std::shared_ptr<weight_init::function> weight_init_; | 604 | 614 | std::shared_ptr<weight_init::function> bias_init_; | 605 | 615 |
| |
|