Cavity Message¶
-
template<class
Mag
>
classCavity_Message
¶ Abstract type used to store weights and messages of rFBP algorithm.
The initial messages can be loaded from file and the resulting ones can be saved to file using the appropriated member functions.
- Template Parameters
Mag
: magnetization chosen for training
Public Functions
-
Cavity_Message
()¶ Default constructor.
-
Cavity_Message
(const std::string &filename, const bool &bin)¶ Load messages from file.
The input file must have a very precise format.
For the binary format we require a file with the following instructions:
- N (long int)
- M (long int)
- K (long int)
- m_star_j (K * N, double)
- m_j_star (K * N, double)
- m_in (M * K, double)
- weights (M * K * N, double)
- m_no (M * K, double)
- m_on (M, double)
- m_ni (M * K, double)
All the double values are converted into the respective Mag format.
For the ascii version the required file must have the following format:
fmt: plain N,M,K: `N` `M` `K`
where
N
represents the numerical value of the N parameter. This header file is followed by ravel version of the required arrays (m_star_j, m_j_star, m_in, weights, m_no, m_on, m_ni) one per each line, divided by white spaces (or\t
).- Note
- A valid file can be generated by the function
save_messages
. - Parameters
filename
: Input filenamebin
: switch between binary/ascii files (default = false)
-
Cavity_Message
(const long int &m, const long int &n, const long int &k, const double &x, const int &start)¶ Generate random messages.
The cavity_messages’ arrays are generated according a uniform distribution. The [0, 1] range is converted using the formula
x * (2. * dist() - 1.)
where
dist
represents the uniform random generator. All the values are converted to Mag types.- Parameters
m
: number of samplesn
: number of probesk
: number of hidden layersx
: initial valuestart
: random seed
-
Cavity_Message
(const Cavity_Message<Mag> &m)¶ Copy constructor.
The copy constructor provides a deep copy of the object, i.e. all the arrays are copied and not moved.
- Parameters
m
: Cavity_Message object
-
Cavity_Message<Mag> &
operator=
(const Cavity_Message<Mag> &m)¶ Copy operator.
The operator performs a deep copy of the object and if there are buffers already allocated, the operatore deletes them and then re-allocates an appropriated portion of memory.
- Parameters
m
: Cavity_Message object
-
~Cavity_Message
()¶ Destructor.
Completely delete the object and release the memory of the arrays.
-
long int **
get_weights
()¶ Return weights matrix.
The weights are converted from double to long int, using the sign of each element, i.e.
1L - 2L * signbit(x)
This function can be used as getter member for the weight matrix used to predict new patterns.
- Note
- The weight matrix used in the prediction is the
m_j_star
array!
-
void
save_weights
(const std::string &filename, Params<Mag> ¶meters)¶ Save weight matrix to file.
Save the weight matrix to file and the related training parameters. This function provides a valid file for the function
read_weights
in ascii format. Only the weight matrix, i.e.m_j_star
, is saved, since it is the only informative array for the prediction of new patterns. The training parameters are saved as header in the file.- Return
- The binirized format of the weight matrix, ready for the prediction.
- Parameters
filename
: output filenameparameters
: Params object
-
void
save_weights
(const std::string &filename)¶ Save weight matrix to file.
Save only the weight matrix to file. This function provides a valid file for the function
read_weights
in binary format. Only the weight matrix, i.e.m_j_star
, is saved, since it is the only informative array for the prediction of new patterns. The weight values are saved as double values and thus before use them to predict new values, it is necessary to apply the “get_weights” function.- Parameters
filename
: output filename
-
void
read_weights
(const std::string &filename, const bool &bin)¶ Load weight matrix from file.
This function read the weight matrix from a binary or ascii file. Its usage is in relation to the
save_weights
member function.- Parameters
filename
: input filenamebin
: switch between binary/ascii fmt
-
void
save_messages
(const std::string &filename, Params<Mag> ¶meters)¶ Save all the messages to file.
This function dump the complete object to file with also the parameters used for the training section, according to the format required by the constructor.
- Parameters
filename
: output filenameparameters
: Params object
-
void
save_messages
(const std::string &filename)¶ Save all the messages to a binary file.
This function dump the complete object to binary file, according to the format required by the constructor.
- Parameters
filename
: output filename
Public Members
-
long int
M
¶ Input sample size.
-
long int
N
¶ Input layers size.
-
long int
K
¶ Number of hidden layers.
-
long int
seed
¶ Random seed.
-
MagVec3<Mag>
weights
¶ uw in the paper nomeclature
-
MagVec2<Mag>
m_star_j
¶ ux in the paper nomeclature
-
MagVec2<Mag>
m_j_star
¶ mw in the paper nomeclature
-
MagVec2<Mag>
m_in
¶ mτ1 in the paper nomeclature
-
MagVec2<Mag>
m_no
¶ Uτ1 in the paper nomeclature.
-
MagVec2<Mag>
m_ni
¶ uτ1 in the paper nomeclature
-
MagVec<Mag>
m_on
¶ mτ2 in the paper nomeclature