SHOGUN  3.2.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups 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;
53 }
54 
56 {
57 }
58 
60  SGVector< int32_t > input_indices)
61 {
62  m_input_indices = input_indices;
63  m_input_sizes = SGVector<int32_t>(input_indices.vlen);
64 
65  for (int32_t i=0; i<m_input_sizes.vlen; i++)
66  {
67  CNeuralLayer* layer = (CNeuralLayer*)layers->element(m_input_indices[i]);
68  m_input_sizes[i] = layer->get_num_neurons();
69  SG_UNREF(layer);
70  }
71 }
72 
73 void CNeuralLayer::set_batch_size(int32_t batch_size)
74 {
75  m_batch_size = batch_size;
76 
79 
80  if (!is_input())
81  {
85  }
86 }
87 
89 {
90  if (dropout_prop==0.0) return;
91 
92  if (is_training)
93  {
94  int32_t len = m_num_neurons*m_batch_size;
95  for (int32_t i=0; i<len; i++)
96  {
97  m_dropout_mask[i] = CMath::random(0.0,1.0) >= dropout_prop;
99  }
100  }
101  else
102  {
103  int32_t len = m_num_neurons*m_batch_size;
104  for (int32_t i=0; i<len; i++)
105  m_activations[i] *= (1.0-dropout_prop);
106  }
107 }
108 
109 void CNeuralLayer::init()
110 {
111  m_num_neurons = 0;
112  m_num_parameters = 0;
113  m_batch_size = 0;
114  dropout_prop = 0.0;
116  is_training = false;
117 
118  SG_ADD(&m_num_neurons, "num_neurons",
119  "Number of Neurons", MS_NOT_AVAILABLE);
120  SG_ADD(&m_num_parameters, "num_parameters",
121  "Number of Parameters", MS_NOT_AVAILABLE);
122  SG_ADD(&m_input_indices, "input_indices",
123  "Input Indices", MS_NOT_AVAILABLE);
124  SG_ADD(&m_input_sizes, "input_sizes",
125  "Input Sizes", MS_NOT_AVAILABLE);
126  SG_ADD(&dropout_prop, "dropout_prop",
127  "Dropout Probabilty", MS_NOT_AVAILABLE);
128  SG_ADD(&contraction_coefficient, "contraction_coefficient",
129  "Contraction Coefficient", MS_NOT_AVAILABLE);
130  SG_ADD(&is_training, "is_training",
131  "is_training", MS_NOT_AVAILABLE);
132  SG_ADD(&m_batch_size, "batch_size",
133  "Batch Size", MS_NOT_AVAILABLE);
134  SG_ADD(&m_activations, "activations",
135  "Activations", MS_NOT_AVAILABLE);
136  SG_ADD(&m_activation_gradients, "activation_gradients",
137  "Activation Gradients", MS_NOT_AVAILABLE);
138  SG_ADD(&m_local_gradients, "local_gradients",
139  "Local Gradients", MS_NOT_AVAILABLE);
140  SG_ADD(&m_dropout_mask, "dropout_mask",
141  "Dropout mask", MS_NOT_AVAILABLE);
142 }

SHOGUN Machine Learning Toolbox - Documentation