SHOGUN  3.2.2
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups 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(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 
234 protected:
236  virtual bool train_machine(CFeatures* data=NULL);
237 
239  virtual bool train_gradient_descent(SGMatrix<float64_t> inputs,
240  SGMatrix<float64_t> targets);
241 
243  virtual bool train_lbfgs(SGMatrix<float64_t> inputs,
244  SGMatrix<float64_t> targets);
245 
255  virtual SGMatrix<float64_t> forward_propagate(CFeatures* data, int32_t j=-1);
256 
267  virtual SGMatrix<float64_t> forward_propagate(SGMatrix<float64_t> inputs, int32_t j=-1);
268 
277  virtual void set_batch_size(int32_t batch_size);
278 
293  SGMatrix<float64_t> targets, SGVector<float64_t> gradients);
294 
305  SGMatrix<float64_t> targets);
306 
314 
315  virtual bool is_label_valid(CLabels *lab) const;
316 
318  CNeuralLayer* get_layer(int32_t i);
319 
324 
330 
331 private:
332  void init();
333 
335  static float64_t lbfgs_evaluate(void *userdata,
336  const float64_t *W,
337  float64_t *grad,
338  const int32_t n,
339  const float64_t step);
340 
342  static int lbfgs_progress(void *instance,
343  const float64_t *x,
344  const float64_t *g,
345  const float64_t fx,
346  const float64_t xnorm,
347  const float64_t gnorm,
348  const float64_t step,
349  int n,
350  int k,
351  int ls
352  );
353 
355  template<class T>
356  SGVector<T> get_section(SGVector<T> v, int32_t i);
357 public:
360 
363 
366 
376 
386 
393 
401 
406  int32_t max_num_epochs;
407 
413 
416 
423 
433 
445 protected:
447  int32_t m_num_inputs;
448 
450  int32_t m_num_layers;
451 
454 
459 
462 
465 
470 
476 
480  int32_t m_batch_size;
481 
486 
487 private:
491  const SGMatrix<float64_t>* m_lbfgs_temp_inputs;
492  const SGMatrix<float64_t>* m_lbfgs_temp_targets;
493 };
494 
495 }
496 #endif

SHOGUN Machine Learning Toolbox - Documentation