SHOGUN  3.2.1
 全部  命名空间 文件 函数 变量 类型定义 枚举 枚举值 友元 宏定义 
NeuralNetwork.h
浏览该文件的文档.
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
EMachineType
Definition: Machine.h:33
SGVector< int32_t > m_index_offsets
virtual CBinaryLabels * apply_binary(CFeatures *data)
Real Labels are real-valued labels.
virtual const char * get_name() const
The class Labels models labels, i.e. class assignments of objects.
Definition: Labels.h:43
SGVector< float64_t > get_parameters()
virtual bool train_machine(CFeatures *data=NULL)
SGVector< float64_t > m_params
A generic multi-layer neural network.
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()
virtual float64_t compute_error(SGMatrix< float64_t > inputs, SGMatrix< float64_t > targets)
A generic learning machine interface.
Definition: Machine.h:143
float64_t gd_learning_rate_decay
SGVector< bool > m_param_regularizable
virtual CMulticlassLabels * apply_multiclass(CFeatures *data)
Multiclass Labels for multi-class classification.
ENNOptimizationMethod optimization_method
A Deep Belief Network.
EProblemType
Definition: Machine.h:110
CDynamicObjectArray * m_layers
float64_t gd_error_damping_coeff
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)
Dynamic array class for CSGObject pointers that creates an array that can be used like a list or an a...
CDynamicObjectArray * get_layers()
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)
all of classes and functions are contained in the shogun namespace
Definition: class_list.h:18
virtual void set_labels(CLabels *lab)
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)
virtual EProblemType get_machine_problem_type() const
Binary Labels for binary classification.
Definition: BinaryLabels.h:37
virtual void initialize(float64_t sigma=0.01f)
virtual void set_layers(CDynamicObjectArray *layers)
SGVector< float64_t > * get_layer_parameters(int32_t i)
ENNOptimizationMethod
Definition: NeuralNetwork.h:49
virtual float64_t compute_gradients(SGMatrix< float64_t > inputs, SGMatrix< float64_t > targets, SGVector< float64_t > gradients)

SHOGUN 机器学习工具包 - 项目文档