Apache SINGA
A distributed deep learning platform .
 All Classes Namespaces Files Functions Variables Typedefs Macros
neuralnet.h
1 /************************************************************
2 *
3 * Licensed to the Apache Software Foundation (ASF) under one
4 * or more contributor license agreements. See the NOTICE file
5 * distributed with this work for additional information
6 * regarding copyright ownership. The ASF licenses this file
7 * to you under the Apache License, Version 2.0 (the
8 * "License"); you may not use this file except in compliance
9 * with the License. You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing,
14 * software distributed under the License is distributed on an
15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16 * KIND, either express or implied. See the License for the
17 * specific language governing permissions and limitations
18 * under the License.
19 *
20 *************************************************************/
21 
22 #ifndef SINGA_NEURALNET_NEURALNET_H_
23 #define SINGA_NEURALNET_NEURALNET_H_
24 
25 #include <vector>
26 #include <map>
27 #include <memory>
28 #include <string>
29 
30 #include "proto/job.pb.h"
31 #include "neuralnet/layer.h"
32 #include "utils/factory.h"
33 #include "utils/graph.h"
34 
35 namespace singa {
36 using std::vector;
37 using std::string;
38 using std::map;
39 using std::shared_ptr;
40 
48 class NeuralNet {
49  public:
61  static shared_ptr<NeuralNet> Create(const NetProto& np, Phase phase, int num);
62 
63  public:
69  explicit NeuralNet(NetProto netproto, int npartitions = 1);
70  ~NeuralNet();
74  std::string ToAdjacency();
78  void ShareParamsFrom(shared_ptr<NeuralNet> other);
79 
80  const std::vector<Layer*>& layers() {
81  return layers_;
82  }
83  const std::vector<ParserLayer*>& parserlayers() const {
84  LOG(FATAL)<< " not implemented";
85  return parserlayers_;
86  }
87  const std::vector<LossLayer*>& losslayers() const {
88  LOG(FATAL)<< " not implemented";
89  return losslayers_;
90  }
91  const std::vector<DataLayer*>& datalayers() const {
92  LOG(FATAL)<< " not implemented";
93  return datalayers_;
94  }
95  const std::vector<Param*>& params() const {
96  return params_;
97  }
98  Layer* name2layer(string name) const {
99  if (name2layer_.find(name) != name2layer_.end())
100  return name2layer_.at(name);
101  else
102  return nullptr;
103  }
104  Param* paramid2param(int id) const {
105  return paramid2param_.at(id);
106  }
107 
108  protected:
118  Graph* CreateGraph(const NetProto& netproto, int npartitions);
122  void CreateNetFromGraph(Graph* graph, int npartitions);
126  void PrepareDataStructures();
127 
128  protected:
129  vector<Layer*> layers_;
130  vector<ParserLayer*> parserlayers_;
131  vector<LossLayer*> losslayers_;
132  vector<DataLayer*> datalayers_;
133  vector<Param*> params_;
134 
135  map<string, Layer*> name2layer_;
136  map<int, Param*> paramid2param_;
137 };
138 } // namespace singa
139 #endif // SINGA_NEURALNET_NEURALNET_H_
void PrepareDataStructures()
prepare data structures, e.g., params_, layers_, etc.
static shared_ptr< NeuralNet > Create(const NetProto &np, Phase phase, int num)
Create the neural network for training, test or validation.
Base paramter class.
Definition: param.h:93
Graph * CreateGraph(const NetProto &netproto, int npartitions)
Create a neural net graph, one node for each layer.
void ShareParamsFrom(shared_ptr< NeuralNet > other)
Share memory of parameter values from other neuralnet.
NeuralNet(NetProto netproto, int npartitions=1)
construct the net structure from protocol buffer.
std::string ToAdjacency()
To display the adjacency layers.
Base layer class.
Definition: layer.h:44
Neuralnet is constructed by creating a graph with each node representing one layer at first...
Definition: graph.h:72
void CreateNetFromGraph(Graph *graph, int npartitions)
Create neural net from graph, one layer per node.
The neural network is constructed from user configurations in NetProto.
Definition: neuralnet.h:48