gtn.Graph¶
- class Graph(calc_grad=True)¶
A weighted finite-state acceptor or transducer.
- Parameters
calc_grad (bool) – Specify if a gradient is required for the graph.
- add_node(start=False, accept=False)¶
Add a node to the graph.
- add_arc(src_node, dst_node, label)
Add an accepting arc to the graph.
- add_arc(src_node, dst_node, ilabel, olabel, weight=0.0)¶
Add a transducing arc to the graph.
- arc_sort(olabel=False)¶
Sort the arcs entering and exiting a node by label.
This function is intended to be used prior to calls to
intersect()
andcompose()
to improve the efficiency of the algorithms.- Parameters
olabel (bool) – Sort by increasing order on the arc input label (default) or output label if
olabel == True
.
- mark_arc_sorted(olabel=False)¶
Mark the arcs entering and exiting the nodes of a graph as sorted. This method is intended to be used when a graph is constructed in sorted order to avoid paying for a call to
arc_sort()
.- Parameters
olabel (bool) – Mark as sorted by input label (default) or output label if
olabel == True
.
- item()¶
Get the weight on a single arc graph.
- num_arcs()¶
Get the number of arcs in the graph.
- num_nodes()¶
Get the number of nodes in the graph.
- num_start()¶
Get the number of starting nodes in the graph.
- num_accept()¶
Get the number of accepting nodes in the graph.
- weights()¶
Get a pointer to an array of the graph’s arc weights.
- weights_to_numpy()¶
Get a 1D
numpy.ndarray
with the graph’s arc weights.
- set_weights(weights)¶
Set all of the arc weights of the graph.
- Parameters
weights (int or list or numpy.ndarray) – The weights of the arcs to set. An
int
type is treated as the pointer to the first entry of an array of weights.
- labels_to_list(ilabel=True)¶
Get the graph’s arc labels as a list.
- Parameters
ilabel (bool) – If True return the input labels, otherwise return the output labels.
- zero_grad()¶
Clear the graph’s gradient.