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.

Parameters
  • start (int) – Mark the node as a starting state

  • accept (int) – Mark the node as an accepting state

Returns

The integer id of the node.

Return type

int

add_arc(src_node, dst_node, label)

Add an accepting arc to the graph.

Parameters
  • src_node (int) – The id of the source node

  • dst_node (int) – The id of the destination node

  • label (int) – Label for the arc

Returns

The integer id of the arc.

add_arc(src_node, dst_node, ilabel, olabel, weight=0.0)

Add a transducing arc to the graph.

Parameters
  • src_node (int) – The id of the source node

  • dst_node (int) – The id of the destination node

  • ilabel (int) – Input label for the arc

  • olabel (int) – Output label for the arc

  • weight (float) – Weight for the arc

Returns

The integer id of the arc.

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() and compose() 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_list()

Get a list 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.

calc_grad: bool

Set to True to compute the gradient for the graph.

grad()

Access the graph’s gradient Graph.

zero_grad()

Clear the graph’s gradient.