From 6f8c677bf73bfdaef3c3acb3db4abd3cc04d3e6a Mon Sep 17 00:00:00 2001 From: Jonathan L Long Date: Sat, 27 Dec 2014 01:39:15 -0800 Subject: [PATCH] layers get a pointer back to their owning Net This allows layers to do things that depend, e.g., on net topology. --- include/caffe/layer.hpp | 9 +++++++++ src/caffe/net.cpp | 1 + 2 files changed, 10 insertions(+) diff --git a/include/caffe/layer.hpp b/include/caffe/layer.hpp index b14b632e694..6c768b9bbe2 100644 --- a/include/caffe/layer.hpp +++ b/include/caffe/layer.hpp @@ -15,6 +15,8 @@ namespace caffe { +template class Net; + /** * @brief An interface for the units of computation which can be composed into a * Net. @@ -301,6 +303,10 @@ class Layer { return DiagonalAffineMap(vector >()); } + /** + * @brief Used by Net to give layers a pointer to their owning net. + */ + void set_net(Net* net) { net_ = net; } protected: /** The protobuf that stores the layer parameters */ @@ -314,6 +320,9 @@ class Layer { * the objective function. */ vector loss_; + /** The net to which this layer belongs. */ + Net* net_; + /** @brief Using the CPU device, compute the layer output. */ virtual void Forward_cpu(const vector*>& bottom, const vector*>& top) = 0; diff --git a/src/caffe/net.cpp b/src/caffe/net.cpp index e4492cfd6e3..67f8a22b186 100644 --- a/src/caffe/net.cpp +++ b/src/caffe/net.cpp @@ -64,6 +64,7 @@ void Net::Init(const NetParameter& in_param) { const LayerParameter& layer_param = param.layers(layer_id); layers_.push_back(shared_ptr >( LayerRegistry::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;