Skip to content

Commit

Permalink
introduced variable arrays #51
Browse files Browse the repository at this point in the history
  • Loading branch information
psycharo committed Jun 23, 2013
1 parent 8c12b1b commit d71dc5d
Show file tree
Hide file tree
Showing 24 changed files with 1,148 additions and 817 deletions.
13 changes: 13 additions & 0 deletions TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,19 @@ cases:
* it could be done, e.g. by introducing mix-ins - that implement special
cases.

messageToParent(VariableVector *parent)
receiveFromParent(VariableVector *parent)
* does this change updatePosterior() ??

messageToParent(Variable *)
recieveFromParent(Variable *)


* VariableVector should also introduce something like a




# Later TODO

* Not clear whether the messages should be passed by the network itself or locally
Expand Down
3 changes: 1 addition & 2 deletions algonquin.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
#include <algonquin.h>

using namespace algonquin;


using namespace vmp;


pair<double, double> AlgonquinNode::run()
Expand Down
108 changes: 34 additions & 74 deletions algonquin.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,7 @@



namespace algonquin
{

using namespace vmp;




namespace vmp {

/**
* @brief a compound noise implementing algonquin algorithm for a single
Expand All @@ -29,8 +22,8 @@ class AlgonquinNode : public HasParent<Gaussian>
// TODO: do this up until convergence
static const size_t NUM_ITERATIONS = 20;

AlgonquinNode(const MoG *_speechPrior,
const MoG *_noisePrior):
AlgonquinNode(const MoGArray *_speechPrior,
const MoGArray *_noisePrior):
m_speechParent(_speechPrior),
m_noiseParent(_noisePrior),
m_speechMeans(numParameters(), 0.0), m_noiseMeans(numParameters(), 0.0),
Expand Down Expand Up @@ -95,8 +88,8 @@ class AlgonquinNode : public HasParent<Gaussian>
inline size_t index(size_t s, size_t n) const { return numSpeech() * n + s; }

//! the number of speech/noise components
inline size_t numSpeech() const { return m_speechParent->numComponents(); }
inline size_t numNoise() const { return m_noiseParent->numComponents(); }
inline size_t numSpeech() const { return m_speechParent->dims(); }
inline size_t numNoise() const { return m_noiseParent->dims(); }
//! get the total number of variational parameters
inline size_t numParameters() const { return numSpeech() * numNoise(); }

Expand All @@ -105,19 +98,19 @@ class AlgonquinNode : public HasParent<Gaussian>
inline double meanNoise(size_t index) const { return m_noiseMeans[index]; }

// prior accessors
// TODO: add to MoG convenient functions?
inline double priorMeanSpeech(size_t s) const { return m_speechParent->parameters(s).mean(); }
inline double priorMeanNoise(size_t n) const { return m_noiseParent->parameters(n).mean(); }
inline double priorPrecSpeech(size_t s) const { return m_speechParent->parameters(s).precision; }
inline double priorPrecNoise(size_t n) const { return m_noiseParent->parameters(n).precision;; }
// TODO: add to MoG convenient functions? TODO: parameters() ?
inline double priorMeanSpeech(size_t s) const { throw NotImplementedException; }// { return m_speechParent->mean()->moments(s).mean; }
inline double priorMeanNoise(size_t n) const { throw NotImplementedException; }//{ return m_noiseParent->mean()->moments(n).mean; }
inline double priorPrecSpeech(size_t s) const { throw NotImplementedException; }//{ return m_speechParent->precision()->moments(s).precision; }
inline double priorPrecNoise(size_t n) const { throw NotImplementedException; }// { return m_noiseParent->precision()->moments(n).precision; }

// prior weights
inline double priorWeightSpeech(size_t s) const { return m_speechParent->weight(s); }
inline double priorWeightNoise(size_t n) const { return m_noiseParent->weight(n); }
inline double priorWeightSpeech(size_t s) const { throw NotImplementedException; }//return m_speechParent->weight(s); }
inline double priorWeightNoise(size_t n) const { throw NotImplementedException; }//return m_noiseParent->weight(n); }

// parents
const MoG *m_speechParent;
const MoG *m_noiseParent;
const MoGArray *m_speechParent;
const MoGArray *m_noiseParent;

// TODO: add a prior over the indices

Expand Down Expand Up @@ -174,14 +167,20 @@ class Network : public PersistentObject


// TODO: make a constructor instead?
static MoG *mixtureFromParameters(const Parameters<MoG> &params, Discrete* &discr)
static MoG *mixtureFromParameters(const Parameters<MoG> &params, Discrete* &discrete)
{
vector<Gaussian*> comps(params.numComponents(), NULL);
discr = new Discrete(logv(params.weights));
for (size_t m = 0; m < params.numComponents(); ++m)
comps[m] = new Gaussian(params.components[m].mean(),
params.components[m].precision);
return new MoG(comps, discr);
VariableArray<Gaussian> *means = new GaussianArray<Gaussian, Gamma>(params.dims(), NULL, NULL);
// VariableArray<Gamma> *precs = new GammaArray(params.dims(), );
discrete = new Discrete(logv(params.weights));

// priors

// for (size_t m = 0; m < params.dims(); ++m)
// {
// comps[m] = new ConstGaussian(params.components[m].mean(), params.components[m].precision);
// }
// return new MoGArray(1, means, precs, _selector);
return NULL;
}

void train(const double *speechFrames, size_t numSpeechFrames, size_t numSpeechComps,
Expand All @@ -197,15 +196,15 @@ class Network : public PersistentObject
iterations);
m_speechPrior = mixtureFromParameters(params, m_weightSpeech);

params = trainGMM(noiseFrames, numNoiseFrames,
maxNumIters, numNoiseComps,
1, GaussianParameters(0, 1e-3), GammaParameters(1e-3, 1e-3),
evidence,
iterations);
// params = trainGMM(noiseFrames, numNoiseFrames,
// maxNumIters, numNoiseComps,
// 1, GaussianParameters(0, 1e-3), GammaParameters(1e-3, 1e-3),
// evidence,
// iterations);

m_noisePrior = mixtureFromParameters(params, m_weightNoise);

m_node = new AlgonquinNode(m_speechPrior, m_noisePrior);
// m_node = new AlgonquinNode(m_speechPrior, m_noisePrior);
}


Expand All @@ -214,7 +213,7 @@ class Network : public PersistentObject
m_speechPrior = mixtureFromParameters(paramsSpeech, m_weightSpeech);
m_noisePrior = mixtureFromParameters(paramsNoise, m_weightNoise);

m_node = new AlgonquinNode(m_speechPrior, m_noisePrior);
// m_node = new AlgonquinNode(m_speechPrior, m_noisePrior);
}

inline const MoG *speechPrior() const { return m_speechPrior; }
Expand All @@ -239,47 +238,8 @@ class Network : public PersistentObject



/**
* an abstraction representing an array of networks, each for a specific frequency
*/
class NetworkArray : public PersistentObject
{
public:
NetworkArray(size_t _numFreqs):
m_networks(_numFreqs)
{}

inline size_t numFreqs() const { return m_networks.size(); }

//
void train(double *speech, size_t numSpeech,
double *noise, size_t numNoise)
{
for (size_t f = 0; f < numFreqs(); ++f)
{
double *s = speech + f * numSpeech;
double *n = noise + f * numNoise;
throw NotImplementedException;
m_networks[f].train(s, numSpeech, 4, n, numNoise, 1, 150);
}
}

void process(double *frame)
{
for (size_t f = 0; f < numFreqs(); ++f)
m_networks[f].process(frame[f]);
}

inline const Network &network(size_t f) { return m_networks[f]; }


private:
vector<Network> m_networks;
};



}


#endif // ALGONQUIN_H
53 changes: 34 additions & 19 deletions dirichlet.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,17 +46,17 @@ inline ostream &operator<<(ostream &out, const Parameters<Dirichlet> &params)
}


