Road to AI

Jupyter Notebook

Download notebook (.ipynb)

Reading and exploring the dataset 00:03:03

words = open('names.txt', 'r').read().splitlines()
words[:10]
Output
['emma',
 'olivia',
 'ava',
 'isabella',
 'sophia',
 'charlotte',
 'mia',
 'amelia',
 'harper',
 'evelyn']
len(words)
Output
32033
min(len(word) for word in words)
Output
2
max(len(word) for word in words)
Output
15

Exploring the bigrams in the dataset 00:06:24

for word in words[:1]:  #Only taking the first word as an example
    for ch1, ch2 in zip(word, word[1:]):    #Two columns are formed where The first character is taken and is zipped with its next one. So when there is only one character left, then it exits.
        print(ch1, ch2)
Output
e m
m m
m a
for word in words[:1]:
    chs = ['<S>'] + list(word) + ['<E>']    #Here we are just adding our own set of characters in the list, so as to get like this custom output - Labelling the first and the last character of the word
    for ch1, ch2 in zip(chs, chs[1:]):
        print(ch1, ch2)
Output
<S> e
e m
m m
m a
a <E>
for word in words[:3]:
    chs = ['<S>'] + list(word) + ['<E>']
    for ch1, ch2 in zip(chs, chs[1:]):
        print(ch1, ch2)
Output
<S> e
e m
m m
m a
a <E>
<S> o
o l
l i
i v
v i
i a
a <E>
<S> a
a v
v a
a <E>

Counting bigrams in a python dictionary 00:09:24

b = {}  #We have created a dictionary 'b' - First data structure
for word in words[:1]:
    chs = ['<S>'] + list(word) + ['<E>']
    for ch1, ch2 in zip(chs, chs[1:]):
        bigram = (ch1, ch2) #Then we have created a set 'bigram' which is just a set of two characters - Second data structure
        b[bigram] = b.get(bigram, 0) + 1    #Here we are adding it to the dictionary 'b', where the key is 'bigram' (which is a set of character pairs) has the value counts of the set occurred (The number of times that set has occurred)
        print(ch1, ch2)
Output
<S> e
e m
m m
m a
a <E>

Note:
b.get(bigram) is the same as b[bigram]
Just that here: b.get(bigram, 0) if we don't get a bigram value, we want it to assign to 0
Finally we are adding one +1 as we want to count the occurance.

b
Output
{('<S>', 'e'): 1, ('e', 'm'): 1, ('m', 'm'): 1, ('m', 'a'): 1, ('a', '<E>'): 1}
b = {}
for word in words[:3]:
    chs = ['<S>'] + list(word) + ['<E>']
    for ch1, ch2 in zip(chs, chs[1:]):
        bigram = (ch1, ch2)
        b[bigram] = b.get(bigram, 0) + 1
        print(ch1, ch2)
Output
<S> e
e m
m m
m a
a <E>
<S> o
o l
l i
i v
v i
i a
a <E>
<S> a
a v
v a
a <E>
b
Output
{('<S>', 'e'): 1,
 ('e', 'm'): 1,
 ('m', 'm'): 1,
 ('m', 'a'): 1,
 ('a', '<E>'): 3,
 ('<S>', 'o'): 1,
 ('o', 'l'): 1,
 ('l', 'i'): 1,
 ('i', 'v'): 1,
 ('v', 'i'): 1,
 ('i', 'a'): 1,
 ('<S>', 'a'): 1,
 ('a', 'v'): 1,
 ('v', 'a'): 1}
b = {}
for word in words:
    chs = ['<S>'] + list(word) + ['<E>']
    for ch1, ch2 in zip(chs, chs[1:]):
        bigram = (ch1, ch2)
        b[bigram] = b.get(bigram, 0) + 1

