ConvNet  1.0
A GPU-based C++ implementation of Convolutional Neural Nets
 All Classes Namespaces Functions Variables
edge.h
1 #ifndef EDGE_H_
2 #define EDGE_H_
3 #include "util.h"
4 #include "matrix.h"
5 #include <iostream>
6 
7 class Layer;
8 
13 class Edge {
14  public:
16  Edge(const config::Edge& edge_config);
17  ~Edge();
18 
22  virtual void AllocateMemory(bool fprop_only);
23 
25  virtual void Initialize();
26 
30  virtual void SaveParameters(hid_t file);
31 
35  virtual void LoadParameters(hid_t file);
36 
37  virtual void InsertPolyak();
38  virtual void BackupCurrent();
39  virtual void LoadCurrentOnGPU();
40  virtual void LoadPolyakOnGPU();
41 
43  virtual float GetRMSWeight();
44 
46  virtual void ReduceLearningRate(float factor);
47 
49  virtual bool HasNoParameters() const;
50 
54  virtual int GetNumModules() const;
55 
59  virtual void DisplayWeights();
60 
62  virtual void DisplayWeightStats();
63 
65  virtual void SetTiedTo(Edge* e);
66 
70  virtual void ComputeUp(Matrix& input, Matrix& output, bool overwrite) = 0;
71 
79  virtual void ComputeDown(Matrix& deriv_output, Matrix& input,
80  Matrix& output, Matrix& deriv_input,
81  bool overwrite) = 0;
82 
87  virtual void ComputeOuter(Matrix& input, Matrix& deriv_output);
88 
90  virtual void UpdateWeights();
91 
92  //virtual bool RequiresMemoryForDeriv() const;
93 
95  virtual void SetImageSize(int image_size);
96 
98  bool IsBackPropBlocked() const { return block_backprop_; }
99 
100  void SetSource(Layer* source);
101  void SetDest(Layer* dest);
102  Layer* GetSource();
103  Layer* GetDest();
104  const string& GetSourceName();
105  const string& GetDestName();
106  const string& GetName();
107 
109  void SetInputChannels(int a);
111  void SetOutputChannels(int a);
112 
113  void SetMark();
114  bool HasMark();
115  string GetTiedEdgeName();
116  bool IsTied();
117  int GetGPUId() const { return gpu_id_; }
118 
119  // Multi-gpu.
120  void ComputeStart(Matrix& mat);
121  void ComputeEnd(Matrix& mat);
122 
124  static Edge* ChooseEdgeClass(const config::Edge& edge_config);
125 
126  protected:
127  Layer *source_;
129  const string source_node_, dest_node_, name_, tied_edge_name_;
130  Edge* tied_edge_; /* The edge to which this edge is tied.*/
131  int num_input_channels_, num_output_channels_, image_size_, num_modules_;
132  bool mark_;
133  const bool block_backprop_, is_tied_;
134  ImageDisplayer *img_display_;
135  const int gpu_id_;
136 };
137 #endif
const bool block_backprop_
A marker.
Definition: edge.h:133
Layer * dest_
The source layer for this edge.
Definition: edge.h:128
The base class for all layers.
Definition: layer.h:9
virtual void DisplayWeights()
Displays the weights.
Definition: edge.cc:110
Definition: util.h:59
virtual void Initialize()
Initialize the weights and biases.
Definition: edge.cc:99
void SetInputChannels(int a)
Set the number of input channels.
Definition: edge.cc:81
static Edge * ChooseEdgeClass(const config::Edge &edge_config)
Selects the appropriate derived class for the edge config.
Definition: edge.cc:17
virtual void ReduceLearningRate(float factor)
Reduce the learning rate by factor.
Definition: edge.cc:118
virtual void SetImageSize(int image_size)
Set the spatial size of the input to this edge.
Definition: edge.cc:191
This class is intended to be used as a base class for implementing edges.
Definition: edge.h:13
void SetOutputChannels(int a)
Set the number of output channels.
Definition: edge.cc:85
const string source_node_
The destination layer for this edge.
Definition: edge.h:129
virtual void UpdateWeights()
Update the weights.
Definition: edge.cc:126
virtual void LoadParameters(hid_t file)
Load the weights and biases from an hdf5 file.
Definition: edge.cc:94
virtual int GetNumModules() const
Returns the number of modules.
Definition: edge.cc:174
virtual float GetRMSWeight()
Returns the root mean square weight value.
Definition: edge.cc:130
virtual void AllocateMemory(bool fprop_only)
Allocate memory for the model.
Definition: edge.cc:103
A GPU matrix class.
Definition: matrix.h:11
bool IsBackPropBlocked() const
Returns whether back prop is blocked through this edge.
Definition: edge.h:98
virtual void SaveParameters(hid_t file)
Write the weights and biases in an hdf5 file.
Definition: edge.cc:89
Edge(const config::Edge &edge_config)
Instatntiate an Edge from the config.
Definition: edge.cc:54
virtual void ComputeDown(Matrix &deriv_output, Matrix &input, Matrix &output, Matrix &deriv_input, bool overwrite)=0
Computes the derivative w.r.t the inputs of this edge given the derivative w.r.t the outputs of this ...
virtual void ComputeUp(Matrix &input, Matrix &output, bool overwrite)=0
Computes the output layer state given the input.
virtual bool HasNoParameters() const
Returns whether the edge has any parameters.
Definition: edge.cc:170
virtual void SetTiedTo(Edge *e)
Sets the edge to be tied to another edge.
Definition: edge.cc:77
virtual void ComputeOuter(Matrix &input, Matrix &deriv_output)
Computes the gradient for the weights and biases.
Definition: edge.cc:122
virtual void DisplayWeightStats()
Displays the statistics of the weights.
Definition: edge.cc:114