Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added GLM #5004

Closed
wants to merge 13 commits into from
38 changes: 38 additions & 0 deletions src/shogun/regression/GLM.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#include <shogun/lib/config.h>

#include <shogun/labels/RegressionLabels.h>
#include <shogun/mathematics/Math.h>
#include <shogun/mathematics/linalg/LinalgNamespace.h>
#include <shogun/regression/GLM.h>
#include <utility>
using namespace shogun;

GLM::GLM() : LinearMachine()
{
init();
}

void GLM::init()
{
SG_ADD(&m_alpha, "alpha", "alpha parameter", ParameterProperties::HYPER);
Khalifa1997 marked this conversation as resolved.
Show resolved Hide resolved
SG_ADD(&m_lambda, "lambda", "lambda parameter", ParameterProperties::HYPER);
SG_ADD(
(std::shared_ptr<SGObject>*)&m_descend_updater, "descend_updater",
"Descend Updater used for updating weights",
ParameterProperties::SETTING);
Comment on lines +85 to +88
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

as for those lines, I think that's what causing the build to fail

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

any thoughts? @karlnapf

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure what you mean

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I get an error when I test as you could tell in the CI, in the SGObject test, so I have a feeling this is what causing it. what do you think?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

when you report such thing, could you please make sure that you copy here the link to the CI line where you think the error is....

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The issue is probably that m_descend_updater (DescendUpdater) is never initialised, so you are serialising a nullptr, and I think that might be causing issues... @vigsterkr ?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

SG_ADD(&m_family, "family", "family used", ParameterProperties::SETTING);
SG_ADD(
&m_linkfn, "linkfn", "Link function used",
ParameterProperties::SETTING);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you need to initialize all values to defaults here. This brings up an interesting question. What is the default descend updater? I think it would be good to have one set so that users are not forced to pass one (tedious).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think since they are just linear models at the end of the day, there isn't really alot of parameters to learn like in Neural Networks, SGD would work best here which I believe is GradientDescendUpdater

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The loss is convex (for Poisson regression with log link function), so a second order method like Newton will be better. But we can change that later, best is to start with something simple, then work it up from there.

}
GLM::GLM(
std::shared_ptr<DescendUpdater> descend_updater, Family family,
Khalifa1997 marked this conversation as resolved.
Show resolved Hide resolved
LinkFunction Link_fn, float64_t alpha, float64_t lambda)
: LinearMachine()
Khalifa1997 marked this conversation as resolved.
Show resolved Hide resolved
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

call GLM() instead of LinearMachine()... this way you dont need the redundancy to call init()....

{
m_alpha = alpha;
m_lambda = lambda;
m_linkfn = Link_fn;
m_descend_updater = descend_updater;
m_family = family;
Khalifa1997 marked this conversation as resolved.
Show resolved Hide resolved
}
112 changes: 112 additions & 0 deletions src/shogun/regression/GLM.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
/*
* This software is distributed under BSD 3-clause license (see LICENSE file).
*
* Authors: Ahmed Khalifa
*/
/*
References:
https://www.rdocumentation.org/packages/stats/versions/3.6.2/topics/family
Khalifa1997 marked this conversation as resolved.
Show resolved Hide resolved
https://www.statsmodels.org/stable/generated/statsmodels.genmod.generalized_linear_model.GLM.html#statsmodels.genmod.generalized_linear_model.GLM
http://glm-tools.github.io/pyglmnet/api.html
*/
#ifndef _GENERALIZEDLINEARMODEL_H__
#define _GENERALIZEDLINEARMODEL_H__

#include <shogun/lib/config.h>

#include <shogun/features/Features.h>
#include <shogun/machine/FeatureDispatchCRTP.h>
Khalifa1997 marked this conversation as resolved.
Show resolved Hide resolved
#include <shogun/machine/LinearMachine.h>
#include <shogun/optimization/DescendUpdater.h>
#include <shogun/regression/Regression.h>

namespace shogun
{
enum Family
Khalifa1997 marked this conversation as resolved.
Show resolved Hide resolved
{
NORMAL_DISTRIBUTION,
EXPONENTIAL_DISTRIBUTION,
GAMMA_DISTRIBUTION,
BINOMIAL_DISTRIBUTION,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As I said before, I am not very sure of representing these things as enums, this will lead to spaghetti code with that many. BUT you can leave it for now and just focus on the poisson regression. Once that is done we can think about this again

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could make the GLM class accept a GLMDistribution object instead. It will contain the log-likelihood and gradients of the given distribution (e.g., GLMDistributionPoisson). Then, the GLM class will only call the methods of the GLMDistribution to train itself.

However, this is obviously out of the scope of this PR.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that, or have locally defined helper classes that are then instantiated based on an enum.
But the code for single cases (eg posison) should be in a single place (likelihood contribution, gradients, etc)

GAUSS_DISTRIBUTION,
POISSON_DISTRIBUTION
};
enum LinkFunction
{
LOG,
LOGIT,
IDENTITY,
INVERSE
};
/** @brief Class GLM implements Generalized Linear Models, such as poisson,
* gamma, binomial
*/
class GLM : public LinearMachine
{
public:
/** problem type */
MACHINE_PROBLEM_TYPE(PT_REGRESSION);

Khalifa1997 marked this conversation as resolved.
Show resolved Hide resolved
/** Default constructor */
GLM();

/** Constructor
*
* @param descend_updater chosen Descend Updater algorithm
* @param Linkfn the link function check
* https://www.rdocumentation.org/packages/stats/versions/3.6.2/topics/family
* @param Family the family check
* https://www.rdocumentation.org/packages/stats/versions/3.6.2/topics/family
* @param alpha alpha
Khalifa1997 marked this conversation as resolved.
Show resolved Hide resolved
* @param lambda lambda
*/
GLM(std::shared_ptr<DescendUpdater>,
Khalifa1997 marked this conversation as resolved.
Show resolved Hide resolved
Family family = POISSON_DISTRIBUTION, LinkFunction Link_fn = LOG,
Khalifa1997 marked this conversation as resolved.
Show resolved Hide resolved
float64_t alpha = 0.5, float64_t lambda = 0.1);

/** standard constructor
* @param data features
* @param labs labels
* @param learn_rate Learning rate
* @param Linkfn the link function check
* https://www.rdocumentation.org/packages/stats/versions/3.6.2/topics/family
Khalifa1997 marked this conversation as resolved.
Show resolved Hide resolved
* @param Family the distribution/family check
* https://www.rdocumentation.org/packages/stats/versions/3.6.2/topics/family
* @param alpha alpha
* @param lambda lambda
*/

/** default destructor */
Khalifa1997 marked this conversation as resolved.
Show resolved Hide resolved
virtual ~GLM()
{
}

/** train model
*
* @param data training data
*
* @return whether training was successful
*/
virtual bool train_machine(std::shared_ptr<Features> data = NULL)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it's still protected... and as said before if you would be using override then you would get an error actually

{
return true;
};

/** @return object name */
virtual const char* get_name() const
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

override...

{
return "GLM";
}

protected:
std::shared_ptr<DescendUpdater> m_descend_updater;
float64_t m_alpha;
float64_t m_lambda;
Family m_family;
LinkFunction m_linkfn;
Khalifa1997 marked this conversation as resolved.
Show resolved Hide resolved

private:
void init();
};
} // namespace shogun
#endif /* _GLM_H_ */