b
Output
{('<S>', 'e'): 1531,
 ('e', 'm'): 769,
 ('m', 'm'): 168,
 ('m', 'a'): 2590,
 ('a', '<E>'): 6640,
 ('<S>', 'o'): 394,
 ('o', 'l'): 619,
 ('l', 'i'): 2480,
 ('i', 'v'): 269,
 ('v', 'i'): 911,
 ('i', 'a'): 2445,
 ('<S>', 'a'): 4410,
 ('a', 'v'): 834,
 ('v', 'a'): 642,
 ('<S>', 'i'): 591,
 ('i', 's'): 1316,
 ('s', 'a'): 1201,
 ('a', 'b'): 541,
 ('b', 'e'): 655,
 ('e', 'l'): 3248,
 ('l', 'l'): 1345,
 ('l', 'a'): 2623,
 ('<S>', 's'): 2055,
 ('s', 'o'): 531,
 ('o', 'p'): 95,
 ('p', 'h'): 204,
 ('h', 'i'): 729,
 ('<S>', 'c'): 1542,
 ('c', 'h'): 664,
 ('h', 'a'): 2244,
 ('a', 'r'): 3264,
 ('r', 'l'): 413,
 ('l', 'o'): 692,
 ('o', 't'): 118,
 ('t', 't'): 374,
 ('t', 'e'): 716,
 ('e', '<E>'): 3983,
 ('<S>', 'm'): 2538,
 ('m', 'i'): 1256,
 ('a', 'm'): 1634,
 ('m', 'e'): 818,
 ('<S>', 'h'): 874,
 ('r', 'p'): 14,
 ('p', 'e'): 197,
 ('e', 'r'): 1958,
 ('r', '<E>'): 1377,
 ('e', 'v'): 463,
 ('v', 'e'): 568,
 ('l', 'y'): 1588,
 ('y', 'n'): 1826,
 ('n', '<E>'): 6763,
 ('b', 'i'): 217,
 ('i', 'g'): 428,
 ('g', 'a'): 330,
 ('a', 'i'): 1650,
 ('i', 'l'): 1345,
 ('l', '<E>'): 1314,
 ('y', '<E>'): 2007,
 ('i', 'z'): 277,
 ('z', 'a'): 860,
 ('e', 't'): 580,
 ('t', 'h'): 647,
 ('h', '<E>'): 2409,
 ('r', 'y'): 773,
 ('o', 'f'): 34,
 ('f', 'i'): 160,
 ('c', 'a'): 815,
 ('r', 'i'): 3033,
 ('s', 'c'): 60,
 ('l', 'e'): 2921,
 ('t', '<E>'): 483,
 ('<S>', 'v'): 376,
 ('i', 'c'): 509,
 ('c', 't'): 35,
 ('t', 'o'): 667,
 ('o', 'r'): 1059,
 ('a', 'd'): 1042,
 ('d', 'i'): 674,
 ('o', 'n'): 2411,
 ('<S>', 'l'): 1572,
 ('l', 'u'): 324,
 ('u', 'n'): 275,
 ('n', 'a'): 2977,
 ('<S>', 'g'): 669,
 ('g', 'r'): 201,
 ('r', 'a'): 2356,
 ('a', 'c'): 470,
 ('c', 'e'): 551,
 ('h', 'l'): 185,
 ('o', 'e'): 132,
 ('<S>', 'p'): 515,
 ('e', 'n'): 2675,
 ('n', 'e'): 1359,
 ('a', 'y'): 2050,
 ('y', 'l'): 1104,
 ('<S>', 'r'): 1639,
 ('e', 'y'): 1070,
 ('<S>', 'z'): 929,
 ('z', 'o'): 110,
 ('<S>', 'n'): 1146,
 ('n', 'o'): 496,
 ('e', 'a'): 679,
 ('a', 'n'): 5438,
 ('n', 'n'): 1906,
 ('a', 'h'): 2332,
 ('d', 'd'): 149,
 ('a', 'u'): 381,
 ('u', 'b'): 103,
 ('b', 'r'): 842,
 ('r', 'e'): 1697,
 ('i', 'e'): 1653,
 ('s', 't'): 765,
 ('a', 't'): 687,
 ('t', 'a'): 1027,
 ('a', 'l'): 2528,
 ('a', 'z'): 435,
 ('z', 'e'): 373,
 ('i', 'o'): 588,
 ('u', 'r'): 414,
 ('r', 'o'): 869,
 ('u', 'd'): 136,
 ('d', 'r'): 424,
 ('<S>', 'b'): 1306,
 ('o', 'o'): 115,
 ('o', 'k'): 68,
 ('k', 'l'): 139,
 ('c', 'l'): 116,
 ('i', 'r'): 849,
 ('s', 'k'): 82,
 ('k', 'y'): 379,
 ('u', 'c'): 103,
 ('c', 'y'): 104,
 ('p', 'a'): 209,
 ('s', 'l'): 279,
 ('i', 'n'): 2126,
 ('o', 'v'): 176,
 ('g', 'e'): 334,
 ('e', 's'): 861,
 ('s', 'i'): 684,
 ('s', '<E>'): 1169,
 ('<S>', 'k'): 2963,
 ('k', 'e'): 895,
 ('e', 'd'): 384,
 ('d', 'y'): 317,
 ('n', 't'): 443,
 ('y', 'a'): 2143,
 ('<S>', 'w'): 307,
 ('w', 'i'): 148,
 ('o', 'w'): 114,
 ('w', '<E>'): 51,
 ('k', 'i'): 509,
 ('n', 's'): 278,
 ('a', 'o'): 63,
 ('o', 'm'): 261,
 ('i', '<E>'): 2489,
 ('a', 'a'): 556,
 ('i', 'y'): 779,
 ('d', 'e'): 1283,
 ('c', 'o'): 380,
 ('r', 'u'): 252,
 ('b', 'y'): 83,
 ('s', 'e'): 884,
 ('n', 'i'): 1725,
 ('i', 't'): 541,
 ('t', 'y'): 341,
 ('u', 't'): 82,
 ('t', 'u'): 78,
 ('u', 'm'): 154,
 ('m', 'n'): 20,
 ('g', 'i'): 190,
 ('t', 'i'): 532,
 ('<S>', 'q'): 92,
 ('q', 'u'): 206,
 ('u', 'i'): 121,
 ('a', 'e'): 692,
 ('e', 'h'): 152,
 ('v', 'y'): 121,
 ('p', 'i'): 61,
 ('i', 'p'): 53,
 ('y', 'd'): 272,
 ('e', 'x'): 132,
 ('x', 'a'): 103,
 ('<S>', 'j'): 2422,
 ('j', 'o'): 479,
 ('o', 's'): 504,
 ('e', 'p'): 83,
 ('j', 'u'): 202,
 ('u', 'l'): 301,
 ('<S>', 'd'): 1690,
 ('k', 'a'): 1731,
 ('e', 'e'): 1271,
 ('y', 't'): 104,
 ('d', 'l'): 60,
 ('c', 'k'): 316,
 ('n', 'z'): 145,
 ('z', 'i'): 364,
 ('a', 'g'): 168,
 ('d', 'a'): 1303,
 ('j', 'a'): 1473,
 ('h', 'e'): 674,
 ('<S>', 'x'): 134,
 ('x', 'i'): 102,
 ('i', 'm'): 427,
 ('e', 'i'): 818,
 ('<S>', 't'): 1308,
 ('<S>', 'f'): 417,
 ('f', 'a'): 242,
 ('n', 'd'): 704,
 ('r', 'g'): 76,
 ('a', 's'): 1118,
 ('s', 'h'): 1285,
 ('b', 'a'): 321,
 ('k', 'h'): 307,
 ('s', 'm'): 90,
 ('o', 'd'): 190,
 ('r', 's'): 190,
 ('g', 'h'): 360,
 ('s', 'y'): 215,
 ('y', 's'): 401,
 ('s', 's'): 461,
 ('e', 'c'): 153,
 ('c', 'i'): 271,
 ('m', 'o'): 452,
 ('r', 'k'): 90,
 ('n', 'l'): 195,
 ('d', 'n'): 31,
 ('r', 'd'): 187,
 ('o', 'i'): 69,
 ('t', 'r'): 352,
 ('m', 'b'): 112,
 ('r', 'm'): 162,
 ('n', 'y'): 465,
 ('d', 'o'): 378,
 ('o', 'a'): 149,
 ('o', 'c'): 114,
 ('m', 'y'): 287,
 ('s', 'u'): 185,
 ('m', 'c'): 51,
 ('p', 'r'): 151,
 ('o', 'u'): 275,
 ('r', 'n'): 140,
 ('w', 'a'): 280,
 ('e', 'b'): 121,
 ('c', 'c'): 42,
 ('a', 'w'): 161,
 ('w', 'y'): 73,
 ('y', 'e'): 301,
 ('e', 'o'): 269,
 ('a', 'k'): 568,
 ('n', 'g'): 273,
 ('k', 'o'): 344,
 ('b', 'l'): 103,
 ('h', 'o'): 287,
 ('e', 'g'): 125,
 ('f', 'r'): 114,
 ('s', 'p'): 51,
 ('l', 's'): 94,
 ('y', 'z'): 78,
 ('g', 'g'): 25,
 ('z', 'u'): 73,
 ('i', 'd'): 440,
 ('m', '<E>'): 516,
 ('o', 'g'): 44,
 ('j', 'e'): 440,
 ('g', 'n'): 27,
 ('y', 'r'): 291,
 ('c', '<E>'): 97,
 ('c', 'q'): 11,
 ('u', 'e'): 169,
 ('i', 'f'): 101,
 ('f', 'e'): 123,
 ('i', 'x'): 89,
 ('x', '<E>'): 164,
 ('o', 'y'): 103,
 ('g', 'o'): 83,
 ('g', 't'): 31,
 ('l', 't'): 77,
 ('g', 'w'): 26,
 ('w', 'e'): 149,
 ('l', 'd'): 138,
 ('a', 'p'): 82,
 ('h', 'n'): 138,
 ('t', 'l'): 134,
 ('m', 'r'): 97,
 ('n', 'c'): 213,
 ('l', 'b'): 52,
 ('i', 'k'): 445,
 ('<S>', 'y'): 535,
 ('t', 'z'): 105,
 ('h', 'r'): 204,
 ('j', 'i'): 119,
 ('h', 't'): 71,
 ('r', 'r'): 425,
 ('z', 'l'): 123,
 ('w', 'r'): 22,
 ('b', 'b'): 38,
 ('r', 't'): 208,
 ('l', 'v'): 72,
 ('e', 'j'): 55,
 ('o', 'h'): 171,
 ('u', 's'): 474,
 ('i', 'b'): 110,
 ('g', 'l'): 32,
 ('h', 'y'): 213,
 ('p', 'o'): 59,
 ('p', 'p'): 39,
 ('p', 'y'): 12,
 ('n', 'r'): 44,
 ('z', 'm'): 35,
 ('v', 'o'): 153,
 ('l', 'm'): 60,
 ('o', 'x'): 45,
 ('d', '<E>'): 516,
 ('i', 'u'): 109,
 ('v', '<E>'): 88,
 ('f', 'f'): 44,
 ('b', 'o'): 105,
 ('e', 'k'): 178,
 ('c', 'r'): 76,
 ('d', 'g'): 25,
 ('r', 'c'): 99,
 ('r', 'h'): 121,
 ('n', 'k'): 58,
 ('h', 'u'): 166,
 ('d', 's'): 29,
 ('a', 'x'): 182,
 ('y', 'c'): 115,
 ('e', 'w'): 50,
 ('v', 'k'): 3,
 ('z', 'h'): 43,
 ('w', 'h'): 23,
 ('t', 'n'): 22,
 ('x', 'l'): 39,
 ('g', 'u'): 85,
 ('u', 'a'): 163,
 ('u', 'p'): 16,
 ('u', 'g'): 47,
 ('d', 'u'): 92,
 ('l', 'c'): 25,
 ('r', 'b'): 41,
 ('a', 'q'): 60,
 ('b', '<E>'): 114,
 ('g', 'y'): 31,
 ('y', 'p'): 15,
 ('p', 't'): 17,
 ('e', 'z'): 181,
 ('z', 'r'): 32,
 ('f', 'l'): 20,
 ('o', '<E>'): 855,
 ('o', 'b'): 140,
 ('u', 'z'): 45,
 ('z', '<E>'): 160,
 ('i', 'q'): 52,
 ('y', 'v'): 106,
 ('n', 'v'): 55,
 ('d', 'h'): 118,
 ('g', 'd'): 19,
 ('t', 's'): 35,
 ('n', 'h'): 26,
 ('y', 'j'): 23,
 ('k', 'r'): 109,
 ('z', 'b'): 4,
 ('g', '<E>'): 108,
 ('a', 'j'): 175,
 ('r', 'j'): 25,
 ('m', 'p'): 38,
 ('p', 'b'): 2,
 ('y', 'o'): 271,
 ('z', 'y'): 147,
 ('p', 'l'): 16,
 ('l', 'k'): 24,
 ('i', 'j'): 76,
 ('x', 'e'): 36,
 ('y', 'u'): 141,
 ('l', 'n'): 14,
 ('u', 'x'): 34,
 ('i', 'h'): 95,
 ('w', 's'): 20,
 ('k', 's'): 95,
 ('m', 'u'): 139,
 ('y', 'k'): 86,
 ('e', 'f'): 82,
 ('k', '<E>'): 363,
 ('y', 'm'): 148,
 ('z', 'z'): 45,
 ('m', 'd'): 24,
 ('s', 'r'): 55,
 ('e', 'u'): 69,
 ('l', 'h'): 19,
 ('a', 'f'): 134,
 ('r', 'w'): 21,
 ('n', 'u'): 96,
 ('v', 'r'): 48,
 ('m', 's'): 35,
 ('<S>', 'u'): 78,
 ('f', 's'): 6,
 ('y', 'b'): 27,
 ('x', 'o'): 41,
 ('g', 's'): 30,
 ('x', 'y'): 30,
 ('w', 'n'): 58,
 ('j', 'h'): 45,
 ('f', 'n'): 4,
 ('n', 'j'): 44,
 ('r', 'v'): 80,
 ('n', 'm'): 19,
 ('t', 'c'): 17,
 ('s', 'w'): 24,
 ('k', 't'): 17,
 ('f', 't'): 18,
 ('x', 't'): 70,
 ('u', 'v'): 37,
 ('k', 'k'): 20,
 ('s', 'n'): 24,
 ('u', '<E>'): 155,
 ('j', 'r'): 11,
 ('y', 'x'): 28,
 ('h', 'm'): 117,
 ('e', 'q'): 14,
 ('u', 'o'): 10,
 ('f', '<E>'): 80,
 ('h', 'z'): 20,
 ('h', 'k'): 29,
 ('y', 'g'): 30,
 ('q', 'r'): 1,
 ('v', 'n'): 8,
 ('s', 'd'): 9,
 ('y', 'i'): 192,
 ('n', 'w'): 11,
 ('d', 'v'): 17,
 ('h', 'v'): 39,
 ('x', 'w'): 3,
 ('o', 'z'): 54,
 ('k', 'u'): 50,
 ('u', 'h'): 58,
 ('k', 'n'): 26,
 ('s', 'b'): 21,
 ('i', 'i'): 82,
 ('y', 'y'): 23,
 ('r', 'z'): 23,
 ('l', 'g'): 6,
 ('l', 'p'): 15,
 ('p', '<E>'): 33,
 ('b', 'u'): 45,
 ('f', 'u'): 10,
 ('b', 'h'): 41,
 ('f', 'y'): 14,
 ('u', 'w'): 86,
 ('x', 'u'): 5,
 ('q', '<E>'): 28,
 ('l', 'r'): 18,
 ('m', 'h'): 5,
 ('l', 'w'): 16,
 ('j', '<E>'): 71,
 ('s', 'v'): 14,
 ('m', 'l'): 5,
 ('n', 'f'): 11,
 ('u', 'j'): 14,
 ('f', 'o'): 60,
 ('j', 'l'): 9,
 ('t', 'g'): 2,
 ('j', 'm'): 5,
 ('v', 'v'): 7,
 ('p', 's'): 16,
 ('t', 'w'): 11,
 ('x', 'c'): 4,
 ('u', 'k'): 93,
 ('v', 'l'): 14,
 ('h', 'd'): 24,
 ('l', 'z'): 10,
 ('k', 'w'): 34,
 ('n', 'b'): 8,
 ('q', 's'): 2,
 ('i', 'w'): 8,
 ('c', 's'): 5,
 ('h', 's'): 31,
 ('m', 't'): 4,
 ('h', 'w'): 10,
 ('x', 'x'): 38,
 ('t', 'x'): 2,
 ('d', 'z'): 1,
 ('x', 'z'): 19,
 ('t', 'm'): 4,
 ('t', 'j'): 3,
 ('u', 'q'): 10,
 ('q', 'a'): 13,
 ('f', 'k'): 2,
 ('z', 'n'): 4,
 ('l', 'j'): 6,
 ('j', 'w'): 6,
 ('v', 'u'): 7,
 ('c', 'j'): 3,
 ('h', 'b'): 8,
 ('z', 't'): 4,
 ('p', 'u'): 4,
 ('m', 'z'): 11,
 ('x', 's'): 31,
 ('b', 't'): 2,
 ('u', 'y'): 13,
 ('d', 'j'): 9,
 ('j', 's'): 7,
 ('w', 'u'): 25,
 ('o', 'j'): 16,
 ('b', 's'): 8,
 ('d', 'w'): 23,
 ('w', 'o'): 36,
 ('j', 'n'): 2,
 ('w', 't'): 8,
 ('l', 'f'): 22,
 ('d', 'm'): 30,
 ('p', 'j'): 1,
 ('j', 'y'): 10,
 ('y', 'f'): 12,
 ('q', 'i'): 13,
 ('j', 'v'): 5,
 ('q', 'l'): 1,
 ('s', 'z'): 10,
 ('k', 'm'): 9,
 ('w', 'l'): 13,
 ('p', 'f'): 1,
 ('q', 'w'): 3,
 ('n', 'x'): 6,
 ('k', 'c'): 2,
 ('t', 'v'): 15,
 ('c', 'u'): 35,
 ('z', 'k'): 2,
 ('c', 'z'): 4,
 ('y', 'q'): 6,
 ('y', 'h'): 22,
 ('r', 'f'): 9,
 ('s', 'j'): 2,
 ('h', 'j'): 9,
 ('g', 'b'): 3,
 ('u', 'f'): 19,
 ('s', 'f'): 2,
 ('q', 'e'): 1,
 ('b', 'c'): 1,
 ('c', 'd'): 1,
 ('z', 'j'): 2,
 ('n', 'q'): 2,
 ('m', 'f'): 1,
 ('p', 'n'): 1,
 ('f', 'z'): 2,
 ('b', 'n'): 4,
 ('w', 'd'): 8,
 ('w', 'b'): 1,
 ('b', 'd'): 65,
 ('z', 's'): 4,
 ('p', 'c'): 1,
 ('h', 'g'): 2,
 ('m', 'j'): 7,
 ('w', 'w'): 2,
 ('k', 'j'): 2,
 ('h', 'p'): 1,
 ('j', 'k'): 2,
 ('o', 'q'): 3,
 ('f', 'w'): 4,
 ('f', 'h'): 1,
 ('w', 'm'): 2,
 ('b', 'j'): 1,
 ('r', 'q'): 16,
 ('z', 'c'): 2,
 ('z', 'v'): 2,
 ('f', 'g'): 1,
 ('n', 'p'): 5,
 ('z', 'g'): 1,
 ('d', 't'): 4,
 ('w', 'f'): 2,
 ('d', 'f'): 5,
 ('w', 'k'): 6,
 ('q', 'm'): 2,
 ('k', 'z'): 2,
 ('j', 'j'): 2,
 ('c', 'p'): 1,
 ('p', 'k'): 1,
 ('p', 'm'): 1,
 ('j', 'd'): 4,
 ('r', 'x'): 3,
 ('x', 'n'): 1,
 ('d', 'c'): 3,
 ('g', 'j'): 3,
 ('x', 'f'): 3,
 ('j', 'c'): 4,
 ('s', 'q'): 1,
 ('k', 'f'): 1,
 ('z', 'p'): 2,
 ('j', 't'): 2,
 ('k', 'b'): 2,
 ('m', 'k'): 1,
 ('m', 'w'): 2,
 ('x', 'h'): 1,
 ('h', 'f'): 2,
 ('x', 'd'): 5,
 ('y', 'w'): 4,
 ('z', 'w'): 3,
 ('d', 'k'): 3,
 ('c', 'g'): 2,
 ('u', 'u'): 3,
 ('t', 'f'): 2,
 ('g', 'm'): 6,
 ('m', 'v'): 3,
 ('c', 'x'): 3,
 ('h', 'c'): 2,
 ('g', 'f'): 1,
 ('q', 'o'): 2,
 ('l', 'q'): 3,
 ('v', 'b'): 1,
 ('j', 'p'): 1,
 ('k', 'd'): 2,
 ('g', 'z'): 1,
 ('v', 'd'): 1,
 ('d', 'b'): 1,
 ('v', 'h'): 1,
 ('k', 'v'): 2,
 ('h', 'h'): 1,
 ('s', 'g'): 2,
 ('g', 'v'): 1,
 ('d', 'q'): 1,
 ('x', 'b'): 1,
 ('w', 'z'): 1,
 ('h', 'q'): 1,
 ('j', 'b'): 1,
 ('z', 'd'): 2,
 ('x', 'm'): 1,
 ('w', 'g'): 1,
 ('t', 'b'): 1,
 ('z', 'x'): 1}
