SHOGUN  4.2.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
NeuralNetwork.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2014, Shogun Toolbox Foundation
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7 
8  * 1. Redistributions of source code must retain the above copyright notice,
9  * this list of conditions and the following disclaimer.
10  *
11  * 2. Redistributions in binary form must reproduce the above copyright notice,
12  * this list of conditions and the following disclaimer in the documentation
13  * and/or other materials provided with the distribution.
14  *
15  * 3. Neither the name of the copyright holder nor the names of its
16  * contributors may be used to endorse or promote products derived from this
17  * software without specific prior written permission.
18 
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
23  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  *
31  * Written (W) 2014 Khaled Nasr
32  */
33 
34 #ifndef __NEURALNETWORK_H__
35 #define __NEURALNETWORK_H__
36 
37 #include <shogun/lib/common.h>
38 #include <shogun/machine/Machine.h>
39 #include <shogun/lib/SGVector.h>
40 #include <shogun/lib/SGMatrix.h>
41 
42 namespace shogun
43 {
44 template<class T> class CDenseFeatures;
45 class CDynamicObjectArray;
46 class CNeuralLayer;
47 
50 {
53 };
54 
110 class CNeuralNetwork : public CMachine
111 {
112 friend class CDeepBeliefNetwork;
113 
114 public:
116  CNeuralNetwork();
117 
125 
132  virtual void set_layers(CDynamicObjectArray* layers);
133 
137  virtual void connect(int32_t i, int32_t j);
138 
142  virtual void quick_connect();
143 
145  virtual void disconnect(int32_t i, int32_t j);
146 
148  virtual void disconnect_all();
149 
155  virtual void initialize_neural_network(float64_t sigma = 0.01f);
156 
157  virtual ~CNeuralNetwork();
158 
160  virtual CBinaryLabels* apply_binary(CFeatures* data);
165 
177 
182  virtual void set_labels(CLabels* lab);
183 
189 
191  virtual EProblemType get_machine_problem_type() const;
192 
208  virtual float64_t check_gradients(float64_t approx_epsilon=1.0e-3,
209  float64_t s = 1.0e-9);
210 
216 
219 
222 
224  int32_t get_num_inputs() { return m_num_inputs; }
225 
227  int32_t get_num_outputs();
228 
231 
232  virtual const char* get_name() const { return "NeuralNetwork";}
233 
239  {
240  m_optimization_method = optimization_method;
241  }
242 
245  {
246  return m_optimization_method;
247  }
252  void set_l2_coefficient(float64_t l2_coefficient)
253  {
254  m_l2_coefficient = l2_coefficient;
255  }
256 
259  {
260  return m_l2_coefficient;
261  }
266  void set_l1_coefficient(float64_t l1_coefficient)
267  {
268  m_l1_coefficient = l1_coefficient;
269  }
270 
273  {
274  return m_l1_coefficient;
275  }
276 
286  void set_dropout_hidden(float64_t dropout_hidden)
287  {
288  m_dropout_hidden = dropout_hidden;
289  }
290 
293  {
294  return m_dropout_hidden;
295  }
296 
306  void set_dropout_input(float64_t dropout_input)
307  {
308  m_dropout_input = dropout_input;
309  }
310 
313  {
314  return m_dropout_input;
315  }
316 
322  void set_max_norm(float64_t max_norm)
323  {
324  m_max_norm = max_norm;
325  }
326 
329  {
330  return m_max_norm;
331  }
332 
340  void set_epsilon(float64_t epsilon)
341  {
342  m_epsilon = epsilon;
343  }
344 
347  {
348  return m_epsilon;
349  }
350 
356  void set_max_num_epochs(int32_t max_num_epochs)
357  {
358  m_max_num_epochs = max_num_epochs;
359  }
360 
362  int32_t get_max_num_epochs() const
363  {
364  return m_max_num_epochs;
365  }
366 
372  void set_gd_mini_batch_size(int32_t gd_mini_batch_size)
373  {
374  m_gd_mini_batch_size = gd_mini_batch_size;
375  }
376 
378  int32_t get_gd_mini_batch_size() const
379  {
380  return m_gd_mini_batch_size;
381  }
382 
387  void set_gd_learning_rate(float64_t gd_learning_rate)
388  {
389  m_gd_learning_rate = gd_learning_rate;
390  }
391 
394  {
395  return m_gd_learning_rate;
396  }
397 
404  void set_gd_learning_rate_decay(float64_t gd_learning_rate_decay)
405  {
406  m_gd_learning_rate_decay = gd_learning_rate_decay;
407  }
408 
411  {
413  }
414 
425  void set_gd_momentum(float64_t gd_momentum)
426  {
427  m_gd_momentum = gd_momentum;
428  }
429 
432  {
433  return m_gd_momentum;
434  }
435 
449  void set_gd_error_damping_coeff(float64_t gd_error_damping_coeff)
450  {
451  m_gd_error_damping_coeff = gd_error_damping_coeff;
452  }
453 
455  {
457  }
458 
459 protected:
461  virtual bool train_machine(CFeatures* data=NULL);
462 
464  virtual bool train_gradient_descent(SGMatrix<float64_t> inputs,
465  SGMatrix<float64_t> targets);
466 
468  virtual bool train_lbfgs(SGMatrix<float64_t> inputs,
469  SGMatrix<float64_t> targets);
470 
480  virtual SGMatrix<float64_t> forward_propagate(CFeatures* data, int32_t j=-1);
481 
492  virtual SGMatrix<float64_t> forward_propagate(SGMatrix<float64_t> inputs, int32_t j=-1);
493 
502  virtual void set_batch_size(int32_t batch_size);
503 
518  SGMatrix<float64_t> targets, SGVector<float64_t> gradients);
519 
530  SGMatrix<float64_t> targets);
531 
539 
540  virtual bool is_label_valid(CLabels *lab) const;
541 
543  CNeuralLayer* get_layer(int32_t i);
544 
549 
555 
556 private:
557  void init();
558 
560  static float64_t lbfgs_evaluate(void *userdata,
561  const float64_t *W,
562  float64_t *grad,
563  const int32_t n,
564  const float64_t step);
565 
567  static int lbfgs_progress(void *instance,
568  const float64_t *x,
569  const float64_t *g,
570  const float64_t fx,
571  const float64_t xnorm,
572  const float64_t gnorm,
573  const float64_t step,
574  int n,
575  int k,
576  int ls
577  );
578 
580  template<class T>
581  SGVector<T> get_section(SGVector<T> v, int32_t i);
582 
583 protected:
585  int32_t m_num_inputs;
586 
588  int32_t m_num_layers;
589 
592 
597 
600 
603 
608 
614 
618  int32_t m_batch_size;
619 
624 
627 
630 
633 
643 
653 
660 
668 
674 
680 
683 
690 
700 
712 
713 private:
717  const SGMatrix<float64_t>* m_lbfgs_temp_inputs;
718  const SGMatrix<float64_t>* m_lbfgs_temp_targets;
719 };
720 
721 }
722 #endif
void set_gd_learning_rate(float64_t gd_learning_rate)
EMachineType
Definition: Machine.h:33
SGVector< int32_t > m_index_offsets
virtual CBinaryLabels * apply_binary(CFeatures *data)
void set_gd_momentum(float64_t gd_momentum)
Real Labels are real-valued labels.
virtual void initialize_neural_network(float64_t sigma=0.01f)
int32_t get_gd_mini_batch_size() const
float64_t get_l2_coefficient() const
virtual const char * get_name() const
float64_t get_gd_learning_rate() const
void set_max_norm(float64_t max_norm)
void set_gd_mini_batch_size(int32_t gd_mini_batch_size)
The class Labels models labels, i.e. class assignments of objects.
Definition: Labels.h:43
SGVector< float64_t > get_parameters()
float64_t m_gd_error_damping_coeff
virtual bool train_machine(CFeatures *data=NULL)
SGVector< float64_t > m_params
void set_dropout_hidden(float64_t dropout_hidden)
A generic multi-layer neural network.
float64_t get_dropout_input() const
float64_t get_gd_learning_rate_decay() const
SGMatrix< bool > m_adj_matrix
SGMatrix< float64_t > features_to_matrix(CFeatures *features)
virtual void disconnect(int32_t i, int32_t j)
Base class for neural network layers.
Definition: NeuralLayer.h:87
virtual bool train_gradient_descent(SGMatrix< float64_t > inputs, SGMatrix< float64_t > targets)
virtual void quick_connect()
float64_t get_gd_error_damping_coeff() const
void set_max_num_epochs(int32_t max_num_epochs)
virtual float64_t compute_error(SGMatrix< float64_t > inputs, SGMatrix< float64_t > targets)
A generic learning machine interface.
Definition: Machine.h:143
void set_epsilon(float64_t epsilon)
float64_t get_gd_momentum() const
SGVector< bool > m_param_regularizable
virtual CMulticlassLabels * apply_multiclass(CFeatures *data)
Multiclass Labels for multi-class classification.
A Deep Belief Network.
int32_t get_max_num_epochs() const
EProblemType
Definition: Machine.h:110
CDynamicObjectArray * m_layers
virtual void connect(int32_t i, int32_t j)
virtual void set_batch_size(int32_t batch_size)
virtual void disconnect_all()
double float64_t
Definition: common.h:50
virtual CRegressionLabels * apply_regression(CFeatures *data)
ENNOptimizationMethod get_optimization_method() const
void set_gd_error_damping_coeff(float64_t gd_error_damping_coeff)
void set_l2_coefficient(float64_t l2_coefficient)
Dynamic array class for CSGObject pointers that creates an array that can be used like a list or an a...
ENNOptimizationMethod m_optimization_method
float64_t m_gd_learning_rate_decay
CDynamicObjectArray * get_layers()
float64_t get_max_norm() const
virtual float64_t check_gradients(float64_t approx_epsilon=1.0e-3, float64_t s=1.0e-9)
CNeuralLayer * get_layer(int32_t i)
virtual bool is_label_valid(CLabels *lab) const
virtual CDenseFeatures< float64_t > * transform(CDenseFeatures< float64_t > *data)
float64_t get_dropout_hidden() const
all of classes and functions are contained in the shogun namespace
Definition: class_list.h:18
virtual void set_labels(CLabels *lab)
void set_l1_coefficient(float64_t l1_coefficient)
virtual bool train_lbfgs(SGMatrix< float64_t > inputs, SGMatrix< float64_t > targets)
virtual EMachineType get_classifier_type()
The class Features is the base class of all feature objects.
Definition: Features.h:68
SGMatrix< float64_t > labels_to_matrix(CLabels *labs)
virtual SGMatrix< float64_t > forward_propagate(CFeatures *data, int32_t j=-1)
float64_t get_l1_coefficient() const
virtual EProblemType get_machine_problem_type() const
void set_gd_learning_rate_decay(float64_t gd_learning_rate_decay)
Binary Labels for binary classification.
Definition: BinaryLabels.h:37
virtual void set_layers(CDynamicObjectArray *layers)
void set_optimization_method(ENNOptimizationMethod optimization_method)
SGVector< float64_t > * get_layer_parameters(int32_t i)
ENNOptimizationMethod
Definition: NeuralNetwork.h:49
float64_t get_epsilon() const
virtual float64_t compute_gradients(SGMatrix< float64_t > inputs, SGMatrix< float64_t > targets, SGVector< float64_t > gradients)
void set_dropout_input(float64_t dropout_input)

SHOGUN Machine Learning Toolbox - Documentation