Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

test_maxpool_dropout_layers.cpp 4.6 KB

You have to be logged in to leave a comment. Sign In
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
  1. #include <vector>
  2. #include "gtest/gtest.h"
  3. #include "caffe/blob.hpp"
  4. #include "caffe/common.hpp"
  5. #include "caffe/filler.hpp"
  6. #include "caffe/layers/dropout_layer.hpp"
  7. #include "caffe/layers/pooling_layer.hpp"
  8. #include "caffe/test/test_caffe_main.hpp"
  9. #include "caffe/test/test_gradient_check_util.hpp"
  10. namespace caffe {
  11. template <typename TypeParam>
  12. class MaxPoolingDropoutTest : public MultiDeviceTest<TypeParam> {
  13. typedef typename TypeParam::Dtype Dtype;
  14. protected:
  15. MaxPoolingDropoutTest()
  16. : blob_bottom_(new Blob<Dtype>()),
  17. blob_top_(new Blob<Dtype>()) {}
  18. virtual void SetUp() {
  19. Caffe::set_random_seed(1703);
  20. blob_bottom_->Reshape(2, 3, 6, 5);
  21. // fill the values
  22. FillerParameter filler_param;
  23. filler_param.set_value(1.);
  24. ConstantFiller<Dtype> filler(filler_param);
  25. filler.Fill(this->blob_bottom_);
  26. blob_bottom_vec_.push_back(blob_bottom_);
  27. blob_top_vec_.push_back(blob_top_);
  28. }
  29. virtual ~MaxPoolingDropoutTest() { delete blob_bottom_; delete blob_top_; }
  30. Blob<Dtype>* const blob_bottom_;
  31. Blob<Dtype>* const blob_top_;
  32. vector<Blob<Dtype>*> blob_bottom_vec_;
  33. vector<Blob<Dtype>*> blob_top_vec_;
  34. };
  35. TYPED_TEST_CASE(MaxPoolingDropoutTest, TestDtypesAndDevices);
  36. TYPED_TEST(MaxPoolingDropoutTest, TestSetup) {
  37. typedef typename TypeParam::Dtype Dtype;
  38. LayerParameter layer_param;
  39. PoolingParameter* pooling_param = layer_param.mutable_pooling_param();
  40. pooling_param->set_kernel_size(3);
  41. pooling_param->set_stride(2);
  42. PoolingLayer<Dtype> max_layer(layer_param);
  43. max_layer.SetUp(this->blob_bottom_vec_, this->blob_top_vec_);
  44. DropoutLayer<Dtype> dropout_layer(layer_param);
  45. dropout_layer.SetUp(this->blob_top_vec_, this->blob_top_vec_);
  46. EXPECT_EQ(this->blob_top_->num(), this->blob_bottom_->num());
  47. EXPECT_EQ(this->blob_top_->channels(), this->blob_bottom_->channels());
  48. EXPECT_EQ(this->blob_top_->height(), 3);
  49. EXPECT_EQ(this->blob_top_->width(), 2);
  50. }
  51. TYPED_TEST(MaxPoolingDropoutTest, TestForward) {
  52. typedef typename TypeParam::Dtype Dtype;
  53. LayerParameter layer_param;
  54. PoolingParameter* pooling_param = layer_param.mutable_pooling_param();
  55. pooling_param->set_kernel_size(3);
  56. pooling_param->set_stride(2);
  57. PoolingLayer<Dtype> layer(layer_param);
  58. layer.SetUp(this->blob_bottom_vec_, this->blob_top_vec_);
  59. layer.Forward(this->blob_bottom_vec_, this->blob_top_vec_);
  60. const Dtype* top_data = this->blob_top_->cpu_data();
  61. Dtype sum = 0.;
  62. for (int i = 0; i < this->blob_top_->count(); ++i) {
  63. sum += top_data[i];
  64. }
  65. EXPECT_EQ(sum, this->blob_top_->count());
  66. // Dropout in-place
  67. DropoutLayer<Dtype> dropout_layer(layer_param);
  68. dropout_layer.SetUp(this->blob_top_vec_, this->blob_top_vec_);
  69. dropout_layer.Forward(this->blob_top_vec_, this->blob_top_vec_);
  70. sum = 0.;
  71. Dtype scale = 1. / (1. - layer_param.dropout_param().dropout_ratio());
  72. top_data = this->blob_top_->cpu_data();
  73. for (int i = 0; i < this->blob_top_->count(); ++i) {
  74. sum += top_data[i];
  75. }
  76. EXPECT_GE(sum, 0);
  77. EXPECT_LE(sum, this->blob_top_->count()*scale);
  78. }
  79. TYPED_TEST(MaxPoolingDropoutTest, TestBackward) {
  80. typedef typename TypeParam::Dtype Dtype;
  81. LayerParameter layer_param;
  82. layer_param.set_phase(TRAIN);
  83. PoolingParameter* pooling_param = layer_param.mutable_pooling_param();
  84. pooling_param->set_kernel_size(3);
  85. pooling_param->set_stride(2);
  86. PoolingLayer<Dtype> layer(layer_param);
  87. layer.SetUp(this->blob_bottom_vec_, this->blob_top_vec_);
  88. layer.Forward(this->blob_bottom_vec_, this->blob_top_vec_);
  89. for (int i = 0; i < this->blob_top_->count(); ++i) {
  90. this->blob_top_->mutable_cpu_diff()[i] = 1.;
  91. }
  92. vector<bool> propagate_down(this->blob_bottom_vec_.size(), true);
  93. layer.Backward(this->blob_top_vec_, propagate_down,
  94. this->blob_bottom_vec_);
  95. const Dtype* bottom_diff = this->blob_bottom_->cpu_diff();
  96. Dtype sum = 0.;
  97. for (int i = 0; i < this->blob_bottom_->count(); ++i) {
  98. sum += bottom_diff[i];
  99. }
  100. EXPECT_EQ(sum, this->blob_top_->count());
  101. // Dropout in-place
  102. DropoutLayer<Dtype> dropout_layer(layer_param);
  103. dropout_layer.SetUp(this->blob_top_vec_, this->blob_top_vec_);
  104. dropout_layer.Forward(this->blob_top_vec_, this->blob_top_vec_);
  105. dropout_layer.Backward(this->blob_top_vec_, propagate_down,
  106. this->blob_top_vec_);
  107. layer.Backward(this->blob_top_vec_, propagate_down,
  108. this->blob_bottom_vec_);
  109. Dtype sum_with_dropout = 0.;
  110. bottom_diff = this->blob_bottom_->cpu_diff();
  111. for (int i = 0; i < this->blob_bottom_->count(); ++i) {
  112. sum_with_dropout += bottom_diff[i];
  113. }
  114. EXPECT_GE(sum_with_dropout, sum);
  115. }
  116. } // namespace caffe
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...