SHOGUN  3.2.1
 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 
107 class CNeuralNetwork : public CMachine
108 {
109 friend class CDeepBeliefNetwork;
110 
111 public:
113  CNeuralNetwork();
114 
122 
129  virtual void set_layers(CDynamicObjectArray* layers);
130 
134  virtual void connect(int32_t i, int32_t j);
135 
139  virtual void quick_connect();
140 
142  virtual void disconnect(int32_t i, int32_t j);
143 
145  virtual void disconnect_all();
146 
152  virtual void initialize(float64_t sigma = 0.01f);
153 
154  virtual ~CNeuralNetwork();
155 
157  virtual CBinaryLabels* apply_binary(CFeatures* data);
162 
174 
179  virtual void set_labels(CLabels* lab);
180 
186 
188  virtual EProblemType get_machine_problem_type() const;
189 
205  virtual float64_t check_gradients(float64_t approx_epsilon=1.0e-3,
206  float64_t s = 1.0e-9);
207 
213 
216 
219 
221  int32_t get_num_inputs() { return m_num_inputs; }
222 
224  int32_t get_num_outputs();
225 
228 
229  virtual const char* get_name() const { return "NeuralNetwork";}
230 
231 protected:
233  virtual bool train_machine(CFeatures* data=NULL);
234 
236  virtual bool train_gradient_descent(SGMatrix<float64_t> inputs,
237  SGMatrix<float64_t> targets);
238 
240  virtual bool train_lbfgs(SGMatrix<float64_t> inputs,
241  SGMatrix<float64_t> targets);
242 
252  virtual SGMatrix<float64_t> forward_propagate(CFeatures* data, int32_t j=-1);
253 
264  virtual SGMatrix<float64_t> forward_propagate(SGMatrix<float64_t> inputs, int32_t j=-1);
265 
274  virtual void set_batch_size(int32_t batch_size);
275 
290  SGMatrix<float64_t> targets, SGVector<float64_t> gradients);
291 
302  SGMatrix<float64_t> targets);
303 
311 
312  virtual bool is_label_valid(CLabels *lab) const;
313 
315  CNeuralLayer* get_layer(int32_t i);
316 
321 
327 
328 private:
329  void init();
330 
332  static float64_t lbfgs_evaluate(void *userdata,
333  const float64_t *W,
334  float64_t *grad,
335  const int32_t n,
336  const float64_t step);
337 
339  static int lbfgs_progress(void *instance,
340  const float64_t *x,
341  const float64_t *g,
342  const float64_t fx,
343  const float64_t xnorm,
344  const float64_t gnorm,
345  const float64_t step,
346  int n,
347  int k,
348  int ls
349  );
350 
352  template<class T>
353  SGVector<T> get_section(SGVector<T> v, int32_t i);
354 public:
357 
360 
363 
373 
383 
390 
398 
403  int32_t max_num_epochs;
404 
410 
413 
420 
430 
442 protected:
444  int32_t m_num_inputs;
445 
447  int32_t m_num_layers;
448 
451 
456 
459 
462 
467 
473 
477  int32_t m_batch_size;
478 
483 
484 private:
488  const SGMatrix<float64_t>* m_lbfgs_temp_inputs;
489  const SGMatrix<float64_t>* m_lbfgs_temp_targets;
490 };
491 
492 }
493 #endif

SHOGUN Machine Learning Toolbox - Documentation