Apache SINGA
A distributed deep learning platform .
 All Classes Namespaces Files Functions Variables Typedefs Enumerator Macros
neuralnet.h
1 #ifndef INCLUDE_NET_NET_H_
2 #define INCLUDE_NET_NET_H_
3 
4 #include <glog/logging.h>
5 #include <vector>
6 #include <map>
7 #include <memory>
8 
9 #include "proto/model.pb.h"
10 #include "neuralnet/layer.h"
11 #include "utils/factory.h"
12 #include "utils/graph.h"
13 
14 using std::vector;
15 using std::string;
16 using std::map;
17 using std::shared_ptr;
18 namespace singa {
27 class NeuralNet {
28  public:
32  static void RegisterLayers();
43  static shared_ptr<NeuralNet> SetupNeuralNet(const NetProto& np, Phase phase,
44  int group_size);
45 
46  public:
50  NeuralNet(NetProto net_proto, int group_size=1);
56  std::string ToString();
61  string DebugInfo();
62 
66  std::string ToAdjacency();
70  void AddLayer(const LayerProto &layer_proto){};
74  void AddLayer(const Layer* layer){};
78  void ShareParams(shared_ptr<NeuralNet> other,int flag);
79  void ToProto(NetProto *net_proto, bool copyData=false);
80  const std::vector<shared_ptr<Layer>>& layers() {
81  return layers_;
82  }
86  const std::vector<ParserLayer*>& parserlayers() {
87  if(parserlayers_.size()==0){
88  for(auto& layer: layers_)
89  if(layer->is_parserlayer())
90  parserlayers_.push_back(static_cast<ParserLayer*>(layer.get()));
91  }
92  return parserlayers_;
93  }
94  const std::vector<LossLayer*>& losslayers() {
95  if(losslayers_.size()==0){
96  for(auto& layer: layers_)
97  if(layer->is_losslayer())
98  losslayers_.push_back(static_cast<LossLayer*>(layer.get()));
99  }
100  return losslayers_;
101  }
102  const std::vector<DataLayer*>& datalayers() {
103  if(datalayers_.size()==0){
104  for(auto& layer: layers_)
105  if(layer->is_datalayer())
106  datalayers_.push_back(static_cast<DataLayer*>(layer.get()));
107  }
108  return datalayers_;
109  }
110  const std::vector<shared_ptr<Param>> &params()const {
111  return params_;
112  }
113  shared_ptr<Layer> name2layer(string name){
114  if (name2layer_.find(name)!=name2layer_.end())
115  return name2layer_[name];
116  else return nullptr;
117  }
118 
119  shared_ptr<Param> paramid2param(int id) {
120  if(paramid2param_.size()==0){
121  for(auto& layer: layers_){
122  for(shared_ptr<Param> p: layer->GetParams()){
123  paramid2param_[p->id()]=p;
124  }
125  }
126  }
127  return paramid2param_[id];
128  }
129 
130  protected:
131  void ConstructNeuralNet(const NetProto &net_proto);
132  void PartitionNeuralNet();
133  map<string, shared_ptr<Layer>> GetNameToLayer(
134  const vector<shared_ptr<Layer>>& layers);
135  Graph CreatePartitonedGraph(const vector<shared_ptr<Layer>>& layers,
136  const map<string, shared_ptr<Layer>>& name2layer);
137 
142  map<string, vector<shared_ptr<Layer>>> PartitionLayers(
143  const vector<shared_ptr<Layer>>& layers);
144 
145  protected:
146  vector<shared_ptr<Layer>> layers_;
147  vector<ParserLayer*> parserlayers_;
148  vector<LossLayer*> losslayers_;
149  vector<DataLayer*> datalayers_;
150  vector<shared_ptr<Param>> params_;
151  map<string, shared_ptr<Layer>> name2layer_;
152  map<int, shared_ptr<Param>> paramid2param_;
153 
154  map<string, LayerProto> name2layerproto_;
155  int group_size_;
156  Graph graph_;
157 };
158 } // namespace singa
159 #endif // INCLUDE_NET_NET_H_
const std::vector< ParserLayer * > & parserlayers()
return ParserLayer of the neuralnet.
Definition: neuralnet.h:86
Definition: model.pb.h:1128
For partition neuralnet and displaying the neuralnet structure.
Definition: graph.h:81
void AddLayer(const LayerProto &layer_proto)
Add layer explicitly used in manually programming/constructing neural net.
Definition: neuralnet.h:70
static shared_ptr< NeuralNet > SetupNeuralNet(const NetProto &np, Phase phase, int group_size)
Setup the neural network for training, test or validation.
std::string ToAdjacency()
to display the adjacency layers
map< string, vector< shared_ptr< Layer > > > PartitionLayers(const vector< shared_ptr< Layer >> &layers)
Partition each layer according its partition type and dimension.
string DebugInfo()
Print Norm1 of data and grad of each Layer and parameter.
Definition: model.pb.h:669
Base layer class.
Definition: base_layer.h:37
void ShareParams(shared_ptr< NeuralNet > other, int flag)
share weights from other neuralnet
NeuralNet(NetProto net_proto, int group_size=1)
construct the net structure from protocol buffer.
The neural network is constructed from user configured layers through google protocol buffer...
Definition: neuralnet.h:27
std::string ToString()
construct a json string representing the neuralnet graph.
static void RegisterLayers()
Register Layers.
void AddLayer(const Layer *layer)
Add layer explicitly used in manually programming/constructing neural net.
Definition: neuralnet.h:74