Skip to content

Commit

Permalink
layers get a pointer back to their owning Net
Browse files Browse the repository at this point in the history
This allows layers to do things that depend, e.g., on net topology.
  • Loading branch information
longjon committed Dec 29, 2014
1 parent 98fc02f commit 1081373
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 0 deletions.
9 changes: 9 additions & 0 deletions include/caffe/layer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@

namespace caffe {

template <typename Dtype> class Net;

/**
* @brief An interface for the units of computation which can be composed into a
* Net.
Expand Down Expand Up @@ -301,6 +303,10 @@ class Layer {
return DiagonalAffineMap<Dtype>(vector<pair<Dtype, Dtype> >());
}

/**
* @brief Used by Net to give layers a pointer to their owning net.
*/
void set_net(Net<Dtype>* net) { net_ = net; }

protected:
/** The protobuf that stores the layer parameters */
Expand All @@ -314,6 +320,9 @@ class Layer {
* the objective function. */
vector<Dtype> loss_;

/** The net to which this layer belongs. */
Net<Dtype>* net_;

/** @brief Using the CPU device, compute the layer output. */
virtual void Forward_cpu(const vector<Blob<Dtype>*>& bottom,
const vector<Blob<Dtype>*>& top) = 0;
Expand Down
1 change: 1 addition & 0 deletions src/caffe/net.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ void Net<Dtype>::Init(const NetParameter& in_param) {
const LayerParameter& layer_param = param.layers(layer_id);
layers_.push_back(shared_ptr<Layer<Dtype> >(
LayerRegistry<Dtype>::CreateLayer(layer_param)));
layers_[layer_id]->set_net(this);
layer_names_.push_back(layer_param.name());
LOG(INFO) << "Creating Layer " << layer_param.name();
bool need_backward = false;
Expand Down

0 comments on commit 1081373

Please sign in to comment.