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.

Graph negate(const Graph &g)

Negate a scalar graph.

Graph add(const Graph &g1, const Graph &g2)

Add two scalar graphs.

Graph subtract(const Graph &g1, const Graph &g2)

Subtract one scalar graph from another.

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}), see gtn::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 (or x_i:y_i is transduced) by graphs[i] then the concatenated graph accepts the sequence x_1x_2...x_n if graphs contains n graphs. The score of the path x_1...x_n is the sum of the scores of the individual x_i in graphs[i]. The concatenated graph is constructuted by connecting every accepting state of graphs[i-1] to every starting state of graphs[i] with an epsilon transition. The starting state of the concatenated graphs are starting states of graphs[0] and the accepting states are accepting states of graphs.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 closure(const Graph &g)

Create the (Kleene) closure of the graph.

Graph union_(const std::vector<Graph> &graphs)

Create the union of a vector of graphs.

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 and y:z is transduced by g2 then the composition will transduce x: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 where graph.ilabel(a) != graph.olabel(a) for some a is undefined and may yield incorrect results. The intersected graph accepts any path x which is accepted by both g1 and g2. 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.