b.items()   #We know 'b' is a dictionary. So .items() basically gives us that it's values in a (Key, Value) set
Output
dict_items([(('<S>', 'e'), 1531), (('e', 'm'), 769), (('m', 'm'), 168), (('m', 'a'), 2590), (('a', '<E>'), 6640), (('<S>', 'o'), 394), (('o', 'l'), 619), (('l', 'i'), 2480), (('i', 'v'), 269), (('v', 'i'), 911), (('i', 'a'), 2445), (('<S>', 'a'), 4410), (('a', 'v'), 834), (('v', 'a'), 642), (('<S>', 'i'), 591), (('i', 's'), 1316), (('s', 'a'), 1201), (('a', 'b'), 541), (('b', 'e'), 655), (('e', 'l'), 3248), (('l', 'l'), 1345), (('l', 'a'), 2623), (('<S>', 's'), 2055), (('s', 'o'), 531), (('o', 'p'), 95), (('p', 'h'), 204), (('h', 'i'), 729), (('<S>', 'c'), 1542), (('c', 'h'), 664), (('h', 'a'), 2244), (('a', 'r'), 3264), (('r', 'l'), 413), (('l', 'o'), 692), (('o', 't'), 118), (('t', 't'), 374), (('t', 'e'), 716), (('e', '<E>'), 3983), (('<S>', 'm'), 2538), (('m', 'i'), 1256), (('a', 'm'), 1634), (('m', 'e'), 818), (('<S>', 'h'), 874), (('r', 'p'), 14), (('p', 'e'), 197), (('e', 'r'), 1958), (('r', '<E>'), 1377), (('e', 'v'), 463), (('v', 'e'), 568), (('l', 'y'), 1588), (('y', 'n'), 1826), (('n', '<E>'), 6763), (('b', 'i'), 217), (('i', 'g'), 428), (('g', 'a'), 330), (('a', 'i'), 1650), (('i', 'l'), 1345), (('l', '<E>'), 1314), (('y', '<E>'), 2007), (('i', 'z'), 277), (('z', 'a'), 860), (('e', 't'), 580), (('t', 'h'), 647), (('h', '<E>'), 2409), (('r', 'y'), 773), (('o', 'f'), 34), (('f', 'i'), 160), (('c', 'a'), 815), (('r', 'i'), 3033), (('s', 'c'), 60), (('l', 'e'), 2921), (('t', '<E>'), 483), (('<S>', 'v'), 376), (('i', 'c'), 509), (('c', 't'), 35), (('t', 'o'), 667), (('o', 'r'), 1059), (('a', 'd'), 1042), (('d', 'i'), 674), (('o', 'n'), 2411), (('<S>', 'l'), 1572), (('l', 'u'), 324), (('u', 'n'), 275), (('n', 'a'), 2977), (('<S>', 'g'), 669), (('g', 'r'), 201), (('r', 'a'), 2356), (('a', 'c'), 470), (('c', 'e'), 551), (('h', 'l'), 185), (('o', 'e'), 132), (('<S>', 'p'), 515), (('e', 'n'), 2675), (('n', 'e'), 1359), (('a', 'y'), 2050), (('y', 'l'), 1104), (('<S>', 'r'), 1639), (('e', 'y'), 1070), (('<S>', 'z'), 929), (('z', 'o'), 110), (('<S>', 'n'), 1146), (('n', 'o'), 496), (('e', 'a'), 679), (('a', 'n'), 5438), (('n', 'n'), 1906), (('a', 'h'), 2332), (('d', 'd'), 149), (('a', 'u'), 381), (('u', 'b'), 103), (('b', 'r'), 842), (('r', 'e'), 1697), (('i', 'e'), 1653), (('s', 't'), 765), (('a', 't'), 687), (('t', 'a'), 1027), (('a', 'l'), 2528), (('a', 'z'), 435), (('z', 'e'), 373), (('i', 'o'), 588), (('u', 'r'), 414), (('r', 'o'), 869), (('u', 'd'), 136), (('d', 'r'), 424), (('<S>', 'b'), 1306), (('o', 'o'), 115), (('o', 'k'), 68), (('k', 'l'), 139), (('c', 'l'), 116), (('i', 'r'), 849), (('s', 'k'), 82), (('k', 'y'), 379), (('u', 'c'), 103), (('c', 'y'), 104), (('p', 'a'), 209), (('s', 'l'), 279), (('i', 'n'), 2126), (('o', 'v'), 176), (('g', 'e'), 334), (('e', 's'), 861), (('s', 'i'), 684), (('s', '<E>'), 1169), (('<S>', 'k'), 2963), (('k', 'e'), 895), (('e', 'd'), 384), (('d', 'y'), 317), (('n', 't'), 443), (('y', 'a'), 2143), (('<S>', 'w'), 307), (('w', 'i'), 148), (('o', 'w'), 114), (('w', '<E>'), 51), (('k', 'i'), 509), (('n', 's'), 278), (('a', 'o'), 63), (('o', 'm'), 261), (('i', '<E>'), 2489), (('a', 'a'), 556), (('i', 'y'), 779), (('d', 'e'), 1283), (('c', 'o'), 380), (('r', 'u'), 252), (('b', 'y'), 83), (('s', 'e'), 884), (('n', 'i'), 1725), (('i', 't'), 541), (('t', 'y'), 341), (('u', 't'), 82), (('t', 'u'), 78), (('u', 'm'), 154), (('m', 'n'), 20), (('g', 'i'), 190), (('t', 'i'), 532), (('<S>', 'q'), 92), (('q', 'u'), 206), (('u', 'i'), 121), (('a', 'e'), 692), (('e', 'h'), 152), (('v', 'y'), 121), (('p', 'i'), 61), (('i', 'p'), 53), (('y', 'd'), 272), (('e', 'x'), 132), (('x', 'a'), 103), (('<S>', 'j'), 2422), (('j', 'o'), 479), (('o', 's'), 504), (('e', 'p'), 83), (('j', 'u'), 202), (('u', 'l'), 301), (('<S>', 'd'), 1690), (('k', 'a'), 1731), (('e', 'e'), 1271), (('y', 't'), 104), (('d', 'l'), 60), (('c', 'k'), 316), (('n', 'z'), 145), (('z', 'i'), 364), (('a', 'g'), 168), (('d', 'a'), 1303), (('j', 'a'), 1473), (('h', 'e'), 674), (('<S>', 'x'), 134), (('x', 'i'), 102), (('i', 'm'), 427), (('e', 'i'), 818), (('<S>', 't'), 1308), (('<S>', 'f'), 417), (('f', 'a'), 242), (('n', 'd'), 704), (('r', 'g'), 76), (('a', 's'), 1118), (('s', 'h'), 1285), (('b', 'a'), 321), (('k', 'h'), 307), (('s', 'm'), 90), (('o', 'd'), 190), (('r', 's'), 190), (('g', 'h'), 360), (('s', 'y'), 215), (('y', 's'), 401), (('s', 's'), 461), (('e', 'c'), 153), (('c', 'i'), 271), (('m', 'o'), 452), (('r', 'k'), 90), (('n', 'l'), 195), (('d', 'n'), 31), (('r', 'd'), 187), (('o', 'i'), 69), (('t', 'r'), 352), (('m', 'b'), 112), (('r', 'm'), 162), (('n', 'y'), 465), (('d', 'o'), 378), (('o', 'a'), 149), (('o', 'c'), 114), (('m', 'y'), 287), (('s', 'u'), 185), (('m', 'c'), 51), (('p', 'r'), 151), (('o', 'u'), 275), (('r', 'n'), 140), (('w', 'a'), 280), (('e', 'b'), 121), (('c', 'c'), 42), (('a', 'w'), 161), (('w', 'y'), 73), (('y', 'e'), 301), (('e', 'o'), 269), (('a', 'k'), 568), (('n', 'g'), 273), (('k', 'o'), 344), (('b', 'l'), 103), (('h', 'o'), 287), (('e', 'g'), 125), (('f', 'r'), 114), (('s', 'p'), 51), (('l', 's'), 94), (('y', 'z'), 78), (('g', 'g'), 25), (('z', 'u'), 73), (('i', 'd'), 440), (('m', '<E>'), 516), (('o', 'g'), 44), (('j', 'e'), 440), (('g', 'n'), 27), (('y', 'r'), 291), (('c', '<E>'), 97), (('c', 'q'), 11), (('u', 'e'), 169), (('i', 'f'), 101), (('f', 'e'), 123), (('i', 'x'), 89), (('x', '<E>'), 164), (('o', 'y'), 103), (('g', 'o'), 83), (('g', 't'), 31), (('l', 't'), 77), (('g', 'w'), 26), (('w', 'e'), 149), (('l', 'd'), 138), (('a', 'p'), 82), (('h', 'n'), 138), (('t', 'l'), 134), (('m', 'r'), 97), (('n', 'c'), 213), (('l', 'b'), 52), (('i', 'k'), 445), (('<S>', 'y'), 535), (('t', 'z'), 105), (('h', 'r'), 204), (('j', 'i'), 119), (('h', 't'), 71), (('r', 'r'), 425), (('z', 'l'), 123), (('w', 'r'), 22), (('b', 'b'), 38), (('r', 't'), 208), (('l', 'v'), 72), (('e', 'j'), 55), (('o', 'h'), 171), (('u', 's'), 474), (('i', 'b'), 110), (('g', 'l'), 32), (('h', 'y'), 213), (('p', 'o'), 59), (('p', 'p'), 39), (('p', 'y'), 12), (('n', 'r'), 44), (('z', 'm'), 35), (('v', 'o'), 153), (('l', 'm'), 60), (('o', 'x'), 45), (('d', '<E>'), 516), (('i', 'u'), 109), (('v', '<E>'), 88), (('f', 'f'), 44), (('b', 'o'), 105), (('e', 'k'), 178), (('c', 'r'), 76), (('d', 'g'), 25), (('r', 'c'), 99), (('r', 'h'), 121), (('n', 'k'), 58), (('h', 'u'), 166), (('d', 's'), 29), (('a', 'x'), 182), (('y', 'c'), 115), (('e', 'w'), 50), (('v', 'k'), 3), (('z', 'h'), 43), (('w', 'h'), 23), (('t', 'n'), 22), (('x', 'l'), 39), (('g', 'u'), 85), (('u', 'a'), 163), (('u', 'p'), 16), (('u', 'g'), 47), (('d', 'u'), 92), (('l', 'c'), 25), (('r', 'b'), 41), (('a', 'q'), 60), (('b', '<E>'), 114), (('g', 'y'), 31), (('y', 'p'), 15), (('p', 't'), 17), (('e', 'z'), 181), (('z', 'r'), 32), (('f', 'l'), 20), (('o', '<E>'), 855), (('o', 'b'), 140), (('u', 'z'), 45), (('z', '<E>'), 160), (('i', 'q'), 52), (('y', 'v'), 106), (('n', 'v'), 55), (('d', 'h'), 118), (('g', 'd'), 19), (('t', 's'), 35), (('n', 'h'), 26), (('y', 'j'), 23), (('k', 'r'), 109), (('z', 'b'), 4), (('g', '<E>'), 108), (('a', 'j'), 175), (('r', 'j'), 25), (('m', 'p'), 38), (('p', 'b'), 2), (('y', 'o'), 271), (('z', 'y'), 147), (('p', 'l'), 16), (('l', 'k'), 24), (('i', 'j'), 76), (('x', 'e'), 36), (('y', 'u'), 141), (('l', 'n'), 14), (('u', 'x'), 34), (('i', 'h'), 95), (('w', 's'), 20), (('k', 's'), 95), (('m', 'u'), 139), (('y', 'k'), 86), (('e', 'f'), 82), (('k', '<E>'), 363), (('y', 'm'), 148), (('z', 'z'), 45), (('m', 'd'), 24), (('s', 'r'), 55), (('e', 'u'), 69), (('l', 'h'), 19), (('a', 'f'), 134), (('r', 'w'), 21), (('n', 'u'), 96), (('v', 'r'), 48), (('m', 's'), 35), (('<S>', 'u'), 78), (('f', 's'), 6), (('y', 'b'), 27), (('x', 'o'), 41), (('g', 's'), 30), (('x', 'y'), 30), (('w', 'n'), 58), (('j', 'h'), 45), (('f', 'n'), 4), (('n', 'j'), 44), (('r', 'v'), 80), (('n', 'm'), 19), (('t', 'c'), 17), (('s', 'w'), 24), (('k', 't'), 17), (('f', 't'), 18), (('x', 't'), 70), (('u', 'v'), 37), (('k', 'k'), 20), (('s', 'n'), 24), (('u', '<E>'), 155), (('j', 'r'), 11), (('y', 'x'), 28), (('h', 'm'), 117), (('e', 'q'), 14), (('u', 'o'), 10), (('f', '<E>'), 80), (('h', 'z'), 20), (('h', 'k'), 29), (('y', 'g'), 30), (('q', 'r'), 1), (('v', 'n'), 8), (('s', 'd'), 9), (('y', 'i'), 192), (('n', 'w'), 11), (('d', 'v'), 17), (('h', 'v'), 39), (('x', 'w'), 3), (('o', 'z'), 54), (('k', 'u'), 50), (('u', 'h'), 58), (('k', 'n'), 26), (('s', 'b'), 21), (('i', 'i'), 82), (('y', 'y'), 23), (('r', 'z'), 23), (('l', 'g'), 6), (('l', 'p'), 15), (('p', '<E>'), 33), (('b', 'u'), 45), (('f', 'u'), 10), (('b', 'h'), 41), (('f', 'y'), 14), (('u', 'w'), 86), (('x', 'u'), 5), (('q', '<E>'), 28), (('l', 'r'), 18), (('m', 'h'), 5), (('l', 'w'), 16), (('j', '<E>'), 71), (('s', 'v'), 14), (('m', 'l'), 5), (('n', 'f'), 11), (('u', 'j'), 14), (('f', 'o'), 60), (('j', 'l'), 9), (('t', 'g'), 2), (('j', 'm'), 5), (('v', 'v'), 7), (('p', 's'), 16), (('t', 'w'), 11), (('x', 'c'), 4), (('u', 'k'), 93), (('v', 'l'), 14), (('h', 'd'), 24), (('l', 'z'), 10), (('k', 'w'), 34), (('n', 'b'), 8), (('q', 's'), 2), (('i', 'w'), 8), (('c', 's'), 5), (('h', 's'), 31), (('m', 't'), 4), (('h', 'w'), 10), (('x', 'x'), 38), (('t', 'x'), 2), (('d', 'z'), 1), (('x', 'z'), 19), (('t', 'm'), 4), (('t', 'j'), 3), (('u', 'q'), 10), (('q', 'a'), 13), (('f', 'k'), 2), (('z', 'n'), 4), (('l', 'j'), 6), (('j', 'w'), 6), (('v', 'u'), 7), (('c', 'j'), 3), (('h', 'b'), 8), (('z', 't'), 4), (('p', 'u'), 4), (('m', 'z'), 11), (('x', 's'), 31), (('b', 't'), 2), (('u', 'y'), 13), (('d', 'j'), 9), (('j', 's'), 7), (('w', 'u'), 25), (('o', 'j'), 16), (('b', 's'), 8), (('d', 'w'), 23), (('w', 'o'), 36), (('j', 'n'), 2), (('w', 't'), 8), (('l', 'f'), 22), (('d', 'm'), 30), (('p', 'j'), 1), (('j', 'y'), 10), (('y', 'f'), 12), (('q', 'i'), 13), (('j', 'v'), 5), (('q', 'l'), 1), (('s', 'z'), 10), (('k', 'm'), 9), (('w', 'l'), 13), (('p', 'f'), 1), (('q', 'w'), 3), (('n', 'x'), 6), (('k', 'c'), 2), (('t', 'v'), 15), (('c', 'u'), 35), (('z', 'k'), 2), (('c', 'z'), 4), (('y', 'q'), 6), (('y', 'h'), 22), (('r', 'f'), 9), (('s', 'j'), 2), (('h', 'j'), 9), (('g', 'b'), 3), (('u', 'f'), 19), (('s', 'f'), 2), (('q', 'e'), 1), (('b', 'c'), 1), (('c', 'd'), 1), (('z', 'j'), 2), (('n', 'q'), 2), (('m', 'f'), 1), (('p', 'n'), 1), (('f', 'z'), 2), (('b', 'n'), 4), (('w', 'd'), 8), (('w', 'b'), 1), (('b', 'd'), 65), (('z', 's'), 4), (('p', 'c'), 1), (('h', 'g'), 2), (('m', 'j'), 7), (('w', 'w'), 2), (('k', 'j'), 2), (('h', 'p'), 1), (('j', 'k'), 2), (('o', 'q'), 3), (('f', 'w'), 4), (('f', 'h'), 1), (('w', 'm'), 2), (('b', 'j'), 1), (('r', 'q'), 16), (('z', 'c'), 2), (('z', 'v'), 2), (('f', 'g'), 1), (('n', 'p'), 5), (('z', 'g'), 1), (('d', 't'), 4), (('w', 'f'), 2), (('d', 'f'), 5), (('w', 'k'), 6), (('q', 'm'), 2), (('k', 'z'), 2), (('j', 'j'), 2), (('c', 'p'), 1), (('p', 'k'), 1), (('p', 'm'), 1), (('j', 'd'), 4), (('r', 'x'), 3), (('x', 'n'), 1), (('d', 'c'), 3), (('g', 'j'), 3), (('x', 'f'), 3), (('j', 'c'), 4), (('s', 'q'), 1), (('k', 'f'), 1), (('z', 'p'), 2), (('j', 't'), 2), (('k', 'b'), 2), (('m', 'k'), 1), (('m', 'w'), 2), (('x', 'h'), 1), (('h', 'f'), 2), (('x', 'd'), 5), (('y', 'w'), 4), (('z', 'w'), 3), (('d', 'k'), 3), (('c', 'g'), 2), (('u', 'u'), 3), (('t', 'f'), 2), (('g', 'm'), 6), (('m', 'v'), 3), (('c', 'x'), 3), (('h', 'c'), 2), (('g', 'f'), 1), (('q', 'o'), 2), (('l', 'q'), 3), (('v', 'b'), 1), (('j', 'p'), 1), (('k', 'd'), 2), (('g', 'z'), 1), (('v', 'd'), 1), (('d', 'b'), 1), (('v', 'h'), 1), (('k', 'v'), 2), (('h', 'h'), 1), (('s', 'g'), 2), (('g', 'v'), 1), (('d', 'q'), 1), (('x', 'b'), 1), (('w', 'z'), 1), (('h', 'q'), 1), (('j', 'b'), 1), (('z', 'd'), 2), (('x', 'm'), 1), (('w', 'g'), 1), (('t', 'b'), 1), (('z', 'x'), 1)])
sorted(b.items()) #Now this by default sorts the values based on the Key
Output
[(('<S>', 'a'), 4410),
 (('<S>', 'b'), 1306),
 (('<S>', 'c'), 1542),
 (('<S>', 'd'), 1690),
 (('<S>', 'e'), 1531),
 (('<S>', 'f'), 417),
 (('<S>', 'g'), 669),
 (('<S>', 'h'), 874),
 (('<S>', 'i'), 591),
 (('<S>', 'j'), 2422),
 (('<S>', 'k'), 2963),
 (('<S>', 'l'), 1572),
 (('<S>', 'm'), 2538),
 (('<S>', 'n'), 1146),
 (('<S>', 'o'), 394),
 (('<S>', 'p'), 515),
 (('<S>', 'q'), 92),
 (('<S>', 'r'), 1639),
 (('<S>', 's'), 2055),
 (('<S>', 't'), 1308),
 (('<S>', 'u'), 78),
 (('<S>', 'v'), 376),
 (('<S>', 'w'), 307),
 (('<S>', 'x'), 134),
 (('<S>', 'y'), 535),
 (('<S>', 'z'), 929),
 (('a', '<E>'), 6640),
 (('a', 'a'), 556),
 (('a', 'b'), 541),
 (('a', 'c'), 470),
 (('a', 'd'), 1042),
 (('a', 'e'), 692),
 (('a', 'f'), 134),
 (('a', 'g'), 168),
 (('a', 'h'), 2332),
 (('a', 'i'), 1650),
 (('a', 'j'), 175),
 (('a', 'k'), 568),
 (('a', 'l'), 2528),
 (('a', 'm'), 1634),
 (('a', 'n'), 5438),
 (('a', 'o'), 63),
 (('a', 'p'), 82),
 (('a', 'q'), 60),
 (('a', 'r'), 3264),
 (('a', 's'), 1118),
 (('a', 't'), 687),
 (('a', 'u'), 381),
 (('a', 'v'), 834),
 (('a', 'w'), 161),
 (('a', 'x'), 182),
 (('a', 'y'), 2050),
 (('a', 'z'), 435),
 (('b', '<E>'), 114),
 (('b', 'a'), 321),
 (('b', 'b'), 38),
 (('b', 'c'), 1),
 (('b', 'd'), 65),
 (('b', 'e'), 655),
 (('b', 'h'), 41),
 (('b', 'i'), 217),
 (('b', 'j'), 1),
 (('b', 'l'), 103),
 (('b', 'n'), 4),
 (('b', 'o'), 105),
 (('b', 'r'), 842),
 (('b', 's'), 8),
 (('b', 't'), 2),
 (('b', 'u'), 45),
 (('b', 'y'), 83),
 (('c', '<E>'), 97),
 (('c', 'a'), 815),
 (('c', 'c'), 42),
 (('c', 'd'), 1),
 (('c', 'e'), 551),
 (('c', 'g'), 2),
 (('c', 'h'), 664),
 (('c', 'i'), 271),
 (('c', 'j'), 3),
 (('c', 'k'), 316),
 (('c', 'l'), 116),
 (('c', 'o'), 380),
 (('c', 'p'), 1),
 (('c', 'q'), 11),
 (('c', 'r'), 76),
 (('c', 's'), 5),
 (('c', 't'), 35),
 (('c', 'u'), 35),
 (('c', 'x'), 3),
 (('c', 'y'), 104),
 (('c', 'z'), 4),
 (('d', '<E>'), 516),
 (('d', 'a'), 1303),
 (('d', 'b'), 1),
 (('d', 'c'), 3),
 (('d', 'd'), 149),
 (('d', 'e'), 1283),
 (('d', 'f'), 5),
 (('d', 'g'), 25),
 (('d', 'h'), 118),
 (('d', 'i'), 674),
 (('d', 'j'), 9),
 (('d', 'k'), 3),
 (('d', 'l'), 60),
 (('d', 'm'), 30),
 (('d', 'n'), 31),
 (('d', 'o'), 378),
 (('d', 'q'), 1),
 (('d', 'r'), 424),
 (('d', 's'), 29),
 (('d', 't'), 4),
 (('d', 'u'), 92),
 (('d', 'v'), 17),
 (('d', 'w'), 23),
 (('d', 'y'), 317),
 (('d', 'z'), 1),
 (('e', '<E>'), 3983),
 (('e', 'a'), 679),
 (('e', 'b'), 121),
 (('e', 'c'), 153),
 (('e', 'd'), 384),
 (('e', 'e'), 1271),
 (('e', 'f'), 82),
 (('e', 'g'), 125),
 (('e', 'h'), 152),
 (('e', 'i'), 818),
 (('e', 'j'), 55),
 (('e', 'k'), 178),
 (('e', 'l'), 3248),
 (('e', 'm'), 769),
 (('e', 'n'), 2675),
 (('e', 'o'), 269),
 (('e', 'p'), 83),
 (('e', 'q'), 14),
 (('e', 'r'), 1958),
 (('e', 's'), 861),
 (('e', 't'), 580),
 (('e', 'u'), 69),
 (('e', 'v'), 463),
 (('e', 'w'), 50),
 (('e', 'x'), 132),
 (('e', 'y'), 1070),
 (('e', 'z'), 181),
 (('f', '<E>'), 80),
 (('f', 'a'), 242),
 (('f', 'e'), 123),
 (('f', 'f'), 44),
 (('f', 'g'), 1),
 (('f', 'h'), 1),
 (('f', 'i'), 160),
 (('f', 'k'), 2),
 (('f', 'l'), 20),
 (('f', 'n'), 4),
 (('f', 'o'), 60),
 (('f', 'r'), 114),
 (('f', 's'), 6),
 (('f', 't'), 18),
 (('f', 'u'), 10),
 (('f', 'w'), 4),
 (('f', 'y'), 14),
 (('f', 'z'), 2),
 (('g', '<E>'), 108),
 (('g', 'a'), 330),
 (('g', 'b'), 3),
 (('g', 'd'), 19),
 (('g', 'e'), 334),
 (('g', 'f'), 1),
 (('g', 'g'), 25),
 (('g', 'h'), 360),
 (('g', 'i'), 190),
 (('g', 'j'), 3),
 (('g', 'l'), 32),
 (('g', 'm'), 6),
 (('g', 'n'), 27),
 (('g', 'o'), 83),
 (('g', 'r'), 201),
 (('g', 's'), 30),
 (('g', 't'), 31),
 (('g', 'u'), 85),
 (('g', 'v'), 1),
 (('g', 'w'), 26),
 (('g', 'y'), 31),
 (('g', 'z'), 1),
 (('h', '<E>'), 2409),
 (('h', 'a'), 2244),
 (('h', 'b'), 8),
 (('h', 'c'), 2),
 (('h', 'd'), 24),
 (('h', 'e'), 674),
 (('h', 'f'), 2),
 (('h', 'g'), 2),
 (('h', 'h'), 1),
 (('h', 'i'), 729),
 (('h', 'j'), 9),
 (('h', 'k'), 29),
 (('h', 'l'), 185),
 (('h', 'm'), 117),
 (('h', 'n'), 138),
 (('h', 'o'), 287),
 (('h', 'p'), 1),
 (('h', 'q'), 1),
 (('h', 'r'), 204),
 (('h', 's'), 31),
 (('h', 't'), 71),
 (('h', 'u'), 166),
 (('h', 'v'), 39),
 (('h', 'w'), 10),
 (('h', 'y'), 213),
 (('h', 'z'), 20),
 (('i', '<E>'), 2489),
 (('i', 'a'), 2445),
 (('i', 'b'), 110),
 (('i', 'c'), 509),
 (('i', 'd'), 440),
 (('i', 'e'), 1653),
 (('i', 'f'), 101),
 (('i', 'g'), 428),
 (('i', 'h'), 95),
 (('i', 'i'), 82),
 (('i', 'j'), 76),
 (('i', 'k'), 445),
 (('i', 'l'), 1345),
 (('i', 'm'), 427),
 (('i', 'n'), 2126),
 (('i', 'o'), 588),
 (('i', 'p'), 53),
 (('i', 'q'), 52),
 (('i', 'r'), 849),
 (('i', 's'), 1316),
 (('i', 't'), 541),
 (('i', 'u'), 109),
 (('i', 'v'), 269),
 (('i', 'w'), 8),
 (('i', 'x'), 89),
 (('i', 'y'), 779),
 (('i', 'z'), 277),
 (('j', '<E>'), 71),
 (('j', 'a'), 1473),
 (('j', 'b'), 1),
 (('j', 'c'), 4),
 (('j', 'd'), 4),
 (('j', 'e'), 440),
 (('j', 'h'), 45),
 (('j', 'i'), 119),
 (('j', 'j'), 2),
 (('j', 'k'), 2),
 (('j', 'l'), 9),
 (('j', 'm'), 5),
 (('j', 'n'), 2),
 (('j', 'o'), 479),
 (('j', 'p'), 1),
 (('j', 'r'), 11),
 (('j', 's'), 7),
 (('j', 't'), 2),
 (('j', 'u'), 202),
 (('j', 'v'), 5),
 (('j', 'w'), 6),
 (('j', 'y'), 10),
 (('k', '<E>'), 363),
 (('k', 'a'), 1731),
 (('k', 'b'), 2),
 (('k', 'c'), 2),
 (('k', 'd'), 2),
 (('k', 'e'), 895),
 (('k', 'f'), 1),
 (('k', 'h'), 307),
 (('k', 'i'), 509),
 (('k', 'j'), 2),
 (('k', 'k'), 20),
 (('k', 'l'), 139),
 (('k', 'm'), 9),
 (('k', 'n'), 26),
 (('k', 'o'), 344),
 (('k', 'r'), 109),
 (('k', 's'), 95),
 (('k', 't'), 17),
 (('k', 'u'), 50),
 (('k', 'v'), 2),
 (('k', 'w'), 34),
 (('k', 'y'), 379),
 (('k', 'z'), 2),
 (('l', '<E>'), 1314),
 (('l', 'a'), 2623),
 (('l', 'b'), 52),
 (('l', 'c'), 25),
 (('l', 'd'), 138),
 (('l', 'e'), 2921),
 (('l', 'f'), 22),
 (('l', 'g'), 6),
 (('l', 'h'), 19),
 (('l', 'i'), 2480),
 (('l', 'j'), 6),
 (('l', 'k'), 24),
 (('l', 'l'), 1345),
 (('l', 'm'), 60),
 (('l', 'n'), 14),
 (('l', 'o'), 692),
 (('l', 'p'), 15),
 (('l', 'q'), 3),
 (('l', 'r'), 18),
 (('l', 's'), 94),
 (('l', 't'), 77),
 (('l', 'u'), 324),
 (('l', 'v'), 72),
 (('l', 'w'), 16),
 (('l', 'y'), 1588),
 (('l', 'z'), 10),
 (('m', '<E>'), 516),
 (('m', 'a'), 2590),
 (('m', 'b'), 112),
 (('m', 'c'), 51),
 (('m', 'd'), 24),
 (('m', 'e'), 818),
 (('m', 'f'), 1),
 (('m', 'h'), 5),
 (('m', 'i'), 1256),
 (('m', 'j'), 7),
 (('m', 'k'), 1),
 (('m', 'l'), 5),
 (('m', 'm'), 168),
 (('m', 'n'), 20),
 (('m', 'o'), 452),
 (('m', 'p'), 38),
 (('m', 'r'), 97),
 (('m', 's'), 35),
 (('m', 't'), 4),
 (('m', 'u'), 139),
 (('m', 'v'), 3),
 (('m', 'w'), 2),
 (('m', 'y'), 287),
 (('m', 'z'), 11),
 (('n', '<E>'), 6763),
 (('n', 'a'), 2977),
 (('n', 'b'), 8),
 (('n', 'c'), 213),
 (('n', 'd'), 704),
 (('n', 'e'), 1359),
 (('n', 'f'), 11),
 (('n', 'g'), 273),
 (('n', 'h'), 26),
 (('n', 'i'), 1725),
 (('n', 'j'), 44),
 (('n', 'k'), 58),
 (('n', 'l'), 195),
 (('n', 'm'), 19),
 (('n', 'n'), 1906),
 (('n', 'o'), 496),
 (('n', 'p'), 5),
 (('n', 'q'), 2),
 (('n', 'r'), 44),
 (('n', 's'), 278),
 (('n', 't'), 443),
 (('n', 'u'), 96),
 (('n', 'v'), 55),
 (('n', 'w'), 11),
 (('n', 'x'), 6),
 (('n', 'y'), 465),
 (('n', 'z'), 145),
 (('o', '<E>'), 855),
 (('o', 'a'), 149),
 (('o', 'b'), 140),
 (('o', 'c'), 114),
 (('o', 'd'), 190),
 (('o', 'e'), 132),
 (('o', 'f'), 34),
 (('o', 'g'), 44),
 (('o', 'h'), 171),
 (('o', 'i'), 69),
 (('o', 'j'), 16),
 (('o', 'k'), 68),
 (('o', 'l'), 619),
 (('o', 'm'), 261),
 (('o', 'n'), 2411),
 (('o', 'o'), 115),
 (('o', 'p'), 95),
 (('o', 'q'), 3),
 (('o', 'r'), 1059),
 (('o', 's'), 504),
 (('o', 't'), 118),
 (('o', 'u'), 275),
 (('o', 'v'), 176),
 (('o', 'w'), 114),
 (('o', 'x'), 45),
 (('o', 'y'), 103),
 (('o', 'z'), 54),
 (('p', '<E>'), 33),
 (('p', 'a'), 209),
 (('p', 'b'), 2),
 (('p', 'c'), 1),
 (('p', 'e'), 197),
 (('p', 'f'), 1),
 (('p', 'h'), 204),
 (('p', 'i'), 61),
 (('p', 'j'), 1),
 (('p', 'k'), 1),
 (('p', 'l'), 16),
 (('p', 'm'), 1),
 (('p', 'n'), 1),
 (('p', 'o'), 59),
 (('p', 'p'), 39),
 (('p', 'r'), 151),
 (('p', 's'), 16),
 (('p', 't'), 17),
 (('p', 'u'), 4),
 (('p', 'y'), 12),
 (('q', '<E>'), 28),
 (('q', 'a'), 13),
 (('q', 'e'), 1),
 (('q', 'i'), 13),
 (('q', 'l'), 1),
 (('q', 'm'), 2),
 (('q', 'o'), 2),
 (('q', 'r'), 1),
 (('q', 's'), 2),
 (('q', 'u'), 206),
 (('q', 'w'), 3),
 (('r', '<E>'), 1377),
 (('r', 'a'), 2356),
 (('r', 'b'), 41),
 (('r', 'c'), 99),
 (('r', 'd'), 187),
 (('r', 'e'), 1697),
 (('r', 'f'), 9),
 (('r', 'g'), 76),
 (('r', 'h'), 121),
 (('r', 'i'), 3033),
 (('r', 'j'), 25),
 (('r', 'k'), 90),
 (('r', 'l'), 413),
 (('r', 'm'), 162),
 (('r', 'n'), 140),
 (('r', 'o'), 869),
 (('r', 'p'), 14),
 (('r', 'q'), 16),
 (('r', 'r'), 425),
 (('r', 's'), 190),
 (('r', 't'), 208),
 (('r', 'u'), 252),
 (('r', 'v'), 80),
 (('r', 'w'), 21),
 (('r', 'x'), 3),
 (('r', 'y'), 773),
 (('r', 'z'), 23),
 (('s', '<E>'), 1169),
 (('s', 'a'), 1201),
 (('s', 'b'), 21),
 (('s', 'c'), 60),
 (('s', 'd'), 9),
 (('s', 'e'), 884),
 (('s', 'f'), 2),
 (('s', 'g'), 2),
 (('s', 'h'), 1285),
 (('s', 'i'), 684),
 (('s', 'j'), 2),
 (('s', 'k'), 82),
 (('s', 'l'), 279),
 (('s', 'm'), 90),
 (('s', 'n'), 24),
 (('s', 'o'), 531),
 (('s', 'p'), 51),
 (('s', 'q'), 1),
 (('s', 'r'), 55),
 (('s', 's'), 461),
 (('s', 't'), 765),
 (('s', 'u'), 185),
 (('s', 'v'), 14),
 (('s', 'w'), 24),
 (('s', 'y'), 215),
 (('s', 'z'), 10),
 (('t', '<E>'), 483),
 (('t', 'a'), 1027),
 (('t', 'b'), 1),
 (('t', 'c'), 17),
 (('t', 'e'), 716),
 (('t', 'f'), 2),
 (('t', 'g'), 2),
 (('t', 'h'), 647),
 (('t', 'i'), 532),
 (('t', 'j'), 3),
 (('t', 'l'), 134),
 (('t', 'm'), 4),
 (('t', 'n'), 22),
 (('t', 'o'), 667),
 (('t', 'r'), 352),
 (('t', 's'), 35),
 (('t', 't'), 374),
 (('t', 'u'), 78),
 (('t', 'v'), 15),
 (('t', 'w'), 11),
 (('t', 'x'), 2),
 (('t', 'y'), 341),
 (('t', 'z'), 105),
 (('u', '<E>'), 155),
 (('u', 'a'), 163),
 (('u', 'b'), 103),
 (('u', 'c'), 103),
 (('u', 'd'), 136),
 (('u', 'e'), 169),
 (('u', 'f'), 19),
 (('u', 'g'), 47),
 (('u', 'h'), 58),
 (('u', 'i'), 121),
 (('u', 'j'), 14),
 (('u', 'k'), 93),
 (('u', 'l'), 301),
 (('u', 'm'), 154),
 (('u', 'n'), 275),
 (('u', 'o'), 10),
 (('u', 'p'), 16),
 (('u', 'q'), 10),
 (('u', 'r'), 414),
 (('u', 's'), 474),
 (('u', 't'), 82),
 (('u', 'u'), 3),
 (('u', 'v'), 37),
 (('u', 'w'), 86),
 (('u', 'x'), 34),
 (('u', 'y'), 13),
 (('u', 'z'), 45),
 (('v', '<E>'), 88),
 (('v', 'a'), 642),
 (('v', 'b'), 1),
 (('v', 'd'), 1),
 (('v', 'e'), 568),
 (('v', 'h'), 1),
 (('v', 'i'), 911),
 (('v', 'k'), 3),
 (('v', 'l'), 14),
 (('v', 'n'), 8),
 (('v', 'o'), 153),
 (('v', 'r'), 48),
 (('v', 'u'), 7),
 (('v', 'v'), 7),
 (('v', 'y'), 121),
 (('w', '<E>'), 51),
 (('w', 'a'), 280),
 (('w', 'b'), 1),
 (('w', 'd'), 8),
 (('w', 'e'), 149),
 (('w', 'f'), 2),
 (('w', 'g'), 1),
 (('w', 'h'), 23),
 (('w', 'i'), 148),
 (('w', 'k'), 6),
 (('w', 'l'), 13),
 (('w', 'm'), 2),
 (('w', 'n'), 58),
 (('w', 'o'), 36),
 (('w', 'r'), 22),
 (('w', 's'), 20),
 (('w', 't'), 8),
 (('w', 'u'), 25),
 (('w', 'w'), 2),
 (('w', 'y'), 73),
 (('w', 'z'), 1),
 (('x', '<E>'), 164),
 (('x', 'a'), 103),
 (('x', 'b'), 1),
 (('x', 'c'), 4),
 (('x', 'd'), 5),
 (('x', 'e'), 36),
 (('x', 'f'), 3),
 (('x', 'h'), 1),
 (('x', 'i'), 102),
 (('x', 'l'), 39),
 (('x', 'm'), 1),
 (('x', 'n'), 1),
 (('x', 'o'), 41),
 (('x', 's'), 31),
 (('x', 't'), 70),
 (('x', 'u'), 5),
 (('x', 'w'), 3),
 (('x', 'x'), 38),
 (('x', 'y'), 30),
 (('x', 'z'), 19),
 (('y', '<E>'), 2007),
 (('y', 'a'), 2143),
 (('y', 'b'), 27),
 (('y', 'c'), 115),
 (('y', 'd'), 272),
 (('y', 'e'), 301),
 (('y', 'f'), 12),
 (('y', 'g'), 30),
 (('y', 'h'), 22),
 (('y', 'i'), 192),
 (('y', 'j'), 23),
 (('y', 'k'), 86),
 (('y', 'l'), 1104),
 (('y', 'm'), 148),
 (('y', 'n'), 1826),
 (('y', 'o'), 271),
 (('y', 'p'), 15),
 (('y', 'q'), 6),
 (('y', 'r'), 291),
 (('y', 's'), 401),
 (('y', 't'), 104),
 (('y', 'u'), 141),
 (('y', 'v'), 106),
 (('y', 'w'), 4),
 (('y', 'x'), 28),
 (('y', 'y'), 23),
 (('y', 'z'), 78),
 (('z', '<E>'), 160),
 (('z', 'a'), 860),
 (('z', 'b'), 4),
 (('z', 'c'), 2),
 (('z', 'd'), 2),
 (('z', 'e'), 373),
 (('z', 'g'), 1),
 (('z', 'h'), 43),
 (('z', 'i'), 364),
 (('z', 'j'), 2),
 (('z', 'k'), 2),
 (('z', 'l'), 123),
 (('z', 'm'), 35),
 (('z', 'n'), 4),
 (('z', 'o'), 110),
 (('z', 'p'), 2),
 (('z', 'r'), 32),
 (('z', 's'), 4),
 (('z', 't'), 4),
 (('z', 'u'), 73),
 (('z', 'v'), 2),
 (('z', 'w'), 3),
 (('z', 'x'), 1),
 (('z', 'y'), 147),
 (('z', 'z'), 45)]
