Cambridge SMT System
fsttools/include/tropical-sparse-tuple-weight.h
Go to the documentation of this file.
1 // Licensed under the Apache License, Version 2.0 (the "License");
2 // you may not use these files except in compliance with the License.
3 // You may obtain a copy of the License at
4 //
5 // http://www.apache.org/licenses/LICENSE-2.0
6 //
7 // Unless required by applicable law or agreed to in writing, software
8 // distributed under the License is distributed on an "AS IS" BASIS,
9 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10 // See the License for the specific language governing permissions and
11 // limitations under the License.
12 
13 // Copyright 2012 - Gonzalo Iglesias, AdriĆ  de Gispert, William Byrne
14 
15 #ifndef TROPICALSPARSETUPLEWEIGHT_H_
16 #define TROPICALSPARSETUPLEWEIGHT_H_
17 
25 # include <params.hpp>
26 
27 namespace fst {
28 
34 template<typename T>
36  SparsePowerWeight<TropicalWeightTpl<T> > {
37 
38  public:
39 
40  static std::vector<T>& Params() {
41  static ucam::util::ParamsInit<T> params;
42  return params.params;
43  }
44 
45  typedef TropicalWeightTpl<T> W;
46 
47  using SparsePowerWeight<W>::Zero;
48  using SparsePowerWeight<W>::One;
49  using SparsePowerWeight<W>::Quantize;
50  using SparsePowerWeight<W>::Reverse;
51  using SparsePowerWeight<W>::Type;
52  using SparsePowerWeight<W>::Properties;
53 
55 
57  SparsePowerWeight<W>() {
58  this->SetDefaultValue ( W::One() );
59  }
60 
61  TropicalSparseTupleWeight ( const SparsePowerWeight<W>& sw ) :
62  SparsePowerWeight<W> ( sw ) {
63  }
64 
66  SparsePowerWeight<W> ( w ) {
67  }
68 
69  inline static std::string GetPrecisionString() {
70  int64 size = sizeof ( T );
71  if ( size == sizeof ( float ) ) {
72  return "";
73  }
74  size *= CHAR_BIT;
75  std::string result;
76  Int64ToStr ( size, &result );
77  return result;
78  }
79 
80  static const std::string& Type() {
81  static const std::string type = "tropicalsparsetuple"
83  return type;
84  }
85 
86  static uint64 Properties() {
87  return W::Properties()
88  & ( kLeftSemiring | kRightSemiring | kCommutative | kIdempotent
89  | kPath );
90  }
91 
93  static TropicalSparseTupleWeight<T> zero ( W::Zero() );
94  return zero;
95  }
96 
98  static TropicalSparseTupleWeight<T> one ( W::One() );
99  return one;
100  }
101 
102  TropicalSparseTupleWeight<T> Quantize ( float delta = kDelta ) const {
103  TropicalSparseTupleWeight<T> w = SparsePowerWeight<W>::Quantize ( delta );
104  w.SetDefaultValue ( this->DefaultValue() );
105  return w;
106  }
107 
108  ReverseWeight Reverse() const {
109  return *this;
110  }
111 
112  template<typename TT>
116 };
117 
119 template<typename T>
121  const std::vector<T>& vw ) {
122  T result = w.DefaultValue().Value();
123  for ( SparseTupleWeightIterator<TropicalWeightTpl<T>, int> it ( w ); !it.Done();
124  it.Next() ) {
125  T param;
126  // Special case for flat params
127  if ( vw.empty() ) {
128  param = 1;
129  } else if ( it.Value().first > int ( vw.size() ) ) {
130  cerr
131  << "feature vector has a larger dimensionality than the parameters. "
132  << "Params: " << vw.size() << " Features: "
133  << it.Value().first << endl;
134  exit ( 1 );
135  } else if (it.Value().first < 0) // ignore negative indices
136  param = 0;
137  else {
138  param = vw[it.Value().first - 1];
139  }
140  result += it.Value().second.Value() * param;
141  }
142  return result;
143 }
144 
145 template<class T>
147  const TropicalSparseTupleWeight<T>& vw1,
148  const TropicalSparseTupleWeight<T>& vw2 ) {
151  return w1 < w2 ? vw1 : vw2;
152 }
153 
154 template<typename T>
155 inline bool ApproxEqual ( const TropicalSparseTupleWeight<T>& vw1,
156  const TropicalSparseTupleWeight<T>& vw2, float delta = kDelta ) {
157  const SparsePowerWeight<TropicalWeightTpl<T> >& spw1 = vw1;
158  const SparsePowerWeight<TropicalWeightTpl<T> >& spw2 = vw2;
159  return ApproxEqual ( spw1, spw2, delta );
160 }
161 
162 template<class T>
165  const TropicalSparseTupleWeight<T>& w2 ) {
167  SparseTupleWeightTimesMapper<TropicalWeightTpl<T>, int> operator_mapper;
168  SparseTupleWeightMap ( &ret, w1, w2, operator_mapper );
169  return ret;
170 }
171 
172 // Semimodule divide operation
173 template<class T>
176  const TropicalSparseTupleWeight<T>& w2, DivideType type = DIVIDE_ANY ) {
178  SparseTupleWeightDivideMapper<TropicalWeightTpl<T>, int> operator_mapper (
179  type );
180  SparseTupleWeightMap ( &ret, w1, w2, operator_mapper );
181  return ret;
182 };
183 
184 /*
185 
186 template<typename W>
187 struct StdToTropicalSparseMapper {
188  explicit StdToTropicalSparseMapper(uint k_ = 1) :
189  k_(k_) {
190  }
191  ;
192 
193  TropicalSparseTupleWeight<W> operator()(W w) {
194  TropicalSparseTupleWeight<W> sparse;
195  sparse.push(k_, w);
196  return sparse;
197  }
198 
199 private:
200  uint k_;
201 };
202 
203 struct Expand {
204 
205  TropicalSparseTupleWeight<double> operator()(
206  const TropicalSparseTupleWeight<float>& w32) const {
207  TropicalSparseTupleWeight<double> result;
208  for (SparseTupleWeightIterator<TropicalWeight, int> it(w32); !it.Done();
209  it.Next()) {
210  result.Push(it.Value().first,
211  TropicalWeightTpl<double>(it.Value().second.Value()));
212  }
213  return result;
214 
215  }
216 }
217 ;
218 
219 typedef GeneralMapper<ArcTpl<TropicalSparseTupleWeight<float> >,
220  ArcTpl<TropicalSparseTupleWeight<double> >, Expand> ExpandMapper;
221 
222 */
223 
225 template<typename T>
227  DotProductMap ( const std::vector<T>& param ) :
228  param ( param ) {
229  }
230 
231  inline TropicalWeightTpl<T> operator() ( const TropicalSparseTupleWeight<T>& w )
232  const {
233  return DotProduct ( w, param );
234  }
235 
236  private:
237  std::vector<T> param;
238 };
239 
241 template<typename T>
242 struct VectorToStd {
243 
244  VectorToStd ( int32_t k = 1 ) :
245  k ( k ) {
246  }
247 
248  TropicalWeightTpl<T> operator() ( const TropicalSparseTupleWeight<T>& w )
249  const {
250  for ( SparseTupleWeightIterator<TropicalWeightTpl<T>, int> it ( w );
251  !it.Done(); it.Next() ) {
252  if ( it.Value().first - 1 == k ) {
253  return it.Value().second;
254  }
255  }
256  return w.DefaultValue();
257  }
258 
259  private:
260  int32_t k;
261 };
262 
264 template<typename T>
265 struct StdToVector {
266 
267  StdToVector ( int32_t k ) :
268  k ( k ) {
269  }
270 
271  TropicalSparseTupleWeight<T> operator() ( const TropicalWeightTpl<T>& w )
272  const {
274  result.Push ( k + 1, w.Value() );
275  return result;
276  }
277  private:
278  int32_t k;
279 };
280 
281 //typedef GenericWeightMapper< ArcTpl<TropicalWeightTpl<float> > , ArcTpl<TropicalSparseTupleWeight<float> >, StdToVector<float> > StdToSparseMapper;
282 //typedef GenericWeightMapper<ArcTpl<TropicalSparseTupleWeight<float> >,ArcTpl<TropicalWeightTpl<float> >, DotProductMap<float> > DotProduct32Mapper;
283 //typedef GenericWeightMapper<ArcTpl<TropicalSparseTupleWeight<double> >,ArcTpl<TropicalWeightTpl<double> >, DotProductMap<double> > DotProductMapper;
284 //typedef GenericWeightMapper<ArcTpl<TropicalSparseTupleWeight<float> >,ArcTpl<TropicalWeightTpl<float> >, VectorToStd<float> > SparseToStdMapper;
285 
286 } //namespace fst
287 
288 #endif /* TROPICALSPARSETUPLEWEIGHT_H_ */
bool ApproxEqual(const TropicalSparseTupleWeight< T > &vw1, const TropicalSparseTupleWeight< T > &vw2, float delta=kDelta)
std::vector< T > params
Definition: params.hpp:132
Definition: fstio.hpp:27
TropicalSparseTupleWeight< T > Quantize(float delta=kDelta) const
Initializes a set of parameters from environment variables PARAMS_FILE or PARAMS. ...
Definition: params.hpp:131
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)
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)
Convenience functions to parse parameters from a string.
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()
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.