<jupyter_start><jupyter_text>GTN quickstart for beginners GTN is a framework for automatic differentiation with weighted finite-state transducers. The following is a minimal example of building two WFSAs with GTN, constructing a simple function on the graphs, and computing gradients using the python interface. More ResourcesGitbub - https://github.com/facebookresearch/gtnDocumentation - https://gtn.readthedocs.io/en/latest/Paper - https://arxiv.org/abs/2010.01003 Install GTN<jupyter_code>!pip install gtn<jupyter_output>Collecting gtn
[?25l  Downloading https://files.pythonhosted.org/packages/82/b2/08fe7f2684d39eb6abec0d15aa7f8d495761ff3d44508d6e59ecc001b295/gtn-0.0.0.tar.gz (45kB)

[K     |███████▏                        | 10kB 11.4MB/s eta 0:00:01
[K     |██████████████▎                 | 20kB 1.6MB/s eta 0:00:01
[K     |█████████████████████▍          | 30kB 2.1MB/s eta 0:00:01
[K     |████████████████████████████▋   | 40kB 2.3MB/s eta 0:00:01
[K     |████████████████████████████████| 51kB 1.4MB/s 
[?25hBuilding wheels for collected packages: gtn
  Building wheel for gtn (setup.py) ... [?25l[?25hdone
  Created wheel for gtn: filename=gtn-0.0.0-cp36-cp36m-linux_x86_64.whl size=554005 sha256=86dbeaf9ee01d2c8a86a0351cc968e12fdd4b4f263804fce89ef481a7740cae2
  Stored in directory: /root/.cache/pip/wheels/52/4e/42/1cab375512e74e8fd09da8a78800f250376941bfc63c6b4525
Successfully built gtn
Installing collected packages: gtn
Successfully installed gtn-0.0.0<jupyter_text>Importing required libraries<jupyter_code>import gtn
from IPython.display import display, Image<jupyter_output><empty_output><jupyter_text>Create simple graphs<jupyter_code># Make some graphs:
g1 = gtn.Graph()
g1.add_node(True)  # Add a start node
g1.add_node()  # Add an internal node
g1.add_node(False, True)  # Add an accepting node


# Add arcs with (src node, dst node, label):
g1.add_arc(0, 1, 1)
g1.add_arc(0, 1, 2)
g1.add_arc(1, 2, 1)
g1.add_arc(1, 2, 0)


g2 = gtn.Graph()
g2.add_node(True, True)
g2.add_arc(0, 0, 1)
g2.add_arc(0, 0, 0)<jupyter_output><empty_output><jupyter_text>Visualize the graphs<jupyter_code>gtn.draw(g1, "g1.png")
gtn.draw(g2, "g2.png")
display(Image("g1.png"), Image("g2.png"))<jupyter_output><empty_output><jupyter_text>Functions on Graphs<jupyter_code>intersect = gtn.intersect(g1, g2)
gtn.draw(intersect, "intersect.png")
Image("intersect.png")<jupyter_output><empty_output><jupyter_text>Backpropagation with Graphs<jupyter_code>score = gtn.viterbi_score(intersect)
gtn.backward(score)

# print gradients of arc weights 
print(g1.grad().weights_to_list())<jupyter_output>[1.0, 0.0, 1.0, 0.0]
{'fraction_non_alphanumeric': 0.15821578345406023, 'fraction_numerical': 0.09416698436904308, 'mean_word_length': 5.384428223844282, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 23, 'https://': 4, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '596416', 'license': 'MIT', 'n_tokens_mistral': 1290, 'n_tokens_neox': 1085, 'n_words': 199, 'path': 'examples/notebooks/quick-start.ipynb', 'repo_name': 'shubho/gtn-1', 'size': '2623'}