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

FeatureVector.h « moses - github.com/moses-smt/mosesdecoder.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 5ad2e4ad49656d0043fc379e1141720ad1e8f8aa (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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
// -*- mode: c++; indent-tabs-mode: nil; tab-width:2  -*-
/*
   Moses - statistical machine translation system
   Copyright (C) 2005-2015 University of Edinburgh

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
*/
#pragma once

#ifndef FEATUREVECTOR_H
#define FEATUREVECTOR_H

#include <iostream>
#include <map>
#include <sstream>
#include <string>
#include <valarray>
#include <vector>

#include <boost/functional/hash.hpp>
#include <boost/unordered_map.hpp>

#ifdef MPI_ENABLE
#include <boost/serialization/access.hpp>
#include <boost/serialization/split_member.hpp>
#include <boost/serialization/string.hpp>
#include <boost/serialization/vector.hpp>
#include <boost/serialization/valarray.hpp>
#endif

#ifdef WITH_THREADS
#include <boost/thread/shared_mutex.hpp>
#endif

#include "util/exception.hh"
#include "util/string_piece.hh"

namespace Moses
{

typedef float FValue;

/**
 * Feature name
 **/
struct FName {

  static const std::string SEP;

  typedef boost::unordered_map<std::string,size_t> Name2Id;
  typedef boost::unordered_map<size_t,size_t> Id2Count;
  //typedef std::map<std::string, size_t> Name2Id;
  static Name2Id name2id;
  static std::vector<std::string> id2name;
  static Id2Count id2hopeCount;
  static Id2Count id2fearCount;

  //A feature name can either be initialised as a pair of strings,
  //which will be concatenated with a SEP between them, or as
  //a single string, which will be used as-is.
  FName(const StringPiece &root, const StringPiece &name) {
    std::string assembled(root.data(), root.size());
    assembled += SEP;
    assembled.append(name.data(), name.size());
    init(assembled);
  }
  explicit FName(const StringPiece &name) {
    init(name);
  }

  const std::string& name() const;
  //const std::string& root() const {return m_root;}

  size_t hash() const;

  bool operator==(const FName& rhs) const ;
  bool operator!=(const FName& rhs) const ;

  static size_t getId(const std::string& name);
  static size_t getHopeIdCount(const std::string& name);
  static size_t getFearIdCount(const std::string& name);
  static void incrementHopeId(const std::string& name);
  static void incrementFearId(const std::string& name);
  static void eraseId(size_t id);

private:
  void init(const StringPiece& name);
  size_t m_id;
#ifdef WITH_THREADS
  //reader-writer lock
  static boost::shared_mutex m_idLock;
#endif
};

std::ostream& operator<<(std::ostream& out,const FName& name);

struct FNameEquals {
  inline bool operator() (const FName& lhs, const FName& rhs) const {
    return (lhs == rhs);
  }
};

struct FNameHash
    : std::unary_function<FName, std::size_t> {
  std::size_t operator()(const FName& x) const {
    return x.hash();
  }
};

class ProxyFVector;

/**
 * A sparse feature (or weight) vector.
 **/
class FVector
{
public:
  /** Empty feature vector */
  FVector(size_t coreFeatures = 0);

  FVector& operator=( const FVector& rhs ) {
    m_features = rhs.m_features;
    m_coreFeatures = rhs.m_coreFeatures;
    return *this;
  }

  /*
   * Change the number of core features
  **/
  void resize(size_t newsize);

  typedef boost::unordered_map<FName,FValue,FNameHash, FNameEquals> FNVmap;
  /** Iterators */
  typedef FNVmap::iterator iterator;
  typedef FNVmap::const_iterator const_iterator;
  iterator begin() {
    return m_features.begin();
  }
  iterator end() {
    return m_features.end();
  }
  const_iterator cbegin() const {
    return m_features.cbegin();
  }
  const_iterator cend() const {
    return m_features.cend();
  }

  bool hasNonDefaultValue(FName name) const {
    return m_features.find(name) != m_features.end();
  }
  void clear();


  /** Load from file - each line should be 'root[_name] value' */
  bool load(const std::string& filename);
  void save(const std::string& filename) const;
  void write(std::ostream& out, const std::string& sep=" ", const std::string& linesep="\n") const ;

  /** Element access */
  ProxyFVector operator[](const FName& name);
  FValue& operator[](size_t index);
  FValue operator[](const FName& name) const;
  FValue operator[](size_t index) const;

  /** Size */
  size_t size() const {
    return m_features.size() + m_coreFeatures.size();
  }

  size_t coreSize() const {
    return m_coreFeatures.size();
  }

  const std::valarray<FValue> &getCoreFeatures() const {
    return m_coreFeatures;
  }

  /** Equality */
  bool operator== (const FVector& rhs) const;
  bool operator!= (const FVector& rhs) const;

  FValue inner_product(const FVector& rhs) const;

  friend class ProxyFVector;

  /**arithmetic */
  //Element-wise
  //If one side has fewer core features, take the missing ones to be 0.
  FVector& operator+= (const FVector& rhs);
  FVector& operator-= (const FVector& rhs);
  FVector& operator*= (const FVector& rhs);
  FVector& operator/= (const FVector& rhs);
  //Scalar
  FVector& operator*= (const FValue& rhs);
  FVector& operator/= (const FValue& rhs);

  FVector& multiplyEqualsBackoff(const FVector& rhs, float backoff);
  FVector& multiplyEquals(float core_r0, float sparse_r0);

  FVector& max_equals(const FVector& rhs);

  /** norms and sums */
  FValue l1norm() const;
  FValue l1norm_coreFeatures() const;
  FValue l2norm() const;
  FValue linfnorm() const;
  size_t l1regularize(float lambda);
  void l2regularize(float lambda);
  size_t sparseL1regularize(float lambda);
  void sparseL2regularize(float lambda);
  FValue sum() const;

  /** pretty printing */
  std::ostream& print(std::ostream& out) const;

  /** additional */
  void printCoreFeatures();
  //scale so that abs. value is less than maxvalue
  void thresholdScale(float maxValue );

  void capMax(FValue maxValue);
  void capMin(FValue minValue);

  void sparsePlusEquals(const FVector& rhs);
  void corePlusEquals(const FVector& rhs);
  void coreAssign(const FVector& rhs);

  void incrementSparseHopeFeatures();
  void incrementSparseFearFeatures();
  void printSparseHopeFeatureCounts(std::ofstream& out);
  void printSparseFearFeatureCounts(std::ofstream& out);
  void printSparseHopeFeatureCounts();
  void printSparseFearFeatureCounts();
  size_t pruneSparseFeatures(size_t threshold);
  size_t pruneZeroWeightFeatures();
  void updateConfidenceCounts(const FVector& weightUpdate, bool signedCounts);
  void updateLearningRates(float decay_core, float decay_sparse, const FVector& confidence_counts, float core_r0, float sparse_r0);

  // vector which, for each element of the original vector, reflects whether an element is zero or non-zero
  void setToBinaryOf(const FVector& rhs);

  // divide only core features by scalar
  FVector& coreDivideEquals(float scalar);

  // divide each element by the number given in the rhs vector
  FVector& divideEquals(const FVector& rhs);

  void merge(const FVector &other);

#ifdef MPI_ENABLE
  friend class boost::serialization::access;
#endif

private:
  friend void swap(FVector &first, FVector &second);

  /** Internal get and set. */
  const FValue& get(const FName& name) const;
  FValue getBackoff(const FName& name, float backoff) const;
  void set(const FName& name, const FValue& value);

  FNVmap m_features;
  std::valarray<FValue> m_coreFeatures;

#ifdef MPI_ENABLE
  //serialization
  template<class Archive>
  void save(Archive &ar, const unsigned int version) const {
    std::vector<std::string> names;
    std::vector<FValue> values;
    for (const_iterator i = cbegin(); i != cend(); ++i) {
      std::ostringstream ostr;
      ostr << i->first;
      names.push_back(ostr.str());
      values.push_back(i->second);
    }
    ar << names;
    ar << values;
    ar << m_coreFeatures;
  }

  template<class Archive>
  void load(Archive &ar, const unsigned int version) {
    clear();
    std::vector<std::string> names;
    std::vector<FValue> values;
    ar >> names;
    ar >> values;
    ar >> m_coreFeatures;
    UTIL_THROW_IF2(names.size() != values.size(), "Error");
    for (size_t i = 0; i < names.size(); ++i) {
      set(FName(names[i]), values[i]);
    }
  }

  BOOST_SERIALIZATION_SPLIT_MEMBER()

#endif

};

inline void swap(FVector &first, FVector &second)
{
  swap(first.m_features, second.m_features);
  swap(first.m_coreFeatures, second.m_coreFeatures);
}

std::ostream& operator<<( std::ostream& out, const FVector& fv);
//Element-wise operations
const FVector operator+(const FVector& lhs, const FVector& rhs);
const FVector operator-(const FVector& lhs, const FVector& rhs);
const FVector operator*(const FVector& lhs, const FVector& rhs);
const FVector operator/(const FVector& lhs, const FVector& rhs);

//Scalar operations
const FVector operator*(const FVector& lhs, const FValue& rhs);
const FVector operator/(const FVector& lhs, const FValue& rhs);

const FVector fvmax(const FVector& lhs, const FVector& rhs);

FValue inner_product(const FVector& lhs, const FVector& rhs);

struct FVectorPlus {
  FVector operator()(const FVector& lhs, const FVector& rhs) const {
    return lhs + rhs;
  }
};

/**
 * Used to help with subscript operator overloading.
 * See http://stackoverflow.com/questions/1386075/overloading-operator-for-a-sparse-vector
 **/
class ProxyFVector
{
public:
  ProxyFVector(FVector *fv, const FName& name ) : m_fv(fv), m_name(name) {}
  ProxyFVector &operator=(const FValue& value) {
    // If we get here, we know that operator[] was called to perform a write access,
    // so we can insert an item in the vector if needed
    //std::cerr << "Inserting " << value << " into " << m_name << std::endl;
    m_fv->set(m_name,value);
    return *this;

  }

  operator FValue() {
    // If we get here, we know that operator[] was called to perform a read access,
    // so we can simply return the value from the vector
    return m_fv->get(m_name);
  }

  /*operator FValue&() {
   return m_fv->m_features[m_name];
   }*/

  FValue operator++() {
    return ++m_fv->m_features[m_name];
  }

  FValue operator +=(FValue lhs) {
    return (m_fv->m_features[m_name] += lhs);
  }

  FValue operator -=(FValue lhs) {
    return (m_fv->m_features[m_name] -= lhs);
  }

private:
  FVector* m_fv;
  const FName& m_name;

};

}

#endif