Welcome to mirror list, hosted at ThFree Co, Russian Federation.

MiraWeightVector.h « mert - github.com/moses-smt/mosesdecoder.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 8200d6013e48c7f230e987f397f5dabfb4ebbf05 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
/*
 * MiraWeightVector.h
 * kbmira - k-best Batch MIRA
 *
 * A self-averaging weight-vector. Good for
 * perceptron learning as well.
 *
 */

#ifndef MERT_MIRA_WEIGHT_VECTOR_H
#define MERT_MIRA_WEIGHT_VECTOR_H

#include <vector>
#include <iostream>

#include "MiraFeatureVector.h"

namespace MosesTuning
{


class AvgWeightVector;

class MiraWeightVector
{
public:
  /**
   * Constructor, initializes to the zero vector
   */
  MiraWeightVector();

  /**
   * Constructor with provided initial vector
   * \param init Initial feature values
   */
  MiraWeightVector(const std::vector<ValType>& init);

  /**
   * 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();

  /**
    * Convert to sparse vector, interpreting all features as sparse. Only used by hgmira.
   **/
  void ToSparse(SparseVector* sparse, size_t denseSize) const;

  friend class AvgWeightVector;

  friend std::ostream& operator<<(std::ostream& o, const MiraWeightVector& e);

private:
  /**
   * Updates a weight and lazily updates its total
   */
  void update(std::size_t index, ValType delta);

  /**
   * Make sure everyone's total is up-to-date
   */
  void fixTotals();

  /**
   * Helper to handle out-of-range weights
   */
  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;
};

/**
 * Averaged view of a weight vector
 */
class AvgWeightVector
{
public:
  AvgWeightVector(const MiraWeightVector& wv);
  ValType score(const MiraFeatureVector& fv) const;
  ValType weight(std::size_t index) const;
  std::size_t size() const;
  void ToSparse(SparseVector* sparse, size_t num_dense) const;
private:
  const MiraWeightVector& m_wv;
};



// --Emacs trickery--
// Local Variables:
// mode:c++
// c-basic-offset:2
// End:

}
#endif // MERT_WEIGHT_VECTOR_H