OpenNN has been written in ANSI C++. This means that the library can be built on any operating system with little effort.Contents:
The Vector class is a template, which means that it can be applied to different types. That is, we can create a Vector or int numbers, MyClass objects, etc.
The Vector in OpenNN is derived from the vector in the Standard Template Library.
The OpenNN Vector derives from the std vector and it does not include any additional member.
Multiple constructors are defined in the Vector class, where the different constructors take different parameters.
The easiest way of creating a vector object is by means of the default constructor, wich builds a vector of size zero. For example, in order to construct an empty Vector of int numbers we use the following.
The following sentence constructs a Vector of 3 double numbers.
If we want to construct Vector of 5 bool variables and initialize all the elements to false, we can use
It is also possible to construct an object of the Vector class and at the same time load its members from a file. In order to do that we can do
The file `Vector.dat' contains a first row with the size of the vector and an aditional row for each element of the vector.
The following sentence constructs a Vector which is a copy of another Vector,
A very useful way to create a vector object is to through a list:
The Vector class also implements different types of operators for assignment, reference, arithmetic or comparison.
The assignment operator copies a vector into another vector,
The following sentence constructs a vector and sets the values of their elements using the reference operator. Note that indexing goes from 0 to n-1, where n is the Vector size.
Sum, difference, product and quotient operators are included in the Vector class to perform arithmetic operations with a scalar or another Vector. Note that the arithmetic operators with another Vector require that they have the same sizes.
The following sentence uses the vector-scalar sum operator,
An example of the use of the vector-vector multiplication operator is given below,
Assignment by sum, difference, product or quotient with a scalar or another Vector is also possible by using the arithmetic and assignment operators. If another Vector is to be used, it must have the same size.
For instance, to assign by difference with a scalar, we might do
In order to assign by quotation with another Vector, we can write
Equality and relational operators are also implemented here. They can be used with a scalar or another Vector. For the last case the same sizes are assumed. An example of the equal to operator with a scalar is
The less than operator with another Vector can be used as follows,
Get and set methods for each member of this class are implemented to exchange information among objects.
The method get_size() returns the size of a Vector.
On the other hand, the method set() sets a new size to a Vector. Note that the element values of that Vector are lost.
If we want to initialize a vector at random we can use the randomize_uniform() or randomize_normal() methods,
To get the first or last elements of a vector the following methods are used:
The Vector class also includes some mathematical methods which can be useful in the development of neural networks algorithms and applications.
The calculate_L1_norm() method calculates the norm of the vector,
In order to calculate the dot product between this Vector and another Vector we can do
We can calculate the mean or the standard deviation values of the elements in a Vector by using the calculate_mean() and calculate_standard_deviation() methods, respectively. For instance
Finally, utility methods for serialization or loading and saving the class members to a file are also included. In order to obtain a std::string representation of a Vector object we can make the following.
To save a Vector object to a file we can do
The save method only needs the path and the separator.
If we want to load a Vector object from a data file we could write
As it happens with the Vector class, the Matrix class is also a template. Therefore, a Matrix of any type can be created.
That members are private. Private members can be accessed only within methods of the class itself.
The Matrix class also implements multiple constructors, with different parameters.
The default constructor creates a matrix with zero rows and zero columns.
In order to construct an empty Matrix with a specified number of rows and columns we use
We can specify the number of rows and columns and initialize the Matrix elements at the same time by doing
To build a Matrix object by loading its members from a data file the following constructor is used,
A matrix data file contains the matrix elements arranged in rows and columns. For instance, the next data will correspond to a Matrix of zeros with 2 rows and 3 columns.
The copy constructor builds an object which is a copy of another object.
One of the most convenient ways to initialize a matrix object is through a Vector List:
The first argument of m is a list of vectors that correspond to the columns of the matrix. The second argument concerns to the header.
The Matrix class also implements the assignment operator.
Below there is an usage example of the reference operator here. Note that row indexing goes from 0 to rows_number-1 and column indexing goes from 0 to columns_number-1.
The use of the arithmetic operators for the Matrix class are very similar to those for the Vector class. The following sentence uses the scalar difference operator,
Also, using the arithmetic and assignment operators with the Matrix class is similar than with the Vector class. For instance, to assign by sum with another Matrix we can write
The not equal to operator with another Matrix can be used in the following way,
The use of the greater than operator with a scalar is listed below
As it happens for the Vector class, the Matrix class implements get and set methods for all the members. The get_rows_number() and get_columns_number() methods are very useful.
In order to set a new number of rows or columns to a Matrix object, the set_rows_number() or set_columns_number() methods are used.
A Matrix can be initialized with a given value, at random with an uniform distribution or at random with a normal distribution,
A set of mathematical methods are also implemented for convenience. For instance, the dot() method computes the dot product of this Matrix with a Vector or with another Matrix.
Finally, string serializing, printing, saving or loading utility methods are also implemented. For example, the use of the print() method or print_preview() is as follows.