Skip to content

Commit

Permalink
add parameter layer for learning any bottom
Browse files Browse the repository at this point in the history
  • Loading branch information
longjon committed May 5, 2016
1 parent af04325 commit c419f85
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 1 deletion.
45 changes: 45 additions & 0 deletions include/caffe/layers/parameter_layer.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#ifndef CAFFE_PARAMETER_LAYER_HPP_
#define CAFFE_PARAMETER_LAYER_HPP_

#include <vector>

#include "caffe/layer.hpp"

namespace caffe {

template <typename Dtype>
class ParameterLayer : public Layer<Dtype> {
public:
explicit ParameterLayer(const LayerParameter& param)
: Layer<Dtype>(param) {}
virtual void LayerSetUp(const vector<Blob<Dtype>*>& bottom,
const vector<Blob<Dtype>*>& top) {
if (this->blobs_.size() > 0) {
LOG(INFO) << "Skipping parameter initialization";
} else {
this->blobs_.resize(1);
this->blobs_[0].reset(new Blob<Dtype>());
this->blobs_[0]->Reshape(this->layer_param_.parameter_param().shape());
}
top[0]->Reshape(this->layer_param_.parameter_param().shape());
}
virtual void Reshape(const vector<Blob<Dtype>*>& bottom,
const vector<Blob<Dtype>*>& top) { }
virtual inline const char* type() const { return "Parameter"; }
virtual inline int ExactNumBottomBlobs() const { return 0; }
virtual inline int ExactNumTopBlobs() const { return 1; }

protected:
virtual void Forward_cpu(const vector<Blob<Dtype>*>& bottom,
const vector<Blob<Dtype>*>& top) {
top[0]->ShareData(*(this->blobs_[0]));
top[0]->ShareDiff(*(this->blobs_[0]));
}
virtual void Backward_cpu(const vector<Blob<Dtype>*>& top,
const vector<bool>& propagate_down, const vector<Blob<Dtype>*>& bottom)
{ }
};

} // namespace caffe

#endif
8 changes: 8 additions & 0 deletions src/caffe/layers/parameter_layer.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#include "caffe/layers/parameter_layer.hpp"

namespace caffe {

INSTANTIATE_CLASS(ParameterLayer);
REGISTER_LAYER_CLASS(Parameter);

} // namespace caffe
7 changes: 6 additions & 1 deletion src/caffe/proto/caffe.proto
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,7 @@ message ParamSpec {
// NOTE
// Update the next available ID when you add a new LayerParameter field.
//
// LayerParameter next available layer-specific ID: 145 (last added: crop_param)
// LayerParameter next available layer-specific ID: 146 (last added: parameter_param)
message LayerParameter {
optional string name = 1; // the layer name
optional string type = 2; // the layer type
Expand Down Expand Up @@ -385,6 +385,7 @@ message LayerParameter {
optional LRNParameter lrn_param = 118;
optional MemoryDataParameter memory_data_param = 119;
optional MVNParameter mvn_param = 120;
optional ParameterParameter parameter_param = 145;
optional PoolingParameter pooling_param = 121;
optional PowerParameter power_param = 122;
optional PReLUParameter prelu_param = 131;
Expand Down Expand Up @@ -873,6 +874,10 @@ message MVNParameter {
optional float eps = 3 [default = 1e-9];
}

message ParameterParameter {
optional BlobShape shape = 1;
}

message PoolingParameter {
enum PoolMethod {
MAX = 0;
Expand Down

0 comments on commit c419f85

Please sign in to comment.