GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Add set_trainable() method to freeze layers (#346) · Randl/tiny-cnn@a1c6ab0

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • docs/how_tos
    • How-Tos.md
  • test
    • picotest
    • test_network.h
  • tiny_dnn/layers
    • layer.h
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎docs/how_tos/How-Tos.md‎

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 numberDiff line numberDiff line change
@@ -174,6 +174,17 @@ nn.train<cross_entropy>(optimizer, train_images, train_labels, 50, 20, [](){},
174174
175175
### train unbalanced data
176176
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+
177188
## use/evaluate trained model
178189
### predict a value
179190

Collapse file

‎test/picotest‎

Copy file name to clipboard
Submodule picotest updated 1 file
  • picotest.h
    +22
    -13
Collapse file

‎test/test_network.h‎

Copy file name to clipboardExpand all lines: test/test_network.h
+40Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -548,4 +548,44 @@ TEST(network, read_write)
548548
}
549549
}
550550

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+
551591
} // namespace tiny-dnn
Collapse file

‎tiny_dnn/layers/layer.h‎

Copy file name to clipboardExpand all lines: tiny_dnn/layers/layer.h
+11-1Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ class layer : public node {
8282
out_type_(out_type) {
8383
weight_init_ = std::make_shared<weight_init::xavier>();
8484
bias_init_ = std::make_shared<weight_init::constant>();
85+
trainable_ = true;
8586
}
8687

8788
layer(const layer&) = default;
@@ -267,6 +268,9 @@ class layer : public node {
267268

268269
std::vector<vector_type> out_types() const { return out_type_; }
269270

271+
void set_trainable(bool trainable) { trainable_ = trainable; }
272+
273+
bool trainable() const { return trainable_; }
270274

271275
/**
272276
* return output value range
@@ -496,6 +500,11 @@ class layer : public node {
496500
}
497501

498502
void init_weight() {
503+
if (!trainable_) {
504+
initialized_ = true;
505+
return;
506+
}
507+
499508
for (cnn_size_t i = 0; i < in_channels_; i++) {
500509
switch (in_type_[i]) {
501510
case vector_type::weight:
@@ -522,7 +531,7 @@ class layer : public node {
522531
void update_weight(optimizer *o, cnn_size_t batch_size) {
523532
float_t rcp_batch_size = float_t(1) / float_t(batch_size);
524533
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])) {
526535
vec_t diff;
527536
vec_t& target = *get_weight_data(i);
528537

@@ -600,6 +609,7 @@ class layer : public node {
600609
Device* device_ptr_ = nullptr;
601610

602611
private:
612+
bool trainable_;
603613
std::shared_ptr<weight_init::function> weight_init_;
604614
std::shared_ptr<weight_init::function> bias_init_;
605615

0 commit comments

Comments
0 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.