forked from BVLC/caffe
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_neuron_layer.cpp
More file actions
533 lines (485 loc) · 18.9 KB
/
Copy pathtest_neuron_layer.cpp
File metadata and controls
533 lines (485 loc) · 18.9 KB
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
#include <cstring>
#include <vector>
#include "google/protobuf/text_format.h"
#include "gtest/gtest.h"
#include "caffe/blob.hpp"
#include "caffe/common.hpp"
#include "caffe/filler.hpp"
#include "caffe/vision_layers.hpp"
#include "caffe/test/test_caffe_main.hpp"
#include "caffe/test/test_gradient_check_util.hpp"
namespace caffe {
template <typename TypeParam>
class NeuronLayerTest : public MultiDeviceTest<TypeParam> {
typedef typename TypeParam::Dtype Dtype;
protected:
NeuronLayerTest()
: blob_bottom_(new Blob<Dtype>(2, 3, 4, 5)),
blob_top_(new Blob<Dtype>()) {
Caffe::set_random_seed(1701);
// fill the values
FillerParameter filler_param;
GaussianFiller<Dtype> filler(filler_param);
filler.Fill(this->blob_bottom_);
blob_bottom_vec_.push_back(blob_bottom_);
blob_top_vec_.push_back(blob_top_);
}
virtual ~NeuronLayerTest() { delete blob_bottom_; delete blob_top_; }
Blob<Dtype>* const blob_bottom_;
Blob<Dtype>* const blob_top_;
vector<Blob<Dtype>*> blob_bottom_vec_;
vector<Blob<Dtype>*> blob_top_vec_;
void TestDropoutForward(const float dropout_ratio) {
LayerParameter layer_param;
// Fill in the given dropout_ratio, unless it's 0.5, in which case we don't
// set it explicitly to test that 0.5 is the default.
if (dropout_ratio != 0.5) {
layer_param.mutable_dropout_param()->set_dropout_ratio(dropout_ratio);
}
Caffe::set_phase(Caffe::TRAIN);
DropoutLayer<Dtype> layer(layer_param);
layer.SetUp(this->blob_bottom_vec_, this->blob_top_vec_);
layer.Forward(this->blob_bottom_vec_, this->blob_top_vec_);
// Now, check values
const Dtype* bottom_data = this->blob_bottom_->cpu_data();
const Dtype* top_data = this->blob_top_->cpu_data();
float scale = 1. / (1. - layer_param.dropout_param().dropout_ratio());
const int count = this->blob_bottom_->count();
// Initialize num_kept to count the number of inputs NOT dropped out.
int num_kept = 0;
for (int i = 0; i < count; ++i) {
if (top_data[i] != 0) {
++num_kept;
EXPECT_EQ(top_data[i], bottom_data[i] * scale);
}
}
const Dtype std_error = sqrt(dropout_ratio * (1 - dropout_ratio) / count);
// Fail if the number dropped was more than 1.96 * std_error away from the
// expected number -- requires 95% confidence that the dropout layer is not
// obeying the given dropout_ratio for test failure.
const Dtype empirical_dropout_ratio = 1 - num_kept / Dtype(count);
EXPECT_NEAR(empirical_dropout_ratio, dropout_ratio, 1.96 * std_error);
}
void TestExpForward(const float base, const float scale, const float shift) {
LayerParameter layer_param;
layer_param.mutable_exp_param()->set_base(base);
layer_param.mutable_exp_param()->set_scale(scale);
layer_param.mutable_exp_param()->set_shift(shift);
ExpLayer<Dtype> layer(layer_param);
layer.SetUp(blob_bottom_vec_, blob_top_vec_);
layer.Forward(blob_bottom_vec_, blob_top_vec_);
const Dtype kDelta = 2e-4;
const Dtype* bottom_data = blob_bottom_->cpu_data();
const Dtype* top_data = blob_top_->cpu_data();
for (int i = 0; i < blob_bottom_->count(); ++i) {
const Dtype bottom_val = bottom_data[i];
const Dtype top_val = top_data[i];
if (base == -1) {
EXPECT_NEAR(top_val, exp(shift + scale * bottom_val), kDelta);
} else {
EXPECT_NEAR(top_val, pow(base, shift + scale * bottom_val), kDelta);
}
}
}
void TestExpGradient(const float base, const float scale, const float shift) {
LayerParameter layer_param;
layer_param.mutable_exp_param()->set_base(base);
layer_param.mutable_exp_param()->set_scale(scale);
layer_param.mutable_exp_param()->set_shift(shift);
ExpLayer<Dtype> layer(layer_param);
GradientChecker<Dtype> checker(1e-2, 1e-3);
checker.CheckGradientEltwise(&layer, blob_bottom_vec_, blob_top_vec_);
}
};
TYPED_TEST_CASE(NeuronLayerTest, TestDtypesAndDevices);
TYPED_TEST(NeuronLayerTest, TestAbsVal) {
typedef typename TypeParam::Dtype Dtype;
LayerParameter layer_param;
AbsValLayer<Dtype> layer(layer_param);
layer.SetUp(this->blob_bottom_vec_, this->blob_top_vec_);
layer.Forward(this->blob_bottom_vec_, this->blob_top_vec_);
const Dtype* bottom_data = this->blob_bottom_->cpu_data();
const Dtype* top_data = this->blob_top_->cpu_data();
const int count = this->blob_bottom_->count();
for (int i = 0; i < count; ++i) {
EXPECT_EQ(top_data[i], fabs(bottom_data[i]));
}
}
TYPED_TEST(NeuronLayerTest, TestAbsGradient) {
typedef typename TypeParam::Dtype Dtype;
LayerParameter layer_param;
AbsValLayer<Dtype> layer(layer_param);
GradientChecker<Dtype> checker(1e-2, 1e-3, 1701, 0., 0.01);
checker.CheckGradientEltwise(&layer, this->blob_bottom_vec_,
this->blob_top_vec_);
}
TYPED_TEST(NeuronLayerTest, TestReLU) {
typedef typename TypeParam::Dtype Dtype;
LayerParameter layer_param;
ReLULayer<Dtype> layer(layer_param);
layer.SetUp(this->blob_bottom_vec_, this->blob_top_vec_);
layer.Forward(this->blob_bottom_vec_, this->blob_top_vec_);
// Now, check values
const Dtype* bottom_data = this->blob_bottom_->cpu_data();
const Dtype* top_data = this->blob_top_->cpu_data();
for (int i = 0; i < this->blob_bottom_->count(); ++i) {
EXPECT_GE(top_data[i], 0.);
EXPECT_TRUE(top_data[i] == 0 || top_data[i] == bottom_data[i]);
}
}
TYPED_TEST(NeuronLayerTest, TestReLUGradient) {
typedef typename TypeParam::Dtype Dtype;
LayerParameter layer_param;
ReLULayer<Dtype> layer(layer_param);
GradientChecker<Dtype> checker(1e-2, 1e-3, 1701, 0., 0.01);
checker.CheckGradientEltwise(&layer, this->blob_bottom_vec_,
this->blob_top_vec_);
}
TYPED_TEST(NeuronLayerTest, TestReLUWithNegativeSlope) {
typedef typename TypeParam::Dtype Dtype;
LayerParameter layer_param;
CHECK(google::protobuf::TextFormat::ParseFromString(
"relu_param { negative_slope: 0.01 }", &layer_param));
ReLULayer<Dtype> layer(layer_param);
layer.SetUp(this->blob_bottom_vec_, this->blob_top_vec_);
layer.Forward(this->blob_bottom_vec_, this->blob_top_vec_);
// Now, check values
const Dtype* bottom_data = this->blob_bottom_->cpu_data();
const Dtype* top_data = this->blob_top_->cpu_data();
for (int i = 0; i < this->blob_bottom_->count(); ++i) {
if (top_data[i] >= 0) {
EXPECT_FLOAT_EQ(top_data[i], bottom_data[i]);
} else {
EXPECT_FLOAT_EQ(top_data[i], bottom_data[i] * 0.01);
}
}
}
TYPED_TEST(NeuronLayerTest, TestReLUGradientWithNegativeSlope) {
typedef typename TypeParam::Dtype Dtype;
LayerParameter layer_param;
CHECK(google::protobuf::TextFormat::ParseFromString(
"relu_param { negative_slope: 0.01 }", &layer_param));
ReLULayer<Dtype> layer(layer_param);
GradientChecker<Dtype> checker(1e-2, 1e-3, 1701, 0., 0.01);
checker.CheckGradientEltwise(&layer, this->blob_bottom_vec_,
this->blob_top_vec_);
}
TYPED_TEST(NeuronLayerTest, TestSigmoid) {
typedef typename TypeParam::Dtype Dtype;
LayerParameter layer_param;
SigmoidLayer<Dtype> layer(layer_param);
layer.SetUp(this->blob_bottom_vec_, this->blob_top_vec_);
layer.Forward(this->blob_bottom_vec_, this->blob_top_vec_);
// Now, check values
const Dtype* bottom_data = this->blob_bottom_->cpu_data();
const Dtype* top_data = this->blob_top_->cpu_data();
for (int i = 0; i < this->blob_bottom_->count(); ++i) {
EXPECT_FLOAT_EQ(top_data[i], 1. / (1 + exp(-bottom_data[i])));
// check that we squashed the value between 0 and 1
EXPECT_GE(top_data[i], 0.);
EXPECT_LE(top_data[i], 1.);
}
}
TYPED_TEST(NeuronLayerTest, TestSigmoidGradient) {
typedef typename TypeParam::Dtype Dtype;
LayerParameter layer_param;
SigmoidLayer<Dtype> layer(layer_param);
GradientChecker<Dtype> checker(1e-2, 1e-3, 1701, 0., 0.01);
checker.CheckGradientEltwise(&layer, this->blob_bottom_vec_,
this->blob_top_vec_);
}
TYPED_TEST(NeuronLayerTest, TestTanH) {
typedef typename TypeParam::Dtype Dtype;
LayerParameter layer_param;
TanHLayer<Dtype> layer(layer_param);
layer.SetUp(this->blob_bottom_vec_, this->blob_top_vec_);
layer.Forward(this->blob_bottom_vec_, this->blob_top_vec_);
// Test exact values
for (int i = 0; i < this->blob_bottom_->num(); ++i) {
for (int j = 0; j < this->blob_bottom_->channels(); ++j) {
for (int k = 0; k < this->blob_bottom_->height(); ++k) {
for (int l = 0; l < this->blob_bottom_->width(); ++l) {
EXPECT_GE(this->blob_top_->data_at(i, j, k, l) + 1e-4,
(exp(2*this->blob_bottom_->data_at(i, j, k, l)) - 1) /
(exp(2*this->blob_bottom_->data_at(i, j, k, l)) + 1));
EXPECT_LE(this->blob_top_->data_at(i, j, k, l) - 1e-4,
(exp(2*this->blob_bottom_->data_at(i, j, k, l)) - 1) /
(exp(2*this->blob_bottom_->data_at(i, j, k, l)) + 1));
}
}
}
}
}
TYPED_TEST(NeuronLayerTest, TestTanHGradient) {
typedef typename TypeParam::Dtype Dtype;
LayerParameter layer_param;
TanHLayer<Dtype> layer(layer_param);
GradientChecker<Dtype> checker(1e-2, 1e-3);
checker.CheckGradientEltwise(&layer, this->blob_bottom_vec_,
this->blob_top_vec_);
}
TYPED_TEST(NeuronLayerTest, TestExpLayer) {
typedef typename TypeParam::Dtype Dtype;
// Test default base of "-1" -- should actually set base := e.
const Dtype kBase = -1;
const Dtype kScale = 1;
const Dtype kShift = 0;
this->TestExpForward(kBase, kScale, kShift);
}
TYPED_TEST(NeuronLayerTest, TestExpGradient) {
typedef typename TypeParam::Dtype Dtype;
// Test default base of "-1" -- should actually set base := e.
const Dtype kBase = -1;
const Dtype kScale = 1;
const Dtype kShift = 0;
this->TestExpGradient(kBase, kScale, kShift);
}
TYPED_TEST(NeuronLayerTest, TestExpLayerBase2) {
typedef typename TypeParam::Dtype Dtype;
const Dtype kBase = 2;
const Dtype kScale = 1;
const Dtype kShift = 0;
this->TestExpForward(kBase, kScale, kShift);
}
TYPED_TEST(NeuronLayerTest, TestExpGradientBase2) {
typedef typename TypeParam::Dtype Dtype;
const Dtype kBase = 2;
const Dtype kScale = 1;
const Dtype kShift = 0;
this->TestExpGradient(kBase, kScale, kShift);
}
TYPED_TEST(NeuronLayerTest, TestExpLayerBase2Shift1) {
typedef typename TypeParam::Dtype Dtype;
const Dtype kBase = 2;
const Dtype kScale = 1;
const Dtype kShift = 1;
this->TestExpForward(kBase, kScale, kShift);
}
TYPED_TEST(NeuronLayerTest, TestExpGradientBase2Shift1) {
typedef typename TypeParam::Dtype Dtype;
const Dtype kBase = 2;
const Dtype kScale = 1;
const Dtype kShift = 1;
this->TestExpGradient(kBase, kScale, kShift);
}
TYPED_TEST(NeuronLayerTest, TestExpLayerBase2Scale3) {
typedef typename TypeParam::Dtype Dtype;
const Dtype kBase = 2;
const Dtype kScale = 3;
const Dtype kShift = 0;
this->TestExpForward(kBase, kScale, kShift);
}
TYPED_TEST(NeuronLayerTest, TestExpGradientBase2Scale3) {
typedef typename TypeParam::Dtype Dtype;
const Dtype kBase = 2;
const Dtype kScale = 3;
const Dtype kShift = 0;
this->TestExpGradient(kBase, kScale, kShift);
}
TYPED_TEST(NeuronLayerTest, TestExpLayerBase2Shift1Scale3) {
typedef typename TypeParam::Dtype Dtype;
const Dtype kBase = 2;
const Dtype kScale = 3;
const Dtype kShift = 1;
this->TestExpForward(kBase, kScale, kShift);
}
TYPED_TEST(NeuronLayerTest, TestExpGradientBase2Shift1Scale3) {
typedef typename TypeParam::Dtype Dtype;
const Dtype kBase = 2;
const Dtype kScale = 3;
const Dtype kShift = 1;
this->TestExpGradient(kBase, kScale, kShift);
}
TYPED_TEST(NeuronLayerTest, TestDropoutHalf) {
const float kDropoutRatio = 0.5;
this->TestDropoutForward(kDropoutRatio);
}
TYPED_TEST(NeuronLayerTest, TestDropoutThreeQuarters) {
const float kDropoutRatio = 0.75;
this->TestDropoutForward(kDropoutRatio);
}
TYPED_TEST(NeuronLayerTest, TestDropoutTestPhase) {
typedef typename TypeParam::Dtype Dtype;
LayerParameter layer_param;
Caffe::set_phase(Caffe::TEST);
DropoutLayer<Dtype> layer(layer_param);
layer.SetUp(this->blob_bottom_vec_, this->blob_top_vec_);
layer.Forward(this->blob_bottom_vec_, this->blob_top_vec_);
// Now, check values
const Dtype* bottom_data = this->blob_bottom_->cpu_data();
const Dtype* top_data = this->blob_top_->cpu_data();
for (int i = 0; i < this->blob_bottom_->count(); ++i) {
if (top_data[i] != 0) {
EXPECT_EQ(top_data[i], bottom_data[i]);
}
}
}
TYPED_TEST(NeuronLayerTest, TestDropoutGradient) {
typedef typename TypeParam::Dtype Dtype;
LayerParameter layer_param;
Caffe::set_phase(Caffe::TRAIN);
DropoutLayer<Dtype> layer(layer_param);
GradientChecker<Dtype> checker(1e-2, 1e-3);
checker.CheckGradientEltwise(&layer, this->blob_bottom_vec_,
this->blob_top_vec_);
}
TYPED_TEST(NeuronLayerTest, TestDropoutGradientTest) {
typedef typename TypeParam::Dtype Dtype;
LayerParameter layer_param;
Caffe::set_phase(Caffe::TEST);
DropoutLayer<Dtype> layer(layer_param);
GradientChecker<Dtype> checker(1e-2, 1e-3);
checker.CheckGradientEltwise(&layer, this->blob_bottom_vec_,
this->blob_top_vec_);
}
TYPED_TEST(NeuronLayerTest, TestBNLL) {
typedef typename TypeParam::Dtype Dtype;
LayerParameter layer_param;
BNLLLayer<Dtype> layer(layer_param);
layer.SetUp(this->blob_bottom_vec_, this->blob_top_vec_);
layer.Forward(this->blob_bottom_vec_, this->blob_top_vec_);
// Now, check values
const Dtype* bottom_data = this->blob_bottom_->cpu_data();
const Dtype* top_data = this->blob_top_->cpu_data();
for (int i = 0; i < this->blob_bottom_->count(); ++i) {
EXPECT_GE(top_data[i], 0.);
EXPECT_GE(top_data[i], bottom_data[i]);
}
}
TYPED_TEST(NeuronLayerTest, TestBNLLGradient) {
typedef typename TypeParam::Dtype Dtype;
LayerParameter layer_param;
BNLLLayer<Dtype> layer(layer_param);
GradientChecker<Dtype> checker(1e-2, 1e-3);
checker.CheckGradientEltwise(&layer, this->blob_bottom_vec_,
this->blob_top_vec_);
}
#ifdef USE_CUDNN
template <typename Dtype>
class CuDNNNeuronLayerTest : public ::testing::Test {
protected:
CuDNNNeuronLayerTest()
: blob_bottom_(new Blob<Dtype>(2, 3, 4, 5)),
blob_top_(new Blob<Dtype>()) {
Caffe::set_random_seed(1701);
// fill the values
FillerParameter filler_param;
GaussianFiller<Dtype> filler(filler_param);
filler.Fill(this->blob_bottom_);
blob_bottom_vec_.push_back(blob_bottom_);
blob_top_vec_.push_back(blob_top_);
}
virtual ~CuDNNNeuronLayerTest() { delete blob_bottom_; delete blob_top_; }
Blob<Dtype>* const blob_bottom_;
Blob<Dtype>* const blob_top_;
vector<Blob<Dtype>*> blob_bottom_vec_;
vector<Blob<Dtype>*> blob_top_vec_;
};
TYPED_TEST_CASE(CuDNNNeuronLayerTest, TestDtypes);
TYPED_TEST(CuDNNNeuronLayerTest, TestReLUCuDNN) {
Caffe::set_mode(Caffe::GPU);
LayerParameter layer_param;
CuDNNReLULayer<TypeParam> layer(layer_param);
layer.SetUp(this->blob_bottom_vec_, this->blob_top_vec_);
layer.Forward(this->blob_bottom_vec_, this->blob_top_vec_);
// Now, check values
const TypeParam* bottom_data = this->blob_bottom_->cpu_data();
const TypeParam* top_data = this->blob_top_->cpu_data();
for (int i = 0; i < this->blob_bottom_->count(); ++i) {
EXPECT_GE(top_data[i], 0.);
EXPECT_TRUE(top_data[i] == 0 || top_data[i] == bottom_data[i]);
}
}
TYPED_TEST(CuDNNNeuronLayerTest, TestReLUGradientCuDNN) {
Caffe::set_mode(Caffe::GPU);
LayerParameter layer_param;
CuDNNReLULayer<TypeParam> layer(layer_param);
GradientChecker<TypeParam> checker(1e-2, 1e-3, 1701, 0., 0.01);
checker.CheckGradientEltwise(&layer, this->blob_bottom_vec_,
this->blob_top_vec_);
}
TYPED_TEST(CuDNNNeuronLayerTest, TestReLUWithNegativeSlopeCuDNN) {
Caffe::set_mode(Caffe::GPU);
LayerParameter layer_param;
CHECK(google::protobuf::TextFormat::ParseFromString(
"relu_param { negative_slope: 0.01 }", &layer_param));
CuDNNReLULayer<TypeParam> layer(layer_param);
layer.SetUp(this->blob_bottom_vec_, this->blob_top_vec_);
layer.Forward(this->blob_bottom_vec_, this->blob_top_vec_);
// Now, check values
const TypeParam* bottom_data = this->blob_bottom_->cpu_data();
const TypeParam* top_data = this->blob_top_->cpu_data();
for (int i = 0; i < this->blob_bottom_->count(); ++i) {
if (top_data[i] >= 0) {
EXPECT_FLOAT_EQ(top_data[i], bottom_data[i]);
} else {
EXPECT_FLOAT_EQ(top_data[i], bottom_data[i] * 0.01);
}
}
}
TYPED_TEST(CuDNNNeuronLayerTest, TestReLUGradientWithNegativeSlopeCuDNN) {
Caffe::set_mode(Caffe::GPU);
LayerParameter layer_param;
CHECK(google::protobuf::TextFormat::ParseFromString(
"relu_param { negative_slope: 0.01 }", &layer_param));
CuDNNReLULayer<TypeParam> layer(layer_param);
GradientChecker<TypeParam> checker(1e-2, 1e-3, 1701, 0., 0.01);
checker.CheckGradientEltwise(&layer, this->blob_bottom_vec_,
this->blob_top_vec_);
}
TYPED_TEST(CuDNNNeuronLayerTest, TestSigmoidCuDNN) {
Caffe::set_mode(Caffe::GPU);
LayerParameter layer_param;
CuDNNSigmoidLayer<TypeParam> layer(layer_param);
layer.SetUp(this->blob_bottom_vec_, this->blob_top_vec_);
layer.Forward(this->blob_bottom_vec_, this->blob_top_vec_);
// Now, check values
const TypeParam* bottom_data = this->blob_bottom_->cpu_data();
const TypeParam* top_data = this->blob_top_->cpu_data();
for (int i = 0; i < this->blob_bottom_->count(); ++i) {
EXPECT_FLOAT_EQ(top_data[i], 1. / (1 + exp(-bottom_data[i])));
// check that we squashed the value between 0 and 1
EXPECT_GE(top_data[i], 0.);
EXPECT_LE(top_data[i], 1.);
}
}
TYPED_TEST(CuDNNNeuronLayerTest, TestSigmoidGradientCuDNN) {
Caffe::set_mode(Caffe::GPU);
LayerParameter layer_param;
CuDNNSigmoidLayer<TypeParam> layer(layer_param);
GradientChecker<TypeParam> checker(1e-2, 1e-3, 1701, 0., 0.01);
checker.CheckGradientEltwise(&layer, this->blob_bottom_vec_,
this->blob_top_vec_);
}
TYPED_TEST(CuDNNNeuronLayerTest, TestTanHCuDNN) {
Caffe::set_mode(Caffe::GPU);
LayerParameter layer_param;
CuDNNTanHLayer<TypeParam> layer(layer_param);
layer.SetUp(this->blob_bottom_vec_, this->blob_top_vec_);
layer.Forward(this->blob_bottom_vec_, this->blob_top_vec_);
// Test exact values
for (int i = 0; i < this->blob_bottom_->num(); ++i) {
for (int j = 0; j < this->blob_bottom_->channels(); ++j) {
for (int k = 0; k < this->blob_bottom_->height(); ++k) {
for (int l = 0; l < this->blob_bottom_->width(); ++l) {
EXPECT_GE(this->blob_top_->data_at(i, j, k, l) + 1e-4,
(exp(2*this->blob_bottom_->data_at(i, j, k, l)) - 1) /
(exp(2*this->blob_bottom_->data_at(i, j, k, l)) + 1));
EXPECT_LE(this->blob_top_->data_at(i, j, k, l) - 1e-4,
(exp(2*this->blob_bottom_->data_at(i, j, k, l)) - 1) /
(exp(2*this->blob_bottom_->data_at(i, j, k, l)) + 1));
}
}
}
}
}
TYPED_TEST(CuDNNNeuronLayerTest, TestTanHGradientCuDNN) {
Caffe::set_mode(Caffe::GPU);
LayerParameter layer_param;
CuDNNTanHLayer<TypeParam> layer(layer_param);
GradientChecker<TypeParam> checker(1e-2, 1e-3);
checker.CheckGradientEltwise(&layer, this->blob_bottom_vec_,
this->blob_top_vec_);
}
#endif
} // namespace caffe