sorted(b.items(), key= lambda kv: -kv[1])  #Now, here we are specifying we want to sort based on the values. So we select the key, then in the lambda function, we take the keyvalue (kv) and select the second element in the set, which is what we want
# - sign is to go backwards
Output
[(('n', '<E>'), 6763),
 (('a', '<E>'), 6640),
 (('a', 'n'), 5438),
 (('<S>', 'a'), 4410),
 (('e', '<E>'), 3983),
 (('a', 'r'), 3264),
 (('e', 'l'), 3248),
 (('r', 'i'), 3033),
 (('n', 'a'), 2977),
 (('<S>', 'k'), 2963),
 (('l', 'e'), 2921),
 (('e', 'n'), 2675),
 (('l', 'a'), 2623),
 (('m', 'a'), 2590),
 (('<S>', 'm'), 2538),
 (('a', 'l'), 2528),
 (('i', '<E>'), 2489),
 (('l', 'i'), 2480),
 (('i', 'a'), 2445),
 (('<S>', 'j'), 2422),
 (('o', 'n'), 2411),
 (('h', '<E>'), 2409),
 (('r', 'a'), 2356),
 (('a', 'h'), 2332),
 (('h', 'a'), 2244),
 (('y', 'a'), 2143),
 (('i', 'n'), 2126),
 (('<S>', 's'), 2055),
 (('a', 'y'), 2050),
 (('y', '<E>'), 2007),
 (('e', 'r'), 1958),
 (('n', 'n'), 1906),
 (('y', 'n'), 1826),
 (('k', 'a'), 1731),
 (('n', 'i'), 1725),
 (('r', 'e'), 1697),
 (('<S>', 'd'), 1690),
 (('i', 'e'), 1653),
 (('a', 'i'), 1650),
 (('<S>', 'r'), 1639),
 (('a', 'm'), 1634),
 (('l', 'y'), 1588),
 (('<S>', 'l'), 1572),
 (('<S>', 'c'), 1542),
 (('<S>', 'e'), 1531),
 (('j', 'a'), 1473),
 (('r', '<E>'), 1377),
 (('n', 'e'), 1359),
 (('l', 'l'), 1345),
 (('i', 'l'), 1345),
 (('i', 's'), 1316),
 (('l', '<E>'), 1314),
 (('<S>', 't'), 1308),
 (('<S>', 'b'), 1306),
 (('d', 'a'), 1303),
 (('s', 'h'), 1285),
 (('d', 'e'), 1283),
 (('e', 'e'), 1271),
 (('m', 'i'), 1256),
 (('s', 'a'), 1201),
 (('s', '<E>'), 1169),
 (('<S>', 'n'), 1146),
 (('a', 's'), 1118),
 (('y', 'l'), 1104),
 (('e', 'y'), 1070),
 (('o', 'r'), 1059),
 (('a', 'd'), 1042),
 (('t', 'a'), 1027),
 (('<S>', 'z'), 929),
 (('v', 'i'), 911),
 (('k', 'e'), 895),
 (('s', 'e'), 884),
 (('<S>', 'h'), 874),
 (('r', 'o'), 869),
 (('e', 's'), 861),
 (('z', 'a'), 860),
 (('o', '<E>'), 855),
 (('i', 'r'), 849),
 (('b', 'r'), 842),
 (('a', 'v'), 834),
 (('m', 'e'), 818),
 (('e', 'i'), 818),
 (('c', 'a'), 815),
 (('i', 'y'), 779),
 (('r', 'y'), 773),
 (('e', 'm'), 769),
 (('s', 't'), 765),
 (('h', 'i'), 729),
 (('t', 'e'), 716),
 (('n', 'd'), 704),
 (('l', 'o'), 692),
 (('a', 'e'), 692),
 (('a', 't'), 687),
 (('s', 'i'), 684),
 (('e', 'a'), 679),
 (('d', 'i'), 674),
 (('h', 'e'), 674),
 (('<S>', 'g'), 669),
 (('t', 'o'), 667),
 (('c', 'h'), 664),
 (('b', 'e'), 655),
 (('t', 'h'), 647),
 (('v', 'a'), 642),
 (('o', 'l'), 619),
 (('<S>', 'i'), 591),
 (('i', 'o'), 588),
 (('e', 't'), 580),
 (('v', 'e'), 568),
 (('a', 'k'), 568),
 (('a', 'a'), 556),
 (('c', 'e'), 551),
 (('a', 'b'), 541),
 (('i', 't'), 541),
 (('<S>', 'y'), 535),
 (('t', 'i'), 532),
 (('s', 'o'), 531),
 (('m', '<E>'), 516),
 (('d', '<E>'), 516),
 (('<S>', 'p'), 515),
 (('i', 'c'), 509),
 (('k', 'i'), 509),
 (('o', 's'), 504),
 (('n', 'o'), 496),
 (('t', '<E>'), 483),
 (('j', 'o'), 479),
 (('u', 's'), 474),
 (('a', 'c'), 470),
 (('n', 'y'), 465),
 (('e', 'v'), 463),
 (('s', 's'), 461),
 (('m', 'o'), 452),
 (('i', 'k'), 445),
 (('n', 't'), 443),
 (('i', 'd'), 440),
 (('j', 'e'), 440),
 (('a', 'z'), 435),
 (('i', 'g'), 428),
 (('i', 'm'), 427),
 (('r', 'r'), 425),
 (('d', 'r'), 424),
 (('<S>', 'f'), 417),
 (('u', 'r'), 414),
 (('r', 'l'), 413),
 (('y', 's'), 401),
 (('<S>', 'o'), 394),
 (('e', 'd'), 384),
 (('a', 'u'), 381),
 (('c', 'o'), 380),
 (('k', 'y'), 379),
 (('d', 'o'), 378),
 (('<S>', 'v'), 376),
 (('t', 't'), 374),
 (('z', 'e'), 373),
 (('z', 'i'), 364),
 (('k', '<E>'), 363),
 (('g', 'h'), 360),
 (('t', 'r'), 352),
 (('k', 'o'), 344),
 (('t', 'y'), 341),
 (('g', 'e'), 334),
 (('g', 'a'), 330),
 (('l', 'u'), 324),
 (('b', 'a'), 321),
 (('d', 'y'), 317),
 (('c', 'k'), 316),
 (('<S>', 'w'), 307),
 (('k', 'h'), 307),
 (('u', 'l'), 301),
 (('y', 'e'), 301),
 (('y', 'r'), 291),
 (('m', 'y'), 287),
 (('h', 'o'), 287),
 (('w', 'a'), 280),
 (('s', 'l'), 279),
 (('n', 's'), 278),
 (('i', 'z'), 277),
 (('u', 'n'), 275),
 (('o', 'u'), 275),
 (('n', 'g'), 273),
 (('y', 'd'), 272),
 (('c', 'i'), 271),
 (('y', 'o'), 271),
 (('i', 'v'), 269),
 (('e', 'o'), 269),
 (('o', 'm'), 261),
 (('r', 'u'), 252),
 (('f', 'a'), 242),
 (('b', 'i'), 217),
 (('s', 'y'), 215),
 (('n', 'c'), 213),
 (('h', 'y'), 213),
 (('p', 'a'), 209),
 (('r', 't'), 208),
 (('q', 'u'), 206),
 (('p', 'h'), 204),
 (('h', 'r'), 204),
 (('j', 'u'), 202),
 (('g', 'r'), 201),
 (('p', 'e'), 197),
 (('n', 'l'), 195),
 (('y', 'i'), 192),
 (('g', 'i'), 190),
 (('o', 'd'), 190),
 (('r', 's'), 190),
 (('r', 'd'), 187),
 (('h', 'l'), 185),
 (('s', 'u'), 185),
 (('a', 'x'), 182),
 (('e', 'z'), 181),
 (('e', 'k'), 178),
 (('o', 'v'), 176),
 (('a', 'j'), 175),
 (('o', 'h'), 171),
 (('u', 'e'), 169),
 (('m', 'm'), 168),
 (('a', 'g'), 168),
 (('h', 'u'), 166),
 (('x', '<E>'), 164),
 (('u', 'a'), 163),
 (('r', 'm'), 162),
 (('a', 'w'), 161),
 (('f', 'i'), 160),
 (('z', '<E>'), 160),
 (('u', '<E>'), 155),
 (('u', 'm'), 154),
 (('e', 'c'), 153),
 (('v', 'o'), 153),
 (('e', 'h'), 152),
 (('p', 'r'), 151),
 (('d', 'd'), 149),
 (('o', 'a'), 149),
 (('w', 'e'), 149),
 (('w', 'i'), 148),
 (('y', 'm'), 148),
 (('z', 'y'), 147),
 (('n', 'z'), 145),
 (('y', 'u'), 141),
 (('r', 'n'), 140),
 (('o', 'b'), 140),
 (('k', 'l'), 139),
 (('m', 'u'), 139),
 (('l', 'd'), 138),
 (('h', 'n'), 138),
 (('u', 'd'), 136),
 (('<S>', 'x'), 134),
 (('t', 'l'), 134),
 (('a', 'f'), 134),
 (('o', 'e'), 132),
 (('e', 'x'), 132),
 (('e', 'g'), 125),
 (('f', 'e'), 123),
 (('z', 'l'), 123),
 (('u', 'i'), 121),
 (('v', 'y'), 121),
 (('e', 'b'), 121),
 (('r', 'h'), 121),
 (('j', 'i'), 119),
 (('o', 't'), 118),
 (('d', 'h'), 118),
 (('h', 'm'), 117),
 (('c', 'l'), 116),
 (('o', 'o'), 115),
 (('y', 'c'), 115),
 (('o', 'w'), 114),
 (('o', 'c'), 114),
 (('f', 'r'), 114),
 (('b', '<E>'), 114),
 (('m', 'b'), 112),
 (('z', 'o'), 110),
 (('i', 'b'), 110),
 (('i', 'u'), 109),
 (('k', 'r'), 109),
 (('g', '<E>'), 108),
 (('y', 'v'), 106),
 (('t', 'z'), 105),
 (('b', 'o'), 105),
 (('c', 'y'), 104),
 (('y', 't'), 104),
 (('u', 'b'), 103),
 (('u', 'c'), 103),
 (('x', 'a'), 103),
 (('b', 'l'), 103),
 (('o', 'y'), 103),
 (('x', 'i'), 102),
 (('i', 'f'), 101),
 (('r', 'c'), 99),
 (('c', '<E>'), 97),
 (('m', 'r'), 97),
 (('n', 'u'), 96),
 (('o', 'p'), 95),
 (('i', 'h'), 95),
 (('k', 's'), 95),
 (('l', 's'), 94),
 (('u', 'k'), 93),
 (('<S>', 'q'), 92),
 (('d', 'u'), 92),
 (('s', 'm'), 90),
 (('r', 'k'), 90),
 (('i', 'x'), 89),
 (('v', '<E>'), 88),
 (('y', 'k'), 86),
 (('u', 'w'), 86),
 (('g', 'u'), 85),
 (('b', 'y'), 83),
 (('e', 'p'), 83),
 (('g', 'o'), 83),
 (('s', 'k'), 82),
 (('u', 't'), 82),
 (('a', 'p'), 82),
 (('e', 'f'), 82),
 (('i', 'i'), 82),
 (('r', 'v'), 80),
 (('f', '<E>'), 80),
 (('t', 'u'), 78),
 (('y', 'z'), 78),
 (('<S>', 'u'), 78),
 (('l', 't'), 77),
 (('r', 'g'), 76),
 (('c', 'r'), 76),
 (('i', 'j'), 76),
 (('w', 'y'), 73),
 (('z', 'u'), 73),
 (('l', 'v'), 72),
 (('h', 't'), 71),
 (('j', '<E>'), 71),
 (('x', 't'), 70),
 (('o', 'i'), 69),
 (('e', 'u'), 69),
 (('o', 'k'), 68),
 (('b', 'd'), 65),
 (('a', 'o'), 63),
 (('p', 'i'), 61),
 (('s', 'c'), 60),
 (('d', 'l'), 60),
 (('l', 'm'), 60),
 (('a', 'q'), 60),
 (('f', 'o'), 60),
 (('p', 'o'), 59),
 (('n', 'k'), 58),
 (('w', 'n'), 58),
 (('u', 'h'), 58),
 (('e', 'j'), 55),
 (('n', 'v'), 55),
 (('s', 'r'), 55),
 (('o', 'z'), 54),
 (('i', 'p'), 53),
 (('l', 'b'), 52),
 (('i', 'q'), 52),
 (('w', '<E>'), 51),
 (('m', 'c'), 51),
 (('s', 'p'), 51),
 (('e', 'w'), 50),
 (('k', 'u'), 50),
 (('v', 'r'), 48),
 (('u', 'g'), 47),
 (('o', 'x'), 45),
 (('u', 'z'), 45),
 (('z', 'z'), 45),
 (('j', 'h'), 45),
 (('b', 'u'), 45),
 (('o', 'g'), 44),
 (('n', 'r'), 44),
 (('f', 'f'), 44),
 (('n', 'j'), 44),
 (('z', 'h'), 43),
 (('c', 'c'), 42),
 (('r', 'b'), 41),
 (('x', 'o'), 41),
 (('b', 'h'), 41),
 (('p', 'p'), 39),
 (('x', 'l'), 39),
 (('h', 'v'), 39),
 (('b', 'b'), 38),
 (('m', 'p'), 38),
 (('x', 'x'), 38),
 (('u', 'v'), 37),
 (('x', 'e'), 36),
 (('w', 'o'), 36),
 (('c', 't'), 35),
 (('z', 'm'), 35),
 (('t', 's'), 35),
 (('m', 's'), 35),
 (('c', 'u'), 35),
 (('o', 'f'), 34),
 (('u', 'x'), 34),
 (('k', 'w'), 34),
 (('p', '<E>'), 33),
 (('g', 'l'), 32),
 (('z', 'r'), 32),
 (('d', 'n'), 31),
 (('g', 't'), 31),
 (('g', 'y'), 31),
 (('h', 's'), 31),
 (('x', 's'), 31),
 (('g', 's'), 30),
 (('x', 'y'), 30),
 (('y', 'g'), 30),
 (('d', 'm'), 30),
 (('d', 's'), 29),
 (('h', 'k'), 29),
 (('y', 'x'), 28),
 (('q', '<E>'), 28),
 (('g', 'n'), 27),
 (('y', 'b'), 27),
 (('g', 'w'), 26),
 (('n', 'h'), 26),
 (('k', 'n'), 26),
 (('g', 'g'), 25),
 (('d', 'g'), 25),
 (('l', 'c'), 25),
 (('r', 'j'), 25),
 (('w', 'u'), 25),
 (('l', 'k'), 24),
 (('m', 'd'), 24),
 (('s', 'w'), 24),
 (('s', 'n'), 24),
 (('h', 'd'), 24),
 (('w', 'h'), 23),
 (('y', 'j'), 23),
 (('y', 'y'), 23),
 (('r', 'z'), 23),
 (('d', 'w'), 23),
 (('w', 'r'), 22),
 (('t', 'n'), 22),
 (('l', 'f'), 22),
 (('y', 'h'), 22),
 (('r', 'w'), 21),
 (('s', 'b'), 21),
 (('m', 'n'), 20),
 (('f', 'l'), 20),
 (('w', 's'), 20),
 (('k', 'k'), 20),
 (('h', 'z'), 20),
 (('g', 'd'), 19),
 (('l', 'h'), 19),
 (('n', 'm'), 19),
 (('x', 'z'), 19),
 (('u', 'f'), 19),
 (('f', 't'), 18),
 (('l', 'r'), 18),
 (('p', 't'), 17),
 (('t', 'c'), 17),
 (('k', 't'), 17),
 (('d', 'v'), 17),
 (('u', 'p'), 16),
 (('p', 'l'), 16),
 (('l', 'w'), 16),
 (('p', 's'), 16),
 (('o', 'j'), 16),
 (('r', 'q'), 16),
 (('y', 'p'), 15),
 (('l', 'p'), 15),
 (('t', 'v'), 15),
 (('r', 'p'), 14),
 (('l', 'n'), 14),
 (('e', 'q'), 14),
 (('f', 'y'), 14),
 (('s', 'v'), 14),
 (('u', 'j'), 14),
 (('v', 'l'), 14),
 (('q', 'a'), 13),
 (('u', 'y'), 13),
 (('q', 'i'), 13),
 (('w', 'l'), 13),
 (('p', 'y'), 12),
 (('y', 'f'), 12),
 (('c', 'q'), 11),
 (('j', 'r'), 11),
 (('n', 'w'), 11),
 (('n', 'f'), 11),
 (('t', 'w'), 11),
 (('m', 'z'), 11),
 (('u', 'o'), 10),
 (('f', 'u'), 10),
 (('l', 'z'), 10),
 (('h', 'w'), 10),
 (('u', 'q'), 10),
 (('j', 'y'), 10),
 (('s', 'z'), 10),
 (('s', 'd'), 9),
 (('j', 'l'), 9),
 (('d', 'j'), 9),
 (('k', 'm'), 9),
 (('r', 'f'), 9),
 (('h', 'j'), 9),
 (('v', 'n'), 8),
 (('n', 'b'), 8),
 (('i', 'w'), 8),
 (('h', 'b'), 8),
 (('b', 's'), 8),
 (('w', 't'), 8),
 (('w', 'd'), 8),
 (('v', 'v'), 7),
 (('v', 'u'), 7),
 (('j', 's'), 7),
 (('m', 'j'), 7),
 (('f', 's'), 6),
 (('l', 'g'), 6),
 (('l', 'j'), 6),
 (('j', 'w'), 6),
 (('n', 'x'), 6),
 (('y', 'q'), 6),
 (('w', 'k'), 6),
 (('g', 'm'), 6),
 (('x', 'u'), 5),
 (('m', 'h'), 5),
 (('m', 'l'), 5),
 (('j', 'm'), 5),
 (('c', 's'), 5),
 (('j', 'v'), 5),
 (('n', 'p'), 5),
 (('d', 'f'), 5),
 (('x', 'd'), 5),
 (('z', 'b'), 4),
 (('f', 'n'), 4),
 (('x', 'c'), 4),
 (('m', 't'), 4),
 (('t', 'm'), 4),
 (('z', 'n'), 4),
 (('z', 't'), 4),
 (('p', 'u'), 4),
 (('c', 'z'), 4),
 (('b', 'n'), 4),
 (('z', 's'), 4),
 (('f', 'w'), 4),
 (('d', 't'), 4),
 (('j', 'd'), 4),
 (('j', 'c'), 4),
 (('y', 'w'), 4),
 (('v', 'k'), 3),
 (('x', 'w'), 3),
 (('t', 'j'), 3),
 (('c', 'j'), 3),
 (('q', 'w'), 3),
 (('g', 'b'), 3),
 (('o', 'q'), 3),
 (('r', 'x'), 3),
 (('d', 'c'), 3),
 (('g', 'j'), 3),
 (('x', 'f'), 3),
 (('z', 'w'), 3),
 (('d', 'k'), 3),
 (('u', 'u'), 3),
 (('m', 'v'), 3),
 (('c', 'x'), 3),
 (('l', 'q'), 3),
 (('p', 'b'), 2),
 (('t', 'g'), 2),
 (('q', 's'), 2),
 (('t', 'x'), 2),
 (('f', 'k'), 2),
 (('b', 't'), 2),
 (('j', 'n'), 2),
 (('k', 'c'), 2),
 (('z', 'k'), 2),
 (('s', 'j'), 2),
 (('s', 'f'), 2),
 (('z', 'j'), 2),
 (('n', 'q'), 2),
 (('f', 'z'), 2),
 (('h', 'g'), 2),
 (('w', 'w'), 2),
 (('k', 'j'), 2),
 (('j', 'k'), 2),
 (('w', 'm'), 2),
 (('z', 'c'), 2),
 (('z', 'v'), 2),
 (('w', 'f'), 2),
 (('q', 'm'), 2),
 (('k', 'z'), 2),
 (('j', 'j'), 2),
 (('z', 'p'), 2),
 (('j', 't'), 2),
 (('k', 'b'), 2),
 (('m', 'w'), 2),
 (('h', 'f'), 2),
 (('c', 'g'), 2),
 (('t', 'f'), 2),
 (('h', 'c'), 2),
 (('q', 'o'), 2),
 (('k', 'd'), 2),
 (('k', 'v'), 2),
 (('s', 'g'), 2),
 (('z', 'd'), 2),
 (('q', 'r'), 1),
 (('d', 'z'), 1),
 (('p', 'j'), 1),
 (('q', 'l'), 1),
 (('p', 'f'), 1),
 (('q', 'e'), 1),
 (('b', 'c'), 1),
 (('c', 'd'), 1),
 (('m', 'f'), 1),
 (('p', 'n'), 1),
 (('w', 'b'), 1),
 (('p', 'c'), 1),
 (('h', 'p'), 1),
 (('f', 'h'), 1),
 (('b', 'j'), 1),
 (('f', 'g'), 1),
 (('z', 'g'), 1),
 (('c', 'p'), 1),
 (('p', 'k'), 1),
 (('p', 'm'), 1),
 (('x', 'n'), 1),
 (('s', 'q'), 1),
 (('k', 'f'), 1),
 (('m', 'k'), 1),
 (('x', 'h'), 1),
 (('g', 'f'), 1),
 (('v', 'b'), 1),
 (('j', 'p'), 1),
 (('g', 'z'), 1),
 (('v', 'd'), 1),
 (('d', 'b'), 1),
 (('v', 'h'), 1),
 (('h', 'h'), 1),
 (('g', 'v'), 1),
 (('d', 'q'), 1),
 (('x', 'b'), 1),
 (('w', 'z'), 1),
 (('h', 'q'), 1),
 (('j', 'b'), 1),
 (('x', 'm'), 1),
 (('w', 'g'), 1),
 (('t', 'b'), 1),
 (('z', 'x'), 1)]

