shark::RecurrentStructure Class Reference

Offers a basic structure for recurrent networks. More...

#include <shark/Models/RecurrentStructure.h>

+ Inheritance diagram for shark::RecurrentStructure:

Public Types

enum  SigmoidType { Linear, Logistic, Tanh, FastSigmoid }
 type enum for the different variants of sigmoids More...
 

Public Member Functions

SHARK_EXPORT_SYMBOL RecurrentStructure ()
 
const IntMatrix & connections () const
 
bool connection (std::size_t i, std::size_t j) const
 returns whether the connection between neuron i and j exists More...
 
const RealMatrix & weights () const
 returns the current weight matrix More...
 
double weight (std::size_t i, std::size_t j) const
 returns the weight of the connection between neuron i and j More...
 
SigmoidType sigmoidType () const
 returns the type of sigmoid used in this network More...
 
void setSigmoidType (SigmoidType sigmoidType)
 
SHARK_EXPORT_SYMBOL void setWeights (const RealMatrix &weights)
 
SHARK_EXPORT_SYMBOL void setStructure (std::size_t inputs, std::size_t outputs, const IntMatrix &connections, SigmoidType sigmoidType=Logistic)
 Based on a given connection matrix a network is created. More...
 
SHARK_EXPORT_SYMBOL void setStructure (std::size_t in, std::size_t hidden, std::size_t out, bool bias=true, SigmoidType sigmoidType=Logistic)
 Creates a fully connected topology for the network with optional bias. More...
 
SHARK_EXPORT_SYMBOL RealVector parameterVector () const
 get internal parameters of the model More...
 
SHARK_EXPORT_SYMBOL void setParameterVector (RealVector const &newParameters)
 set internal parameters of the model More...
 
SHARK_EXPORT_SYMBOL void read (InArchive &archive)
 From ISerializable, reads the Network from an archive. More...
 
SHARK_EXPORT_SYMBOL void write (OutArchive &archive) const
 From ISerializable, writes the Network to an archive. More...
 
std::size_t inputs () const
 The number of input neurons of the network. More...
 
std::size_t outputs () const
 The number of output neurons of the network. More...
 
std::size_t numberOfNeurons () const
 
std::size_t numberOfUnits () const
 
std::size_t bias () const
 The index of the bias unit. More...
 
std::size_t parameters () const
 number of parameters of the network More...
 
SHARK_EXPORT_SYMBOL double neuron (double activation)
 Activation function for a neuron. More...
 
SHARK_EXPORT_SYMBOL double neuronDerivative (double activation)
 Computes the derivative of the neuron. More...
 
- Public Member Functions inherited from shark::ISerializable
virtual ~ISerializable ()
 Virtual d'tor. More...
 
void load (InArchive &archive, unsigned int version)
 Versioned loading of components, calls read(...). More...
 
void save (OutArchive &archive, unsigned int version) const
 Versioned storing of components, calls write(...). More...
 
 BOOST_SERIALIZATION_SPLIT_MEMBER ()
 

Protected Attributes

std::size_t m_numberOfNeurons
 The total number of neurons of the network (input, output and hidden). More...
 
std::size_t m_numberOfUnits
 total number units of the network (input, output, hidden and bias) More...
 
std::size_t m_inputNeurons
 The number of input neurons of the network. More...
 
std::size_t m_outputNeurons
 The number of output neurons of the network. More...
 
std::size_t m_hidden
 The number of hidden neurons of the network. More...
 
std::size_t m_bias
 index of the bias unit More...
 
SigmoidType m_sigmoidType
 type of Sigmoid used by the network More...
 
std::size_t m_numberOfParameters
 The absolute number of parameters of the network. More...
 
IntMatrix m_connectionMatrix
 
RealMatrix m_weights
 

Detailed Description

Offers a basic structure for recurrent networks.

it is possible to define the tzpe of sigmoids and the form of the connection matrix. this structure can be shared between different types of ents like the RNNet and the OnlineRNNet

Definition at line 38 of file RecurrentStructure.h.

Member Enumeration Documentation

◆ SigmoidType

type enum for the different variants of sigmoids

Enumerator
Linear 

f(x) = x

Logistic 

f(x) = 1/(1+exp(-x))

Tanh 

f(x) = tanh(x)

FastSigmoid 

f(x) = x/(1+|x|)

Definition at line 47 of file RecurrentStructure.h.

Constructor & Destructor Documentation

◆ RecurrentStructure()

SHARK_EXPORT_SYMBOL shark::RecurrentStructure::RecurrentStructure ( )

Creates an empty recurrent neural network. A call to setStructure is needed afterwards to configure the topology of the network

Member Function Documentation

◆ bias()

std::size_t shark::RecurrentStructure::bias ( ) const
inline

The index of the bias unit.

Definition at line 175 of file RecurrentStructure.h.

References m_bias.

Referenced by setSigmoidType().

◆ connection()

