use Blob directly instead of shared_ptr for DropoutLayer::rand_vec_
authorJonathan L Long <jonlong@cs.berkeley.edu>
Tue, 1 Jul 2014 19:52:48 +0000 (12:52 -0700)
committerJonathan L Long <jonlong@cs.berkeley.edu>
Sun, 20 Jul 2014 01:31:32 +0000 (18:31 -0700)
This will simplify the implementation of Reshape by making allocation
automatic. There is no need for shared_ptr here, and this is more
uniform with buffers used by other layers.

include/caffe/neuron_layers.hpp
src/caffe/layers/dropout_layer.cpp
src/caffe/layers/dropout_layer.cu

index 070b890..8f0588d 100644 (file)
@@ -100,7 +100,7 @@ class DropoutLayer : public NeuronLayer<Dtype> {
   virtual void Backward_gpu(const vector<Blob<Dtype>*>& top,
       const vector<bool>& propagate_down, vector<Blob<Dtype>*>* bottom);
 
-  shared_ptr<Blob<unsigned int> > rand_vec_;
+  Blob<unsigned int> rand_vec_;
   Dtype threshold_;
   Dtype scale_;
   unsigned int uint_thres_;
index a937297..65360d6 100644 (file)
@@ -17,8 +17,8 @@ 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 Blob<unsigned int>(bottom[0]->num(),
-      bottom[0]->channels(), bottom[0]->height(), bottom[0]->width()));
+  rand_vec_.Reshape(bottom[0]->num(), bottom[0]->channels(),
+      bottom[0]->height(), bottom[0]->width());
   threshold_ = this->layer_param_.dropout_param().dropout_ratio();
   DCHECK(threshold_ > 0.);
   DCHECK(threshold_ < 1.);
@@ -31,7 +31,7 @@ Dtype 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();
-  unsigned int* mask = rand_vec_->mutable_cpu_data();
+  unsigned int* mask = rand_vec_.mutable_cpu_data();
   const int count = bottom[0]->count();
   if (Caffe::phase() == Caffe::TRAIN) {
     // Create random numbers
@@ -53,7 +53,7 @@ void DropoutLayer<Dtype>::Backward_cpu(const vector<Blob<Dtype>*>& top,
     const Dtype* top_diff = top[0]->cpu_diff();
     Dtype* bottom_diff = (*bottom)[0]->mutable_cpu_diff();
     if (Caffe::phase() == Caffe::TRAIN) {
-      const unsigned int* mask = rand_vec_->cpu_data();
+      const unsigned int* mask = 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_;
index c9f3ecd..a53eb5e 100644 (file)
@@ -32,7 +32,7 @@ Dtype DropoutLayer<Dtype>::Forward_gpu(const vector<Blob<Dtype>*>& bottom,
   const int count = bottom[0]->count();
   if (Caffe::phase() == Caffe::TRAIN) {
     unsigned int* mask =
-        static_cast<unsigned int*>(rand_vec_->mutable_gpu_data());
+        static_cast<unsigned int*>(rand_vec_.mutable_gpu_data());
     caffe_gpu_rng_uniform(count, mask);
     // set thresholds
     // NOLINT_NEXT_LINE(whitespace/operators)
@@ -63,7 +63,7 @@ void DropoutLayer<Dtype>::Backward_gpu(const vector<Blob<Dtype>*>& top,
     Dtype* bottom_diff = (*bottom)[0]->mutable_gpu_diff();
     if (Caffe::phase() == Caffe::TRAIN) {
       const unsigned int* mask =
-          static_cast<const unsigned int*>(rand_vec_->gpu_data());
+          static_cast<const unsigned int*>(rand_vec_.gpu_data());
       const int count = (*bottom)[0]->count();
       // NOLINT_NEXT_LINE(whitespace/operators)
       DropoutBackward<Dtype><<<CAFFE_GET_BLOCKS(count),