mirror of
https://hub.njuu.cf/TheAlgorithms/C-Plus-Plus.git
synced 2023-10-11 13:05:55 +08:00
add namespace - machine_learning
This commit is contained in:
parent
e9f795e9c3
commit
579620271f
@ -31,13 +31,18 @@
|
|||||||
|
|
||||||
#define MAX_ITER 500 // INT_MAX ///< Maximum number of iterations to learn
|
#define MAX_ITER 500 // INT_MAX ///< Maximum number of iterations to learn
|
||||||
|
|
||||||
|
/** \namespace machine_learning
|
||||||
|
* \brief Machine learning algorithms
|
||||||
|
*/
|
||||||
|
namespace machine_learning {
|
||||||
class adaline {
|
class adaline {
|
||||||
public:
|
public:
|
||||||
/**
|
/**
|
||||||
* Default constructor
|
* Default constructor
|
||||||
* \param[in] num_features number of features present
|
* \param[in] num_features number of features present
|
||||||
* \param[in] eta learning rate (optional, default=0.1)
|
* \param[in] eta learning rate (optional, default=0.1)
|
||||||
* \param[in] convergence accuracy (optional, default=\f$1\times10^{-5}\f$)
|
* \param[in] convergence accuracy (optional,
|
||||||
|
* default=\f$1\times10^{-5}\f$)
|
||||||
*/
|
*/
|
||||||
adaline(int num_features, const double eta = 0.01f,
|
adaline(int num_features, const double eta = 0.01f,
|
||||||
const double accuracy = 1e-5)
|
const double accuracy = 1e-5)
|
||||||
@ -74,9 +79,9 @@ class adaline {
|
|||||||
/**
|
/**
|
||||||
* predict the output of the model for given set of features
|
* predict the output of the model for given set of features
|
||||||
* \param[in] x input vector
|
* \param[in] x input vector
|
||||||
* \param[out] out optional argument to return neuron output before applying
|
* \param[out] out optional argument to return neuron output before
|
||||||
* activation function (optional, `nullptr` to ignore)
|
* applying activation function (optional, `nullptr` to ignore) \returns
|
||||||
* \returns model prediction output
|
* model prediction output
|
||||||
*/
|
*/
|
||||||
int predict(const std::vector<double> &x, double *out = nullptr) {
|
int predict(const std::vector<double> &x, double *out = nullptr) {
|
||||||
if (!check_size_match(x))
|
if (!check_size_match(x))
|
||||||
@ -90,15 +95,14 @@ class adaline {
|
|||||||
if (out != nullptr) // if out variable is provided
|
if (out != nullptr) // if out variable is provided
|
||||||
*out = y;
|
*out = y;
|
||||||
|
|
||||||
return activation(y); // quantizer: apply ADALINE threshold function
|
return activation(
|
||||||
|
y); // quantizer: apply ADALINE threshold function
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Update the weights of the model using supervised learning for one feature
|
* Update the weights of the model using supervised learning for one
|
||||||
* vector
|
* feature vector \param[in] x feature vector \param[in] y known output
|
||||||
* \param[in] x feature vector
|
* value \returns correction factor
|
||||||
* \param[in] y known output value
|
|
||||||
* \returns correction factor
|
|
||||||
*/
|
*/
|
||||||
double fit(const std::vector<double> &x, const int &y) {
|
double fit(const std::vector<double> &x, const int &y) {
|
||||||
if (!check_size_match(x))
|
if (!check_size_match(x))
|
||||||
@ -119,10 +123,9 @@ class adaline {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Update the weights of the model using supervised learning for an array of
|
* Update the weights of the model using supervised learning for an
|
||||||
* vectors.
|
* array of vectors. \param[in] X array of feature vector \param[in] y
|
||||||
* \param[in] X array of feature vector
|
* known output value for each feature vector
|
||||||
* \param[in] y known output value for each feature vector
|
|
||||||
*/
|
*/
|
||||||
template <int N>
|
template <int N>
|
||||||
void fit(std::vector<double> const (&X)[N], const int *y) {
|
void fit(std::vector<double> const (&X)[N], const int *y) {
|
||||||
@ -142,7 +145,8 @@ class adaline {
|
|||||||
|
|
||||||
// Print updates every 200th iteration
|
// Print updates every 200th iteration
|
||||||
// if (iter % 100 == 0)
|
// if (iter % 100 == 0)
|
||||||
std::cout << "\tIter " << iter << ": Training weights: " << *this
|
std::cout << "\tIter " << iter
|
||||||
|
<< ": Training weights: " << *this
|
||||||
<< "\tAvg error: " << avg_pred_error << std::endl;
|
<< "\tAvg error: " << avg_pred_error << std::endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -167,7 +171,8 @@ class adaline {
|
|||||||
*/
|
*/
|
||||||
bool check_size_match(const std::vector<double> &x) {
|
bool check_size_match(const std::vector<double> &x) {
|
||||||
if (x.size() != (weights.size() - 1)) {
|
if (x.size() != (weights.size() - 1)) {
|
||||||
std::cerr << __func__ << ": "
|
std::cerr
|
||||||
|
<< __func__ << ": "
|
||||||
<< "Number of features in x does not match the feature "
|
<< "Number of features in x does not match the feature "
|
||||||
"dimension in model!"
|
"dimension in model!"
|
||||||
<< std::endl;
|
<< std::endl;
|
||||||
@ -181,6 +186,10 @@ class adaline {
|
|||||||
std::vector<double> weights; ///< weights of the neural network
|
std::vector<double> weights; ///< weights of the neural network
|
||||||
};
|
};
|
||||||
|
|
||||||
|
} // namespace machine_learning
|
||||||
|
|
||||||
|
using machine_learning::adaline;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* test function to predict points in a 2D coordinate system above the line
|
* test function to predict points in a 2D coordinate system above the line
|
||||||
* \f$x=y\f$ as +1 and others as -1.
|
* \f$x=y\f$ as +1 and others as -1.
|
||||||
|
Loading…
x
Reference in New Issue
Block a user