Shark machine learning library
About Shark
News!
Contribute
Credits and copyright
Downloads
Getting Started
Installation
Using the docs
Documentation
Tutorials
Quick references
Class list
Global functions
FAQ
Showroom
include
shark
Models
RecurrentStructure.h
Go to the documentation of this file.
1
//===========================================================================
2
/*! \brief Offers a basic structure for recurrent networks
3
*
4
* \author O. Krause
5
* \date 2011
6
*
7
* \par Copyright 1995-2017 Shark Development Team
8
*
9
* <BR><HR>
10
* This file is part of Shark.
11
* <http://shark-ml.org/>
12
*
13
* Shark is free software: you can redistribute it and/or modify
14
* it under the terms of the GNU Lesser General Public License as published
15
* by the Free Software Foundation, either version 3 of the License, or
16
* (at your option) any later version.
17
*
18
* Shark is distributed in the hope that it will be useful,
19
* but WITHOUT ANY WARRANTY; without even the implied warranty of
20
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21
* GNU Lesser General Public License for more details.
22
*
23
* You should have received a copy of the GNU Lesser General Public License
24
* along with Shark. If not, see <http://www.gnu.org/licenses/>.
25
*
26
*/
27
#ifndef SHARK_ML_MODEL_RECURENTNETWORK_H
28
#define SHARK_ML_MODEL_RECURENTNETWORK_H
29
30
#include <
shark/Core/DLLSupport.h
>
31
#include <
shark/LinAlg/Base.h
>
32
#include <
shark/Core/ISerializable.h
>
33
namespace
shark
{
34
//! \brief Offers a basic structure for recurrent networks.
35
//!
36
//! it is possible to define the tzpe of sigmoids and the form of the connection matrix.
37
//! this structure can be shared between different types of ents like the RNNet and the OnlineRNNet
38
class
RecurrentStructure
:
public
ISerializable
39
{
40
public
:
41
//! Creates an empty recurrent neural network.
42
//! A call to setStructure is needed afterwards to configure the topology of the network
43
SHARK_EXPORT_SYMBOL
RecurrentStructure
();
44
45
46
//! type enum for the different variants of sigmoids
47
enum
SigmoidType
{
48
///f(x) = x
49
Linear
,
50
///f(x) = 1/(1+exp(-x))
51
Logistic
,
52
///f(x) = tanh(x)
53
Tanh
,
54
/// f(x) = x/(1+|x|)
55
FastSigmoid
56
};
57
58
//!returns the connection Matrix of the network.
59
//!
60
//!The format is best described with an example:
61
//!given a Network with 2 inputs, 2 outputs and 2 hidden and no bias unit,
62
//!where the inputs are only connected to the hidden units.
63
//!The corresponding matrix looks like this:
64
//!
65
//!1 2 3 4 5 6 7
66
//!1 1 0 1 1 1 1 first hidden
67
//!1 1 0 1 1 1 1 second hidden
68
//!0 0 0 1 1 1 1 first output
69
//!0 0 0 1 1 1 1 second output
70
//!
71
//!The ith row stands for the ith neuron of the network and when an element
72
//!(i,j) is 1, the ith unit will receive input from unit j.
73
//! the first =0,..,inputs-1 columns are the input neurons followd by the column of the bias,
74
//! which is completely zero in this example
75
//!if j is a hidden or output neuron, the activation from the PREVIOUS time step
76
//!is used. if j is an input neuron, the current input is used.
77
//!input neurons can't receive activation. This is no limitation, since the hidden
78
//!layer can be subdivided in arbitrary sublayers when the right topology is used.
79
//! the last column of the matrix is reserved for the bias neuron. So the matrix has size
80
//! NxN+1
81
const
IntMatrix&
connections
()
const
{
82
return
m_connectionMatrix
;
83
}
84
//! returns whether the connection between neuron i and j exists
85
bool
connection
(std::size_t i, std::size_t j)
const
{
86
return
m_connectionMatrix
(i,j);
87
}
88
89
//!returns the current weight matrix
90
const
RealMatrix&
weights
()
const
{
91
return
m_weights
;
92
}
93
//! returns the weight of the connection between neuron i and j
94
double
weight
(std::size_t i, std::size_t j)
const
{
95
return
m_weights
(i,j);
96
}
97
98
//!returns the type of sigmoid used in this network
99
SigmoidType
sigmoidType
()
const
{
100
return
m_sigmoidType
;
101
}
102
103
//!sets the type of sigmoid used in this network
104
//!\param sigmoidType the type of sigmoid
105
void
setSigmoidType
(
SigmoidType
sigmoidType
){
106
m_sigmoidType
=
sigmoidType
;
107
}
108
109
//!Sets the weight matrix. It is not allowed that elements are non-zero
110
//!when the element in the connection matrix is 0!
111
//!\param weights the new weight matrix
112
SHARK_EXPORT_SYMBOL
void
setWeights
(
const
RealMatrix&
weights
);
113
114
//! \brief Based on a given connection matrix a network is created.
115
//!
116
//! This method needs to know how many inputs and outputs the network has
117
//! and how the units are connected.
118
//!
119
//! If a standard structure is needed, see the other version of this method.
120
//! Also see #connections for a quick explanation of the matrix format
121
//!
122
//! The same mechanic applies alo to recurrentConnections
123
//! but every element can be set, not only the lower triangular part.
124
//!
125
//! After this operation, all weights are initialized to 0.
126
//!
127
//!
128
//! \param inputs number of input neurons of the network
129
//! \param outputs number of output neurons of the network
130
//! \param connections feed-forward connections. default is true
131
//! \param sigmoidType the type of the sigmoid to be used. the default is the Logistic function
132
SHARK_EXPORT_SYMBOL
void
setStructure
(std::size_t
inputs
, std::size_t
outputs
,
const
IntMatrix&
connections
,
SigmoidType
sigmoidType
=
Logistic
);
133
134
135
//! \brief Creates a fully connected topology for the network with optional bias
136
//!
137
//! After a call, the network will have hidden+out units.
138
//!
139
//!
140
//! \param in number of input neurons
141
//! \param hidden number of output neurons
142
//! \param out number of input neurons
143
//! \param bias enables bias neuron, default is true
144
//! \param sigmoidType the type of the sigmoid to be used. the default is the Logistic function
145
SHARK_EXPORT_SYMBOL
void
setStructure
(std::size_t in, std::size_t hidden, std::size_t out,
bool
bias
=
true
,
SigmoidType
sigmoidType
=
Logistic
);
146
147
//! get internal parameters of the model
148
SHARK_EXPORT_SYMBOL
RealVector
parameterVector
()
const
;
149
150
//! set internal parameters of the model
151
SHARK_EXPORT_SYMBOL
void
setParameterVector
(RealVector
const
& newParameters);
152
153
//! From ISerializable, reads the Network from an archive
154
SHARK_EXPORT_SYMBOL
void
read
(
InArchive
& archive );
155
156
//! From ISerializable, writes the Network to an archive
157
SHARK_EXPORT_SYMBOL
void
write
(
OutArchive
& archive )
const
;
158
159
//! The number of input neurons of the network
160
std::size_t
inputs
()
const
{
161
return
m_inputNeurons
;
162
}
163
//! The number of output neurons of the network
164
std::size_t
outputs
()
const
{
165
return
m_outputNeurons
;
166
}
167
std::size_t
numberOfNeurons
()
const
{
168
return
m_numberOfNeurons
;
169
}
170
std::size_t
numberOfUnits
()
const
{
171
return
m_numberOfUnits
;
172
}
173
174
//! The index of the bias unit
175
std::size_t
bias
()
const
{
176
return
m_bias
;
177
}
178
179
//! number of parameters of the network
180
std::size_t
parameters
()
const
{
181
return
m_numberOfParameters
;
182
}
183
184
//! Activation function for a neuron.
185
SHARK_EXPORT_SYMBOL
double
neuron
(
double
activation);
186
187
//! Computes the derivative of the neuron.
188
SHARK_EXPORT_SYMBOL
double
neuronDerivative
(
double
activation);
189
190
protected
:
191
192
//================Convenience index variables=====================
193
//! The total number of neurons of the network (input, output and hidden).
194
std::size_t
m_numberOfNeurons
;
195
196
//! total number units of the network (input, output, hidden and bias)
197
std::size_t
m_numberOfUnits
;
198
199
//! The number of input neurons of the network
200
std::size_t
m_inputNeurons
;
201
//! The number of output neurons of the network
202
std::size_t
m_outputNeurons
;
203
//! The number of hidden neurons of the network
204
std::size_t
m_hidden
;
205
206
//! index of the bias unit
207
std::size_t
m_bias
;
208
209
//! type of Sigmoid used by the network
210
SigmoidType
m_sigmoidType
;
211
212
//===================network variables========================
213
//! The absolute number of parameters of the network
214
std::size_t
m_numberOfParameters
;
215
216
//! stores the topology of the network.
217
//! Element (i,j) is 1 if the ith neuron receives input from neuron j.
218
//! The data for neuron i is stored in the ith row of the matrix.
219
IntMatrix
m_connectionMatrix
;
220
221
//! stores the feed-forward part of the weights. the recurrent part is added
222
//! via m_recurrentWeights. The weights for neuron i are stored in the ith row of the matrix
223
RealMatrix
m_weights
;
224
};
225
}
226
227
#endif //RNNET_H
228
229
230
231
232
233
234
235
236