Counting bigrams in a 2D torch tensor ("training the model") 00:12:45

import torch
N = torch.zeros((28, 28), dtype = torch.int32)
chars = sorted(list(set(''.join(words))))

stoi = {s:i for i,s in enumerate(chars)}
stoi['<S>'] = 26
stoi['<E>'] = 27
stoi    #Btw this is just 's to i'
Output
{'a': 0,
 'b': 1,
 'c': 2,
 'd': 3,
 'e': 4,
 'f': 5,
 'g': 6,
 'h': 7,
 'i': 8,
 'j': 9,
 'k': 10,
 'l': 11,
 'm': 12,
 'n': 13,
 'o': 14,
 'p': 15,
 'q': 16,
 'r': 17,
 's': 18,
 't': 19,
 'u': 20,
 'v': 21,
 'w': 22,
 'x': 23,
 'y': 24,
 'z': 25,
 '<S>': 26,
 '<E>': 27}
for word in words:
    chs = ['<S>'] + list(word) + ['<E>']
    for ch1, ch2 in zip(chs, chs[1:]):
        ix1 = stoi[ch1]
        ix2 = stoi[ch2]
        N[ix1, ix2] += 1
N
Output
tensor([[ 556,  541,  470, 1042,  692,  134,  168, 2332, 1650,  175,  568, 2528,
         1634, 5438,   63,   82,   60, 3264, 1118,  687,  381,  834,  161,  182,
         2050,  435,    0, 6640],
        [ 321,   38,    1,   65,  655,    0,    0,   41,  217,    1,    0,  103,
            0,    4,  105,    0,    0,  842,    8,    2,   45,    0,    0,    0,
           83,    0,    0,  114],
        [ 815,    0,   42,    1,  551,    0,    2,  664,  271,    3,  316,  116,
            0,    0,  380,    1,   11,   76,    5,   35,   35,    0,    0,    3,
          104,    4,    0,   97],
        [1303,    1,    3,  149, 1283,    5,   25,  118,  674,    9,    3,   60,
           30,   31,  378,    0,    1,  424,   29,    4,   92,   17,   23,    0,
          317,    1,    0,  516],
        [ 679,  121,  153,  384, 1271,   82,  125,  152,  818,   55,  178, 3248,
          769, 2675,  269,   83,   14, 1958,  861,  580,   69,  463,   50,  132,
         1070,  181,    0, 3983],
        [ 242,    0,    0,    0,  123,   44,    1,    1,  160,    0,    2,   20,
            0,    4,   60,    0,    0,  114,    6,   18,   10,    0,    4,    0,
           14,    2,    0,   80],
        [ 330,    3,    0,   19,  334,    1,   25,  360,  190,    3,    0,   32,
            6,   27,   83,    0,    0,  201,   30,   31,   85,    1,   26,    0,
           31,    1,    0,  108],
        [2244,    8,    2,   24,  674,    2,    2,    1,  729,    9,   29,  185,
          117,  138,  287,    1,    1,  204,   31,   71,  166,   39,   10,    0,
          213,   20,    0, 2409],
        [2445,  110,  509,  440, 1653,  101,  428,   95,   82,   76,  445, 1345,
          427, 2126,  588,   53,   52,  849, 1316,  541,  109,  269,    8,   89,
          779,  277,    0, 2489],
        [1473,    1,    4,    4,  440,    0,    0,   45,  119,    2,    2,    9,
            5,    2,  479,    1,    0,   11,    7,    2,  202,    5,    6,    0,
           10,    0,    0,   71],
        [1731,    2,    2,    2,  895,    1,    0,  307,  509,    2,   20,  139,
            9,   26,  344,    0,    0,  109,   95,   17,   50,    2,   34,    0,
          379,    2,    0,  363],
        [2623,   52,   25,  138, 2921,   22,    6,   19, 2480,    6,   24, 1345,
           60,   14,  692,   15,    3,   18,   94,   77,  324,   72,   16,    0,
         1588,   10,    0, 1314],
        [2590,  112,   51,   24,  818,    1,    0,    5, 1256,    7,    1,    5,
          168,   20,  452,   38,    0,   97,   35,    4,  139,    3,    2,    0,
          287,   11,    0,  516],
        [2977,    8,  213,  704, 1359,   11,  273,   26, 1725,   44,   58,  195,
           19, 1906,  496,    5,    2,   44,  278,  443,   96,   55,   11,    6,
          465,  145,    0, 6763],
        [ 149,  140,  114,  190,  132,   34,   44,  171,   69,   16,   68,  619,
          261, 2411,  115,   95,    3, 1059,  504,  118,  275,  176,  114,   45,
          103,   54,    0,  855],
        [ 209,    2,    1,    0,  197,    1,    0,  204,   61,    1,    1,   16,
            1,    1,   59,   39,    0,  151,   16,   17,    4,    0,    0,    0,
           12,    0,    0,   33],
        [  13,    0,    0,    0,    1,    0,    0,    0,   13,    0,    0,    1,
            2,    0,    2,    0,    0,    1,    2,    0,  206,    0,    3,    0,
            0,    0,    0,   28],
        [2356,   41,   99,  187, 1697,    9,   76,  121, 3033,   25,   90,  413,
          162,  140,  869,   14,   16,  425,  190,  208,  252,   80,   21,    3,
          773,   23,    0, 1377],
        [1201,   21,   60,    9,  884,    2,    2, 1285,  684,    2,   82,  279,
           90,   24,  531,   51,    1,   55,  461,  765,  185,   14,   24,    0,
          215,   10,    0, 1169],
        [1027,    1,   17,    0,  716,    2,    2,  647,  532,    3,    0,  134,
            4,   22,  667,    0,    0,  352,   35,  374,   78,   15,   11,    2,
          341,  105,    0,  483],
        [ 163,  103,  103,  136,  169,   19,   47,   58,  121,   14,   93,  301,
          154,  275,   10,   16,   10,  414,  474,   82,    3,   37,   86,   34,
           13,   45,    0,  155],
        [ 642,    1,    0,    1,  568,    0,    0,    1,  911,    0,    3,   14,
            0,    8,  153,    0,    0,   48,    0,    0,    7,    7,    0,    0,
          121,    0,    0,   88],
        [ 280,    1,    0,    8,  149,    2,    1,   23,  148,    0,    6,   13,
            2,   58,   36,    0,    0,   22,   20,    8,   25,    0,    2,    0,
           73,    1,    0,   51],
        [ 103,    1,    4,    5,   36,    3,    0,    1,  102,    0,    0,   39,
            1,    1,   41,    0,    0,    0,   31,   70,    5,    0,    3,   38,
           30,   19,    0,  164],
        [2143,   27,  115,  272,  301,   12,   30,   22,  192,   23,   86, 1104,
          148, 1826,  271,   15,    6,  291,  401,  104,  141,  106,    4,   28,
           23,   78,    0, 2007],
        [ 860,    4,    2,    2,  373,    0,    1,   43,  364,    2,    2,  123,
           35,    4,  110,    2,    0,   32,    4,    4,   73,    2,    3,    1,
          147,   45,    0,  160],
        [4410, 1306, 1542, 1690, 1531,  417,  669,  874,  591, 2422, 2963, 1572,
         2538, 1146,  394,  515,   92, 1639, 2055, 1308,   78,  376,  307,  134,
          535,  929,    0,    0],
        [   0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,
            0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,
            0,    0,    0,    0]], dtype=torch.int32)