inline Parameters<Dirichlet> operator+(const Parameters<Dirichlet> &a,
const Parameters<Dirichlet> &b)
{
return Parameters<Dirichlet>(a.U + b.U);
}
//inline Parameters<Dirichlet> operator+(const Parameters<Dirichlet> &a,
// const Parameters<Dirichlet> &b)
//{
// return Parameters<Dirichlet>(a.U + b.U);
//}

inline Parameters<Dirichlet> operator-(const Parameters<Dirichlet> &a,
const Parameters<Dirichlet> &b)
{
return Parameters<Dirichlet>(a.U - b.U);
}
//inline Parameters<Dirichlet> operator-(const Parameters<Dirichlet> &a,
// const Parameters<Dirichlet> &b)
//{
// return Parameters<Dirichlet>(a.U - b.U);
//}


template<>
Expand Down Expand Up @@ -102,10 +102,12 @@ class Dirichlet : public Variable<Dirichlet>
* @param _dims the dimensionality
*/
Dirichlet(const vector<double> &u):
Variable(Parameters<Dirichlet>(u)),
Variable(Parameters<Dirichlet>(u),
Moments<Dirichlet>(u.size())), // TODO: decent initialisation
m_priorU(u),
m_dims(u.size())
{
m_moments.logProb.resize(m_dims);
updatePosterior();
}

Expand All @@ -132,33 +134,46 @@ class Dirichlet : public Variable<Dirichlet>


//! override Variable
inline Moments<Dirichlet> updatedMoments() const
inline void updateMoments()
{
Moments<Dirichlet> result(dims());
// lambda functions/matrices?
double dgSumU = digamma(sumv(parameters().U));
for (size_t i = 0; i < dims(); ++i)
result.logProb[i] = digamma(parameters().U[i]) - dgSumU;
m_moments.logProb[i] = digamma(parameters().U[i]) - dgSumU;
// TODO: is normalization required here?
result.logProb -= lognorm(result.logProb);
return result;
m_moments.logProb -= lognorm(m_moments.logProb);
}

//! override HasForm<Dirichlet>
inline Parameters<Dirichlet> parametersFromParents() const
{
return Parameters<Dirichlet>(m_priorU);
return m_priorU;
}


private:
protected:
//! the prior parameter vector
const vector<double> m_priorU;

//! dimensionality
size_t m_dims;
};


class ConstDirichlet : public Dirichlet
{
public:
ConstDirichlet(const vector<double> &_logProb):
Dirichlet(_logProb),
m_constMoments(_logProb)
{}

inline const Moments<Dirichlet> &moments() const { return m_constMoments; }

private:
Moments<Dirichlet> m_constMoments;
};


}


Expand Down
Loading

0 comments on commit d71dc5d

Please sign in to comment.