Functions¶
-
enum Projection¶
Projection type used with
gtn::clone
.Values:
-
enumerator NONE¶
Keep both input and output labels.
-
enumerator INPUT¶
Keep only the input lables.
-
enumerator OUTPUT¶
Keep only the output lables.
-
enumerator NONE¶
-
Graph clone(const Graph &g, Projection projection = Projection::NONE)¶
Performs a deep clone of a graph with an option to project to either the input or output labels.
The operation is recorded in the autograd tape. For a version which is not recorded on the autograd tape, see
Graph::deepCopy
.
-
Graph projectInput(const Graph &g)¶
Removes the output labels from the graph and records the operation in the autograd tape.
This function makes a copy of the input graph.
-
Graph projectOutput(const Graph &g)¶
Removes the input labels from the graph and records the operation in the autograd tape.
This function makes a copy of the input graph.
-
Graph concat(const Graph &g1, const Graph &g2)¶
Create the concatenation of two graphs.
This operation is recorded in the autograd tape.
Equivalent to
concat({g1, g2})
, seegtn::concat(const std::vector<Graph>&)
.
-
Graph concat(const std::vector<Graph> &graphs)¶
Concatenate a vector of graphs.
This operation is recorded in the autograd tape.
If
x_i
is a sequence accepted (orx_i:y_i
is transduced) bygraphs[i]
then the concatenated graph accepts the sequencex_1x_2...x_n
ifgraphs
containsn
graphs. The score of the pathx_1...x_n
is the sum of the scores of the individualx_i
ingraphs[i]
. The concatenated graph is constructuted by connecting every accepting state ofgraphs[i-1]
to every starting state ofgraphs[i]
with an epsilon transition. The starting state of the concatenated graphs are starting states ofgraphs[0]
and the accepting states are accepting states ofgraphs.back()
.Note the concatenation of 0 graphs
gtn::concat({})
is the graph which accepts the empty string (epsilon). The concatentation of a single graph is equivalent to a clone.
-
Graph remove(const Graph &g, int label = epsilon)¶
Create the equivalent graph without epsilon transitions.
If label is specified then instead of removing epsilon transitions, arcs with the matching label are removed. The removed arc labels are treated as if they were epsilon transitions. Note this is different than simply pruning the arc.
-
Graph remove(const Graph &g, int ilabel, int olabel)¶
Create the equivalent graph without
ilabel:olabel
transitions.The removed arc labels are treated as if they were epsilon transitions. Note this is different than simply pruning the arc.
-
Graph compose(const Graph &g1, const Graph &g2)¶
Compose two transducers.
This operation is recorded in the autograd tape. If x:y is transduced by
g1
andy:z
is transduced byg2
then the composition will transducex:z
. The arc scores are added in the composed graph.
-
Graph intersect(const Graph &g1, const Graph &g2)¶
Intersect two acceptors.
This operation is recorded in the autograd tape. This function only works on acceptors, calling it on a
graph
wheregraph.ilabel(a) != graph.olabel(a)
for somea
is undefined and may yield incorrect results. The intersected graph accepts any pathx
which is accepted by bothg1
andg2
. The arc scores are added in the intersected graph.The result of
gtn::compose
will yield an equivalent result, however; this function should be preferred since the implementation may be faster.
-
Graph forwardScore(const Graph &g)¶
Compute the forward score of a graph.
Returns the score in a scalar graph which can be accessed with
Graph::item()
. This is equivalent to the shortest distance from the start nodes to the accept nodes in the log semiring. NB: This assumes the input graph is acyclic.
-
Graph viterbiScore(const Graph &g)¶
Compute the Viterbi score of a graph.
Returns the score in a scalar graph which can be accessed with
Graph::item()
. This is equivalent to the shortest distance from the start nodes to the accepting nodes in the tropical semiring. NB: This assumes the input graph is acyclic.
-
Graph viterbiPath(const Graph &g)¶
Compue the Viterbi shortest path of a graph and return it in a single chain graph with the labels and weights of the shortest path.
This is equivalent to the shortest path from the start nodes to the accepting nodes in the tropical semiring. NB: This assumes the input graph is acyclic.
-
Graph sample(const Graph &g, size_t maxLength = 1000)¶
Attempt to sample an accepting path from the graph.
If the graph has dead-ends or does accept any paths, then an empty path may be returned. Note that the empty path “{}” is different from the path which is the empty string “{ε}”.
- Parameters
g – The graph to sample from.
maxLength – The maximum length of a sampled path.