Visualizing the bigram tensor 00:18:19

#Okay lmao yeah, dont run this cell. Although the logic provides the same output, it's causing some issue with the plotting

# itos = {i: s for i, s in enumerate(chars)}
# itos
itos = {i:s for s,i in stoi.items()}
itos
#This code is directly taken from the video. Andrej prolly spent a lot of time designing this so

import matplotlib.pyplot as plt
%matplotlib inline

plt.figure(figsize=(16,16))
plt.imshow(N, cmap='Blues')
for i in range(28):
    for j in range(28):
        chstr = itos[i] + itos[j]
        plt.text(j, i, chstr, ha="center", va="bottom", color='gray')
        plt.text(j, i, N[i, j].item(), ha="center", va="top", color='gray')
plt.axis('off');

Cell 31 output

Deleting spurious (S) and (E) tokens in favor of a single . token 00:20:54

N = torch.zeros((27, 27), dtype = torch.int32)

chars = sorted(list(set(''.join(words))))

stoi = {s:i+1 for i,s in enumerate(chars)}
stoi['.'] = 0
itos = {i:s for s,i in stoi.items()}
for word in words:
    chs = ['.'] + list(word) + ['.']
    for ch1, ch2 in zip(chs, chs[1:]):
        ix1 = stoi[ch1]
        ix2 = stoi[ch2]
        N[ix1, ix2] += 1
