Apache SINGA
A distributed deep learning platform .
 All Classes Namespaces Files Functions Variables Typedefs Enumerator Macros
layer.h
1 #ifndef INCLUDE_NET_LAYER_H_
2 #define INCLUDE_NET_LAYER_H_
3 
4 #include <vector>
5 #include <string>
6 #include <map>
7 #include <functional>
8 #include <utility>
9 #include <memory>
10 #include <chrono>
11 #include <random>
12 #include <lmdb.h>
13 
14 #include "proto/model.pb.h"
15 #include "utils/data_shard.h"
16 #include "neuralnet/base_layer.h"
17 
18 
23 namespace singa {
24 
28 class ConvolutionLayer: public Layer {
29  public:
30  using Layer::Setup;
34 
35  virtual void Setup(const LayerProto& proto,
36  const vector<SLayer>& srclayers);
41  virtual void SetupAfterPartition(const LayerProto& proto,
42  const vector<int> &shape,
43  const vector<SLayer>& srclayers);
44 
45  virtual void ComputeFeature(bool training, const vector<shared_ptr<Layer>>& srclayers);
46  virtual void ComputeGradient(const vector<shared_ptr<Layer>>& srclayers);
47  virtual vector<shared_ptr<Param>> GetParams() {
48  return vector<shared_ptr<Param>>{weight_, bias_};
49  }
50  virtual ConnectionType connection_type(int k) const {
51  CHECK_LT(k, srclayers_.size());
52  return kOneToAll;
53  }
54  protected:
55  int kernel_, pad_, stride_ ;
56  int batchsize_, channels_, height_,width_;
57  int col_height_, col_width_, conv_height_, conv_width_, num_filters_;
58  shared_ptr<Param> weight_, bias_;
59  Blob<float> col_data_, col_grad_;
60 };
61 
62 class DropoutLayer: public Layer {
63  public:
64  using Layer::Setup;
68 
69  virtual void Setup(const LayerProto& proto,
70  const vector<SLayer>& srclayers);
71 
72  virtual void SetupAfterPartition(const LayerProto& proto,
73  const vector<int> &shape,
74  const vector<SLayer>& srclayers);
75 
76  virtual void ComputeFeature(bool training, const vector<shared_ptr<Layer>>& srclayers);
77  virtual void ComputeGradient(const vector<shared_ptr<Layer>>& srclayers);
78  protected:
79  // drop probability
80  float pdrop_;
81  /* record which neuron is dropped, required for back propagating gradients,
82  * if mask[i]=0, then the i-th neuron is dropped.
83  */
84  Blob<float> mask_;
85 };
86 
90 class InnerProductLayer: public Layer {
91  public:
92  using Layer::Setup;
96 
97  virtual void Setup(const LayerProto& proto,
98  const vector<SLayer>& srclayers);
99 
103  virtual void SetupAfterPartition(const LayerProto& proto,
104  const vector<int> &shape,
105  const vector<SLayer>& srclayers);
106  virtual ConnectionType connection_type(int k) const {
107  CHECK_LT(k, srclayers_.size());
108  return kOneToAll;
109  }
110 
111  virtual void ComputeFeature(bool training, const vector<shared_ptr<Layer>>& srclayers);
112  virtual void ComputeGradient(const vector<shared_ptr<Layer>>& srclayers);
113  //virtual void ToProto(LayerProto *layer_proto, bool copyData);
114  virtual vector<shared_ptr<Param>> GetParams() {
115  return vector<shared_ptr<Param>>{weight_, bias_};
116  }
117 
118  private:
120  int hdim_;
122  int vdim_;
123  int batchsize_;
124  shared_ptr<Param> weight_, bias_;
125 };
126 
127 class LabelLayer: public ParserLayer {
128  public:
129  using ParserLayer::Setup;
130 
131  virtual void Setup(const LayerProto& proto, const vector<SLayer>& srclayers);
132  virtual void ParseRecords(bool training, const vector<Record>& records,
133  Blob<float>* blob);
134 };
135 
136 class LRNLayer: public Layer {
145  using Layer::Setup;
147  using Layer::ComputeFeature;
149 
150  public:
151  virtual void Setup(const LayerProto& proto,
152  const vector<SLayer>& srclayers);
153 
154  virtual void SetupAfterPartition(const LayerProto& proto,
155  const vector<int> &shape,
156  const vector<SLayer>& srclayers);
157 
158 
159  virtual void ComputeFeature(bool training, const vector<shared_ptr<Layer>>& srclayers);
160  virtual void ComputeGradient(const vector<shared_ptr<Layer>>& srclayers);
161  protected:
163  int batchsize_, channels_, height_, width_;
165  int lsize_;
167  float alpha_, beta_, knorm_;
168  Blob<float> norm_;
169 };
170 
172  public:
173  using Layer::Setup;
174 
175  virtual void Setup(const LayerProto& proto, const vector<SLayer>& srclayers);
176  virtual void ParseRecords(bool training, const vector<Record>& records,
177  Blob<float>* blob);
178 
179  protected:
180  // height and width of the image after deformation
181  // kernel size for elastic distortion
182  // n^2 images are processed as a batch for elastic distortion
183  // conv height and conv width
184  // gauss kernel values, displacements, column image and tmp buffer
185  //float* gauss_, *displacementx_, *displacementy_, *colimg_, *tmpimg_;
186  float gamma_, beta_, sigma_, kernel_, alpha_, norm_a_, norm_b_;
187  int resize_, elastic_freq_;
188 };
189 
190 class PoolingLayer: public Layer {
191  public:
192  using Layer::Setup;
194  using Layer::ComputeFeature;
196 
197  virtual void Setup(const LayerProto& proto,
198  const vector<SLayer>& srclayers);
199  virtual void SetupAfterPartition(const LayerProto& proto,
200  const vector<int> &shape,
201  const vector<SLayer>& srclayers);
202  virtual void ComputeFeature(bool training, const vector<shared_ptr<Layer>>& srclayers);
203  virtual void ComputeGradient(const vector<shared_ptr<Layer>>& srclayers);
204  protected:
205  int kernel_, pad_, stride_;
206  int batchsize_,channels_, height_, width_, pooled_height_, pooled_width_;
207  PoolingProto_PoolMethod pool_;
208 };
209 
210 class ReLULayer: public Layer {
211  public:
212  using Layer::Setup;
214  using Layer::ComputeFeature;
216 
217 
218  virtual void Setup(const LayerProto& proto,
219  const vector<SLayer>& srclayers);
220  virtual void SetupAfterPartition(const LayerProto& proto,
221  const vector<int> &shape,
222  const vector<SLayer>& srclayers);
223 
224  virtual void ComputeFeature(bool training, const vector<shared_ptr<Layer>>& srclayers);
225  virtual void ComputeGradient(const vector<shared_ptr<Layer>>& srclayers);
226 };
227 
228 
230  /*
231  * connected from the label layer and the last fc layer
232  */
233  public:
234  using Layer::Setup;
236  using Layer::ComputeFeature;
238 
239  virtual void Setup(const LayerProto& proto,
240  const vector<SLayer>& srclayers);
241 
242  virtual void SetupAfterPartition(const LayerProto& proto,
243  const vector<int> &shape,
244  const vector<SLayer>& srclayers);
249  virtual PartitionType partition_type() const {
250  if(layer_proto_.partition_type()==kLayerPartition)
251  return kNone;
252  else
253  return layer_proto_.partition_type();
254  }
255  virtual ConnectionType connection_type(int k) const {
256  CHECK_LT(k, srclayers_.size());
257  return kOneToAll;
258  }
259 
260  virtual void ComputeFeature(bool training, const vector<shared_ptr<Layer>>& srclayers);
261  virtual void ComputeGradient(const vector<shared_ptr<Layer>>& srclayers);
262  private:
263  int batchsize_;
264  int dim_;
265  float scale_;
266  int topk_;
267 };
268 
269 class RGBImageLayer: public ParserLayer {
270  public:
271  using Layer::Setup;
272 
273  virtual void Setup(const LayerProto& proto, const vector<SLayer>& srclayers);
274  virtual void ParseRecords(bool training, const vector<Record>& records,
275  Blob<float>* blob);
276 
277  private:
278  float scale_;
279  int cropsize_;
280  bool mirror_;
281  Blob<float> mean_;
282 };
283 
284 class ShardDataLayer: public DataLayer{
285  public:
286  using Layer::Setup;
287  using Layer::ComputeFeature;
289 
290  virtual void ComputeFeature(bool training, const vector<shared_ptr<Layer>>& srclayers);
291  virtual void ComputeGradient(const vector<shared_ptr<Layer>>& srclayers){};
292  virtual void Setup(const LayerProto& proto, const vector<SLayer>& srclayers);
293  private:
294  shared_ptr<DataShard> shard_;
295 };
296 class LMDBDataLayer: public DataLayer{
297  public:
298  using Layer::Setup;
299  using Layer::ComputeFeature;
301 
302  virtual void ComputeFeature(bool training, const vector<shared_ptr<Layer>>& srclayers);
303  virtual void ComputeGradient(const vector<shared_ptr<Layer>>& srclayers){};
304  virtual void Setup(const LayerProto& proto, const vector<SLayer>& srclayers);
305  void ConvertDatumToSingleLableImageRecord(const Datum& datum,
306  SingleLabelImageRecord* record);
307 
308  private:
309  MDB_env* mdb_env_;
310  MDB_dbi mdb_dbi_;
311  MDB_txn* mdb_txn_;
312  MDB_cursor* mdb_cursor_;
313  MDB_val mdb_key_, mdb_value_;
314 };
315 
321 class TanhLayer: public Layer {
322  public:
323  using Layer::Setup;
325  using Layer::ComputeFeature;
327 
328  virtual void Setup(const LayerProto& proto,
329  const vector<SLayer>& srclayers);
330 
331  virtual void SetupAfterPartition(const LayerProto& proto,
332  const vector<int> &shape,
333  const vector<SLayer>& srclayers);
334 
335 
336  virtual void ComputeFeature(bool training, const vector<shared_ptr<Layer>>& srclayers);
337  virtual void ComputeGradient(const vector<shared_ptr<Layer>>& srclayers);
338  private:
339  float outer_scale_, inner_scale_;
340 };
341 
342 
343 } // namespace singa
344 
345 #endif // INCLUDE_NET_LAYER_H_
Definition: layer.h:210
int lsize_
size local response (neighbor) area
Definition: layer.h:165
parse the input records into Blobs.
Definition: base_layer.h:551
float alpha_
hyper-parameter
Definition: layer.h:167
Definition: layer.h:190
virtual void ComputeGradient()
Compute gradients for parameters and connecting layers.
Definition: model.pb.h:3309
Definition: model.pb.h:1128
This layer apply Tan function to neuron activations.
Definition: layer.h:321
Definition: layer.h:127
virtual void ParseRecords(bool training, const vector< Record > &records, Blob< float > *blob)
Parse records from DataLayer into blob.
virtual ConnectionType connection_type(int k) const
Return connection type between two layers.
Definition: layer.h:50
Definition: layer.h:269
virtual ConnectionType connection_type(int k) const
Return connection type between two layers.
Definition: layer.h:106
int batchsize_
shape of the bottom layer feature
Definition: layer.h:163
virtual const vector< SLayer > srclayers() const
return LayerS that connected to this layer
Definition: base_layer.h:221
Definition: model.pb.h:3159
virtual void ComputeFeature(bool training, const vector< SLayer > &srclayers)=0
Compute features of this layer based on connected layers.
virtual void Setup()
Setup layer properties.
Definition: layer.h:284
Base layer for reading records from local Shard, HDFS, lmdb, etc.
Definition: base_layer.h:375
Definition: layer.h:296
virtual vector< shared_ptr< Param > > GetParams()
Layers that have paramters must overload this function.
Definition: layer.h:47
virtual ConnectionType connection_type(int k) const
Return connection type between two layers.
Definition: layer.h:255
virtual void ParseRecords(bool training, const vector< Record > &records, Blob< float > *blob)
Parse records from DataLayer into blob.
Loss layer to calculate loss and other metrics, e.g., precison.
Definition: base_layer.h:520
virtual void Setup()
Setup layer properties.
Definition: base_layer.h:577
Definition: layer.h:136
virtual void ParseRecords(bool training, const vector< Record > &records, Blob< float > *blob)
Parse records from DataLayer into blob.
virtual void SetupAfterPartition()
Setup the layer properties except shape.
Base layer class.
Definition: base_layer.h:37
Definition: layer.h:62
Definition: layer.h:229
virtual void Setup()
Setup layer properties.
Definition: base_layer.h:390
fully connected layer
Definition: layer.h:90
Convolution layer.
Definition: layer.h:28
Definition: layer.h:171
virtual PartitionType partition_type() const
softmax is not recommendeded for partition because it requires the whole src layer for normalization...
Definition: layer.h:249
virtual vector< shared_ptr< Param > > GetParams()
Layers that have paramters must overload this function.
Definition: layer.h:114