bool shark::RecurrentStructure::connection ( std::size_t  i,
std::size_t  j 
) const
inline

returns whether the connection between neuron i and j exists

Definition at line 85 of file RecurrentStructure.h.

References m_connectionMatrix.

◆ connections()

const IntMatrix& shark::RecurrentStructure::connections ( ) const
inline

returns the connection Matrix of the network.

The format is best described with an example: given a Network with 2 inputs, 2 outputs and 2 hidden and no bias unit, where the inputs are only connected to the hidden units. The corresponding matrix looks like this:

1 2 3 4 5 6 7 1 1 0 1 1 1 1 first hidden 1 1 0 1 1 1 1 second hidden 0 0 0 1 1 1 1 first output 0 0 0 1 1 1 1 second output

The ith row stands for the ith neuron of the network and when an element (i,j) is 1, the ith unit will receive input from unit j. the first =0,..,inputs-1 columns are the input neurons followd by the column of the bias, which is completely zero in this example if j is a hidden or output neuron, the activation from the PREVIOUS time step is used. if j is an input neuron, the current input is used. input neurons can't receive activation. This is no limitation, since the hidden layer can be subdivided in arbitrary sublayers when the right topology is used. the last column of the matrix is reserved for the bias neuron. So the matrix has size NxN+1

Definition at line 81 of file RecurrentStructure.h.

References m_connectionMatrix.

Referenced by setSigmoidType().

◆ inputs()

std::size_t shark::RecurrentStructure::inputs ( ) const
inline

The number of input neurons of the network.

Definition at line 160 of file RecurrentStructure.h.

References m_inputNeurons.

Referenced by shark::OnlineRNNet::inputSize(), shark::RNNet::inputSize(), and setSigmoidType().

◆ neuron()

SHARK_EXPORT_SYMBOL double shark::RecurrentStructure::neuron ( double  activation)

Activation function for a neuron.

Referenced by parameters().

◆ neuronDerivative()

SHARK_EXPORT_SYMBOL double shark::RecurrentStructure::neuronDerivative ( double  activation)

Computes the derivative of the neuron.

Referenced by parameters().

◆ numberOfNeurons()

std::size_t shark::RecurrentStructure::numberOfNeurons ( ) const
inline

Definition at line 167 of file RecurrentStructure.h.

References m_numberOfNeurons.

◆ numberOfUnits()

std::size_t shark::RecurrentStructure::numberOfUnits ( ) const
inline

Definition at line 170 of file RecurrentStructure.h.

References m_numberOfUnits.

Referenced by shark::OnlineRNNet::setOutputActivation().

◆ outputs()

std::size_t shark::RecurrentStructure::outputs ( ) const
inline

The number of output neurons of the network.

Definition at line 164 of file RecurrentStructure.h.

References m_outputNeurons.

Referenced by shark::OnlineRNNet::outputSize(), shark::RNNet::outputSize(), and setSigmoidType().

◆ parameters()

std::size_t shark::RecurrentStructure::parameters ( ) const
inline

number of parameters of the network

Definition at line 180 of file RecurrentStructure.h.

References m_numberOfParameters, neuron(), neuronDerivative(), and SHARK_EXPORT_SYMBOL.

Referenced by shark::OnlineRNNet::numberOfParameters(), and shark::RNNet::numberOfParameters().

◆ parameterVector()

SHARK_EXPORT_SYMBOL RealVector shark::RecurrentStructure::parameterVector ( ) const

get internal parameters of the model

Referenced by shark::OnlineRNNet::parameterVector(), shark::RNNet::parameterVector(), and setSigmoidType().

◆ read()

SHARK_EXPORT_SYMBOL void shark::RecurrentStructure::read ( InArchive archive)
virtual

From ISerializable, reads the Network from an archive.

Reimplemented from shark::ISerializable.

Referenced by setSigmoidType().

◆ setParameterVector()

SHARK_EXPORT_SYMBOL void shark::RecurrentStructure::setParameterVector ( RealVector const &  newParameters)

set internal parameters of the model

Referenced by shark::OnlineRNNet::setParameterVector(), shark::RNNet::setParameterVector(), and setSigmoidType().

◆ setSigmoidType()

void shark::RecurrentStructure::setSigmoidType ( SigmoidType  sigmoidType)
inline

sets the type of sigmoid used in this network

Parameters
sigmoidTypethe type of sigmoid

Definition at line 105 of file RecurrentStructure.h.

References bias(), connections(), inputs(), Logistic, m_sigmoidType, outputs(), parameterVector(), read(), setParameterVector(), setStructure(), setWeights(), SHARK_EXPORT_SYMBOL, sigmoidType(), weights(), and write().

◆ setStructure() [1/2]

SHARK_EXPORT_SYMBOL void shark::RecurrentStructure::setStructure ( std::size_t  inputs,
std::size_t  outputs,
const IntMatrix &  connections,
SigmoidType  sigmoidType = Logistic 
)