import matplotlib.pyplot as plt
%matplotlib inline

plt.figure(figsize=(16,16))
plt.imshow(N, cmap='Blues')
for i in range(27):
    for j in range(27):
        chstr = itos[i] + itos[j]
        plt.text(j, i, chstr, ha="center", va="bottom", color='gray')
        plt.text(j, i, N[i, j].item(), ha="center", va="top", color='gray')
plt.axis('off');

Cell 36 output

Sampling from the model 00:24:02

N[0]    #Viewing just the first row
Output
tensor([   0, 4410, 1306, 1542, 1690, 1531,  417,  669,  874,  591, 2422, 2963,
        1572, 2538, 1146,  394,  515,   92, 1639, 2055, 1308,   78,  376,  307,
         134,  535,  929], dtype=torch.int32)

First we make them all into float.
Then we make a probability distribution
We do that by dividing p with p.sum()

p = N[0].float()
p = p / p.sum()
p
Output
tensor([0.0000, 0.1377, 0.0408, 0.0481, 0.0528, 0.0478, 0.0130, 0.0209, 0.0273,
        0.0184, 0.0756, 0.0925, 0.0491, 0.0792, 0.0358, 0.0123, 0.0161, 0.0029,
        0.0512, 0.0642, 0.0408, 0.0024, 0.0117, 0.0096, 0.0042, 0.0167, 0.0290])
