summaryrefslogtreecommitdiff
path: root/src/caffe/layers/dropout_layer.cpp
diff options
context:
space:
mode:
authorEric Tzeng <eric.s.tzeng@gmail.com>2014-02-26 18:45:45 -0800
committerEric Tzeng <eric.s.tzeng@gmail.com>2014-02-26 18:45:45 -0800
commitb17ac6620b4e6ae33d4d889b6cdbde1c447bb944 (patch)
tree537d1c964fed4191b424c105328a1c6f72281c4a /src/caffe/layers/dropout_layer.cpp
parentde4f7a419f176a49d77b2a0506c78333d988058c (diff)
downloadcaffeonacl-b17ac6620b4e6ae33d4d889b6cdbde1c447bb944.tar.gz
caffeonacl-b17ac6620b4e6ae33d4d889b6cdbde1c447bb944.tar.bz2
caffeonacl-b17ac6620b4e6ae33d4d889b6cdbde1c447bb944.zip
Splitting source files between CUDA and CPU code.
Diffstat (limited to 'src/caffe/layers/dropout_layer.cpp')
-rw-r--r--src/caffe/layers/dropout_layer.cpp63
1 files changed, 63 insertions, 0 deletions
diff --git a/src/caffe/layers/dropout_layer.cpp b/src/caffe/layers/dropout_layer.cpp
new file mode 100644
index 00000000..4e1fbfae
--- /dev/null
+++ b/src/caffe/layers/dropout_layer.cpp
@@ -0,0 +1,63 @@
+// Copyright 2013 Yangqing Jia
+
+#include "caffe/common.hpp"
+#include "caffe/layer.hpp"
+#include "caffe/syncedmem.hpp"
+#include "caffe/vision_layers.hpp"
+
+namespace caffe {
+
+template <typename Dtype>
+void DropoutLayer<Dtype>::SetUp(const vector<Blob<Dtype>*>& bottom,
+ vector<Blob<Dtype>*>* top) {
+ NeuronLayer<Dtype>::SetUp(bottom, top);
+ // Set up the cache for random number generation
+ rand_vec_.reset(new SyncedMemory(bottom[0]->count() * sizeof(int)));
+ threshold_ = this->layer_param_.dropout_ratio();
+ DCHECK(threshold_ > 0.);
+ DCHECK(threshold_ < 1.);
+ scale_ = 1. / (1. - threshold_);
+ uint_thres_ = (unsigned int)(UINT_MAX * threshold_);
+}
+
+template <typename Dtype>
+void DropoutLayer<Dtype>::Forward_cpu(const vector<Blob<Dtype>*>& bottom,
+ vector<Blob<Dtype>*>* top) {
+ const Dtype* bottom_data = bottom[0]->cpu_data();
+ Dtype* top_data = (*top)[0]->mutable_cpu_data();
+ int* mask = reinterpret_cast<int*>(rand_vec_->mutable_cpu_data());
+ const int count = bottom[0]->count();
+ if (Caffe::phase() == Caffe::TRAIN) {
+ // Create random numbers
+ viRngBernoulli(VSL_RNG_METHOD_BERNOULLI_ICDF, Caffe::vsl_stream(),
+ count, mask, 1. - threshold_);
+ for (int i = 0; i < count; ++i) {
+ top_data[i] = bottom_data[i] * mask[i] * scale_;
+ }
+ } else {
+ memcpy(top_data, bottom_data, bottom[0]->count() * sizeof(Dtype));
+ }
+}
+
+template <typename Dtype>
+Dtype DropoutLayer<Dtype>::Backward_cpu(const vector<Blob<Dtype>*>& top,
+ const bool propagate_down,
+ vector<Blob<Dtype>*>* bottom) {
+ CHECK(Caffe::phase() == Caffe::TRAIN);
+ if (propagate_down) {
+ const Dtype* top_diff = top[0]->cpu_diff();
+ Dtype* bottom_diff = (*bottom)[0]->mutable_cpu_diff();
+ const int* mask = reinterpret_cast<const int*>(rand_vec_->cpu_data());
+ const int count = (*bottom)[0]->count();
+ for (int i = 0; i < count; ++i) {
+ bottom_diff[i] = top_diff[i] * mask[i] * scale_;
+ }
+ }
+ return Dtype(0);
+}
+
+
+INSTANTIATE_CLASS(DropoutLayer);
+
+
+} // namespace caffe