virtual void SetUp(const vector<Blob<Dtype>*>& bottom,
vector<Blob<Dtype>*>* top) = 0;
+
// Forward and backward wrappers. You should implement the cpu and
// gpu specific implementations instead, and should not change these
// functions.
- inline void Forward(const vector<Blob<Dtype>*>& bottom,
+ inline Dtype Forward(const vector<Blob<Dtype>*>& bottom,
vector<Blob<Dtype>*>* top);
- inline Dtype Backward(const vector<Blob<Dtype>*>& top,
+ inline void Backward(const vector<Blob<Dtype>*>& top,
const bool propagate_down,
vector<Blob<Dtype>*>* bottom);
vector<shared_ptr<Blob<Dtype> > > blobs_;
// Forward functions
- virtual void Forward_cpu(const vector<Blob<Dtype>*>& bottom,
+ virtual Dtype Forward_cpu(const vector<Blob<Dtype>*>& bottom,
vector<Blob<Dtype>*>* top) = 0;
// If no gpu code is provided, we will simply use cpu code.
- virtual void Forward_gpu(const vector<Blob<Dtype>*>& bottom,
+ virtual Dtype Forward_gpu(const vector<Blob<Dtype>*>& bottom,
vector<Blob<Dtype>*>* top) {
// LOG(WARNING) << "Using CPU code as backup.";
- Forward_cpu(bottom, top);
+ return Forward_cpu(bottom, top);
}
// Backward functions: the backward function will compute the gradients for
// any parameters and also for the bottom blobs if propagate_down is true.
// It will return the loss produced from this layer.
- virtual Dtype Backward_cpu(const vector<Blob<Dtype>*>& top,
+ virtual void Backward_cpu(const vector<Blob<Dtype>*>& top,
const bool propagate_down,
vector<Blob<Dtype>*>* bottom) = 0;
- virtual Dtype Backward_gpu(const vector<Blob<Dtype>*>& top,
+ virtual void Backward_gpu(const vector<Blob<Dtype>*>& top,
const bool propagate_down,
vector<Blob<Dtype>*>* bottom) {
// LOG(WARNING) << "Using CPU code as backup.";
- return Backward_cpu(top, propagate_down, bottom);
+ Backward_cpu(top, propagate_down, bottom);
}
DISABLE_COPY_AND_ASSIGN(Layer);
// gpu specific implementations instead, and should not change these
// functions.
template <typename Dtype>
-inline void Layer<Dtype>::Forward(const vector<Blob<Dtype>*>& bottom,
+inline Dtype Layer<Dtype>::Forward(const vector<Blob<Dtype>*>& bottom,
vector<Blob<Dtype>*>* top) {
switch (Caffe::mode()) {
case Caffe::CPU:
- Forward_cpu(bottom, top);
- break;
+ return Forward_cpu(bottom, top);
case Caffe::GPU:
- Forward_gpu(bottom, top);
- break;
+ return Forward_gpu(bottom, top);
default:
LOG(FATAL) << "Unknown caffe mode.";
+ return Dtype(0);
}
}
template <typename Dtype>
-inline Dtype Layer<Dtype>::Backward(const vector<Blob<Dtype>*>& top,
+inline void Layer<Dtype>::Backward(const vector<Blob<Dtype>*>& top,
const bool propagate_down,
vector<Blob<Dtype>*>* bottom) {
switch (Caffe::mode()) {
case Caffe::CPU:
- return Backward_cpu(top, propagate_down, bottom);
+ Backward_cpu(top, propagate_down, bottom);
+ break;
case Caffe::GPU:
- return Backward_gpu(top, propagate_down, bottom);
+ Backward_gpu(top, propagate_down, bottom);
+ break;
default:
LOG(FATAL) << "Unknown caffe mode.";
}
// Run forward with the input blobs already fed separately. You can get the
// input blobs using input_blobs().
+ const vector<Blob<Dtype>*>& ForwardPrefilled(Dtype* loss);
const vector<Blob<Dtype>*>& ForwardPrefilled();
// Run forward using a set of bottom blobs, and return the result.
+ const vector<Blob<Dtype>*>& Forward(const vector<Blob<Dtype>* > & bottom,
+ Dtype* loss);
const vector<Blob<Dtype>*>& Forward(const vector<Blob<Dtype>* > & bottom);
// Run forward using a serialized BlobProtoVector and return the result
// as a serialized BlobProtoVector
- string Forward(const string& input_blob_protos);
+ string Forward(const string& input_blob_protos, Dtype* loss);
// The network backward should take no input and output, since it solely
// computes the gradient w.r.t the parameters, and the data has already
// been provided during the forward pass.
- Dtype Backward();
+ void Backward();
Dtype ForwardBackward(const vector<Blob<Dtype>* > & bottom) {
- Forward(bottom);
- return Backward();
+ Dtype loss;
+ Forward(bottom, &loss);
+ Backward();
+ return loss;
}
// Updates the network weights based on the diff values computed.
: NeuronLayer<Dtype>(param) {}
protected:
- virtual void Forward_cpu(const vector<Blob<Dtype>*>& bottom,
+ virtual Dtype Forward_cpu(const vector<Blob<Dtype>*>& bottom,
vector<Blob<Dtype>*>* top);
- virtual void Forward_gpu(const vector<Blob<Dtype>*>& bottom,
+ virtual Dtype Forward_gpu(const vector<Blob<Dtype>*>& bottom,
vector<Blob<Dtype>*>* top);
- virtual Dtype Backward_cpu(const vector<Blob<Dtype>*>& top,
+ virtual void Backward_cpu(const vector<Blob<Dtype>*>& top,
const bool propagate_down, vector<Blob<Dtype>*>* bottom);
- virtual Dtype Backward_gpu(const vector<Blob<Dtype>*>& top,
+ virtual void Backward_gpu(const vector<Blob<Dtype>*>& top,
const bool propagate_down, vector<Blob<Dtype>*>* bottom);
};
: NeuronLayer<Dtype>(param) {}
protected:
- virtual void Forward_cpu(const vector<Blob<Dtype>*>& bottom,
+ virtual Dtype Forward_cpu(const vector<Blob<Dtype>*>& bottom,
vector<Blob<Dtype>*>* top);
- virtual void Forward_gpu(const vector<Blob<Dtype>*>& bottom,
+ virtual Dtype Forward_gpu(const vector<Blob<Dtype>*>& bottom,
vector<Blob<Dtype>*>* top);
- virtual Dtype Backward_cpu(const vector<Blob<Dtype>*>& top,
+ virtual void Backward_cpu(const vector<Blob<Dtype>*>& top,
const bool propagate_down, vector<Blob<Dtype>*>* bottom);
- virtual Dtype Backward_gpu(const vector<Blob<Dtype>*>& top,
+ virtual void Backward_gpu(const vector<Blob<Dtype>*>& top,
const bool propagate_down, vector<Blob<Dtype>*>* bottom);
};
: NeuronLayer<Dtype>(param) {}
protected:
- virtual void Forward_cpu(const vector<Blob<Dtype>*>& bottom,
+ virtual Dtype Forward_cpu(const vector<Blob<Dtype>*>& bottom,
vector<Blob<Dtype>*>* top);
- virtual void Forward_gpu(const vector<Blob<Dtype>*>& bottom,
+ virtual Dtype Forward_gpu(const vector<Blob<Dtype>*>& bottom,
vector<Blob<Dtype>*>* top);
- virtual Dtype Backward_cpu(const vector<Blob<Dtype>*>& top,
+ virtual void Backward_cpu(const vector<Blob<Dtype>*>& top,
const bool propagate_down, vector<Blob<Dtype>*>* bottom);
- virtual Dtype Backward_gpu(const vector<Blob<Dtype>*>& top,
+ virtual void Backward_gpu(const vector<Blob<Dtype>*>& top,
const bool propagate_down, vector<Blob<Dtype>*>* bottom);
};
: NeuronLayer<Dtype>(param) {}
protected:
- virtual void Forward_cpu(const vector<Blob<Dtype>*>& bottom,
+ virtual Dtype Forward_cpu(const vector<Blob<Dtype>*>& bottom,
vector<Blob<Dtype>*>* top);
- virtual void Forward_gpu(const vector<Blob<Dtype>*>& bottom,
+ virtual Dtype Forward_gpu(const vector<Blob<Dtype>*>& bottom,
vector<Blob<Dtype>*>* top);
- virtual Dtype Backward_cpu(const vector<Blob<Dtype>*>& top,
+ virtual void Backward_cpu(const vector<Blob<Dtype>*>& top,
const bool propagate_down, vector<Blob<Dtype>*>* bottom);
- virtual Dtype Backward_gpu(const vector<Blob<Dtype>*>& top,
+ virtual void Backward_gpu(const vector<Blob<Dtype>*>& top,
const bool propagate_down, vector<Blob<Dtype>*>* bottom);
};
vector<Blob<Dtype>*>* top);
protected:
- virtual void Forward_cpu(const vector<Blob<Dtype>*>& bottom,
+ virtual Dtype Forward_cpu(const vector<Blob<Dtype>*>& bottom,
vector<Blob<Dtype>*>* top);
- virtual void Forward_gpu(const vector<Blob<Dtype>*>& bottom,
+ virtual Dtype Forward_gpu(const vector<Blob<Dtype>*>& bottom,
vector<Blob<Dtype>*>* top);
- virtual Dtype Backward_cpu(const vector<Blob<Dtype>*>& top,
+ virtual void Backward_cpu(const vector<Blob<Dtype>*>& top,
const bool propagate_down, vector<Blob<Dtype>*>* bottom);
- virtual Dtype Backward_gpu(const vector<Blob<Dtype>*>& top,
+ virtual void Backward_gpu(const vector<Blob<Dtype>*>& top,
const bool propagate_down, vector<Blob<Dtype>*>* bottom);
shared_ptr<SyncedMemory> rand_vec_;
float threshold_;
vector<Blob<Dtype>*>* top);
protected:
- virtual void Forward_cpu(const vector<Blob<Dtype>*>& bottom,
+ virtual Dtype Forward_cpu(const vector<Blob<Dtype>*>& bottom,
vector<Blob<Dtype>*>* top);
- virtual void Forward_gpu(const vector<Blob<Dtype>*>& bottom,
+ virtual Dtype Forward_gpu(const vector<Blob<Dtype>*>& bottom,
vector<Blob<Dtype>*>* top);
- virtual Dtype Backward_cpu(const vector<Blob<Dtype>*>& top,
+ virtual void Backward_cpu(const vector<Blob<Dtype>*>& top,
const bool propagate_down, vector<Blob<Dtype>*>* bottom);
- virtual Dtype Backward_gpu(const vector<Blob<Dtype>*>& top,
+ virtual void Backward_gpu(const vector<Blob<Dtype>*>& top,
const bool propagate_down, vector<Blob<Dtype>*>* bottom);
int count_;
};
vector<Blob<Dtype>*>* top);
protected:
- virtual void Forward_cpu(const vector<Blob<Dtype>*>& bottom,
+ virtual Dtype Forward_cpu(const vector<Blob<Dtype>*>& bottom,
vector<Blob<Dtype>*>* top);
- virtual void Forward_gpu(const vector<Blob<Dtype>*>& bottom,
+ virtual Dtype Forward_gpu(const vector<Blob<Dtype>*>& bottom,
vector<Blob<Dtype>*>* top);
- virtual Dtype Backward_cpu(const vector<Blob<Dtype>*>& top,
+ virtual void Backward_cpu(const vector<Blob<Dtype>*>& top,
const bool propagate_down, vector<Blob<Dtype>*>* bottom);
- virtual Dtype Backward_gpu(const vector<Blob<Dtype>*>& top,
+ virtual void Backward_gpu(const vector<Blob<Dtype>*>& top,
const bool propagate_down, vector<Blob<Dtype>*>* bottom);
int count_;
};
vector<Blob<Dtype>*>* top);
protected:
- virtual void Forward_cpu(const vector<Blob<Dtype>*>& bottom,
+ virtual Dtype Forward_cpu(const vector<Blob<Dtype>*>& bottom,
vector<Blob<Dtype>*>* top);
- virtual void Forward_gpu(const vector<Blob<Dtype>*>& bottom,
+ virtual Dtype Forward_gpu(const vector<Blob<Dtype>*>& bottom,
vector<Blob<Dtype>*>* top);
- virtual Dtype Backward_cpu(const vector<Blob<Dtype>*>& top,
+ virtual void Backward_cpu(const vector<Blob<Dtype>*>& top,
const bool propagate_down, vector<Blob<Dtype>*>* bottom);
- virtual Dtype Backward_gpu(const vector<Blob<Dtype>*>& top,
+ virtual void Backward_gpu(const vector<Blob<Dtype>*>& top,
const bool propagate_down, vector<Blob<Dtype>*>* bottom);
int M_;
int K_;
vector<Blob<Dtype>*>* top);
protected:
- virtual void Forward_cpu(const vector<Blob<Dtype>*>& bottom,
+ virtual Dtype Forward_cpu(const vector<Blob<Dtype>*>& bottom,
vector<Blob<Dtype>*>* top);
- virtual void Forward_gpu(const vector<Blob<Dtype>*>& bottom,
+ virtual Dtype Forward_gpu(const vector<Blob<Dtype>*>& bottom,
vector<Blob<Dtype>*>* top);
- virtual Dtype Backward_cpu(const vector<Blob<Dtype>*>& top,
+ virtual void Backward_cpu(const vector<Blob<Dtype>*>& top,
const bool propagate_down, vector<Blob<Dtype>*>* bottom);
- virtual Dtype Backward_gpu(const vector<Blob<Dtype>*>& top,
+ virtual void Backward_gpu(const vector<Blob<Dtype>*>& top,
const bool propagate_down, vector<Blob<Dtype>*>* bottom);
// scale_ stores the intermediate summing results
Blob<Dtype> scale_;
vector<Blob<Dtype>*>* top);
protected:
- virtual void Forward_cpu(const vector<Blob<Dtype>*>& bottom,
+ virtual Dtype Forward_cpu(const vector<Blob<Dtype>*>& bottom,
vector<Blob<Dtype>*>* top);
- virtual void Forward_gpu(const vector<Blob<Dtype>*>& bottom,
+ virtual Dtype Forward_gpu(const vector<Blob<Dtype>*>& bottom,
vector<Blob<Dtype>*>* top);
- virtual Dtype Backward_cpu(const vector<Blob<Dtype>*>& top,
+ virtual void Backward_cpu(const vector<Blob<Dtype>*>& top,
const bool propagate_down, vector<Blob<Dtype>*>* bottom);
- virtual Dtype Backward_gpu(const vector<Blob<Dtype>*>& top,
+ virtual void Backward_gpu(const vector<Blob<Dtype>*>& top,
const bool propagate_down, vector<Blob<Dtype>*>* bottom);
int KSIZE_;
int STRIDE_;
vector<Blob<Dtype>*>* top);
protected:
- virtual void Forward_cpu(const vector<Blob<Dtype>*>& bottom,
+ virtual Dtype Forward_cpu(const vector<Blob<Dtype>*>& bottom,
vector<Blob<Dtype>*>* top);
- virtual void Forward_gpu(const vector<Blob<Dtype>*>& bottom,
+ virtual Dtype Forward_gpu(const vector<Blob<Dtype>*>& bottom,
vector<Blob<Dtype>*>* top);
- virtual Dtype Backward_cpu(const vector<Blob<Dtype>*>& top,
+ virtual void Backward_cpu(const vector<Blob<Dtype>*>& top,
const bool propagate_down, vector<Blob<Dtype>*>* bottom);
- virtual Dtype Backward_gpu(const vector<Blob<Dtype>*>& top,
+ virtual void Backward_gpu(const vector<Blob<Dtype>*>& top,
const bool propagate_down, vector<Blob<Dtype>*>* bottom);
int KSIZE_;
int STRIDE_;
vector<Blob<Dtype>*>* top);
protected:
- virtual void Forward_cpu(const vector<Blob<Dtype>*>& bottom,
+ virtual Dtype Forward_cpu(const vector<Blob<Dtype>*>& bottom,
vector<Blob<Dtype>*>* top);
- virtual void Forward_gpu(const vector<Blob<Dtype>*>& bottom,
+ virtual Dtype Forward_gpu(const vector<Blob<Dtype>*>& bottom,
vector<Blob<Dtype>*>* top);
- virtual Dtype Backward_cpu(const vector<Blob<Dtype>*>& top,
+ virtual void Backward_cpu(const vector<Blob<Dtype>*>& top,
const bool propagate_down, vector<Blob<Dtype>*>* bottom);
- virtual Dtype Backward_gpu(const vector<Blob<Dtype>*>& top,
+ virtual void Backward_gpu(const vector<Blob<Dtype>*>& top,
const bool propagate_down, vector<Blob<Dtype>*>* bottom);
Blob<Dtype> col_bob_;
vector<Blob<Dtype>*>* top);
protected:
- virtual void Forward_cpu(const vector<Blob<Dtype>*>& bottom,
+ virtual Dtype Forward_cpu(const vector<Blob<Dtype>*>& bottom,
vector<Blob<Dtype>*>* top);
- virtual void Forward_gpu(const vector<Blob<Dtype>*>& bottom,
+ virtual Dtype Forward_gpu(const vector<Blob<Dtype>*>& bottom,
vector<Blob<Dtype>*>* top);
- virtual Dtype Backward_cpu(const vector<Blob<Dtype>*>& top,
- const bool propagate_down, vector<Blob<Dtype>*>* bottom);
- virtual Dtype Backward_gpu(const vector<Blob<Dtype>*>& top,
- const bool propagate_down, vector<Blob<Dtype>*>* bottom);
+ virtual void Backward_cpu(const vector<Blob<Dtype>*>& top,
+ const bool propagate_down, vector<Blob<Dtype>*>* bottom) { return; }
+ virtual void Backward_gpu(const vector<Blob<Dtype>*>& top,
+ const bool propagate_down, vector<Blob<Dtype>*>* bottom) { return; }
shared_ptr<leveldb::DB> db_;
shared_ptr<leveldb::Iterator> iter_;
vector<Blob<Dtype>*>* top);
protected:
- virtual void Forward_cpu(const vector<Blob<Dtype>*>& bottom,
+ virtual Dtype Forward_cpu(const vector<Blob<Dtype>*>& bottom,
vector<Blob<Dtype>*>* top);
- virtual void Forward_gpu(const vector<Blob<Dtype>*>& bottom,
+ virtual Dtype Forward_gpu(const vector<Blob<Dtype>*>& bottom,
vector<Blob<Dtype>*>* top);
- virtual Dtype Backward_cpu(const vector<Blob<Dtype>*>& top,
+ virtual void Backward_cpu(const vector<Blob<Dtype>*>& top,
const bool propagate_down, vector<Blob<Dtype>*>* bottom);
- virtual Dtype Backward_gpu(const vector<Blob<Dtype>*>& top,
+ virtual void Backward_gpu(const vector<Blob<Dtype>*>& top,
const bool propagate_down, vector<Blob<Dtype>*>* bottom);
virtual void load_hdf5_file_data(const char* filename);
vector<Blob<Dtype>*>* top);
protected:
- virtual void Forward_cpu(const vector<Blob<Dtype>*>& bottom,
+ virtual Dtype Forward_cpu(const vector<Blob<Dtype>*>& bottom,
vector<Blob<Dtype>*>* top);
- virtual void Forward_gpu(const vector<Blob<Dtype>*>& bottom,
+ virtual Dtype Forward_gpu(const vector<Blob<Dtype>*>& bottom,
vector<Blob<Dtype>*>* top);
- virtual Dtype Backward_cpu(const vector<Blob<Dtype>*>& top,
+ virtual void Backward_cpu(const vector<Blob<Dtype>*>& top,
const bool propagate_down, vector<Blob<Dtype>*>* bottom);
- virtual Dtype Backward_gpu(const vector<Blob<Dtype>*>& top,
+ virtual void Backward_gpu(const vector<Blob<Dtype>*>& top,
const bool propagate_down, vector<Blob<Dtype>*>* bottom);
// sum_multiplier is just used to carry out sum using blas
protected:
// The loss layer will do nothing during forward - all computation are
// carried out in the backward pass.
- virtual void Forward_cpu(const vector<Blob<Dtype>*>& bottom,
- vector<Blob<Dtype>*>* top) { return; }
- virtual void Forward_gpu(const vector<Blob<Dtype>*>& bottom,
- vector<Blob<Dtype>*>* top) { return; }
- virtual Dtype Backward_cpu(const vector<Blob<Dtype>*>& top,
+ virtual Dtype Forward_cpu(const vector<Blob<Dtype>*>& bottom,
+ vector<Blob<Dtype>*>* top);
+ // virtual Dtype Forward_gpu(const vector<Blob<Dtype>*>& bottom,
+ // vector<Blob<Dtype>*>* top);
+ virtual void Backward_cpu(const vector<Blob<Dtype>*>& top,
const bool propagate_down, vector<Blob<Dtype>*>* bottom);
- // virtual Dtype Backward_gpu(const vector<Blob<Dtype>*>& top,
+ // virtual void Backward_gpu(const vector<Blob<Dtype>*>& top,
// const bool propagate_down, vector<Blob<Dtype>*>* bottom);
};
protected:
// The loss layer will do nothing during forward - all computation are
// carried out in the backward pass.
- virtual void Forward_cpu(const vector<Blob<Dtype>*>& bottom,
- vector<Blob<Dtype>*>* top) { return; }
- virtual void Forward_gpu(const vector<Blob<Dtype>*>& bottom,
- vector<Blob<Dtype>*>* top) { return; }
- virtual Dtype Backward_cpu(const vector<Blob<Dtype>*>& top,
+ virtual Dtype Forward_cpu(const vector<Blob<Dtype>*>& bottom,
+ vector<Blob<Dtype>*>* top);
+ // virtual Dtype Forward_gpu(const vector<Blob<Dtype>*>& bottom,
+ // vector<Blob<Dtype>*>* top);
+ virtual void Backward_cpu(const vector<Blob<Dtype>*>& top,
const bool propagate_down, vector<Blob<Dtype>*>* bottom);
- // virtual Dtype Backward_gpu(const vector<Blob<Dtype>*>& top,
+ // virtual void Backward_gpu(const vector<Blob<Dtype>*>& top,
// const bool propagate_down, vector<Blob<Dtype>*>* bottom);
Blob<Dtype> infogain_;
vector<Blob<Dtype>*>* top);
protected:
- virtual void Forward_cpu(const vector<Blob<Dtype>*>& bottom,
+ virtual Dtype Forward_cpu(const vector<Blob<Dtype>*>& bottom,
vector<Blob<Dtype>*>* top);
- virtual void Forward_gpu(const vector<Blob<Dtype>*>& bottom,
+ virtual Dtype Forward_gpu(const vector<Blob<Dtype>*>& bottom,
vector<Blob<Dtype>*>* top);
- virtual Dtype Backward_cpu(const vector<Blob<Dtype>*>& top,
+ virtual void Backward_cpu(const vector<Blob<Dtype>*>& top,
const bool propagate_down, vector<Blob<Dtype>*>* bottom);
- virtual Dtype Backward_gpu(const vector<Blob<Dtype>*>& top,
+ virtual void Backward_gpu(const vector<Blob<Dtype>*>& top,
const bool propagate_down, vector<Blob<Dtype>*>* bottom);
shared_ptr<SoftmaxLayer<Dtype> > softmax_layer_;
protected:
// The loss layer will do nothing during forward - all computation are
// carried out in the backward pass.
- virtual void Forward_cpu(const vector<Blob<Dtype>*>& bottom,
- vector<Blob<Dtype>*>* top) { return; }
- virtual void Forward_gpu(const vector<Blob<Dtype>*>& bottom,
- vector<Blob<Dtype>*>* top) { return; }
- virtual Dtype Backward_cpu(const vector<Blob<Dtype>*>& top,
+ virtual Dtype Forward_cpu(const vector<Blob<Dtype>*>& bottom,
+ vector<Blob<Dtype>*>* top);
+ // virtual Dtype Forward_gpu(const vector<Blob<Dtype>*>& bottom,
+ // vector<Blob<Dtype>*>* top);
+ virtual void Backward_cpu(const vector<Blob<Dtype>*>& top,
const bool propagate_down, vector<Blob<Dtype>*>* bottom);
- // virtual Dtype Backward_gpu(const vector<Blob<Dtype>*>& top,
+ // virtual void Backward_gpu(const vector<Blob<Dtype>*>& top,
// const bool propagate_down, vector<Blob<Dtype>*>* bottom);
Blob<Dtype> difference_;
};
vector<Blob<Dtype>*>* top);
protected:
- virtual void Forward_cpu(const vector<Blob<Dtype>*>& bottom,
+ virtual Dtype Forward_cpu(const vector<Blob<Dtype>*>& bottom,
vector<Blob<Dtype>*>* top);
// The accuracy layer should not be used to compute backward operations.
- virtual Dtype Backward_cpu(const vector<Blob<Dtype>*>& top,
+ virtual void Backward_cpu(const vector<Blob<Dtype>*>& top,
const bool propagate_down, vector<Blob<Dtype>*>* bottom) {
NOT_IMPLEMENTED;
- return Dtype(0.);
}
};
const float kBNLL_THRESHOLD = 50.;
template <typename Dtype>
-void BNLLLayer<Dtype>::Forward_cpu(const vector<Blob<Dtype>*>& bottom,
+Dtype BNLLLayer<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();
bottom_data[i] + log(1. + exp(-bottom_data[i])) :
log(1. + exp(bottom_data[i]));
}
+ return Dtype(0);
}
template <typename Dtype>
-Dtype BNLLLayer<Dtype>::Backward_cpu(const vector<Blob<Dtype>*>& top,
+void BNLLLayer<Dtype>::Backward_cpu(const vector<Blob<Dtype>*>& top,
const bool propagate_down,
vector<Blob<Dtype>*>* bottom) {
if (propagate_down) {
bottom_diff[i] = top_diff[i] * expval / (expval + 1.);
}
}
- return Dtype(0);
}
}
template <typename Dtype>
-void BNLLLayer<Dtype>::Forward_gpu(const vector<Blob<Dtype>*>& bottom,
+Dtype BNLLLayer<Dtype>::Forward_gpu(const vector<Blob<Dtype>*>& bottom,
vector<Blob<Dtype>*>* top) {
const Dtype* bottom_data = bottom[0]->gpu_data();
Dtype* top_data = (*top)[0]->mutable_gpu_data();
BNLLForward<Dtype><<<CAFFE_GET_BLOCKS(count), CAFFE_CUDA_NUM_THREADS>>>(
count, bottom_data, top_data);
CUDA_POST_KERNEL_CHECK;
+ return Dtype(0);
}
template <typename Dtype>
}
template <typename Dtype>
-Dtype BNLLLayer<Dtype>::Backward_gpu(const vector<Blob<Dtype>*>& top,
+void BNLLLayer<Dtype>::Backward_gpu(const vector<Blob<Dtype>*>& top,
const bool propagate_down,
vector<Blob<Dtype>*>* bottom) {
if (propagate_down) {
count, top_diff, bottom_data, bottom_diff);
CUDA_POST_KERNEL_CHECK;
}
- return Dtype(0);
}
INSTANTIATE_CLASS(BNLLLayer);
template <typename Dtype>
-void ConvolutionLayer<Dtype>::Forward_cpu(const vector<Blob<Dtype>*>& bottom,
+Dtype ConvolutionLayer<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();
(Dtype)1., top_data + (*top)[0]->offset(n));
}
}
+ return Dtype(0.);
}
template <typename Dtype>
-Dtype ConvolutionLayer<Dtype>::Backward_cpu(const vector<Blob<Dtype>*>& top,
+void ConvolutionLayer<Dtype>::Backward_cpu(const vector<Blob<Dtype>*>& top,
const bool propagate_down, vector<Blob<Dtype>*>* bottom) {
const Dtype* top_diff = top[0]->cpu_diff();
const Dtype* weight = this->blobs_[0]->cpu_data();
bottom_diff + (*bottom)[0]->offset(n));
}
}
- return Dtype(0.);
}
INSTANTIATE_CLASS(ConvolutionLayer);
namespace caffe {
template <typename Dtype>
-void ConvolutionLayer<Dtype>::Forward_gpu(const vector<Blob<Dtype>*>& bottom,
+Dtype ConvolutionLayer<Dtype>::Forward_gpu(const vector<Blob<Dtype>*>& bottom,
vector<Blob<Dtype>*>* top) {
const Dtype* bottom_data = bottom[0]->gpu_data();
Dtype* top_data = (*top)[0]->mutable_gpu_data();
(Dtype)1., top_data + (*top)[0]->offset(n));
}
}
+ return Dtype(0.);
}
template <typename Dtype>
-Dtype ConvolutionLayer<Dtype>::Backward_gpu(const vector<Blob<Dtype>*>& top,
+void ConvolutionLayer<Dtype>::Backward_gpu(const vector<Blob<Dtype>*>& top,
const bool propagate_down, vector<Blob<Dtype>*>* bottom) {
const Dtype* top_diff = top[0]->gpu_diff();
const Dtype* weight = this->blobs_[0]->gpu_data();
bottom_diff + (*bottom)[0]->offset(n));
}
}
- return Dtype(0.);
}
}
template <typename Dtype>
-void DataLayer<Dtype>::Forward_cpu(const vector<Blob<Dtype>*>& bottom,
+Dtype DataLayer<Dtype>::Forward_cpu(const vector<Blob<Dtype>*>& bottom,
vector<Blob<Dtype>*>* top) {
// First, join the thread
CHECK(!pthread_join(thread_, NULL)) << "Pthread joining failed.";
// Start a new prefetch thread
CHECK(!pthread_create(&thread_, NULL, DataLayerPrefetch<Dtype>,
reinterpret_cast<void*>(this))) << "Pthread execution failed.";
-}
-
-// The backward operations are dummy - they do not carry any computation.
-template <typename Dtype>
-Dtype DataLayer<Dtype>::Backward_cpu(const vector<Blob<Dtype>*>& top,
- const bool propagate_down, vector<Blob<Dtype>*>* bottom) {
return Dtype(0.);
}
namespace caffe {
template <typename Dtype>
-void DataLayer<Dtype>::Forward_gpu(const vector<Blob<Dtype>*>& bottom,
+Dtype DataLayer<Dtype>::Forward_gpu(const vector<Blob<Dtype>*>& bottom,
vector<Blob<Dtype>*>* top) {
// First, join the thread
CHECK(!pthread_join(thread_, NULL)) << "Pthread joining failed.";
// Start a new prefetch thread
CHECK(!pthread_create(&thread_, NULL, DataLayerPrefetch<Dtype>,
reinterpret_cast<void*>(this))) << "Pthread execution failed.";
-}
-
-// The backward operations are dummy - they do not carry any computation.
-template <typename Dtype>
-Dtype DataLayer<Dtype>::Backward_gpu(const vector<Blob<Dtype>*>& top,
- const bool propagate_down, vector<Blob<Dtype>*>* bottom) {
return Dtype(0.);
}
}
template <typename Dtype>
-void DropoutLayer<Dtype>::Forward_cpu(const vector<Blob<Dtype>*>& bottom,
+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();
} else {
memcpy(top_data, bottom_data, bottom[0]->count() * sizeof(Dtype));
}
+ return Dtype(0);
}
template <typename Dtype>
-Dtype DropoutLayer<Dtype>::Backward_cpu(const vector<Blob<Dtype>*>& top,
+void DropoutLayer<Dtype>::Backward_cpu(const vector<Blob<Dtype>*>& top,
const bool propagate_down,
vector<Blob<Dtype>*>* bottom) {
CHECK(Caffe::phase() == Caffe::TRAIN);
bottom_diff[i] = top_diff[i] * mask[i] * scale_;
}
}
- return Dtype(0);
}
}
template <typename Dtype>
-void DropoutLayer<Dtype>::Forward_gpu(const vector<Blob<Dtype>*>& bottom,
+Dtype DropoutLayer<Dtype>::Forward_gpu(const vector<Blob<Dtype>*>& bottom,
vector<Blob<Dtype>*>* top) {
const Dtype* bottom_data = bottom[0]->gpu_data();
Dtype* top_data = (*top)[0]->mutable_gpu_data();
CUDA_CHECK(cudaMemcpy(top_data, bottom_data,
count * sizeof(Dtype), cudaMemcpyDeviceToDevice));
}
+ return Dtype(0);
}
template <typename Dtype>
}
template <typename Dtype>
-Dtype DropoutLayer<Dtype>::Backward_gpu(const vector<Blob<Dtype>*>& top,
+void DropoutLayer<Dtype>::Backward_gpu(const vector<Blob<Dtype>*>& top,
const bool propagate_down,
vector<Blob<Dtype>*>* bottom) {
CHECK(Caffe::phase() == Caffe::TRAIN);
count, top_diff, mask, uint_thres_, scale_, bottom_diff);
CUDA_POST_KERNEL_CHECK;
}
- return Dtype(0);
}
INSTANTIATE_CLASS(DropoutLayer);
}
template <typename Dtype>
-void FlattenLayer<Dtype>::Forward_cpu(const vector<Blob<Dtype>*>& bottom,
+Dtype FlattenLayer<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();
caffe_copy(count_, bottom_data, top_data);
+ return Dtype(0.);
}
template <typename Dtype>
-Dtype FlattenLayer<Dtype>::Backward_cpu(const vector<Blob<Dtype>*>& top,
+void FlattenLayer<Dtype>::Backward_cpu(const vector<Blob<Dtype>*>& top,
const bool propagate_down, vector<Blob<Dtype>*>* bottom) {
const Dtype* top_diff = top[0]->cpu_diff();
Dtype* bottom_diff = (*bottom)[0]->mutable_cpu_diff();
caffe_copy(count_, top_diff, bottom_diff);
- return Dtype(0.);
}
INSTANTIATE_CLASS(FlattenLayer);
namespace caffe {
template <typename Dtype>
-void FlattenLayer<Dtype>::Forward_gpu(const vector<Blob<Dtype>*>& bottom,
+Dtype FlattenLayer<Dtype>::Forward_gpu(const vector<Blob<Dtype>*>& bottom,
vector<Blob<Dtype>*>* top) {
const Dtype* bottom_data = bottom[0]->gpu_data();
Dtype* top_data = (*top)[0]->mutable_gpu_data();
caffe_gpu_copy(count_, bottom_data, top_data);
+ return Dtype(0.);
}
template <typename Dtype>
-Dtype FlattenLayer<Dtype>::Backward_gpu(const vector<Blob<Dtype>*>& top,
+void FlattenLayer<Dtype>::Backward_gpu(const vector<Blob<Dtype>*>& top,
const bool propagate_down, vector<Blob<Dtype>*>* bottom) {
const Dtype* top_diff = top[0]->gpu_diff();
Dtype* bottom_diff = (*bottom)[0]->mutable_gpu_diff();
caffe_gpu_copy(count_, top_diff, bottom_diff);
- return Dtype(0.);
}
INSTANTIATE_CLASS(FlattenLayer);
}
template <typename Dtype>
-void HDF5DataLayer<Dtype>::Forward_cpu(const vector<Blob<Dtype>*>& bottom,
+Dtype HDF5DataLayer<Dtype>::Forward_cpu(const vector<Blob<Dtype>*>& bottom,
vector<Blob<Dtype>*>* top) {
const int batchsize = this->layer_param_.batchsize();
const int data_count = (*top)[0]->count() / (*top)[0]->num();
&label_blob_.cpu_data()[current_row_ * label_data_count],
sizeof(Dtype) * label_data_count);
}
+ return Dtype(0.);
}
// The backward operations are dummy - they do not carry any computation.
template <typename Dtype>
-Dtype HDF5DataLayer<Dtype>::Backward_cpu(const vector<Blob<Dtype>*>& top,
- const bool propagate_down, vector<Blob<Dtype>*>* bottom) {
- return Dtype(0.);
-}
+void HDF5DataLayer<Dtype>::Backward_cpu(const vector<Blob<Dtype>*>& top,
+ const bool propagate_down, vector<Blob<Dtype>*>* bottom) { }
INSTANTIATE_CLASS(HDF5DataLayer);
namespace caffe {
template <typename Dtype>
-void HDF5DataLayer<Dtype>::Forward_gpu(const vector<Blob<Dtype>*>& bottom,
+Dtype HDF5DataLayer<Dtype>::Forward_gpu(const vector<Blob<Dtype>*>& bottom,
vector<Blob<Dtype>*>* top) {
const int batchsize = this->layer_param_.batchsize();
const int data_count = (*top)[0]->count() / (*top)[0]->num();
sizeof(Dtype) * label_data_count,
cudaMemcpyHostToDevice));
}
+ return Dtype(0.);
}
template <typename Dtype>
-Dtype HDF5DataLayer<Dtype>::Backward_gpu(const vector<Blob<Dtype>*>& top,
+void HDF5DataLayer<Dtype>::Backward_gpu(const vector<Blob<Dtype>*>& top,
const bool propagate_down, vector<Blob<Dtype>*>* bottom) {
- return Dtype(0.);
}
INSTANTIATE_CLASS(HDF5DataLayer);
}
template <typename Dtype>
-void Im2colLayer<Dtype>::Forward_cpu(const vector<Blob<Dtype>*>& bottom,
+Dtype Im2colLayer<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();
im2col_cpu(bottom_data + bottom[0]->offset(n), CHANNELS_, HEIGHT_,
WIDTH_, KSIZE_, PAD_, STRIDE_, top_data + (*top)[0]->offset(n));
}
+ return Dtype(0.);
}
template <typename Dtype>
-Dtype Im2colLayer<Dtype>::Backward_cpu(const vector<Blob<Dtype>*>& top,
+void Im2colLayer<Dtype>::Backward_cpu(const vector<Blob<Dtype>*>& top,
const bool propagate_down, vector<Blob<Dtype>*>* bottom) {
const Dtype* top_diff = top[0]->cpu_diff();
Dtype* bottom_diff = (*bottom)[0]->mutable_cpu_diff();
col2im_cpu(top_diff + top[0]->offset(n), CHANNELS_, HEIGHT_,
WIDTH_, KSIZE_, PAD_, STRIDE_, bottom_diff + (*bottom)[0]->offset(n));
}
- return Dtype(0.);
}
INSTANTIATE_CLASS(Im2colLayer);
namespace caffe {
template <typename Dtype>
-void Im2colLayer<Dtype>::Forward_gpu(const vector<Blob<Dtype>*>& bottom,
+Dtype Im2colLayer<Dtype>::Forward_gpu(const vector<Blob<Dtype>*>& bottom,
vector<Blob<Dtype>*>* top) {
const Dtype* bottom_data = bottom[0]->gpu_data();
Dtype* top_data = (*top)[0]->mutable_gpu_data();
im2col_gpu(bottom_data + bottom[0]->offset(n), CHANNELS_, HEIGHT_,
WIDTH_, KSIZE_, PAD_, STRIDE_, top_data + (*top)[0]->offset(n));
}
+ return Dtype(0.);
}
template <typename Dtype>
-Dtype Im2colLayer<Dtype>::Backward_gpu(const vector<Blob<Dtype>*>& top,
+void Im2colLayer<Dtype>::Backward_gpu(const vector<Blob<Dtype>*>& top,
const bool propagate_down, vector<Blob<Dtype>*>* bottom) {
const Dtype* top_diff = top[0]->gpu_diff();
Dtype* bottom_diff = (*bottom)[0]->mutable_gpu_diff();
col2im_gpu(top_diff + top[0]->offset(n), CHANNELS_, HEIGHT_,
WIDTH_, KSIZE_, PAD_, STRIDE_, bottom_diff + (*bottom)[0]->offset(n));
}
- return Dtype(0.);
}
}
template <typename Dtype>
-void InnerProductLayer<Dtype>::Forward_cpu(const vector<Blob<Dtype>*>& bottom,
+Dtype InnerProductLayer<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();
reinterpret_cast<const Dtype*>(bias_multiplier_->cpu_data()),
this->blobs_[1]->cpu_data(), (Dtype)1., top_data);
}
+ return Dtype(0);
}
template <typename Dtype>
-Dtype InnerProductLayer<Dtype>::Backward_cpu(const vector<Blob<Dtype>*>& top,
+void InnerProductLayer<Dtype>::Backward_cpu(const vector<Blob<Dtype>*>& top,
const bool propagate_down,
vector<Blob<Dtype>*>* bottom) {
const Dtype* top_diff = top[0]->cpu_diff();
top_diff, this->blobs_[0]->cpu_data(), (Dtype)0.,
(*bottom)[0]->mutable_cpu_diff());
}
- return Dtype(0);
}
INSTANTIATE_CLASS(InnerProductLayer);
namespace caffe {
template <typename Dtype>
-void InnerProductLayer<Dtype>::Forward_gpu(const vector<Blob<Dtype>*>& bottom,
+Dtype InnerProductLayer<Dtype>::Forward_gpu(const vector<Blob<Dtype>*>& bottom,
vector<Blob<Dtype>*>* top) {
const Dtype* bottom_data = bottom[0]->gpu_data();
Dtype* top_data = (*top)[0]->mutable_gpu_data();
reinterpret_cast<const Dtype*>(bias_multiplier_->gpu_data()),
this->blobs_[1]->gpu_data(), (Dtype)1., top_data);
}
+ return Dtype(0);
}
template <typename Dtype>
-Dtype InnerProductLayer<Dtype>::Backward_gpu(const vector<Blob<Dtype>*>& top,
+void InnerProductLayer<Dtype>::Backward_gpu(const vector<Blob<Dtype>*>& top,
const bool propagate_down,
vector<Blob<Dtype>*>* bottom) {
const Dtype* top_diff = top[0]->gpu_diff();
top_diff, this->blobs_[0]->gpu_data(), (Dtype)0.,
(*bottom)[0]->mutable_gpu_diff());
}
- return Dtype(0);
}
INSTANTIATE_CLASS(InnerProductLayer);
CHECK_EQ(bottom[1]->width(), 1);
}
+template <typename Dtype>
+Dtype MultinomialLogisticLossLayer<Dtype>::Forward_cpu(
+ const vector<Blob<Dtype>*>& bottom, vector<Blob<Dtype>*>* top) {
+ const Dtype* bottom_data = bottom[0]->cpu_data();
+ const Dtype* bottom_label = bottom[1]->cpu_data();
+ int num = bottom[0]->num();
+ int dim = bottom[0]->count() / bottom[0]->num();
+ Dtype loss = 0;
+ for (int i = 0; i < num; ++i) {
+ int label = static_cast<int>(bottom_label[i]);
+ Dtype prob = max(bottom_data[i * dim + label], Dtype(kLOG_THRESHOLD));
+ loss -= log(prob);
+ }
+ return loss / num;
+}
template <typename Dtype>
-Dtype MultinomialLogisticLossLayer<Dtype>::Backward_cpu(
+void MultinomialLogisticLossLayer<Dtype>::Backward_cpu(
const vector<Blob<Dtype>*>& top, const bool propagate_down,
vector<Blob<Dtype>*>* bottom) {
const Dtype* bottom_data = (*bottom)[0]->cpu_data();
int num = (*bottom)[0]->num();
int dim = (*bottom)[0]->count() / (*bottom)[0]->num();
memset(bottom_diff, 0, sizeof(Dtype) * (*bottom)[0]->count());
- Dtype loss = 0;
for (int i = 0; i < num; ++i) {
int label = static_cast<int>(bottom_label[i]);
Dtype prob = max(bottom_data[i * dim + label], Dtype(kLOG_THRESHOLD));
- loss -= log(prob);
- bottom_diff[i * dim + label] = - 1. / prob / num;
+ bottom_diff[i * dim + label] = -1. / prob / num;
}
- return loss / num;
}
-// TODO: implement the GPU version for multinomial loss
-
template <typename Dtype>
void InfogainLossLayer<Dtype>::SetUp(
template <typename Dtype>
-Dtype InfogainLossLayer<Dtype>::Backward_cpu(const vector<Blob<Dtype>*>& top,
+Dtype InfogainLossLayer<Dtype>::Forward_cpu(const vector<Blob<Dtype>*>& bottom,
+ vector<Blob<Dtype>*>* top) {
+ const Dtype* bottom_data = bottom[0]->cpu_data();
+ const Dtype* bottom_label = bottom[1]->cpu_data();
+ const Dtype* infogain_mat = infogain_.cpu_data();
+ int num = bottom[0]->num();
+ int dim = bottom[0]->count() / bottom[0]->num();
+ CHECK_EQ(infogain_.height(), dim);
+ Dtype loss = 0;
+ for (int i = 0; i < num; ++i) {
+ int label = static_cast<int>(bottom_label[i]);
+ for (int j = 0; j < dim; ++j) {
+ Dtype prob = max(bottom_data[i * dim + j], Dtype(kLOG_THRESHOLD));
+ loss -= infogain_mat[label * dim + j] * log(prob);
+ }
+ }
+ return loss / num;
+}
+
+template <typename Dtype>
+void InfogainLossLayer<Dtype>::Backward_cpu(const vector<Blob<Dtype>*>& top,
const bool propagate_down,
vector<Blob<Dtype>*>* bottom) {
const Dtype* bottom_data = (*bottom)[0]->cpu_data();
int num = (*bottom)[0]->num();
int dim = (*bottom)[0]->count() / (*bottom)[0]->num();
CHECK_EQ(infogain_.height(), dim);
- Dtype loss = 0;
for (int i = 0; i < num; ++i) {
int label = static_cast<int>(bottom_label[i]);
for (int j = 0; j < dim; ++j) {
Dtype prob = max(bottom_data[i * dim + j], Dtype(kLOG_THRESHOLD));
- loss -= infogain_mat[label * dim + j] * log(prob);
bottom_diff[i * dim + j] = - infogain_mat[label * dim + j] / prob / num;
}
}
- return loss / num;
}
}
template <typename Dtype>
-Dtype EuclideanLossLayer<Dtype>::Backward_cpu(const vector<Blob<Dtype>*>& top,
- const bool propagate_down, vector<Blob<Dtype>*>* bottom) {
- int count = (*bottom)[0]->count();
- int num = (*bottom)[0]->num();
- caffe_sub(count, (*bottom)[0]->cpu_data(), (*bottom)[1]->cpu_data(),
+Dtype EuclideanLossLayer<Dtype>::Forward_cpu(const vector<Blob<Dtype>*>& bottom,
+ vector<Blob<Dtype>*>* top) {
+ int count = bottom[0]->count();
+ int num = bottom[0]->num();
+ caffe_sub(count, bottom[0]->cpu_data(), bottom[1]->cpu_data(),
difference_.mutable_cpu_data());
Dtype loss = caffe_cpu_dot(
count, difference_.cpu_data(), difference_.cpu_data()) / num / Dtype(2);
+ return loss;
+}
+
+template <typename Dtype>
+void EuclideanLossLayer<Dtype>::Backward_cpu(const vector<Blob<Dtype>*>& top,
+ const bool propagate_down, vector<Blob<Dtype>*>* bottom) {
+ int count = (*bottom)[0]->count();
+ int num = (*bottom)[0]->num();
// Compute the gradient
caffe_axpby(count, Dtype(1) / num, difference_.cpu_data(), Dtype(0),
(*bottom)[0]->mutable_cpu_diff());
- return loss;
}
template <typename Dtype>
}
template <typename Dtype>
-void AccuracyLayer<Dtype>::Forward_cpu(const vector<Blob<Dtype>*>& bottom,
+Dtype AccuracyLayer<Dtype>::Forward_cpu(const vector<Blob<Dtype>*>& bottom,
vector<Blob<Dtype>*>* top) {
Dtype accuracy = 0;
Dtype logprob = 0;
// LOG(INFO) << "Accuracy: " << accuracy;
(*top)[0]->mutable_cpu_data()[0] = accuracy / num;
(*top)[0]->mutable_cpu_data()[1] = logprob / num;
+ // Accuracy layer should not be used as a loss function.
+ return Dtype(0);
}
INSTANTIATE_CLASS(MultinomialLogisticLossLayer);
}
template <typename Dtype>
-void LRNLayer<Dtype>::Forward_cpu(const vector<Blob<Dtype>*>& bottom,
+Dtype LRNLayer<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();
// In the end, compute output
caffe_powx<Dtype>(scale_.count(), scale_data, -beta_, top_data);
caffe_mul<Dtype>(scale_.count(), top_data, bottom_data, top_data);
+
+ return Dtype(0.);
}
template <typename Dtype>
-Dtype LRNLayer<Dtype>::Backward_cpu(const vector<Blob<Dtype>*>& top,
+void LRNLayer<Dtype>::Backward_cpu(const vector<Blob<Dtype>*>& top,
const bool propagate_down, vector<Blob<Dtype>*>* bottom) {
const Dtype* top_diff = top[0]->cpu_diff();
const Dtype* top_data = top[0]->cpu_data();
padded_ratio_data + padded_ratio.offset(0, c), accum_ratio_data);
}
}
- return Dtype(0.);
}
INSTANTIATE_CLASS(LRNLayer);
}
template <typename Dtype>
-void LRNLayer<Dtype>::Forward_gpu(const vector<Blob<Dtype>*>& bottom,
+Dtype LRNLayer<Dtype>::Forward_gpu(const vector<Blob<Dtype>*>& bottom,
vector<Blob<Dtype>*>* top) {
// First, compute scale
const Dtype* bottom_data = bottom[0]->gpu_data();
LRNComputeOutput<<<CAFFE_GET_BLOCKS(n_threads), CAFFE_CUDA_NUM_THREADS>>>(
n_threads, bottom_data, scale_data, -beta_, top_data);
CUDA_POST_KERNEL_CHECK;
+ return Dtype(0.);
}
}
template <typename Dtype>
-Dtype LRNLayer<Dtype>::Backward_gpu(const vector<Blob<Dtype>*>& top,
+void LRNLayer<Dtype>::Backward_gpu(const vector<Blob<Dtype>*>& top,
const bool propagate_down, vector<Blob<Dtype>*>* bottom) {
int n_threads = num_ * height_ * width_;
// NOLINT_NEXT_LINE(whitespace/operators)
scale_.gpu_data(), top[0]->gpu_diff(), num_, channels_, height_, width_,
size_, -beta_, Dtype(2. * alpha_ * beta_ / size_),
(*bottom)[0]->mutable_gpu_diff());
- return Dtype(0.);
}
// TODO(Yangqing): Is there a faster way to do pooling in the channel-first
// case?
template <typename Dtype>
-void PoolingLayer<Dtype>::Forward_cpu(const vector<Blob<Dtype>*>& bottom,
+Dtype PoolingLayer<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();
default:
LOG(FATAL) << "Unknown pooling method.";
}
+ return Dtype(0.);
}
template <typename Dtype>
-Dtype PoolingLayer<Dtype>::Backward_cpu(const vector<Blob<Dtype>*>& top,
+void PoolingLayer<Dtype>::Backward_cpu(const vector<Blob<Dtype>*>& top,
const bool propagate_down, vector<Blob<Dtype>*>* bottom) {
if (!propagate_down) {
- return Dtype(0.);
+ return;
}
const Dtype* top_diff = top[0]->cpu_diff();
const Dtype* top_data = top[0]->cpu_data();
default:
LOG(FATAL) << "Unknown pooling method.";
}
- return Dtype(0.);
}
template <typename Dtype>
-void PoolingLayer<Dtype>::Forward_gpu(const vector<Blob<Dtype>*>& bottom,
+Dtype PoolingLayer<Dtype>::Forward_gpu(const vector<Blob<Dtype>*>& bottom,
vector<Blob<Dtype>*>* top) {
const Dtype* bottom_data = bottom[0]->gpu_data();
Dtype* top_data = (*top)[0]->mutable_gpu_data();
LOG(FATAL) << "Unknown pooling method.";
}
CUDA_POST_KERNEL_CHECK;
+ return Dtype(0.);
}
template <typename Dtype>
template <typename Dtype>
-Dtype PoolingLayer<Dtype>::Backward_gpu(const vector<Blob<Dtype>*>& top,
+void PoolingLayer<Dtype>::Backward_gpu(const vector<Blob<Dtype>*>& top,
const bool propagate_down, vector<Blob<Dtype>*>* bottom) {
if (!propagate_down) {
- return Dtype(0.);
+ return;
}
const Dtype* top_diff = top[0]->gpu_diff();
Dtype* bottom_diff = (*bottom)[0]->mutable_gpu_diff();
LOG(FATAL) << "Unknown pooling method.";
}
CUDA_POST_KERNEL_CHECK;
- return Dtype(0.);
}
namespace caffe {
template <typename Dtype>
-void ReLULayer<Dtype>::Forward_cpu(const vector<Blob<Dtype>*>& bottom,
+Dtype ReLULayer<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();
for (int i = 0; i < count; ++i) {
top_data[i] = max(bottom_data[i], Dtype(0));
}
+ return Dtype(0);
}
template <typename Dtype>
-Dtype ReLULayer<Dtype>::Backward_cpu(const vector<Blob<Dtype>*>& top,
+void ReLULayer<Dtype>::Backward_cpu(const vector<Blob<Dtype>*>& top,
const bool propagate_down,
vector<Blob<Dtype>*>* bottom) {
if (propagate_down) {
bottom_diff[i] = top_diff[i] * (bottom_data[i] > 0);
}
}
- return Dtype(0);
}
}
template <typename Dtype>
-void ReLULayer<Dtype>::Forward_gpu(const vector<Blob<Dtype>*>& bottom,
+Dtype ReLULayer<Dtype>::Forward_gpu(const vector<Blob<Dtype>*>& bottom,
vector<Blob<Dtype>*>* top) {
const Dtype* bottom_data = bottom[0]->gpu_data();
Dtype* top_data = (*top)[0]->mutable_gpu_data();
// << " top_data: " << (unsigned long)top_data
// << " blocks: " << CAFFE_GET_BLOCKS(count)
// << " threads: " << CAFFE_CUDA_NUM_THREADS;
+ return Dtype(0);
}
template <typename Dtype>
}
template <typename Dtype>
-Dtype ReLULayer<Dtype>::Backward_gpu(const vector<Blob<Dtype>*>& top,
+void ReLULayer<Dtype>::Backward_gpu(const vector<Blob<Dtype>*>& top,
const bool propagate_down,
vector<Blob<Dtype>*>* bottom) {
if (propagate_down) {
count, top_diff, bottom_data, bottom_diff);
CUDA_POST_KERNEL_CHECK;
}
- return Dtype(0);
}
INSTANTIATE_CLASS(ReLULayer);
}
template <typename Dtype>
-void SigmoidLayer<Dtype>::Forward_cpu(const vector<Blob<Dtype>*>& bottom,
+Dtype SigmoidLayer<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();
for (int i = 0; i < count; ++i) {
top_data[i] = sigmoid(bottom_data[i]);
}
+ return Dtype(0);
}
template <typename Dtype>
-Dtype SigmoidLayer<Dtype>::Backward_cpu(const vector<Blob<Dtype>*>& top,
+void SigmoidLayer<Dtype>::Backward_cpu(const vector<Blob<Dtype>*>& top,
const bool propagate_down,
vector<Blob<Dtype>*>* bottom) {
if (propagate_down) {
bottom_diff[i] = top_diff[i] * sigmoid_x * (1. - sigmoid_x);
}
}
- return Dtype(0);
}
INSTANTIATE_CLASS(SigmoidLayer);
}
template <typename Dtype>
-void SigmoidLayer<Dtype>::Forward_gpu(const vector<Blob<Dtype>*>& bottom,
+Dtype SigmoidLayer<Dtype>::Forward_gpu(const vector<Blob<Dtype>*>& bottom,
vector<Blob<Dtype>*>* top) {
const Dtype* bottom_data = bottom[0]->gpu_data();
Dtype* top_data = (*top)[0]->mutable_gpu_data();
// << " top_data: " << (unsigned long)top_data
// << " blocks: " << CAFFE_GET_BLOCKS(count)
// << " threads: " << CAFFE_CUDA_NUM_THREADS;
+ return Dtype(0);
}
template <typename Dtype>
}
template <typename Dtype>
-Dtype SigmoidLayer<Dtype>::Backward_gpu(const vector<Blob<Dtype>*>& top,
+void SigmoidLayer<Dtype>::Backward_gpu(const vector<Blob<Dtype>*>& top,
const bool propagate_down,
vector<Blob<Dtype>*>* bottom) {
if (propagate_down) {
count, top_diff, bottom_data, bottom_diff);
CUDA_POST_KERNEL_CHECK;
}
- return Dtype(0);
}
INSTANTIATE_CLASS(SigmoidLayer);
}
template <typename Dtype>
-void SoftmaxLayer<Dtype>::Forward_cpu(const vector<Blob<Dtype>*>& bottom,
+Dtype SoftmaxLayer<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();
for (int i = 0; i < num; ++i) {
caffe_scal<Dtype>(dim, Dtype(1.) / scale_data[i], top_data + i * dim);
}
+ return Dtype(0);
}
template <typename Dtype>
-Dtype SoftmaxLayer<Dtype>::Backward_cpu(const vector<Blob<Dtype>*>& top,
+void SoftmaxLayer<Dtype>::Backward_cpu(const vector<Blob<Dtype>*>& top,
const bool propagate_down,
vector<Blob<Dtype>*>* bottom) {
const Dtype* top_diff = top[0]->cpu_diff();
scale_data, sum_multiplier_.cpu_data(), 1., bottom_diff);
// elementwise multiplication
caffe_mul<Dtype>(top[0]->count(), bottom_diff, top_data, bottom_diff);
- return Dtype(0);
}
}
template <typename Dtype>
-void SoftmaxLayer<Dtype>::Forward_gpu(const vector<Blob<Dtype>*>& bottom,
+Dtype SoftmaxLayer<Dtype>::Forward_gpu(const vector<Blob<Dtype>*>& bottom,
vector<Blob<Dtype>*>* top) {
const Dtype* bottom_data = bottom[0]->gpu_data();
Dtype* top_data = (*top)[0]->mutable_gpu_data();
kernel_softmax_div<Dtype><<<CAFFE_GET_BLOCKS(num * dim),
CAFFE_CUDA_NUM_THREADS>>>(
num, dim, scale_data, top_data);
+ return Dtype(0);
}
// TODO(Yangqing): implement the GPU version of softmax.
template <typename Dtype>
-Dtype SoftmaxLayer<Dtype>::Backward_gpu(const vector<Blob<Dtype>*>& top,
+void SoftmaxLayer<Dtype>::Backward_gpu(const vector<Blob<Dtype>*>& top,
const bool propagate_down, vector<Blob<Dtype>*>* bottom) {
const Dtype* top_diff = top[0]->gpu_diff();
const Dtype* top_data = top[0]->gpu_data();
scale_.gpu_data(), sum_multiplier_.gpu_data(), 1., bottom_diff);
// elementwise multiplication
caffe_gpu_mul<Dtype>(top[0]->count(), bottom_diff, top_data, bottom_diff);
- return Dtype(0);
}
INSTANTIATE_CLASS(SoftmaxLayer);
}
template <typename Dtype>
-void SoftmaxWithLossLayer<Dtype>::Forward_cpu(
+Dtype SoftmaxWithLossLayer<Dtype>::Forward_cpu(
const vector<Blob<Dtype>*>& bottom, vector<Blob<Dtype>*>* top) {
// The forward pass computes the softmax prob values.
softmax_bottom_vec_[0] = bottom[0];
softmax_layer_->Forward(softmax_bottom_vec_, &softmax_top_vec_);
+ const Dtype* prob_data = prob_.cpu_data();
+ const Dtype* label = bottom[1]->cpu_data();
+ int num = prob_.num();
+ int dim = prob_.count() / num;
+ Dtype loss = 0;
+ for (int i = 0; i < num; ++i) {
+ loss += -log(max(prob_data[i * dim + static_cast<int>(label[i])],
+ Dtype(FLT_MIN)));
+ }
+ return loss / num;
}
template <typename Dtype>
-Dtype SoftmaxWithLossLayer<Dtype>::Backward_cpu(const vector<Blob<Dtype>*>& top,
+void SoftmaxWithLossLayer<Dtype>::Backward_cpu(const vector<Blob<Dtype>*>& top,
const bool propagate_down,
vector<Blob<Dtype>*>* bottom) {
- // First, compute the diff
+ // Compute the diff
Dtype* bottom_diff = (*bottom)[0]->mutable_cpu_diff();
const Dtype* prob_data = prob_.cpu_data();
- memcpy(bottom_diff, prob_data, sizeof(Dtype) * prob_.count());
const Dtype* label = (*bottom)[1]->cpu_data();
+ memcpy(bottom_diff, prob_data, sizeof(Dtype) * prob_.count());
int num = prob_.num();
int dim = prob_.count() / num;
- Dtype loss = 0;
for (int i = 0; i < num; ++i) {
bottom_diff[i * dim + static_cast<int>(label[i])] -= 1;
- loss += -log(max(prob_data[i * dim + static_cast<int>(label[i])],
- Dtype(FLT_MIN)));
}
// Scale down gradient
caffe_scal(prob_.count(), Dtype(1) / num, bottom_diff);
- return loss / num;
}
namespace caffe {
template <typename Dtype>
-void SoftmaxWithLossLayer<Dtype>::Forward_gpu(
+Dtype SoftmaxWithLossLayer<Dtype>::Forward_gpu(
const vector<Blob<Dtype>*>& bottom, vector<Blob<Dtype>*>* top) {
// The forward pass computes the softmax prob values.
softmax_bottom_vec_[0] = bottom[0];
softmax_layer_->Forward(softmax_bottom_vec_, &softmax_top_vec_);
+ return Dtype(0);
}
template <typename Dtype>
-Dtype SoftmaxWithLossLayer<Dtype>::Backward_gpu(const vector<Blob<Dtype>*>& top,
+void SoftmaxWithLossLayer<Dtype>::Backward_gpu(const vector<Blob<Dtype>*>& top,
const bool propagate_down, vector<Blob<Dtype>*>* bottom) {
// TODO(Yangqing): implement the GPU version of softmax.
- return Backward_cpu(top, propagate_down, bottom);
+ Backward_cpu(top, propagate_down, bottom);
}
INSTANTIATE_CLASS(SoftmaxWithLossLayer);
}
template <typename Dtype>
-void SplitLayer<Dtype>::Forward_cpu(const vector<Blob<Dtype>*>& bottom,
+Dtype SplitLayer<Dtype>::Forward_cpu(const vector<Blob<Dtype>*>& bottom,
vector<Blob<Dtype>*>* top) {
const Dtype* bottom_data = bottom[0]->cpu_data();
for (int i = 0; i < top->size(); ++i) {
Dtype* top_data = (*top)[i]->mutable_cpu_data();
caffe_copy(count_, bottom_data, top_data);
}
+ return Dtype(0.);
}
template <typename Dtype>
-Dtype SplitLayer<Dtype>::Backward_cpu(const vector<Blob<Dtype>*>& top,
+void SplitLayer<Dtype>::Backward_cpu(const vector<Blob<Dtype>*>& top,
const bool propagate_down, vector<Blob<Dtype>*>* bottom) {
if (propagate_down) {
const Dtype* top_diff = top[0]->cpu_diff();
caffe_axpy(count_, Dtype(1.), top_diff, bottom_diff);
}
}
- return Dtype(0.);
}
namespace caffe {
template <typename Dtype>
-void SplitLayer<Dtype>::Forward_gpu(const vector<Blob<Dtype>*>& bottom,
+Dtype SplitLayer<Dtype>::Forward_gpu(const vector<Blob<Dtype>*>& bottom,
vector<Blob<Dtype>*>* top) {
const Dtype* bottom_data = bottom[0]->gpu_data();
for (int i = 0; i < top->size(); ++i) {
Dtype* top_data = (*top)[i]->mutable_gpu_data();
caffe_gpu_copy(count_, bottom_data, top_data);
}
+ return Dtype(0.);
}
template <typename Dtype>
-Dtype SplitLayer<Dtype>::Backward_gpu(const vector<Blob<Dtype>*>& top,
+void SplitLayer<Dtype>::Backward_gpu(const vector<Blob<Dtype>*>& top,
const bool propagate_down, vector<Blob<Dtype>*>* bottom) {
if (propagate_down) {
const Dtype* top_diff = top[0]->gpu_diff();
caffe_gpu_axpy(count_, Dtype(1.), top_diff, bottom_diff);
}
}
- return Dtype(0.);
}
namespace caffe {
template <typename Dtype>
-void TanHLayer<Dtype>::Forward_cpu(const vector<Blob<Dtype>*>& bottom,
+Dtype TanHLayer<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();
exp2x = exp(2*bottom_data[i]);
top_data[i] = (exp2x - Dtype(1))/(exp2x + Dtype(1));
}
+ return Dtype(0);
}
template <typename Dtype>
-Dtype TanHLayer<Dtype>::Backward_cpu(const vector<Blob<Dtype>*>& top,
+void TanHLayer<Dtype>::Backward_cpu(const vector<Blob<Dtype>*>& top,
const bool propagate_down,
vector<Blob<Dtype>*>* bottom) {
if (propagate_down) {
bottom_diff[i] = top_diff[i] * (1 - tanhx*tanhx);
}
}
- return Dtype(0);
}
INSTANTIATE_CLASS(TanHLayer);
}
template <typename Dtype>
-void TanHLayer<Dtype>::Forward_gpu(const vector<Blob<Dtype>*>& bottom,
+Dtype TanHLayer<Dtype>::Forward_gpu(const vector<Blob<Dtype>*>& bottom,
vector<Blob<Dtype>*>* top) {
const Dtype* bottom_data = bottom[0]->gpu_data();
Dtype* top_data = (*top)[0]->mutable_gpu_data();
// << " top_data: " << (unsigned long)top_data
// << " blocks: " << CAFFE_GET_BLOCKS(count)
// << " threads: " << CAFFE_CUDA_NUM_THREADS;
+ return Dtype(0);
}
template <typename Dtype>
}
template <typename Dtype>
-Dtype TanHLayer<Dtype>::Backward_gpu(const vector<Blob<Dtype>*>& top,
+void TanHLayer<Dtype>::Backward_gpu(const vector<Blob<Dtype>*>& top,
const bool propagate_down,
vector<Blob<Dtype>*>* bottom) {
if (propagate_down) {
count, top_diff, bottom_data, bottom_diff);
CUDA_POST_KERNEL_CHECK;
}
- return Dtype(0);
}
INSTANTIATE_CLASS(TanHLayer);
template <typename Dtype>
const vector<Blob<Dtype>*>& Net<Dtype>::ForwardPrefilled() {
+ Dtype ignored_loss;
+ return ForwardPrefilled(&ignored_loss);
+}
+
+template <typename Dtype>
+const vector<Blob<Dtype>*>& Net<Dtype>::ForwardPrefilled(Dtype* loss) {
+ *loss = Dtype(0.);
for (int i = 0; i < layers_.size(); ++i) {
// LOG(ERROR) << "Forwarding " << layer_names_[i];
- layers_[i]->Forward(bottom_vecs_[i], &top_vecs_[i]);
+ *loss += layers_[i]->Forward(bottom_vecs_[i], &top_vecs_[i]);
}
return net_output_blobs_;
}
template <typename Dtype>
const vector<Blob<Dtype>*>& Net<Dtype>::Forward(
const vector<Blob<Dtype>*> & bottom) {
+ Dtype ignored_loss;
+ return Forward(bottom, &ignored_loss);
+}
+
+template <typename Dtype>
+const vector<Blob<Dtype>*>& Net<Dtype>::Forward(
+ const vector<Blob<Dtype>*> & bottom, Dtype* loss) {
// Copy bottom to internal bottom
for (int i = 0; i < bottom.size(); ++i) {
net_input_blobs_[i]->CopyFrom(*bottom[i]);
}
- return ForwardPrefilled();
+ return ForwardPrefilled(loss);
}
-
template <typename Dtype>
-string Net<Dtype>::Forward(const string& input_blob_protos) {
+string Net<Dtype>::Forward(const string& input_blob_protos, Dtype* loss) {
BlobProtoVector blob_proto_vec;
if (net_input_blobs_.size()) {
blob_proto_vec.ParseFromString(input_blob_protos);
net_input_blobs_[i]->FromProto(blob_proto_vec.blobs(i));
}
}
- ForwardPrefilled();
+ ForwardPrefilled(loss);
blob_proto_vec.Clear();
for (int i = 0; i < net_output_blobs_.size(); ++i) {
net_output_blobs_[i]->ToProto(blob_proto_vec.add_blobs());
template <typename Dtype>
-Dtype Net<Dtype>::Backward() {
- Dtype loss = 0;
+void Net<Dtype>::Backward() {
for (int i = layers_.size() - 1; i >= 0; --i) {
if (layer_need_backward_[i]) {
- Dtype layer_loss = layers_[i]->Backward(
- top_vecs_[i], true, &bottom_vecs_[i]);
- loss += layer_loss;
+ layers_[i]->Backward(top_vecs_[i], true, &bottom_vecs_[i]);
}
}
- return loss;
}
template <typename Dtype>