Apache SINGA
A distributed deep learning platform .
 All Classes Namespaces Files Functions Variables Typedefs Macros
layer.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_LAYER_H_
23 #define SINGA_NEURALNET_LAYER_H_
24 
25 #include <map>
26 #include <string>
27 #include <thread>
28 #include <vector>
29 #include "proto/common.pb.h"
30 #include "proto/job.pb.h"
31 #include "utils/common.h"
32 #include "utils/blob.h"
33 #include "utils/param.h"
34 
35 namespace singa {
36 
44 class Layer {
45  public:
46  static Layer* Create(const LayerProto& proto);
47 
48  Layer() {}
49  virtual ~Layer() {}
60  virtual void Setup(const LayerProto& proto, int npartitions = 1) {
61  CHECK_GE(npartitions, 1);
62  layer_proto_ = proto;
63  }
69  virtual void ComputeFeature(int flag, Metric* perf) = 0;
76  virtual void ComputeGradient(int flag, Metric* perf) = 0;
83  virtual const std::vector<Param*> GetParams() const {
84  return std::vector<Param*> {};
85  }
97  virtual ConnectionType src_neuron_connection(int k) const {
98  // CHECK_LT(k, srclayers_.size());
99  return kOneToOne;
100  }
113  virtual ConnectionType dst_layer_connection() const {
114  return kOneToOne;
115  }
125  virtual const std::string DebugString(int step, int flag);
132  inline int partition_dim() const {
133  CHECK_LE(layer_proto_.partition_dim(), 1);
134  return layer_proto_.partition_dim();
135  }
136  inline int partition_id() const { return layer_proto_.partition_id(); }
137  inline int type() const { return layer_proto_.type(); }
141  inline const std::string &name() const { return layer_proto_.name(); }
154  virtual const Blob<float>& data(const Layer* from) const {
155  return data_;
156  }
157  virtual Blob<float>* mutable_data(const Layer* from) {
158  return &data_;
159  }
160  virtual const Blob<float>& grad(const Layer* from) const {
161  return grad_;
162  }
166  virtual Blob<float>* mutable_grad(const Layer* from) {
167  return &grad_;
168  }
172  inline const std::vector<Layer*> srclayers() const { return srclayers_; }
176  inline const std::vector<Layer*> dstlayers() const { return dstlayers_; }
177  inline int srclayers_size() const { return srclayers_.size(); }
178  inline int dstlayers_size() const { return dstlayers_.size(); }
179  inline void clear_dstlayers() { dstlayers_.clear(); }
180  inline void clear_srclayers() { srclayers_.clear(); }
181  inline void add_srclayer(Layer* src) { srclayers_.push_back(src); }
182  inline void add_dstlayer(Layer* dst) { dstlayers_.push_back(dst); }
183 
184  protected:
185  LayerProto layer_proto_;
186  Blob<float> data_, grad_;
187  std::vector<Layer*> srclayers_, dstlayers_;
188 };
189 
193 class ConnectionLayer : virtual public Layer {
194  // defined as a layer category
195 };
196 
201 class InputLayer : virtual public Layer {
202  // defined as a layer category
203 };
204 
205 
206 class NeuronLayer : virtual public Layer {
207  // defined as a layer category
208 };
209 
213 class LossLayer : virtual public Layer {
214  public:
215  Blob<float>* mutable_grad(const Layer* layer) override {
216  return nullptr;
217  }
218  const Blob<float>& grad(const Layer* from) const override {
219  LOG(FATAL) << "Loss layer has no gradient blob";
220  return grad_;
221  }
222 
223  protected:
224  Blob<float> metric_;
225 };
226 
227 } // namespace singa
228 
229 #include "neuralnet/connection_layer.h"
230 #include "neuralnet/input_layer.h"
231 #include "neuralnet/loss_layer.h"
232 #include "neuralnet/neuron_layer.h"
233 #include "neuralnet/output_layer.h"
234 
235 #endif // SINGA_NEURALNET_LAYER_H_
Definition: layer.h:206
virtual ConnectionType src_neuron_connection(int k) const
Return the connection type between one neuron of this layer and its source layer. ...
Definition: layer.h:97
virtual void Setup(const LayerProto &proto, int npartitions=1)
Setup layer properties.
Definition: layer.h:60
virtual const std::string DebugString(int step, int flag)
For print debug info about each layer, e.g., norm of feature vector, norm of parameters.
const std::string & name() const
Return name of this layer.
Definition: layer.h:141
Base layer for connecting layers when neural net is partitioned.
Definition: layer.h:193
const std::vector< Layer * > srclayers() const
return LayerS that connected to this layer
Definition: layer.h:172
virtual ConnectionType dst_layer_connection() const
Return the connection type of this layer and all dst layers.
Definition: layer.h:113
Base layer for calculating loss and other metrics, e.g., precison.
Definition: layer.h:213
virtual void ComputeFeature(int flag, Metric *perf)=0
Compute features of this layer based on connected layers.
Base layer for getting input data.
Definition: layer.h:201
virtual void ComputeGradient(int flag, Metric *perf)=0
Compute gradients for parameters and connected layers.
const std::vector< Layer * > dstlayers() const
return LayerS that this layer connected to
Definition: layer.h:176
virtual const std::vector< Param * > GetParams() const
Layers that have paramters must override this function.
Definition: layer.h:83
Base layer class.
Definition: layer.h:44
Blob< float > * mutable_grad(const Layer *layer) override
Definition: layer.h:215
int partition_dim() const
Definition: layer.h:132
virtual Blob< float > * mutable_grad(const Layer *from)
Definition: layer.h:166
Performance mtrics.
Definition: common.h:85
virtual const Blob< float > & data(const Layer *from) const
Definition: layer.h:154