Based on a given connection matrix a network is created.

This method needs to know how many inputs and outputs the network has and how the units are connected.

If a standard structure is needed, see the other version of this method. Also see connections for a quick explanation of the matrix format

The same mechanic applies alo to recurrentConnections but every element can be set, not only the lower triangular part.

After this operation, all weights are initialized to 0.

Parameters
inputsnumber of input neurons of the network
outputsnumber of output neurons of the network
connectionsfeed-forward connections. default is true
sigmoidTypethe type of the sigmoid to be used. the default is the Logistic function

Referenced by setSigmoidType().

◆ setStructure() [2/2]

SHARK_EXPORT_SYMBOL void shark::RecurrentStructure::setStructure ( std::size_t  in,
std::size_t  hidden,
std::size_t  out,
bool  bias = true,
SigmoidType  sigmoidType = Logistic 
)

Creates a fully connected topology for the network with optional bias.

After a call, the network will have hidden+out units.

Parameters
innumber of input neurons
hiddennumber of output neurons
outnumber of input neurons
biasenables bias neuron, default is true
sigmoidTypethe type of the sigmoid to be used. the default is the Logistic function

◆ setWeights()

SHARK_EXPORT_SYMBOL void shark::RecurrentStructure::setWeights ( const RealMatrix &  weights)

Sets the weight matrix. It is not allowed that elements are non-zero when the element in the connection matrix is 0!

Parameters
weightsthe new weight matrix

Referenced by setSigmoidType().

◆ sigmoidType()

SigmoidType shark::RecurrentStructure::sigmoidType ( ) const
inline

returns the type of sigmoid used in this network

Definition at line 99 of file RecurrentStructure.h.

References m_sigmoidType.

Referenced by setSigmoidType().

◆ weight()

double shark::RecurrentStructure::weight ( std::size_t  i,
std::size_t  j 
) const
inline

returns the weight of the connection between neuron i and j

Definition at line 94 of file RecurrentStructure.h.

References m_weights.

◆ weights()

const RealMatrix& shark::RecurrentStructure::weights ( ) const
inline

returns the current weight matrix

Definition at line 90 of file RecurrentStructure.h.

References m_weights.

Referenced by setSigmoidType().

◆ write()

SHARK_EXPORT_SYMBOL void shark::RecurrentStructure::write ( OutArchive archive) const
virtual

From ISerializable, writes the Network to an archive.

Reimplemented from shark::ISerializable.

Referenced by setSigmoidType().

Member Data Documentation

◆ m_bias

std::size_t shark::RecurrentStructure::m_bias
protected

index of the bias unit

Definition at line 207 of file RecurrentStructure.h.

Referenced by bias().

◆ m_connectionMatrix

IntMatrix shark::RecurrentStructure::m_connectionMatrix
protected

stores the topology of the network. Element (i,j) is 1 if the ith neuron receives input from neuron j. The data for neuron i is stored in the ith row of the matrix.

Definition at line 219 of file RecurrentStructure.h.

Referenced by connection(), and connections().

◆ m_hidden

std::size_t shark::RecurrentStructure::m_hidden
protected

The number of hidden neurons of the network.

Definition at line 204 of file RecurrentStructure.h.

◆ m_inputNeurons

std::size_t shark::RecurrentStructure::m_inputNeurons
protected

The number of input neurons of the network.

Definition at line 200 of file RecurrentStructure.h.

Referenced by inputs().

◆ m_numberOfNeurons

std::size_t shark::RecurrentStructure::m_numberOfNeurons
protected

The total number of neurons of the network (input, output and hidden).

Definition at line 194 of file RecurrentStructure.h.

Referenced by numberOfNeurons().

◆ m_numberOfParameters

std::size_t shark::RecurrentStructure::m_numberOfParameters
protected

The absolute number of parameters of the network.

Definition at line 214 of file RecurrentStructure.h.

Referenced by parameters().

◆ m_numberOfUnits

std::size_t shark::RecurrentStructure::m_numberOfUnits
protected

total number units of the network (input, output, hidden and bias)

Definition at line 197 of file RecurrentStructure.h.

Referenced by numberOfUnits().

◆ m_outputNeurons

std::size_t shark::RecurrentStructure::m_outputNeurons
protected

The number of output neurons of the network.

Definition at line 202 of file RecurrentStructure.h.

Referenced by outputs().

◆ m_sigmoidType

SigmoidType shark::RecurrentStructure::m_sigmoidType
protected

type of Sigmoid used by the network

Definition at line 210 of file RecurrentStructure.h.

Referenced by setSigmoidType(), and sigmoidType().

◆ m_weights

RealMatrix shark::RecurrentStructure::m_weights
protected

stores the feed-forward part of the weights. the recurrent part is added via m_recurrentWeights. The weights for neuron i are stored in the ith row of the matrix

Definition at line 223 of file RecurrentStructure.h.

Referenced by weight(), and weights().


The documentation for this class was generated from the following file: