SHOGUN  4.2.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
NeuralLayer.cpp
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 #include <shogun/base/Parameter.h>
36 #include <shogun/lib/SGVector.h>
38 
39 using namespace shogun;
40 
42 : CSGObject()
43 {
44  init();
45 }
46 
47 
48 CNeuralLayer::CNeuralLayer(int32_t num_neurons)
49 : CSGObject()
50 {
51  init();
52  m_num_neurons = num_neurons;
54  m_height = 1;
55 }
56 
58 {
59 }
60 
62  SGVector< int32_t > input_indices)
63 {
64  m_input_indices = input_indices;
65  m_input_sizes = SGVector<int32_t>(input_indices.vlen);
66 
67  for (int32_t i=0; i<m_input_sizes.vlen; i++)
68  {
69  CNeuralLayer* layer = (CNeuralLayer*)layers->element(m_input_indices[i]);
70  m_input_sizes[i] = layer->get_num_neurons();
71  SG_UNREF(layer);
72  }
73 }
74 
75 void CNeuralLayer::set_batch_size(int32_t batch_size)
76 {
77  m_batch_size = batch_size;
78 
81 
82  if (!is_input())
83  {
87  }
88 }
89 
91 {
92  if (dropout_prop==0.0) return;
93 
94  if (is_training)
95  {
96  int32_t len = m_num_neurons*m_batch_size;
97  for (int32_t i=0; i<len; i++)
98  {
99  m_dropout_mask[i] = CMath::random(0.0,1.0) >= dropout_prop;
100  m_activations[i] *= m_dropout_mask[i];
101  }
102  }
103  else
104  {
105  int32_t len = m_num_neurons*m_batch_size;
106  for (int32_t i=0; i<len; i++)
107  m_activations[i] *= (1.0-dropout_prop);
108  }
109 }
110 
111 void CNeuralLayer::init()
112 {
113  m_num_neurons = 0;
114  m_width = 0;
115  m_height = 0;
116  m_num_parameters = 0;
117  m_batch_size = 0;
118  dropout_prop = 0.0;
120  is_training = false;
122 
123  SG_ADD(&m_num_neurons, "num_neurons",
124  "Number of Neurons", MS_NOT_AVAILABLE);
125  SG_ADD(&m_width, "width",
126  "Width", MS_NOT_AVAILABLE);
127  SG_ADD(&m_height, "height",
128  "Height", MS_NOT_AVAILABLE);
129  SG_ADD(&m_input_indices, "input_indices",
130  "Input Indices", MS_NOT_AVAILABLE);
131  SG_ADD(&m_input_sizes, "input_sizes",
132  "Input Sizes", MS_NOT_AVAILABLE);
133  SG_ADD(&dropout_prop, "dropout_prop",
134  "Dropout Probabilty", MS_NOT_AVAILABLE);
135  SG_ADD(&contraction_coefficient, "contraction_coefficient",
136  "Contraction Coefficient", MS_NOT_AVAILABLE);
137  SG_ADD(&is_training, "is_training",
138  "is_training", MS_NOT_AVAILABLE);
139  SG_ADD(&m_batch_size, "batch_size",
140  "Batch Size", MS_NOT_AVAILABLE);
141  SG_ADD(&m_activations, "activations",
142  "Activations", MS_NOT_AVAILABLE);
143  SG_ADD(&m_activation_gradients, "activation_gradients",
144  "Activation Gradients", MS_NOT_AVAILABLE);
145  SG_ADD(&m_local_gradients, "local_gradients",
146  "Local Gradients", MS_NOT_AVAILABLE);
147  SG_ADD(&m_dropout_mask, "dropout_mask",
148  "Dropout mask", MS_NOT_AVAILABLE);
149 
150  SG_ADD((machine_int_t*)&autoencoder_position, "autoencoder_position",
151  "Autoencoder Position", MS_NOT_AVAILABLE);
152 }
SGVector< int32_t > m_input_sizes
Definition: NeuralLayer.h:368
SGMatrix< float64_t > m_activations
Definition: NeuralLayer.h:376
virtual void initialize_neural_layer(CDynamicObjectArray *layers, SGVector< int32_t > input_indices)
Definition: NeuralLayer.cpp:61
virtual int32_t get_num_neurons()
Definition: NeuralLayer.h:251
SGVector< int32_t > m_input_indices
Definition: NeuralLayer.h:363
Base class for neural network layers.
Definition: NeuralLayer.h:87
SGMatrix< float64_t > m_activation_gradients
Definition: NeuralLayer.h:381
static uint64_t random()
Definition: Math.h:1019
SGMatrix< float64_t > m_local_gradients
Definition: NeuralLayer.h:387
index_t vlen
Definition: SGVector.h:494
Class SGObject is the base class of all shogun objects.
Definition: SGObject.h:115
CSGObject * element(int32_t idx1, int32_t idx2=0, int32_t idx3=0)
ENLAutoencoderPosition autoencoder_position
Definition: NeuralLayer.h:343
Dynamic array class for CSGObject pointers that creates an array that can be used like a list or an a...
virtual void dropout_activations()
Definition: NeuralLayer.cpp:90
#define SG_UNREF(x)
Definition: SGObject.h:55
all of classes and functions are contained in the shogun namespace
Definition: class_list.h:18
float64_t dropout_prop
Definition: NeuralLayer.h:327
virtual void set_batch_size(int32_t batch_size)
Definition: NeuralLayer.cpp:75
int machine_int_t
Definition: common.h:59
#define SG_ADD(...)
Definition: SGObject.h:84
virtual bool is_input()
Definition: NeuralLayer.h:127
SGMatrix< bool > m_dropout_mask
Definition: NeuralLayer.h:393
float64_t contraction_coefficient
Definition: NeuralLayer.h:338

SHOGUN Machine Learning Toolbox - Documentation