Neuron.h 6.63 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11
/**
 * DESCRIPTION OF THE CLASS
 *
 * @author Martin Beseda
 * @author Martin Mrovec
 * @author Michal Kravčenko
 * @date 2017 - 2018
 */

 #ifndef NEURON_H_
 #define NEURON_H_
12

13
#include <boost/serialization/base_object.hpp>
14 15 16 17
#include <vector>
#include "../NetConnection/Connection.h"

class Connection;
Martin Beseda's avatar
Martin Beseda committed
18

19
/**
20 21 22
  * Abstract class representing a general neuron
  */
class Neuron{
23
    friend class boost::serialization::access;
24

25
protected:
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
    /**
     * Inner potential of the neuron (input of the activation function)
     */
    double potential = 0.0;

    /**
     * State of the neuron (output of the activation function)
     */
    double state = 0.0;

    /**
     * array of the activation function parameters
     */
    double *activation_function_parameters = nullptr;

    /**
     * Number of parameters of the activation function
     */
David Vojtek's avatar
David Vojtek committed
44
    unsigned int n_activation_function_parameters = 0;
45

46 47 48
    /**
     * Keeps track of the number of saturated INCOMING connections
     */
David Vojtek's avatar
David Vojtek committed
49
    unsigned int n_saturated_connections_in = 0;
50 51 52 53

    /**
     * Keeps track of the number of saturated OUTGOING connections
     */
David Vojtek's avatar
David Vojtek committed
54
    unsigned int n_saturated_connections_out = 0;
55

56 57 58 59 60 61 62 63 64 65
    /**
     * A pointer to a vector containing pointers to incoming connections
     */
    std::vector<Connection*> *edges_in = nullptr;

    /**
     * A pointer to a vector containing pointers to outgoing connections
     */
    std::vector<Connection*> *edges_out = nullptr;

66 67 68 69 70 71 72 73 74 75 76
    template<class Archive>
    void serialize(Archive & ar, const unsigned int version){
        //TODO separate implementation to Neuron.cpp!
        ar & this->potential;
        ar & this->state;

        for(unsigned short int i = 0; i < this->n_activation_function_parameters; i++) {
            ar & this->activation_function_parameters[i];
        }
    };

77 78 79 80 81
public:

    /**
     * Destructor of the Neuron object
     * this level deallocates the array 'activation_function_parameters'
82
     * also deallocates the OUTGOING connections
83 84 85 86 87 88
     */
    virtual ~Neuron();

    /**
     * Performs the activation function and stores the result into the 'state' property
     */
89
    virtual void activate( ) = 0;
90

91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
    /**
     * Sets the number of saturated INPUT connections to the prescribed value
     * @param[in] value The number of saturated INCOMING connections
     */
    virtual void set_saturation_in(int value) final;

    /**
     * Increases the saturation counter of the INCOMING connections by the specified amount
     * @param value Amount to be added to the counter
     */
    virtual void adjust_saturation_in(int value) final;

    /**
     * Returns true if the neuron is saturated via the INCOMING connections
     * @return true/false
     */
    virtual bool is_saturated_in() final;

    /**
     * Returns true if the neuron is saturated via the OUTGOING connections
     * @return true/false
     */
    virtual bool is_saturated_out() final;

    /**
     * Sets the number of saturated OUTPUT connections to the prescribed value
     * @param[in] value The number of saturated OUTGOING connections
     */
    virtual void set_saturation_out(int value) final;

    /**
     * Increases the saturation counter of the OUTGOING connections by the specified amount
     * @param value Amount to be added to the counter
     */
    virtual void adjust_saturation_out(int value) final;

127 128 129 130 131 132 133 134 135 136 137 138 139 140
    /**
     * Adds the input signal value to the current potential
     * @param[in] input_signal Input value
     */
    virtual void adjust_potential(double input_signal);

    /**
     * Getter to the 'potential' property
     * @return Value of the neuron's potential (real number)
     */
    virtual double get_potential();

    /**
     * Setter to the 'potential' property
141
     * @param[in] value Value of the neuron's potential (real number)
142 143 144 145 146 147 148 149 150 151 152
     */
    virtual void set_potential(double value);

    /**
     * Getter to the 'state' property
     * @return Value of the current neuron's state
     */
    virtual double get_state();

    /**
     * Setter to the 'state' component
153
     * @param[in] value Value of the current neuron's state
154 155 156 157 158 159 160 161 162 163 164 165
     */
    virtual void set_state(double value);

    /**
     * Returns the number of parameters the activation function relies on
     * @return Number of the parameters
     */
    virtual int activation_function_get_n_parameters();

    /**
     * Adjusts the parameter with index 'param_idx' of the activation function
     * by the value prescribed by 'value'
166 167
     * @param[in] param_idx Index of the parameter to be adjusted
     * @param[in] value Value of the adjustment
168 169 170 171 172 173
     */
    virtual void activation_function_adjust_parameter(int param_idx, double value);

    /**
     * Sets the parameter with index 'param_idx' of the activation function
     * to the value prescribed by 'value'
174 175
     * @param[in] param_idx
     * @param[in] value
176 177 178 179 180
     */
    virtual void activation_function_set_parameter(int param_idx, double value);

    /**
     * Gets the parameter with index 'param_idx' of the activation function
181
     * @param[in] param_idx
182 183 184
     */
    virtual double activation_function_get_parameter(int param_idx);

185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208
    /**
     * Adds a new incoming connection with this neuron as its end-point
     * @param[in] con Incoming connection
     */
    virtual void add_connection_in(Connection *con) final;

    /**
     * Adds a new outgoing connection with this neuron as its source-point
     * @param[in] con Outgoing connection
     */
    virtual void add_connection_out(Connection *con) final;

    /**
     * Returns the pointer to the incoming connection vector
     * @return
     */
    virtual std::vector<Connection*>* get_connections_in( ) final;

    /**
     * Returns the pointer to the outgoing connection vector
     * @return
     */
    virtual std::vector<Connection*>* get_connections_out( ) final;

209 210 211 212

}; /* end of Neuron class */


213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234
/**
 * Class serving as an interface providing 'activation_function_get_partial_derivative'
 * and 'activation_function_get_derivative' methods.
 */
class IDifferentiable {
    /**
     * Calculates the partial derivative of the activation function
     * with respect to the parameter and the current potential
     * @param[in] param_idx Index of the parameter to calculate derivative of
     * @return Partial derivative of the activation function according to the
     * 'param_idx'-th parameter
     */
    virtual double activation_function_get_partial_derivative(int param_idx) = 0;

    /**
     * Calculates the derivative with respect to the argument, ie the 'potential'
     * @return f'(x), where 'f(x)' is the activation function and 'x' = 'potential'
     */
    virtual double activation_function_get_derivative( ) = 0;
}; /* end of IDifferentiable class */

 #endif /* NEURON_H_ */