15 #ifndef TROPICALSPARSETUPLEWEIGHT_H_ 16 #define TROPICALSPARSETUPLEWEIGHT_H_ 36 SparsePowerWeight<TropicalWeightTpl<T> > {
45 typedef TropicalWeightTpl<T>
W;
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;
57 SparsePowerWeight<W>() {
58 this->SetDefaultValue ( W::One() );
62 SparsePowerWeight<W> ( sw ) {
66 SparsePowerWeight<W> ( w ) {
70 int64 size =
sizeof ( T );
71 if ( size ==
sizeof (
float ) ) {
76 Int64ToStr ( size, &result );
80 static const std::string&
Type() {
81 static const std::string type =
"tropicalsparsetuple" 87 return W::Properties()
88 & ( kLeftSemiring | kRightSemiring | kCommutative | kIdempotent
104 w.SetDefaultValue ( this->DefaultValue() );
112 template<
typename TT>
121 const std::vector<T>& vw ) {
122 T result = w.DefaultValue().Value();
123 for ( SparseTupleWeightIterator<TropicalWeightTpl<T>,
int> it ( w ); !it.Done();
129 }
else if ( it.Value().first > int ( vw.size() ) ) {
131 <<
"feature vector has a larger dimensionality than the parameters. " 132 <<
"Params: " << vw.size() <<
" Features: " 133 << it.Value().first << endl;
135 }
else if (it.Value().first < 0)
138 param = vw[it.Value().first - 1];
140 result += it.Value().second.Value() * param;
151 return w1 < w2 ? vw1 : vw2;
157 const SparsePowerWeight<TropicalWeightTpl<T> >& spw1 = vw1;
158 const SparsePowerWeight<TropicalWeightTpl<T> >& spw2 = vw2;
167 SparseTupleWeightTimesMapper<TropicalWeightTpl<T>,
int> operator_mapper;
168 SparseTupleWeightMap ( &ret, w1, w2, operator_mapper );
178 SparseTupleWeightDivideMapper<TropicalWeightTpl<T>,
int> operator_mapper (
180 SparseTupleWeightMap ( &ret, w1, w2, operator_mapper );
237 std::vector<T> param;
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;
256 return w.DefaultValue();
274 result.Push ( k + 1, w.Value() );
bool ApproxEqual(const TropicalSparseTupleWeight< T > &vw1, const TropicalSparseTupleWeight< T > &vw2, float delta=kDelta)
ReverseWeight Reverse() const
TropicalSparseTupleWeight(W w)
TropicalSparseTupleWeight< T > Quantize(float delta=kDelta) const
static std::vector< T > & Params()
TropicalSparseTupleWeight(const SparsePowerWeight< W > &sw)
Initializes a set of parameters from environment variables PARAMS_FILE or PARAMS. ...
TropicalSparseTupleWeight()
static uint64 Properties()
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 > ¶m)
Convenience functions to parse parameters from a string.
TropicalSparseTupleWeight< T > ReverseWeight
Functor that converts tropical to sparse tuple weight.
TropicalSparseTupleWeight< T > Times(const TropicalSparseTupleWeight< T > &w1, const TropicalSparseTupleWeight< T > &w2)
static std::string GetPrecisionString()
static const TropicalSparseTupleWeight< T > & One()
Map functor used with generic weight mapper.
static const TropicalSparseTupleWeight< T > & Zero()
static const std::string & Type()
T DotProduct(const TropicalSparseTupleWeight< T > &w, const std::vector< T > &vw)
Implements Dot product of two vector weights.