Commit 451ac17f authored by Thomas Purcell's avatar Thomas Purcell
Browse files

Update Coverage of the tests and Bug fixes

param_derivitive and graident used the wrong parameters This is now fixed
parent a24c4531
......@@ -499,7 +499,7 @@ public:
* @param params A pointer to the bias and scale terms for this Node and its children
* @param dfdp pointer to where the feature derivative pointers are located
*/
void param_derivative(const double* params, double* dfdp) const {}
void param_derivative(const double* params, double* dfdp, const int depth = 1) const {}
/**
* @brief Get the parameter gradient for non-linear optimization
......@@ -518,8 +518,9 @@ public:
* @param grad pointer to the gradient storage
* @param dfdp pointer to where the feature derivative pointers are located
* @param params A pointer to the bias and scale terms for this Node and its children
* @param depth The current depth in the binary expression tree
*/
inline void gradient(double* grad, double* dfdp, const double* params) const {};
inline void gradient(double* grad, double* dfdp, const double* params, const int depth = 1) const {};
#endif
};
......
......@@ -553,7 +553,7 @@ public:
* @param params A pointer to the bias and scale terms for this Node and its children
* @param samp_ind sample index number
*/
virtual void param_derivative(const double* params, double* dfdp) const = 0;
virtual void param_derivative(const double* params, double* dfdp, const int depth = 1) const = 0;
/**
* @brief Get the parameter gradient for non-linear optimization
......@@ -569,8 +569,9 @@ public:
* @param grad pointer to the gradient storage
* @param dfdp pointer to where the feature derivative pointers are located
* @param params A pointer to the bias and scale terms for this Node and its children
* @param depth The current depth in the binary expression tree
*/
virtual void gradient(double* grad, double* dfdp, const double* params) const = 0;
virtual void gradient(double* grad, double* dfdp, const double* params, const int depth = 1) const = 0;
#endif
// DocString: node_n_feats
......
......@@ -579,7 +579,7 @@ public:
* @param params A pointer to the bias and scale terms for this Node and its children
* @param dfdp pointer to where the feature derivative pointers are located
*/
virtual void param_derivative(const double* params, double* dfdp) const = 0;
virtual void param_derivative(const double* params, double* dfdp, const int depth = 1) const = 0;
/**
* @brief Get the parameter gradient for non-linear optimization
......@@ -603,13 +603,23 @@ public:
* @param grad pointer to the gradient storage
* @param dfdp pointer to where the feature derivative are located
* @param params A pointer to the bias and scale terms for this Node and its children
* @param depth The current depth in the binary expression tree
*/
void gradient(double* grad, double* dfdp, const double* params) const
void gradient(double* grad, double* dfdp, const double* params, const int depth = 1) const
{
int np = n_params_possible();
// Calculate f' and x
param_derivative(params, dfdp);
double* val_ptr = _feats[N - 1]->value_ptr(params + 2);
param_derivative(params, dfdp, depth);
double* val_ptr;
if(depth < nlopt_wrapper::MAX_PARAM_DEPTH)
{
val_ptr = _feats[N - 1]->value_ptr(params + 2, -1, true, depth + 1);
}
else
{
val_ptr = _feats[N - 1]->value_ptr(-1, true);
}
// df / d\alpha = x f'
std::transform(dfdp, dfdp + _n_samp, grad, grad, std::multiplies<double>());
......@@ -627,10 +637,13 @@ public:
// Go down the chain rule
int start = 2;
for(int ff = N - 1; ff >=0; --ff)
if(depth < nlopt_wrapper::MAX_PARAM_DEPTH)
{
_feats[ff]->gradient(grad + start * _n_samp, dfdp, params + start);
start += _feats[ff]->n_params_possible();
for(int ff = N - 1; ff >=0; --ff)
{
_feats[ff]->gradient(grad + start * _n_samp, dfdp, params + start, depth + 1);
start += _feats[ff]->n_params_possible();
}
}
}
......
......@@ -298,9 +298,9 @@ public:
* @param params A pointer to the bias and scale terms for this Node and its children
* @param dfdp pointer to where the feature derivative pointers are located
*/
inline void param_derivative(const double* params, double* dfdp) const
inline void param_derivative(const double* params, double* dfdp, const int depth = 1) const
{
double* val_ptr = _feats[0]->value_ptr(params);
double* val_ptr = (depth < nlopt_wrapper::MAX_PARAM_DEPTH) ? _feats[0]->value_ptr(params + 2, depth + 1, -1, true) : _feats[0]->value_ptr(-1, true);
std::transform(val_ptr, val_ptr + _n_samp, dfdp, [params](double vp){return util_funcs::sign(params[0] * vp + params[1]);});
}
#endif
......
......@@ -314,10 +314,10 @@ public:
* @param params A pointer to the bias and scale terms for this Node and its children
* @param dfdp pointer to where the feature derivative pointers are located
*/
inline void param_derivative(const double* params, double* dfdp) const
inline void param_derivative(const double* params, double* dfdp, const int depth = 1) const
{
double* val_ptr_1 = _feats[0]->value_ptr(params + _feats[1]->n_params_possible() + 2, 2);
double* val_ptr_2 = _feats[1]->value_ptr(params + 2, 1);
double* val_ptr_1 = (depth < nlopt_wrapper::MAX_PARAM_DEPTH) ? _feats[0]->value_ptr(params + 2 + _feats[1]->n_params(), 0, true, depth + 1) : _feats[0]->value_ptr(0, true);
double* val_ptr_2 = (depth < nlopt_wrapper::MAX_PARAM_DEPTH) ? _feats[1]->value_ptr(params + 2, 1, true, depth + 1) : _feats[1]->value_ptr(1, true);
std::transform(
val_ptr_1,
......
......@@ -307,7 +307,7 @@ public:
* @param params A pointer to the bias and scale terms for this Node and its children
* @param dfdp pointer to where the feature derivative pointers are located
*/
inline void param_derivative(const double* params, double* dfdp) const {std::fill_n(dfdp, _n_samp, 1.0);}
inline void param_derivative(const double* params, double* dfdp, const int depth = 1) const {std::fill_n(dfdp, _n_samp, 1.0);}
#endif
};
......
......@@ -295,9 +295,9 @@ public:
* @param params A pointer to the bias and scale terms for this Node and its children
* @param dfdp pointer to where the feature derivative pointers are located
*/
inline void param_derivative(const double* params, double* dfdp) const
inline void param_derivative(const double* params, double* dfdp, const int depth = 1) const
{
double* val_ptr = _feats[0]->value_ptr(params);
double* val_ptr = (depth < nlopt_wrapper::MAX_PARAM_DEPTH) ? _feats[0]->value_ptr(params + 2, depth + 1, -1, true) : _feats[0]->value_ptr(-1, true);
std::transform(val_ptr, val_ptr + _n_samp, dfdp, [params](double vp){return 3.0 * std::pow(params[0] * vp + params[1], 2.0);});
}
#endif
......
......@@ -294,9 +294,9 @@ public:
* @param params A pointer to the bias and scale terms for this Node and its children
* @param dfdp pointer to where the feature derivative pointers are located
*/
inline void param_derivative(const double* params, double* dfdp) const
inline void param_derivative(const double* params, double* dfdp, const int depth = 1) const
{
double* val_ptr = _feats[0]->value_ptr(params);
double* val_ptr = (depth < nlopt_wrapper::MAX_PARAM_DEPTH) ? _feats[0]->value_ptr(params + 2, depth + 1, -1, true) : _feats[0]->value_ptr(-1, true);
std::transform(val_ptr, val_ptr + _n_samp, dfdp, [params](double vp){return 1.0 / 3.0 * std::pow(params[0] * vp + params[1], -2.0 / 3.0);});
}
#endif
......
......@@ -297,9 +297,9 @@ public:
* @param params A pointer to the bias and scale terms for this Node and its children
* @param dfdp pointer to where the feature derivative pointers are located
*/
inline void param_derivative(const double* params, double* dfdp) const
inline void param_derivative(const double* params, double* dfdp, const int depth = 1) const
{
double* val_ptr = _feats[0]->value_ptr(params);
double* val_ptr = (depth < nlopt_wrapper::MAX_PARAM_DEPTH) ? _feats[0]->value_ptr(params + 2, depth + 1, -1, true) : _feats[0]->value_ptr(-1, true);
std::transform(val_ptr, val_ptr + _n_samp, dfdp, [params](double vp){return -1.0 * std::sin(params[0] * vp + params[1]);});
}
#endif
......
......@@ -310,10 +310,11 @@ public:
* @param params A pointer to the bias and scale terms for this Node and its children
* @param dfdp pointer to where the feature derivative pointers are located
*/
inline void param_derivative(const double* params, double* dfdp) const
inline void param_derivative(const double* params, double* dfdp, const int depth = 1) const
{
double* val_ptr_1 = _feats[0]->value_ptr(params, 2);
double* val_ptr_2 = _feats[1]->value_ptr(params, 1);
double* val_ptr_1 = (depth < nlopt_wrapper::MAX_PARAM_DEPTH) ? _feats[0]->value_ptr(params + 2 + _feats[1]->n_params(), 0, true, depth + 1) : _feats[0]->value_ptr(0, true);
double* val_ptr_2 = (depth < nlopt_wrapper::MAX_PARAM_DEPTH) ? _feats[1]->value_ptr(params + 2, 1, true, depth + 1) : _feats[1]->value_ptr(1, true);
std::transform(
val_ptr_1,
val_ptr_1 + _n_samp,
......
......@@ -294,9 +294,9 @@ public:
* @param params A pointer to the bias and scale terms for this Node and its children
* @param dfdp pointer to where the feature derivative pointers are located
*/
inline void param_derivative(const double* params, double* dfdp) const
inline void param_derivative(const double* params, double* dfdp, const int depth = 1) const
{
double* val_ptr = _feats[0]->value_ptr(params);
double* val_ptr = (depth < nlopt_wrapper::MAX_PARAM_DEPTH) ? _feats[0]->value_ptr(params + 2, depth + 1, -1, true) : _feats[0]->value_ptr(-1, true);
std::transform(val_ptr, val_ptr + _n_samp, dfdp, [params](double vp){return std::exp(params[0] * vp + params[1]);});
}
......
......@@ -291,9 +291,9 @@ public:
* @param params A pointer to the bias and scale terms for this Node and its children
* @param dfdp pointer to where the feature derivative pointers are located
*/
inline void param_derivative(const double* params, double* dfdp) const
inline void param_derivative(const double* params, double* dfdp, const int depth = 1) const
{
double* val_ptr = _feats[0]->value_ptr(params);
double* val_ptr = (depth < nlopt_wrapper::MAX_PARAM_DEPTH) ? _feats[0]->value_ptr(params + 2, depth + 1, -1, true) : _feats[0]->value_ptr(-1, true);
std::transform(val_ptr, val_ptr + _n_samp, dfdp, [params](double vp){return -1.0 / std::pow(params[0] * vp + params[1], 2.0);});
}
#endif
......
......@@ -297,9 +297,9 @@ public:
* @param params A pointer to the bias and scale terms for this Node and its children
* @param dfdp pointer to where the feature derivative pointers are located
*/
inline void param_derivative(const double* params, double* dfdp) const
inline void param_derivative(const double* params, double* dfdp, const int depth = 1) const
{
double* val_ptr = _feats[0]->value_ptr(params);
double* val_ptr = (depth < nlopt_wrapper::MAX_PARAM_DEPTH) ? _feats[0]->value_ptr(params + 2, depth + 1, -1, true) : _feats[0]->value_ptr(-1, true);
std::transform(val_ptr, val_ptr + _n_samp, dfdp, [params](double vp){return 1.0 / (params[0] * vp + params[1]);});
}
#endif
......
......@@ -310,7 +310,11 @@ public:
* @param params A pointer to the bias and scale terms for this Node and its children
* @param dfdp pointer to where the feature derivative pointers are located
*/
inline void param_derivative(const double* params, double* dfdp) const {std::copy_n(_feats[0]->value_ptr(params, -1, true), _n_samp, dfdp);}
inline void param_derivative(const double* params, double* dfdp, const int depth = 1) const
{
double* val_ptr_1 = (depth < nlopt_wrapper::MAX_PARAM_DEPTH) ? _feats[0]->value_ptr(params + 2 + _feats[1]->n_params(), 0, true, depth + 1) : _feats[0]->value_ptr(0, true);
std::copy_n(val_ptr_1, _n_samp, dfdp);
}
#endif
};
......
......@@ -295,9 +295,9 @@ public:
* @param params A pointer to the bias and scale terms for this Node and its children
* @param dfdp pointer to where the feature derivative pointers are located
*/
inline void param_derivative(const double* params, double* dfdp) const
inline void param_derivative(const double* params, double* dfdp, const int depth = 1) const
{
double* val_ptr = _feats[0]->value_ptr(params);
double* val_ptr = (depth < nlopt_wrapper::MAX_PARAM_DEPTH) ? _feats[0]->value_ptr(params + 2, depth + 1, -1, true) : _feats[0]->value_ptr(-1, true);
std::transform(val_ptr, val_ptr + _n_samp, dfdp, [params](double vp){return -1.0 * std::exp(-1.0 * (params[0] * vp + params[1]));});
}
#endif
......
......@@ -298,9 +298,9 @@ public:
* @param params A pointer to the bias and scale terms for this Node and its children
* @param dfdp pointer to where the feature derivative pointers are located
*/
inline void param_derivative(const double* params, double* dfdp) const
inline void param_derivative(const double* params, double* dfdp, const int depth = 1) const
{
double* val_ptr = _feats[0]->value_ptr(params);
double* val_ptr = (depth < nlopt_wrapper::MAX_PARAM_DEPTH) ? _feats[0]->value_ptr(params + 2, depth + 1, -1, true) : _feats[0]->value_ptr(-1, true);
std::transform(val_ptr, val_ptr + _n_samp, dfdp, [params](double vp){return std::cos(params[0] * vp + params[1]);});
}
#endif
......
......@@ -296,9 +296,9 @@ public:
* @param params A pointer to the bias and scale terms for this Node and its children
* @param dfdp pointer to where the feature derivative pointers are located
*/
inline void param_derivative(const double* params, double* dfdp) const
inline void param_derivative(const double* params, double* dfdp, const int depth = 1) const
{
double* val_ptr = _feats[0]->value_ptr(params);
double* val_ptr = (depth < nlopt_wrapper::MAX_PARAM_DEPTH) ? _feats[0]->value_ptr(params + 2, depth + 1, -1, true) : _feats[0]->value_ptr(-1, true);
std::transform(val_ptr, val_ptr + _n_samp, dfdp, [params](double vp){return 6.0 * std::pow(params[0] * vp + params[1], 5.0);});
}
#endif
......
......@@ -295,9 +295,9 @@ public:
* @param params A pointer to the bias and scale terms for this Node and its children
* @param dfdp pointer to where the feature derivative pointers are located
*/
inline void param_derivative(const double* params, double* dfdp) const
inline void param_derivative(const double* params, double* dfdp, const int depth = 1) const
{
double* val_ptr = _feats[0]->value_ptr(params);
double* val_ptr = (depth < nlopt_wrapper::MAX_PARAM_DEPTH) ? _feats[0]->value_ptr(params + 2, depth + 1, -1, true) : _feats[0]->value_ptr(-1, true);
std::transform(val_ptr, val_ptr + _n_samp, dfdp, [params](double vp){return 2.0 * (params[0] * vp + params[1]);});
}
#endif
......
......@@ -298,9 +298,9 @@ public:
* @param params A pointer to the bias and scale terms for this Node and its children
* @param dfdp pointer to where the feature derivative pointers are located
*/
inline void param_derivative(const double* params, double* dfdp) const
inline void param_derivative(const double* params, double* dfdp, const int depth = 1) const
{
double* val_ptr = _feats[0]->value_ptr(params);
double* val_ptr = (depth < nlopt_wrapper::MAX_PARAM_DEPTH) ? _feats[0]->value_ptr(params + 2, depth + 1, -1, true) : _feats[0]->value_ptr(-1, true);
std::transform(val_ptr, val_ptr + _n_samp, dfdp, [params](double vp){return 0.5 * std::pow(params[0] * vp + params[1], -0.5);});
}
#endif
......
......@@ -311,7 +311,7 @@ public:
* @param params A pointer to the bias and scale terms for this Node and its children
* @param dfdp pointer to where the feature derivative pointers are located
*/
inline void param_derivative(const double* params, double* dfdp) const {std::fill_n(dfdp, _n_samp, -1.0);}
inline void param_derivative(const double* params, double* dfdp, const int depth = 1) const {std::fill_n(dfdp, _n_samp, -1.0);}
#endif
};
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment