FST  openfst-1.7.2 OpenFst Library
expectation-weight.h
Go to the documentation of this file.
1 // See www.openfst.org for extensive documentation on this weighted
2 // finite-state transducer library.
3 //
4 // Expectation semiring as described by Jason Eisner:
5 // See: doi=10.1.1.22.9398
6 // Multiplex semiring operations and identities:
7 // One: <One, Zero>
8 // Zero: <Zero, Zero>
9 // Plus: <a1, b1> + <a2, b2> = < (a1 + a2) , (b1 + b2) >
10 // Times: <a1, b1> * <a2, b2> = < (a1 * a2) , [(a1 * b2) + (a2 * b1)] >
11 // Division: Undefined (currently)
12 //
13 // Usually used to store the pair <probability, random_variable> so that
14 // ShortestDistance[Fst<ArcTpl<ExpectationWeight<P, V>>>]
15 // == < PosteriorProbability, Expected_Value[V] >
16
17 #ifndef FST_EXPECTATION_WEIGHT_H_
18 #define FST_EXPECTATION_WEIGHT_H_
19
20 #include <string>
21
22 #include <fst/log.h>
23
24 #include <fst/pair-weight.h>
25 #include <fst/product-weight.h>
26
27
28 namespace fst {
29
30 // X1 is usually a probability weight like LogWeight.
31 // X2 is usually a random variable or vector (see SignedLogWeight or
32 // SparsePowerWeight).
33 //
34 // If X1 is distinct from X2, it is required that there is an external product
35 // between X1 and X2 and if both semriring are commutative, or left or right
36 // semirings, then result must have those properties.
37 template <class X1, class X2>
38 class ExpectationWeight : public PairWeight<X1, X2> {
39  public:
42
46
47  using ReverseWeight =
49
50  ExpectationWeight() : PairWeight<X1, X2>(Zero()) {}
51
52  explicit ExpectationWeight(const PairWeight<X1, X2> &weight)
53  : PairWeight<X1, X2>(weight) {}
54
55  ExpectationWeight(const X1 &x1, const X2 &x2) : PairWeight<X1, X2>(x1, x2) {}
56
57  static const ExpectationWeight &Zero() {
58  static const ExpectationWeight zero(X1::Zero(), X2::Zero());
59  return zero;
60  }
61
62  static const ExpectationWeight &One() {
63  static const ExpectationWeight one(X1::One(), X2::Zero());
64  return one;
65  }
66
67  static const ExpectationWeight &NoWeight() {
68  static const ExpectationWeight no_weight(X1::NoWeight(), X2::NoWeight());
69  return no_weight;
70  }
71
72  static const string &Type() {
73  static const string *const type =
74  new string("expectation_" + X1::Type() + "_" + X2::Type());
75  return *type;
76  }
77
78  PairWeight<X1, X2> Quantize(float delta = kDelta) const {
80  }
81
84  }
85
86  bool Member() const { return PairWeight<X1, X2>::Member(); }
87
88  static constexpr uint64 Properties() {
89  return X1::Properties() & X2::Properties() &
91  }
92 };
93
94 template <class X1, class X2>
96  const ExpectationWeight<X1, X2> &w2) {
97  return ExpectationWeight<X1, X2>(Plus(w1.Value1(), w2.Value1()),
98  Plus(w1.Value2(), w2.Value2()));
99 }
100
101 template <class X1, class X2>
103  const ExpectationWeight<X1, X2> &w2) {
105  Times(w1.Value1(), w2.Value1()),
106  Plus(Times(w1.Value1(), w2.Value2()), Times(w1.Value2(), w2.Value1())));
107 }
108
109 template <class X1, class X2>
111  const ExpectationWeight<X1, X2> &w2,
112  DivideType typ = DIVIDE_ANY) {
113  FSTERROR() << "ExpectationWeight::Divide: Not implemented";
115 }
116
117 // This function object generates weights by calling the underlying generators
118 // for the template weight types, like all other pair weight types. This is
119 // intended primarily for testing.
120 template <class X1, class X2>
122  : public WeightGenerate<PairWeight<X1, X2>> {
123  public:
126
127  explicit WeightGenerate(bool allow_zero = true) : Generate(allow_zero) {}
128
129  Weight operator()() const { return Weight(Generate::operator()()); }
130 };
131
132 } // namespace fst
133
134 #endif // FST_EXPECTATION_WEIGHT_H_
ExpectationWeight< X1, X2 > Divide(const ExpectationWeight< X1, X2 > &w1, const ExpectationWeight< X1, X2 > &w2, DivideType typ=DIVIDE_ANY)
uint64_t uint64
Definition: types.h:32
static const ExpectationWeight & NoWeight()
constexpr uint64 kRightSemiring
Definition: weight.h:115
PairWeight< X1, X2 > Quantize(float delta=kDelta) const
bool Member() const
Definition: pair-weight.h:58
ExpectationWeight< X1, X2 > Times(const ExpectationWeight< X1, X2 > &w1, const ExpectationWeight< X1, X2 > &w2)
const X2 & Value2() const
Definition: pair-weight.h:78
constexpr uint64 kCommutative
Definition: weight.h:120
ExpectationWeight(const X1 &x1, const X2 &x2)
constexpr uint64 kLeftSemiring
Definition: weight.h:112
ExpectationWeight< typename X1::ReverseWeight, typename X2::ReverseWeight > ReverseWeight
#define FSTERROR()
Definition: util.h:35
static const ExpectationWeight & Zero()
constexpr uint64 kIdempotent
Definition: weight.h:123
ExpectationWeight< X1, X2 > Plus(const ExpectationWeight< X1, X2 > &w1, const ExpectationWeight< X1, X2 > &w2)
ReverseWeight Reverse() const
static const ExpectationWeight & One()
ExpectationWeight(const PairWeight< X1, X2 > &weight)
static constexpr uint64 Properties()
static const string & Type()
DivideType
Definition: weight.h:142
constexpr float kDelta
Definition: weight.h:109
const X1 & Value1() const
Definition: pair-weight.h:76