2012-05-29 21:38:57 +04:00
|
|
|
/*
|
|
|
|
* MiraWeightVector.h
|
|
|
|
* kbmira - k-best Batch MIRA
|
|
|
|
*
|
|
|
|
* A self-averaging weight-vector. Good for
|
|
|
|
* perceptron learning as well.
|
2012-05-30 18:11:09 +04:00
|
|
|
*
|
2012-05-29 21:38:57 +04:00
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef MERT_MIRA_WEIGHT_VECTOR_H
|
|
|
|
#define MERT_MIRA_WEIGHT_VECTOR_H
|
|
|
|
|
|
|
|
#include <vector>
|
2012-06-26 19:40:16 +04:00
|
|
|
#include <iostream>
|
2012-05-29 21:38:57 +04:00
|
|
|
|
|
|
|
#include "MiraFeatureVector.h"
|
|
|
|
|
2012-06-30 23:23:45 +04:00
|
|
|
namespace MosesTuning
|
|
|
|
{
|
2013-05-29 21:16:15 +04:00
|
|
|
|
2012-06-30 23:23:45 +04:00
|
|
|
|
2012-05-29 21:38:57 +04:00
|
|
|
class AvgWeightVector;
|
|
|
|
|
2013-05-29 21:16:15 +04:00
|
|
|
class MiraWeightVector
|
|
|
|
{
|
2012-05-29 21:38:57 +04:00
|
|
|
public:
|
|
|
|
/**
|
|
|
|
* Constructor, initializes to the zero vector
|
|
|
|
*/
|
|
|
|
MiraWeightVector();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructor with provided initial vector
|
|
|
|
* \param init Initial feature values
|
|
|
|
*/
|
2012-05-30 18:11:09 +04:00
|
|
|
MiraWeightVector(const std::vector<ValType>& init);
|
2012-05-29 21:38:57 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Update a the model
|
|
|
|
* \param fv Feature vector to be added to the weights
|
|
|
|
* \param tau FV will be scaled by this value before update
|
|
|
|
*/
|
|
|
|
void update(const MiraFeatureVector& fv, float tau);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Perform an empty update (affects averaging)
|
|
|
|
*/
|
|
|
|
void tick();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Score a feature vector according to the model
|
|
|
|
* \param fv Feature vector to be scored
|
|
|
|
*/
|
|
|
|
ValType score(const MiraFeatureVector& fv) const;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Squared norm of the weight vector
|
|
|
|
*/
|
|
|
|
ValType sqrNorm() const;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return an averaged view of this weight vector
|
|
|
|
*/
|
|
|
|
AvgWeightVector avg();
|
|
|
|
|
2014-07-21 14:04:43 +04:00
|
|
|
/**
|
|
|
|
* Convert to sparse vector, interpreting all features as sparse.
|
|
|
|
**/
|
|
|
|
void ToSparse(SparseVector* sparse) const;
|
|
|
|
|
2012-05-29 21:38:57 +04:00
|
|
|
friend class AvgWeightVector;
|
2012-05-30 18:11:09 +04:00
|
|
|
|
2012-06-26 19:40:16 +04:00
|
|
|
friend std::ostream& operator<<(std::ostream& o, const MiraWeightVector& e);
|
|
|
|
|
2012-05-29 21:38:57 +04:00
|
|
|
private:
|
|
|
|
/**
|
|
|
|
* Updates a weight and lazily updates its total
|
|
|
|
*/
|
2012-05-30 18:11:09 +04:00
|
|
|
void update(std::size_t index, ValType delta);
|
2012-05-29 21:38:57 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Make sure everyone's total is up-to-date
|
|
|
|
*/
|
|
|
|
void fixTotals();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Helper to handle out-of-range weights
|
|
|
|
*/
|
2012-05-30 18:11:09 +04:00
|
|
|
ValType weight(std::size_t index) const;
|
|
|
|
|
|
|
|
std::vector<ValType> m_weights;
|
|
|
|
std::vector<ValType> m_totals;
|
|
|
|
std::vector<std::size_t> m_lastUpdated;
|
|
|
|
std::size_t m_numUpdates;
|
2012-05-29 21:38:57 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Averaged view of a weight vector
|
|
|
|
*/
|
2013-05-29 21:16:15 +04:00
|
|
|
class AvgWeightVector
|
|
|
|
{
|
2012-05-29 21:38:57 +04:00
|
|
|
public:
|
|
|
|
AvgWeightVector(const MiraWeightVector& wv);
|
|
|
|
ValType score(const MiraFeatureVector& fv) const;
|
2012-05-30 18:11:09 +04:00
|
|
|
ValType weight(std::size_t index) const;
|
|
|
|
std::size_t size() const;
|
2014-07-21 14:04:43 +04:00
|
|
|
void ToSparse(SparseVector* sparse) const;
|
2012-05-29 21:38:57 +04:00
|
|
|
private:
|
|
|
|
const MiraWeightVector& m_wv;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// --Emacs trickery--
|
|
|
|
// Local Variables:
|
|
|
|
// mode:c++
|
|
|
|
// c-basic-offset:2
|
|
|
|
// End:
|
2012-06-30 23:23:45 +04:00
|
|
|
|
|
|
|
}
|
2014-07-21 14:04:43 +04:00
|
|
|
#endif // MERT_WEIGHT_VECTOR_H
|