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;
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 }

SHOGUN Machine Learning Toolbox - Documentation