Cambridge SMT System
latmert/include/tropical-sparse-tuple-weight.h
Go to the documentation of this file.
1 //Copyright (c) 2012, University of Cambridge
2 //All rights reserved.
3 //
4 //Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met://
5 //
6 // * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
7 // * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
8 // * Neither the name of the University of Cambridge nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
9 //
10 //THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
11 
12 #ifndef TROPICALSPARSETUPLEWEIGHT_H_
13 #define TROPICALSPARSETUPLEWEIGHT_H_
14 
15 #include <fst/fstlib.h>
16 #include <vector>
17 #include "params.h"
18 
19 namespace fst {
20 
21 template<typename T>
22 class TropicalSparseTupleWeight: public
23  SparsePowerWeight<TropicalWeightTpl<T> > {
24 
25  public:
26 
27  static std::vector<T>& Params() {
28  static ParamsInit<T> params;
29  return params.params;
30  }
31 
32  typedef TropicalWeightTpl<T> W;
33 
34  using SparsePowerWeight<W>::Zero;
35  using SparsePowerWeight<W>::One;
36  using SparsePowerWeight<W>::Quantize;
37  using SparsePowerWeight<W>::Reverse;
38  using SparsePowerWeight<W>::Type;
39  using SparsePowerWeight<W>::Properties;
40 
42 
44  SparsePowerWeight<W>() {
45  this->SetDefaultValue (W::One() );
46  }
47 
48  TropicalSparseTupleWeight (const SparsePowerWeight<W>& sw) :
49  SparsePowerWeight<W> (sw) {
50  }
51 
53  SparsePowerWeight<W> (w) {
54  }
55 
56  inline static std::string GetPrecisionString() {
57  int64 size = sizeof (T);
58  if (size == sizeof (float) ) {
59  return "";
60  }
61  size *= CHAR_BIT;
62  std::string result;
63  Int64ToStr (size, &result);
64  return result;
65  }
66 
67  static const std::string& Type() {
68  static const std::string type = "tropicalsparsetuple"
70  return type;
71  }
72 
73  static uint64 Properties() {
74  return W::Properties()
75  & (kLeftSemiring | kRightSemiring | kCommutative | kIdempotent
76  | kPath);
77  }
78 
80  static TropicalSparseTupleWeight<T> zero (W::Zero() );
81  return zero;
82  }
83 
85  static TropicalSparseTupleWeight<T> one (W::One() );
86  return one;
87  }
88 
89  TropicalSparseTupleWeight<T> Quantize (float delta = kDelta) const {
90  TropicalSparseTupleWeight<T> w = SparsePowerWeight<W>::Quantize (delta);
91  w.SetDefaultValue (this->DefaultValue() );
92  return w;
93  }
94 
95  ReverseWeight Reverse() const {
96  return *this;
97  }
98 
99  template<typename TT>
103 };
104 
105 template<typename T>
106 T DotProduct (const TropicalSparseTupleWeight<T>& w, const std::vector<T>& vw) {
107  T result = w.DefaultValue().Value();
108  for (SparseTupleWeightIterator<TropicalWeightTpl<T>, int> it (w); !it.Done();
109  it.Next() ) {
110  T param;
111  // Special case for flat params
112  if (vw.empty() ) {
113  param = 1;
114  } else if (it.Value().first > int (vw.size() ) ) {
115  cerr
116  << "feature vector has a larger dimensionality than the parameters. "
117  << "Params: " << vw.size() << " Features: "
118  << it.Value().first << endl;
119  exit (1);
120  } else {
121  param = vw[it.Value().first - 1];
122  }
123  result += it.Value().second.Value() * param;
124  }
125  return result;
126 }
127 
128 template<typename T>
131  unsigned int index
132  , T param) {
133  params.Push (index + 1, param);
134  }
135 };
136 
137 template<class T>
139  const TropicalSparseTupleWeight<T>& vw1,
140  const TropicalSparseTupleWeight<T>& vw2) {
143  return w1 < w2 ? vw1 : vw2;
144 }
145 
146 template<typename T>
147 inline bool ApproxEqual (const TropicalSparseTupleWeight<T>& vw1,
148  const TropicalSparseTupleWeight<T>& vw2, float delta = kDelta) {
149  const SparsePowerWeight<TropicalWeightTpl<T> >& spw1 = vw1;
150  const SparsePowerWeight<TropicalWeightTpl<T> >& spw2 = vw2;
151  return ApproxEqual (spw1, spw2, delta);
152 }
153 
154 template<class T>
157  const TropicalSparseTupleWeight<T>& w2) {
159  SparseTupleWeightTimesMapper<TropicalWeightTpl<T>, int> operator_mapper;
160  SparseTupleWeightMap (&ret, w1, w2, operator_mapper);
161  return ret;
162 }
163 
164 // Semimodule divide operation
165 template<class T>
168  const TropicalSparseTupleWeight<T>& w2, DivideType type = DIVIDE_ANY) {
170  SparseTupleWeightDivideMapper<TropicalWeightTpl<T>, int> operator_mapper (
171  type);
172  SparseTupleWeightMap (&ret, w1, w2, operator_mapper);
173  return ret;
174 }
175 
176 template<typename FromArc, typename ToArc, typename M>
178 
179  GeneralMapper (M& m) :
180  m (m) {
181  }
182  ;
183 
184  ToArc operator() (const FromArc& arc) const {
185  if (arc.weight == FromArc::Weight::Zero() ) {
186  return ToArc (arc.ilabel, arc.olabel, ToArc::Weight::Zero(),
187  arc.nextstate);
188  }
189  if (arc.weight == FromArc::Weight::One() ) {
190  return ToArc (arc.ilabel, arc.olabel, ToArc::Weight::One(),
191  arc.nextstate);
192  }
193  return ToArc (arc.ilabel, arc.olabel, m (arc.weight), arc.nextstate);
194  }
195 
196  MapSymbolsAction InputSymbolsAction() const {
197  return MAP_COPY_SYMBOLS;
198  }
199  MapSymbolsAction OutputSymbolsAction() const {
200  return MAP_COPY_SYMBOLS;
201  }
202  MapFinalAction FinalAction() const {
203  return MAP_NO_SUPERFINAL;
204  }
205  uint Properties (uint props) const {
206  return (props & kWeightInvariantProperties) | kUnweighted;
207  }
208 
209  private:
210  M m;
211 };
212 
213 template<typename W>
215  explicit StdToTropicalSparseMapper (uint k_ = 1) :
216  k_ (k_) {
217  }
218  ;
219 
222  sparse.put (k_, w);
223  return sparse;
224  }
225 
226  private:
227  uint k_;
228 };
229 
230 struct Expand {
231 
233  const TropicalSparseTupleWeight<float>& w32) const {
235  for (SparseTupleWeightIterator<TropicalWeight, int> it (w32); !it.Done();
236  it.Next() ) {
237  result.Push (it.Value().first,
238  TropicalWeightTpl<double> (it.Value().second.Value() ) );
239  }
240  return result;
241  }
242 }
243 ;
244 
246  ArcTpl<TropicalSparseTupleWeight<double> >, Expand> ExpandMapper;
247 
248 template<typename T>
249 struct DotProductMap {
250  DotProductMap (const std::vector<T>& param) :
251  param (param) {
252  }
253 
254  TropicalWeightTpl<T> operator() (
255  const TropicalSparseTupleWeight<T>& w) const {
256  return DotProduct (w, param);
257  }
258 
259  private:
260  std::vector<T> param;
261 };
262 
263 template<typename T>
264 struct VectorToStd {
265 
266  VectorToStd (int32_t k) :
267  k (k) {
268  }
269 
270  TropicalWeightTpl<T> operator() (const TropicalSparseTupleWeight<T>& w) const {
271  for (SparseTupleWeightIterator<TropicalWeightTpl<T>, int> it (w);
272  !it.Done(); it.Next() ) {
273  if (it.Value().first - 1 == k) {
274  return it.Value().second;
275  }
276  }
277  return w.DefaultValue();
278  }
279 
280  private:
281  int32_t k;
282 };
283 
284 //typedef GeneralMapper<ArcTpl<TropicalSparseTupleWeight<float> >,
285 // ArcTpl<TropicalWeightTpl<float> >, VectorToStd<float> > SparseToStdMapper;
286 
287 template<typename T>
288 struct StdToVector {
289 
290  StdToVector (int32_t k) :
291  k (k) {
292  }
293 
294  TropicalSparseTupleWeight<T> operator() (const TropicalWeightTpl<T>& w) const {
296  result.Push (k + 1, w.Value() );
297  return result;
298  }
299 
300  private:
301  int32_t k;
302 };
303 
304 // typedef GeneralMapper<ArcTpl<TropicalWeightTpl<float> >,
305 // ArcTpl<TropicalSparseTupleWeight<float> >, StdToVector<float> > StdToSparseMapper;
306 
307 } // namespace fst
308 
310  fst::ArcTpl<fst::TropicalWeightTpl<float> >, fst::VectorToStd<float> >
312 
314  fst::ArcTpl<fst::TropicalSparseTupleWeight<float> >, fst::StdToVector<float> >
316 
317 #endif /* TROPICALSPARSETUPLEWEIGHT_H_ */
GeneralMapper< ArcTpl< TropicalSparseTupleWeight< float > >, ArcTpl< TropicalSparseTupleWeight< double > >, Expand > ExpandMapper
bool ApproxEqual(const TropicalSparseTupleWeight< T > &vw1, const TropicalSparseTupleWeight< T > &vw2, float delta=kDelta)
Definition: fstio.hpp:27
Definition: params.h:20
TropicalSparseTupleWeight< T > Quantize(float delta=kDelta) const
TropicalSparseTupleWeight(const SparsePowerWeight< W > &sw)
std::vector< T > params
Definition: params.h:82
fst::GeneralMapper< fst::ArcTpl< fst::TropicalSparseTupleWeight< float > >, fst::ArcTpl< fst::TropicalWeightTpl< float > >, fst::VectorToStd< float > > SparseToStdMapper
Implements Tropical Sparse tuple weight semiring, extending from openfst SparsePowerWeight class...
TropicalSparseTupleWeight< T > Divide(const TropicalSparseTupleWeight< T > &w1, const TropicalSparseTupleWeight< T > &w2, DivideType type=DIVIDE_ANY)
fst::GeneralMapper< fst::ArcTpl< fst::TropicalWeightTpl< float > >, fst::ArcTpl< fst::TropicalSparseTupleWeight< float > >, fst::StdToVector< float > > StdToSparseMapper
friend TropicalSparseTupleWeight< TT > Plus(const TropicalSparseTupleWeight< TT > &, const TropicalSparseTupleWeight< TT > &)
Functor to convert sparse tuple weight to tropical (single weight)
DotProductMap(const std::vector< T > &param)
long long int64
Functor that converts tropical to sparse tuple weight.
TropicalSparseTupleWeight< T > Times(const TropicalSparseTupleWeight< T > &w1, const TropicalSparseTupleWeight< T > &w2)
static const TropicalSparseTupleWeight< T > & One()
static void AddElement(TropicalSparseTupleWeight< T > &params, unsigned int index, T param)
Map functor used with generic weight mapper.
static const TropicalSparseTupleWeight< T > & Zero()
T DotProduct(const TropicalSparseTupleWeight< T > &w, const std::vector< T > &vw)
Implements Dot product of two vector weights.