p.sum().item()
Output
1.0

So the total probability sums up to 1. Therefore now we have the probability values for each of those characters.

g = torch.Generator().manual_seed(2147483647)
p = torch.rand(3, generator=g)
p = p / p.sum()
p
Output
tensor([0.6064, 0.3033, 0.0903])
torch.multinomial(p, num_samples=20, replacement=True, generator=g)
Output
tensor([1, 1, 2, 0, 0, 2, 1, 1, 0, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1])

So based on the probability percentages p we get a bunch of sample values
So 0 should be 60%, 1 should be 30%, 2 should be 10% of the total samples generated

g = torch.Generator().manual_seed(2147483647)
ix = torch.multinomial(p, num_samples=1, replacement=True, generator=g).item()
itos[ix]
Output
'.'

So now, if we had got another sampled value, lets say 'm' (so we have taken the column), now we go to the row containing 'm' and then check for its correspondant character.

Keeping this jest in mind, we will be making this into a loop.

g = torch.Generator().manual_seed(2147483647)

for i in range(10):
    out = []
    ix = 0
    while True:
        p = N[ix].float()
        p = p / p.sum()
        ix = torch.multinomial(p, num_samples=1, replacement=True, generator=g).item()
        out.append(itos[ix])
        if ix == 0:
            break
    print(''.join(out))
Output
junide.
janasah.
p.
cony.
a.
nn.
kohin.
tolian.
juee.
ksahnaauranilevias.

And this is why the Bigram model is so bad lol. The output generated not to great (Andrej said "terrible" xd), for example for p. , is that the model doesn't understand that 'p' should have had something before or after it. Right now, it considers that as a name itself.

But now we will see why the output made by the model is not exactly too terrible

g = torch.Generator().manual_seed(2147483647)

for i in range(10):
    out = []
    ix = 0
    while True:
        # p = N[ix].float()
        # p = p / p.sum()
        p = torch.ones(27) / 27.0
        ix = torch.multinomial(p, num_samples=1, replacement=True, generator=g).item()
        out.append(itos[ix])
        if ix == 0:
            break
    print(''.join(out))
Output
juwjdvdipkcqaz.
p.
cfqywocnzqfjiirltozcogsjgwzvudlhnpauyjbilevhajkdbduinrwibtlzsnjyievyvaftbzffvmumthyfodtumjrpfytszwjhrjagq.
coreaysezocfkyjjabdywejfmoifmwyfinwagaasnhsvfihofszxhddgosfmptpagicz.
rjpiufmthdt.
rkrrsru.
iyumuyfy.
mjekujcbkhvupwyhvpvhvccragr.
wdkhwfdztta.
mplyisbxlyhuuiqzavmpocbzthqmimvyqwat.

So now this is what we get when the model is completely untrained, it gives you a garbage of values.

This is happening because we removed the probability distribution and added a distribution of uniform values. So all of the characters are equally likely to occur.

So yeah, if we train it with a Bigram, then its a lot better output. So ultimately it is actually working, just that Bigram is not so great for this xD

#Solving the inefficiency problem
P = N.float()
P = P / P.sum(1, keepdim=True) #Here is where we are applying the sum and broadcasting rules. Sum for the function and Broadcasting for the division part that takes place. 

# 27 27
# 27  1
P[0].sum() #This should return the tensor object with value 1. So that entire row as been normalised

So the rule says:

Two tensors are “broadcastable” if the following rules hold:

Each tensor has at least one dimension.

When iterating over the dimension sizes, starting at the trailing dimension, the dimension sizes must either be equal, one of them is 1, or one of them does not exist.

So in our case, one of the dimensional sizes is one i.e 27 1
And, the dimension sizes are equal:
27
27

g = torch.Generator().manual_seed(2147483647)

for i in range(10):
    out = []
    ix = 0
    while True:
        p = P[ix]
        # p = N[ix].float()
        # p = p / p.sum()
        ix = torch.multinomial(p, num_samples=1, replacement=True, generator=g).item()
        out.append(itos[ix])
        if ix == 0:
            break
    print(''.join(out))
#That will produce the same output.

P = N.float()
P /= P.sum(1, keepdim=True)
g = torch.Generator().manual_seed(2147483647)

for i in range(10):
    out = []
    ix = 0
    while True:
        p = P[ix]
        ix = torch.multinomial(p, num_samples=1, replacement=True, generator=g).item()
        out.append(itos[ix])
        if ix == 0:
            break
    print(''.join(out))

Evaluating our model

import torch
N = torch.zeros((27, 27), dtype = torch.int32)

chars = sorted(list(set(''.join(words))))

stoi = {s:i+1 for i,s in enumerate(chars)}
stoi['.'] = 0
itos = {i:s for s,i in stoi.items()}
P = N.float()
P /= P.sum(1, keepdim=True)
log_likelihood = 0.0
n = 0

for word in words[:3]:
    chs = ['.'] + list(word) + ['.']
    for ch1, ch2 in zip(chs, chs[1:]):
        ix1 = stoi[ch1]
        ix2 = stoi[ch2]
        prob = P[ix1, ix2] #Likelihood - product of all the values
        logprob = torch.log(prob) #Log Likelihood
        log_likelihood += logprob #Log Likelihood - Adding the logs of all probability values
        n += 1 #Log Likelihood - For the average
        print(f'{ch1}{ch2}: {prob:.4f} {logprob: .4f}')

print(f'{log_likelihood=}')
nll = -log_likelihood #Negative Log Likelihood
print(f'{nll=}')
print(f'{nll/n}') #Negative Log Likelihood - Average value
Output
.e: 0.0478 -3.0408
em: 0.0377 -3.2793
mm: 0.0253 -3.6772
ma: 0.3899 -0.9418
a.: 0.1960 -1.6299
.o: 0.0123 -4.3982
ol: 0.0780 -2.5508
li: 0.1777 -1.7278
iv: 0.0152 -4.1867
vi: 0.3541 -1.0383
ia: 0.1381 -1.9796
a.: 0.1960 -1.6299
.a: 0.1377 -1.9829
av: 0.0246 -3.7045
va: 0.2495 -1.3882
a.: 0.1960 -1.6299
log_likelihood=tensor(-38.7856)
nll=tensor(38.7856)
2.424102306365967
#Model smoothening
P = (N+1).float()
P /= P.sum(1, keepdim=True)
log_likelihood = 0.0
n = 0

# for word in words[:3]:
for word in ["muzzammil"]:
    chs = ['.'] + list(word) + ['.']
    for ch1, ch2 in zip(chs, chs[1:]):
        ix1 = stoi[ch1]
        ix2 = stoi[ch2]
        prob = P[ix1, ix2] #Likelihood - product of all the values
        logprob = torch.log(prob) #Log Likelihood
        log_likelihood += logprob #Log Likelihood - Adding the logs of all probability values
        n += 1 #Log Likelihood - For the average
        print(f'{ch1}{ch2}: {prob:.4f} {logprob: .4f}')

print(f'{log_likelihood=}')
nll = -log_likelihood #Negative Log Likelihood
print(f'{nll=}')
print(f'{nll/n}') #Negative Log Likelihood - Average value
Output
.m: 0.0792 -2.5358
mu: 0.0210 -3.8636
uz: 0.0145 -4.2303
zz: 0.0190 -3.9649
za: 0.3551 -1.0355
am: 0.0482 -3.0321
mm: 0.0253 -3.6753
mi: 0.1885 -1.6687
il: 0.0759 -2.5780
l.: 0.0940 -2.3641
log_likelihood=tensor(-28.9485)
nll=tensor(28.9485)
2.894852876663208