tensor-sensor

Форк
0
/
playground.ipynb 
1124 строки · 61.5 Кб
1
{
2
 "cells": [
3
  {
4
   "cell_type": "code",
5
   "execution_count": 8,
6
   "metadata": {},
7
   "outputs": [
8
    {
9
     "ename": "RuntimeError",
10
     "evalue": "mat1 and mat2 shapes cannot be multiplied (10x20 and 10x500)\nCause: @ on tensor operand W w/shape [10, 20] and operand X.T w/shape [10, 500]",
11
     "output_type": "error",
12
     "traceback": [
13
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
14
      "\u001b[0;31mRuntimeError\u001b[0m                              Traceback (most recent call last)",
15
      "\u001b[0;32m/var/folders/93/9kzk2ccm8xj8k70059b28jk80000gp/T/ipykernel_55352/3024069779.py\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[1;32m      5\u001b[0m \u001b[0mX\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mtorch\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mrand\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m500\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m10\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m      6\u001b[0m \u001b[0;32mwith\u001b[0m \u001b[0mtsensor\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mexplain\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0msavefig\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;34m'/tmp/mm.pdf'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 7\u001b[0;31m     \u001b[0mY\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mW\u001b[0m \u001b[0;34m@\u001b[0m \u001b[0mX\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mT\u001b[0m \u001b[0;34m+\u001b[0m \u001b[0mb\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
16
      "\u001b[0;31mRuntimeError\u001b[0m: mat1 and mat2 shapes cannot be multiplied (10x20 and 10x500)\nCause: @ on tensor operand W w/shape [10, 20] and operand X.T w/shape [10, 500]"
17
     ]
18
    }
19
   ],
20
   "source": [
21
    "import torch\n",
22
    "import tsensor\n",
23
    "W = torch.rand(10,20)\n",
24
    "b = torch.rand(10,1)\n",
25
    "X = torch.rand(500,10)\n",
26
    "with tsensor.explain(savefig='/tmp/mm.svg'):\n",
27
    "    Y = W @ X.T + b"
28
   ]
29
  },
30
  {
31
   "cell_type": "code",
32
   "execution_count": 1,
33
   "metadata": {},
34
   "outputs": [
35
    {
36
     "data": {
37
      "image/svg+xml": [
38
       "<svg xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" height=\"52.941034pt\" version=\"1.1\" viewBox=\"0 0 310.923169 52.941034\" width=\"310.923169pt\">\n",
39
       " <metadata>\n",
40
       "  <rdf:RDF xmlns:cc=\"http://creativecommons.org/ns#\" xmlns:dc=\"http://purl.org/dc/elements/1.1/\" xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\">\n",
41
       "   <cc:Work>\n",
42
       "    <dc:type rdf:resource=\"http://purl.org/dc/dcmitype/StillImage\"/>\n",
43
       "    <dc:date>2021-12-11T13:06:18.685523</dc:date>\n",
44
       "    <dc:format>image/svg+xml</dc:format>\n",
45
       "    <dc:creator>\n",
46
       "     <cc:Agent>\n",
47
       "      <dc:title>Matplotlib v3.3.4, https://matplotlib.org/</dc:title>\n",
48
       "     </cc:Agent>\n",
49
       "    </dc:creator>\n",
50
       "   </cc:Work>\n",
51
       "  </rdf:RDF>\n",
52
       " </metadata>\n",
53
       " <defs>\n",
54
       "  <style type=\"text/css\">*{stroke-linecap:butt;stroke-linejoin:round;}</style>\n",
55
       " </defs>\n",
56
       " <g id=\"figure_1\">\n",
57
       "  <g id=\"patch_1\">\n",
58
       "   <path d=\"M 0 52.941034  L 310.923169 52.941034  L 310.923169 0  L 0 0  z \" style=\"fill:none;\"/>\n",
59
       "  </g>\n",
60
       "  <g id=\"axes_1\">\n",
61
       "   <g id=\"patch_2\">\n",
62
       "    <path clip-path=\"url(#pf8405f6392)\" d=\"M 109.57446 45.180316  L 114.79734 45.180316  L 114.79734 22.919896  L 109.57446 22.919896  z \" style=\"fill:#7fa4d3;fill-opacity:0.75;stroke:#808080;stroke-linejoin:miter;stroke-width:0.7;\"/>\n",
63
       "   </g>\n",
64
       "   <g id=\"patch_3\">\n",
65
       "    <path clip-path=\"url(#pf8405f6392)\" d=\"M 143.72406 45.180316  L 148.94694 45.180316  L 148.94694 22.919896  L 143.72406 22.919896  z \" style=\"fill:#7fa4d3;fill-opacity:0.75;stroke:#808080;stroke-linejoin:miter;stroke-width:0.7;\"/>\n",
66
       "   </g>\n",
67
       "   <g id=\"line2d_1\">\n",
68
       "    <path clip-path=\"url(#pf8405f6392)\" d=\"M 108.26874 13.192653  L 116.10306 13.192653  \" style=\"fill:none;stroke:#c2c2c2;stroke-linecap:square;stroke-width:0.5;\"/>\n",
69
       "   </g>\n",
70
       "   <g id=\"line2d_2\">\n",
71
       "    <path clip-path=\"url(#pf8405f6392)\" d=\"M 142.41834 13.192653  L 150.25266 13.192653  \" style=\"fill:none;stroke:#c2c2c2;stroke-linecap:square;stroke-width:0.5;\"/>\n",
72
       "   </g>\n",
73
       "   <g id=\"text_1\">\n",
74
       "    <!-- W -->\n",
75
       "    <g style=\"fill:#b4b4b4;\" transform=\"translate(6.975 9.451406)scale(0.13 -0.13)\">\n",
76
       "     <defs>\n",
77
       "      <path d=\"M 52.78125 63.8125  L 48.484375 0  L 37.203125 0  L 29.296875 22.703125  L 27.09375 29.984375  L 24.8125 22.125  L 17.484375 0  L 6.59375 0  L 2.203125 63.8125  L 10.40625 63.8125  L 12.890625 20.40625  L 13.625 8.5  L 16.703125 18.609375  L 24.421875 42.28125  L 30.421875 42.28125  L 39.3125 16.890625  L 42.28125 8.5  L 42.484375 17.28125  L 45.015625 63.8125  z \" id=\"Consolas-87\"/>\n",
78
       "     </defs>\n",
79
       "     <use xlink:href=\"#Consolas-87\"/>\n",
80
       "    </g>\n",
81
       "   </g>\n",
82
       "   <g id=\"text_2\">\n",
83
       "    <!--   -->\n",
84
       "    <g style=\"fill:#b4b4b4;\" transform=\"translate(13.5036 9.451406)scale(0.13 -0.13)\">\n",
85
       "     <defs>\n",
86
       "      <path id=\"Consolas-32\"/>\n",
87
       "     </defs>\n",
88
       "     <use xlink:href=\"#Consolas-32\"/>\n",
89
       "    </g>\n",
90
       "   </g>\n",
91
       "   <g id=\"text_3\">\n",
92
       "    <!-- @ -->\n",
93
       "    <g style=\"fill:#b4b4b4;\" transform=\"translate(20.0322 9.451406)scale(0.13 -0.13)\">\n",
94
       "     <defs>\n",
95
       "      <path d=\"M 38.09375 19.921875  Q 37.640625 16.796875 37.59375 14.75  Q 37.546875 12.703125 37.859375 11.53125  Q 38.1875 10.359375 38.859375 9.890625  Q 39.546875 9.421875 40.578125 9.421875  Q 41.9375 9.421875 43.15625 10.859375  Q 44.390625 12.3125 45.34375 15.3125  Q 46.296875 18.3125 46.84375 22.890625  Q 47.40625 27.484375 47.40625 33.796875  Q 47.40625 40.828125 46.53125 46.34375  Q 45.65625 51.859375 43.75 55.71875  Q 41.84375 59.578125 38.796875 61.59375  Q 35.75 63.625 31.390625 63.625  Q 28.078125 63.625 24.96875 61.9375  Q 21.875 60.25 19.203125 57.203125  Q 16.546875 54.15625 14.375 49.875  Q 12.203125 45.609375 10.65625 40.421875  Q 9.125 35.25 8.265625 29.296875  Q 7.421875 23.34375 7.421875 16.890625  Q 7.421875 1.171875 12.15625 -6.5625  Q 16.890625 -14.3125 25.484375 -14.3125  Q 29.828125 -14.3125 33.59375 -13.3125  Q 37.359375 -12.3125 41.40625 -10.296875  L 41.40625 -16.609375  Q 37.40625 -18.453125 33.390625 -19.375  Q 29.390625 -20.3125 25 -20.3125  Q 12.546875 -20.3125 6.5625 -10.984375  Q 0.59375 -1.65625 0.484375 16.796875  Q 0.4375 24.171875 1.453125 30.9375  Q 2.484375 37.703125 4.40625 43.5625  Q 6.34375 49.421875 9.09375 54.21875  Q 11.859375 59.03125 15.34375 62.421875  Q 18.84375 65.828125 22.9375 67.703125  Q 27.046875 69.578125 31.6875 69.578125  Q 37.5 69.578125 41.765625 67.203125  Q 46.046875 64.84375 48.828125 60.375  Q 51.609375 55.90625 52.953125 49.40625  Q 54.296875 42.921875 54.296875 34.625  Q 54.25 26.21875 53.203125 20.234375  Q 52.15625 14.265625 50.25 10.453125  Q 48.34375 6.640625 45.625 4.859375  Q 42.921875 3.078125 39.59375 3.078125  Q 35.796875 3.078125 33.9375 5.03125  Q 32.078125 6.984375 32.078125 10.296875  Q 30.21875 6.546875 28.0625 4.8125  Q 25.921875 3.078125 22.90625 3.078125  Q 18.3125 3.078125 16.015625 6.515625  Q 13.71875 9.96875 13.71875 17.390625  Q 13.71875 20.0625 14.03125 23.140625  Q 14.359375 26.21875 15.109375 29.3125  Q 15.875 32.421875 17.09375 35.28125  Q 18.3125 38.140625 20.09375 40.3125  Q 21.875 42.484375 24.3125 43.796875  Q 26.765625 45.125 29.890625 45.125  Q 31.734375 45.125 33.34375 44.671875  Q 34.96875 44.234375 35.6875 43.796875  L 42 45.3125  z M 33.109375 37.3125  Q 32.671875 37.890625 31.765625 38.34375  Q 30.859375 38.8125 29.5 38.8125  Q 28.03125 38.8125 26.859375 37.78125  Q 25.6875 36.765625 24.78125 35.046875  Q 23.875 33.34375 23.265625 31.171875  Q 22.65625 29 22.234375 26.703125  Q 21.828125 24.421875 21.65625 22.1875  Q 21.484375 19.96875 21.484375 18.21875  Q 21.484375 13.53125 22.0625 11.46875  Q 22.65625 9.421875 24.3125 9.421875  Q 24.90625 9.421875 25.484375 9.65625  Q 26.078125 9.90625 26.734375 10.609375  Q 27.390625 11.328125 28.171875 12.59375  Q 28.953125 13.875 29.890625 15.921875  z \" id=\"Consolas-64\"/>\n",
96
       "     </defs>\n",
97
       "     <use xlink:href=\"#Consolas-64\"/>\n",
98
       "    </g>\n",
99
       "   </g>\n",
100
       "   <g id=\"text_4\">\n",
101
       "    <!--   -->\n",
102
       "    <g style=\"fill:#b4b4b4;\" transform=\"translate(26.5608 9.451406)scale(0.13 -0.13)\">\n",
103
       "     <use xlink:href=\"#Consolas-32\"/>\n",
104
       "    </g>\n",
105
       "   </g>\n",
106
       "   <g id=\"text_5\">\n",
107
       "    <!-- t -->\n",
108
       "    <g style=\"fill:#444443;\" transform=\"translate(33.0894 9.451406)scale(0.13 -0.13)\">\n",
109
       "     <defs>\n",
110
       "      <path d=\"M 47.3125 0.6875  Q 44.4375 -0.046875 41.359375 -0.359375  Q 38.28125 -0.6875 35.109375 -0.6875  Q 25.875 -0.6875 21.328125 3.484375  Q 16.796875 7.671875 16.796875 16.3125  L 16.796875 41.890625  L 3.078125 41.890625  L 3.078125 49.03125  L 16.796875 49.03125  L 16.796875 62.5  L 25.296875 64.703125  L 25.296875 49.03125  L 47.3125 49.03125  L 47.3125 41.890625  L 25.296875 41.890625  L 25.296875 17  Q 25.296875 11.71875 28.09375 9.109375  Q 30.90625 6.5 36.375 6.5  Q 38.71875 6.5 41.5 6.859375  Q 44.28125 7.234375 47.3125 8.015625  z \" id=\"Consolas-116\"/>\n",
111
       "     </defs>\n",
112
       "     <use xlink:href=\"#Consolas-116\"/>\n",
113
       "    </g>\n",
114
       "   </g>\n",
115
       "   <g id=\"text_6\">\n",
116
       "    <!-- o -->\n",
117
       "    <g style=\"fill:#444443;\" transform=\"translate(39.618 9.451406)scale(0.13 -0.13)\">\n",
118
       "     <defs>\n",
119
       "      <path d=\"M 50.484375 24.90625  Q 50.484375 19.1875 48.875 14.421875  Q 47.265625 9.671875 44.234375 6.265625  Q 41.21875 2.875 36.859375 1  Q 32.515625 -0.875 27 -0.875  Q 21.734375 -0.875 17.546875 0.75  Q 13.375 2.390625 10.46875 5.5625  Q 7.5625 8.734375 6.03125 13.421875  Q 4.5 18.109375 4.5 24.21875  Q 4.5 29.9375 6.109375 34.640625  Q 7.71875 39.359375 10.734375 42.75  Q 13.765625 46.140625 18.109375 48.015625  Q 22.46875 49.90625 27.984375 49.90625  Q 33.25 49.90625 37.421875 48.265625  Q 41.609375 46.625 44.5 43.46875  Q 47.40625 40.328125 48.9375 35.640625  Q 50.484375 30.953125 50.484375 24.90625  z M 41.796875 24.515625  Q 41.796875 29.046875 40.796875 32.4375  Q 39.796875 35.84375 37.9375 38.109375  Q 36.078125 40.375 33.421875 41.515625  Q 30.765625 42.671875 27.484375 42.671875  Q 23.6875 42.671875 20.96875 41.1875  Q 18.265625 39.703125 16.53125 37.203125  Q 14.796875 34.71875 13.984375 31.421875  Q 13.1875 28.125 13.1875 24.515625  Q 13.1875 19.96875 14.1875 16.546875  Q 15.1875 13.140625 17.03125 10.859375  Q 18.890625 8.59375 21.53125 7.4375  Q 24.171875 6.296875 27.484375 6.296875  Q 31.296875 6.296875 34 7.78125  Q 36.71875 9.28125 38.453125 11.765625  Q 40.1875 14.265625 40.984375 17.546875  Q 41.796875 20.84375 41.796875 24.515625  z \" id=\"Consolas-111\"/>\n",
120
       "     </defs>\n",
121
       "     <use xlink:href=\"#Consolas-111\"/>\n",
122
       "    </g>\n",
123
       "   </g>\n",
124
       "   <g id=\"text_7\">\n",
125
       "    <!-- r -->\n",
126
       "    <g style=\"fill:#444443;\" transform=\"translate(46.1466 9.451406)scale(0.13 -0.13)\">\n",
127
       "     <defs>\n",
128
       "      <path d=\"M 9.8125 49.03125  L 17.578125 49.03125  L 17.828125 39.984375  Q 22.171875 45.21875 26.390625 47.5625  Q 30.609375 49.90625 34.90625 49.90625  Q 42.53125 49.90625 46.453125 44.96875  Q 50.390625 40.046875 50.09375 30.328125  L 41.5 30.328125  Q 41.65625 36.765625 39.625 39.671875  Q 37.59375 42.578125 33.6875 42.578125  Q 31.984375 42.578125 30.25 41.96875  Q 28.515625 41.359375 26.6875 40.015625  Q 24.859375 38.671875 22.796875 36.5625  Q 20.75 34.46875 18.40625 31.5  L 18.40625 0  L 9.8125 0  z \" id=\"Consolas-114\"/>\n",
129
       "     </defs>\n",
130
       "     <use xlink:href=\"#Consolas-114\"/>\n",
131
       "    </g>\n",
132
       "   </g>\n",
133
       "   <g id=\"text_8\">\n",
134
       "    <!-- c -->\n",
135
       "    <g style=\"fill:#444443;\" transform=\"translate(52.6752 9.451406)scale(0.13 -0.13)\">\n",
136
       "     <defs>\n",
137
       "      <path d=\"M 45.90625 1.8125  Q 42.578125 0.53125 39.078125 -0.078125  Q 35.59375 -0.6875 31.890625 -0.6875  Q 20.265625 -0.6875 13.984375 5.609375  Q 7.71875 11.921875 7.71875 24.03125  Q 7.71875 29.828125 9.515625 34.5625  Q 11.328125 39.3125 14.59375 42.671875  Q 17.875 46.046875 22.40625 47.875  Q 26.953125 49.703125 32.421875 49.703125  Q 36.234375 49.703125 39.546875 49.171875  Q 42.875 48.640625 45.90625 47.40625  L 45.90625 39.3125  Q 42.71875 40.96875 39.421875 41.71875  Q 36.140625 42.484375 32.625 42.484375  Q 29.34375 42.484375 26.4375 41.234375  Q 23.53125 39.984375 21.328125 37.640625  Q 19.140625 35.296875 17.875 31.921875  Q 16.609375 28.5625 16.609375 24.3125  Q 16.609375 15.4375 20.921875 11.015625  Q 25.25 6.59375 32.90625 6.59375  Q 36.375 6.59375 39.625 7.375  Q 42.875 8.15625 45.90625 9.71875  z \" id=\"Consolas-99\"/>\n",
138
       "     </defs>\n",
139
       "     <use xlink:href=\"#Consolas-99\"/>\n",
140
       "    </g>\n",
141
       "   </g>\n",
142
       "   <g id=\"text_9\">\n",
143
       "    <!-- h -->\n",
144
       "    <g style=\"fill:#444443;\" transform=\"translate(59.2038 9.451406)scale(0.13 -0.13)\">\n",
145
       "     <defs>\n",
146
       "      <path d=\"M 47.21875 0  L 38.71875 0  L 38.71875 31.296875  Q 38.71875 36.96875 36.59375 39.765625  Q 34.46875 42.578125 30.515625 42.578125  Q 28.8125 42.578125 27.3125 42.109375  Q 25.828125 41.65625 24.21875 40.5  Q 22.609375 39.359375 20.703125 37.40625  Q 18.796875 35.453125 16.3125 32.515625  L 16.3125 0  L 7.8125 0  L 7.8125 69  L 16.3125 69  L 16.3125 49.03125  L 16.015625 41.3125  Q 18.015625 43.703125 19.9375 45.328125  Q 21.875 46.96875 23.796875 48  Q 25.734375 49.03125 27.734375 49.46875  Q 29.734375 49.90625 31.890625 49.90625  Q 39.203125 49.90625 43.203125 45.4375  Q 47.21875 40.96875 47.21875 31.984375  z \" id=\"Consolas-104\"/>\n",
147
       "     </defs>\n",
148
       "     <use xlink:href=\"#Consolas-104\"/>\n",
149
       "    </g>\n",
150
       "   </g>\n",
151
       "   <g id=\"text_10\">\n",
152
       "    <!-- . -->\n",
153
       "    <g style=\"fill:#444443;\" transform=\"translate(65.7324 9.451406)scale(0.13 -0.13)\">\n",
154
       "     <defs>\n",
155
       "      <path d=\"M 27.09375 15.375  Q 28.765625 15.375 30.25 14.734375  Q 31.734375 14.109375 32.828125 12.984375  Q 33.9375 11.859375 34.5625 10.375  Q 35.203125 8.890625 35.203125 7.171875  Q 35.203125 5.515625 34.5625 4.046875  Q 33.9375 2.59375 32.828125 1.484375  Q 31.734375 0.390625 30.25 -0.234375  Q 28.765625 -0.875 27.09375 -0.875  Q 25.390625 -0.875 23.921875 -0.234375  Q 22.46875 0.390625 21.359375 1.484375  Q 20.265625 2.59375 19.625 4.046875  Q 19 5.515625 19 7.171875  Q 19 8.890625 19.625 10.375  Q 20.265625 11.859375 21.359375 12.984375  Q 22.46875 14.109375 23.921875 14.734375  Q 25.390625 15.375 27.09375 15.375  z \" id=\"Consolas-46\"/>\n",
156
       "     </defs>\n",
157
       "     <use xlink:href=\"#Consolas-46\"/>\n",
158
       "    </g>\n",
159
       "   </g>\n",
160
       "   <g id=\"text_11\">\n",
161
       "    <!-- d -->\n",
162
       "    <g style=\"fill:#a40227;\" transform=\"translate(72.261 9.451406)scale(0.13 -0.13)\">\n",
163
       "     <defs>\n",
164
       "      <path d=\"M 5.328125 23.484375  Q 5.328125 29.734375 7.03125 34.59375  Q 8.734375 39.453125 11.890625 42.796875  Q 15.046875 46.140625 19.453125 47.875  Q 23.875 49.609375 29.296875 49.609375  Q 31.640625 49.609375 33.90625 49.3125  Q 36.1875 49.03125 38.375 48.390625  L 38.375 69  L 46.921875 69  L 46.921875 0  L 39.3125 0  L 39.015625 9.28125  Q 35.453125 4.109375 31.296875 1.609375  Q 27.15625 -0.875 22.3125 -0.875  Q 18.109375 -0.875 14.90625 0.875  Q 11.71875 2.640625 9.59375 5.828125  Q 7.46875 9.03125 6.390625 13.515625  Q 5.328125 18.015625 5.328125 23.484375  z M 14.015625 24.03125  Q 14.015625 15.140625 16.625 10.765625  Q 19.234375 6.390625 24.03125 6.390625  Q 27.25 6.390625 30.828125 9.265625  Q 34.421875 12.15625 38.375 17.828125  L 38.375 40.578125  Q 36.28125 41.546875 33.734375 42.0625  Q 31.203125 42.578125 28.71875 42.578125  Q 21.78125 42.578125 17.890625 38.078125  Q 14.015625 33.59375 14.015625 24.03125  z \" id=\"Consolas-100\"/>\n",
165
       "     </defs>\n",
166
       "     <use xlink:href=\"#Consolas-100\"/>\n",
167
       "    </g>\n",
168
       "   </g>\n",
169
       "   <g id=\"text_12\">\n",
170
       "    <!-- o -->\n",
171
       "    <g style=\"fill:#a40227;\" transform=\"translate(78.7896 9.451406)scale(0.13 -0.13)\">\n",
172
       "     <use xlink:href=\"#Consolas-111\"/>\n",
173
       "    </g>\n",
174
       "   </g>\n",
175
       "   <g id=\"text_13\">\n",
176
       "    <!-- t -->\n",
177
       "    <g style=\"fill:#a40227;\" transform=\"translate(85.3182 9.451406)scale(0.13 -0.13)\">\n",
178
       "     <use xlink:href=\"#Consolas-116\"/>\n",
179
       "    </g>\n",
180
       "   </g>\n",
181
       "   <g id=\"text_14\">\n",
182
       "    <!-- ( -->\n",
183
       "    <g style=\"fill:#a40227;\" transform=\"translate(91.8468 9.451406)scale(0.13 -0.13)\">\n",
184
       "     <defs>\n",
185
       "      <path d=\"M 37.15625 -20.515625  Q 14.65625 0.34375 14.65625 25.59375  Q 14.65625 31.5 15.84375 37.375  Q 17.046875 43.265625 19.703125 49.171875  Q 22.359375 55.078125 26.734375 60.984375  Q 31.109375 66.890625 37.359375 72.703125  L 42.28125 67.671875  Q 23.34375 48.96875 23.34375 26.21875  Q 23.34375 14.890625 28.125 4.4375  Q 32.90625 -6 42.28125 -15.28125  z \" id=\"Consolas-40\"/>\n",
186
       "     </defs>\n",
187
       "     <use xlink:href=\"#Consolas-40\"/>\n",
188
       "    </g>\n",
189
       "   </g>\n",
190
       "   <g id=\"text_15\">\n",
191
       "    <!-- b -->\n",
192
       "    <g style=\"fill:#444443;\" transform=\"translate(108.9216 9.451406)scale(0.13 -0.13)\">\n",
193
       "     <defs>\n",
194
       "      <path d=\"M 49.703125 25.390625  Q 49.703125 19.28125 48.015625 14.46875  Q 46.34375 9.671875 43.234375 6.328125  Q 40.140625 2.984375 35.6875 1.1875  Q 31.25 -0.59375 25.6875 -0.59375  Q 21.34375 -0.59375 16.796875 0.234375  Q 12.25 1.078125 7.8125 2.875  L 7.8125 69  L 16.3125 69  L 16.3125 50  L 15.921875 40.921875  Q 19.578125 45.84375 23.75 47.875  Q 27.9375 49.90625 32.71875 49.90625  Q 36.921875 49.90625 40.09375 48.140625  Q 43.265625 46.390625 45.40625 43.1875  Q 47.5625 39.984375 48.625 35.46875  Q 49.703125 30.953125 49.703125 25.390625  z M 41.015625 25  Q 41.015625 28.859375 40.453125 32.078125  Q 39.890625 35.296875 38.671875 37.59375  Q 37.453125 39.890625 35.546875 41.1875  Q 33.640625 42.484375 31 42.484375  Q 29.390625 42.484375 27.734375 41.96875  Q 26.078125 41.453125 24.296875 40.25  Q 22.515625 39.0625 20.53125 37.078125  Q 18.5625 35.109375 16.3125 32.171875  L 16.3125 8.40625  Q 18.796875 7.421875 21.375 6.859375  Q 23.96875 6.296875 26.3125 6.296875  Q 29.25 6.296875 31.90625 7.21875  Q 34.578125 8.15625 36.59375 10.328125  Q 38.625 12.5 39.8125 16.078125  Q 41.015625 19.671875 41.015625 25  z \" id=\"Consolas-98\"/>\n",
195
       "     </defs>\n",
196
       "     <use xlink:href=\"#Consolas-98\"/>\n",
197
       "    </g>\n",
198
       "   </g>\n",
199
       "   <g id=\"text_16\">\n",
200
       "    <!-- , -->\n",
201
       "    <g style=\"fill:#444443;\" transform=\"translate(125.9964 9.451406)scale(0.13 -0.13)\">\n",
202
       "     <defs>\n",
203
       "      <path d=\"M 11.625 -10.015625  Q 14.109375 -10.109375 16.453125 -9.5625  Q 18.796875 -9.03125 20.578125 -7.984375  Q 22.359375 -6.9375 23.4375 -5.375  Q 24.515625 -3.8125 24.515625 -1.8125  Q 24.515625 0.25 23.796875 1.515625  Q 23.09375 2.78125 22.21875 3.859375  Q 21.34375 4.9375 20.625 6.15625  Q 19.921875 7.375 19.921875 9.421875  Q 19.921875 10.453125 20.3125 11.578125  Q 20.703125 12.703125 21.5 13.625  Q 22.3125 14.546875 23.5625 15.125  Q 24.8125 15.71875 26.515625 15.71875  Q 28.21875 15.71875 29.796875 15.015625  Q 31.390625 14.3125 32.59375 12.84375  Q 33.796875 11.375 34.5 9.171875  Q 35.203125 6.984375 35.203125 4  Q 35.203125 -0.046875 33.71875 -3.78125  Q 32.234375 -7.515625 29.265625 -10.390625  Q 26.3125 -13.28125 21.890625 -14.984375  Q 17.484375 -16.703125 11.625 -16.703125  z \" id=\"Consolas-44\"/>\n",
204
       "     </defs>\n",
205
       "     <use xlink:href=\"#Consolas-44\"/>\n",
206
       "    </g>\n",
207
       "   </g>\n",
208
       "   <g id=\"text_17\">\n",
209
       "    <!-- b -->\n",
210
       "    <g style=\"fill:#444443;\" transform=\"translate(143.0712 9.451406)scale(0.13 -0.13)\">\n",
211
       "     <use xlink:href=\"#Consolas-98\"/>\n",
212
       "    </g>\n",
213
       "   </g>\n",
214
       "   <g id=\"text_18\">\n",
215
       "    <!-- ) -->\n",
216
       "    <g style=\"fill:#a40227;\" transform=\"translate(160.146 9.451406)scale(0.13 -0.13)\">\n",
217
       "     <defs>\n",
218
       "      <path d=\"M 17.828125 72.703125  Q 40.328125 51.859375 40.328125 26.3125  Q 40.328125 21.046875 39.25 15.375  Q 38.1875 9.71875 35.59375 3.78125  Q 33.015625 -2.15625 28.640625 -8.25  Q 24.265625 -14.359375 17.625 -20.515625  L 12.703125 -15.484375  Q 22.21875 -6.0625 26.90625 4.1875  Q 31.59375 14.453125 31.59375 25.6875  Q 31.59375 48.921875 12.703125 67.484375  z \" id=\"Consolas-41\"/>\n",
219
       "     </defs>\n",
220
       "     <use xlink:href=\"#Consolas-41\"/>\n",
221
       "    </g>\n",
222
       "   </g>\n",
223
       "   <g id=\"text_19\">\n",
224
       "    <!-- + -->\n",
225
       "    <g style=\"fill:#b4b4b4;\" transform=\"translate(166.6746 9.451406)scale(0.13 -0.13)\">\n",
226
       "     <defs>\n",
227
       "      <path d=\"M 50.875 22.90625  L 31.6875 22.90625  L 31.6875 2.984375  L 23.296875 2.984375  L 23.296875 22.90625  L 4.109375 22.90625  L 4.109375 30.328125  L 23.296875 30.328125  L 23.296875 50.09375  L 31.6875 50.09375  L 31.6875 30.328125  L 50.875 30.328125  z \" id=\"Consolas-43\"/>\n",
228
       "     </defs>\n",
229
       "     <use xlink:href=\"#Consolas-43\"/>\n",
230
       "    </g>\n",
231
       "   </g>\n",
232
       "   <g id=\"text_20\">\n",
233
       "    <!--   -->\n",
234
       "    <g style=\"fill:#b4b4b4;\" transform=\"translate(173.2032 9.451406)scale(0.13 -0.13)\">\n",
235
       "     <use xlink:href=\"#Consolas-32\"/>\n",
236
       "    </g>\n",
237
       "   </g>\n",
238
       "   <g id=\"text_21\">\n",
239
       "    <!-- t -->\n",
240
       "    <g style=\"fill:#b4b4b4;\" transform=\"translate(179.7318 9.451406)scale(0.13 -0.13)\">\n",
241
       "     <use xlink:href=\"#Consolas-116\"/>\n",
242
       "    </g>\n",
243
       "   </g>\n",
244
       "   <g id=\"text_22\">\n",
245
       "    <!-- o -->\n",
246
       "    <g style=\"fill:#b4b4b4;\" transform=\"translate(186.2604 9.451406)scale(0.13 -0.13)\">\n",
247
       "     <use xlink:href=\"#Consolas-111\"/>\n",
248
       "    </g>\n",
249
       "   </g>\n",
250
       "   <g id=\"text_23\">\n",
251
       "    <!-- r -->\n",
252
       "    <g style=\"fill:#b4b4b4;\" transform=\"translate(192.789 9.451406)scale(0.13 -0.13)\">\n",
253
       "     <use xlink:href=\"#Consolas-114\"/>\n",
254
       "    </g>\n",
255
       "   </g>\n",
256
       "   <g id=\"text_24\">\n",
257
       "    <!-- c -->\n",
258
       "    <g style=\"fill:#b4b4b4;\" transform=\"translate(199.3176 9.451406)scale(0.13 -0.13)\">\n",
259
       "     <use xlink:href=\"#Consolas-99\"/>\n",
260
       "    </g>\n",
261
       "   </g>\n",
262
       "   <g id=\"text_25\">\n",
263
       "    <!-- h -->\n",
264
       "    <g style=\"fill:#b4b4b4;\" transform=\"translate(205.8462 9.451406)scale(0.13 -0.13)\">\n",
265
       "     <use xlink:href=\"#Consolas-104\"/>\n",
266
       "    </g>\n",
267
       "   </g>\n",
268
       "   <g id=\"text_26\">\n",
269
       "    <!-- . -->\n",
270
       "    <g style=\"fill:#b4b4b4;\" transform=\"translate(212.3748 9.451406)scale(0.13 -0.13)\">\n",
271
       "     <use xlink:href=\"#Consolas-46\"/>\n",
272
       "    </g>\n",
273
       "   </g>\n",
274
       "   <g id=\"text_27\">\n",
275
       "    <!-- e -->\n",
276
       "    <g style=\"fill:#b4b4b4;\" transform=\"translate(218.9034 9.451406)scale(0.13 -0.13)\">\n",
277
       "     <defs>\n",
278
       "      <path d=\"M 49.21875 27.09375  Q 49.21875 25.296875 49.171875 24.078125  Q 49.125 22.859375 49.03125 21.78125  L 14.59375 21.78125  Q 14.59375 14.265625 18.796875 10.234375  Q 23 6.203125 30.90625 6.203125  Q 33.0625 6.203125 35.203125 6.375  Q 37.359375 6.546875 39.359375 6.828125  Q 41.359375 7.125 43.1875 7.484375  Q 45.015625 7.859375 46.578125 8.296875  L 46.578125 1.3125  Q 43.109375 0.34375 38.734375 -0.265625  Q 34.375 -0.875 29.6875 -0.875  Q 23.390625 -0.875 18.84375 0.828125  Q 14.3125 2.546875 11.40625 5.78125  Q 8.5 9.03125 7.109375 13.734375  Q 5.71875 18.453125 5.71875 24.421875  Q 5.71875 29.59375 7.203125 34.203125  Q 8.6875 38.8125 11.546875 42.296875  Q 14.40625 45.796875 18.546875 47.84375  Q 22.703125 49.90625 27.984375 49.90625  Q 33.109375 49.90625 37.0625 48.296875  Q 41.015625 46.6875 43.71875 43.71875  Q 46.4375 40.765625 47.828125 36.546875  Q 49.21875 32.328125 49.21875 27.09375  z M 40.375 28.328125  Q 40.53125 31.59375 39.75 34.296875  Q 38.96875 37.015625 37.328125 38.96875  Q 35.6875 40.921875 33.25 42.015625  Q 30.8125 43.109375 27.59375 43.109375  Q 24.8125 43.109375 22.515625 42.03125  Q 20.21875 40.96875 18.546875 39.015625  Q 16.890625 37.0625 15.859375 34.328125  Q 14.84375 31.59375 14.59375 28.328125  z \" id=\"Consolas-101\"/>\n",
279
       "     </defs>\n",
280
       "     <use xlink:href=\"#Consolas-101\"/>\n",
281
       "    </g>\n",
282
       "   </g>\n",
283
       "   <g id=\"text_28\">\n",
284
       "    <!-- y -->\n",
285
       "    <g style=\"fill:#b4b4b4;\" transform=\"translate(225.432 9.451406)scale(0.13 -0.13)\">\n",
286
       "     <defs>\n",
287
       "      <path d=\"M 51.421875 49.03125  L 34.71875 5.171875  Q 32.125 -1.515625 29.359375 -6.34375  Q 26.609375 -11.1875 23.328125 -14.28125  Q 20.0625 -17.390625 16.15625 -18.84375  Q 12.25 -20.3125 7.421875 -20.3125  Q 6.15625 -20.3125 5.125 -20.265625  Q 4.109375 -20.21875 2.875 -20.125  L 2.875 -12.40625  Q 3.953125 -12.546875 5.21875 -12.671875  Q 6.5 -12.796875 7.90625 -12.796875  Q 10.25 -12.796875 12.28125 -12.109375  Q 14.3125 -11.421875 16.109375 -9.875  Q 17.921875 -8.34375 19.578125 -5.90625  Q 21.234375 -3.46875 22.796875 0  L 3.21875 49.03125  L 12.890625 49.03125  L 25.296875 16.609375  L 27.78125 8.984375  L 30.609375 16.796875  L 42.09375 49.03125  z \" id=\"Consolas-121\"/>\n",
288
       "     </defs>\n",
289
       "     <use xlink:href=\"#Consolas-121\"/>\n",
290
       "    </g>\n",
291
       "   </g>\n",
292
       "   <g id=\"text_29\">\n",
293
       "    <!-- e -->\n",
294
       "    <g style=\"fill:#b4b4b4;\" transform=\"translate(231.9606 9.451406)scale(0.13 -0.13)\">\n",
295
       "     <use xlink:href=\"#Consolas-101\"/>\n",
296
       "    </g>\n",
297
       "   </g>\n",
298
       "   <g id=\"text_30\">\n",
299
       "    <!-- ( -->\n",
300
       "    <g style=\"fill:#b4b4b4;\" transform=\"translate(238.4892 9.451406)scale(0.13 -0.13)\">\n",
301
       "     <use xlink:href=\"#Consolas-40\"/>\n",
302
       "    </g>\n",
303
       "   </g>\n",
304
       "   <g id=\"text_31\">\n",
305
       "    <!-- 2 -->\n",
306
       "    <g style=\"fill:#b4b4b4;\" transform=\"translate(245.0178 9.451406)scale(0.13 -0.13)\">\n",
307
       "     <defs>\n",
308
       "      <path d=\"M 49.21875 0  L 7.171875 0  L 7.171875 7.625  L 23.6875 24.03125  Q 27.734375 28.03125 30.265625 30.953125  Q 32.8125 33.890625 34.21875 36.296875  Q 35.640625 38.71875 36.125 40.859375  Q 36.625 43.015625 36.625 45.515625  Q 36.625 47.859375 35.984375 49.96875  Q 35.359375 52.09375 34.03125 53.703125  Q 32.71875 55.328125 30.609375 56.25  Q 28.515625 57.171875 25.59375 57.171875  Q 21.53125 57.171875 18.203125 55.359375  Q 14.890625 53.5625 12.109375 50.6875  L 7.421875 56.296875  Q 11.03125 60.109375 15.734375 62.40625  Q 20.453125 64.703125 26.703125 64.703125  Q 30.953125 64.703125 34.4375 63.421875  Q 37.9375 62.15625 40.46875 59.765625  Q 43.015625 57.375 44.40625 53.90625  Q 45.796875 50.4375 45.796875 46.09375  Q 45.796875 42.4375 44.8125 39.3125  Q 43.84375 36.1875 41.859375 33.078125  Q 39.890625 29.984375 36.890625 26.65625  Q 33.890625 23.34375 29.78125 19.390625  L 18.21875 8.109375  L 49.21875 8.109375  z \" id=\"Consolas-50\"/>\n",
309
       "     </defs>\n",
310
       "     <use xlink:href=\"#Consolas-50\"/>\n",
311
       "    </g>\n",
312
       "   </g>\n",
313
       "   <g id=\"text_32\">\n",
314
       "    <!-- , -->\n",
315
       "    <g style=\"fill:#b4b4b4;\" transform=\"translate(251.5464 9.451406)scale(0.13 -0.13)\">\n",
316
       "     <use xlink:href=\"#Consolas-44\"/>\n",
317
       "    </g>\n",
318
       "   </g>\n",
319
       "   <g id=\"text_33\">\n",
320
       "    <!-- 2 -->\n",
321
       "    <g style=\"fill:#b4b4b4;\" transform=\"translate(258.075 9.451406)scale(0.13 -0.13)\">\n",
322
       "     <use xlink:href=\"#Consolas-50\"/>\n",
323
       "    </g>\n",
324
       "   </g>\n",
325
       "   <g id=\"text_34\">\n",
326
       "    <!-- ) -->\n",
327
       "    <g style=\"fill:#b4b4b4;\" transform=\"translate(264.6036 9.451406)scale(0.13 -0.13)\">\n",
328
       "     <use xlink:href=\"#Consolas-41\"/>\n",
329
       "    </g>\n",
330
       "   </g>\n",
331
       "   <g id=\"text_35\">\n",
332
       "    <!-- @ -->\n",
333
       "    <g style=\"fill:#b4b4b4;\" transform=\"translate(271.1322 9.451406)scale(0.13 -0.13)\">\n",
334
       "     <use xlink:href=\"#Consolas-64\"/>\n",
335
       "    </g>\n",
336
       "   </g>\n",
337
       "   <g id=\"text_36\">\n",
338
       "    <!-- x -->\n",
339
       "    <g style=\"fill:#b4b4b4;\" transform=\"translate(277.6608 9.451406)scale(0.13 -0.13)\">\n",
340
       "     <defs>\n",
341
       "      <path d=\"M 52 0  L 40.71875 0  L 27.484375 18.703125  L 14.40625 0  L 3.421875 0  L 22.40625 24.609375  L 4.296875 49.03125  L 15.1875 49.03125  L 28.078125 30.171875  L 40.71875 49.03125  L 51.21875 49.03125  L 32.8125 24.421875  z \" id=\"Consolas-120\"/>\n",
342
       "     </defs>\n",
343
       "     <use xlink:href=\"#Consolas-120\"/>\n",
344
       "    </g>\n",
345
       "   </g>\n",
346
       "   <g id=\"text_37\">\n",
347
       "    <!--   -->\n",
348
       "    <g style=\"fill:#b4b4b4;\" transform=\"translate(284.1894 9.451406)scale(0.13 -0.13)\">\n",
349
       "     <use xlink:href=\"#Consolas-32\"/>\n",
350
       "    </g>\n",
351
       "   </g>\n",
352
       "   <g id=\"text_38\">\n",
353
       "    <!-- + -->\n",
354
       "    <g style=\"fill:#b4b4b4;\" transform=\"translate(290.718 9.451406)scale(0.13 -0.13)\">\n",
355
       "     <use xlink:href=\"#Consolas-43\"/>\n",
356
       "    </g>\n",
357
       "   </g>\n",
358
       "   <g id=\"text_39\">\n",
359
       "    <!--   -->\n",
360
       "    <g style=\"fill:#b4b4b4;\" transform=\"translate(297.2466 9.451406)scale(0.13 -0.13)\">\n",
361
       "     <use xlink:href=\"#Consolas-32\"/>\n",
362
       "    </g>\n",
363
       "   </g>\n",
364
       "   <g id=\"text_40\">\n",
365
       "    <!-- z -->\n",
366
       "    <g style=\"fill:#b4b4b4;\" transform=\"translate(303.7752 9.451406)scale(0.13 -0.13)\">\n",
367
       "     <defs>\n",
368
       "      <path d=\"M 47.515625 0  L 8.015625 0  L 8.015625 6.109375  L 35.6875 41.890625  L 8.6875 41.890625  L 8.6875 49.03125  L 45.90625 49.03125  L 45.90625 42.390625  L 18.703125 7.171875  L 47.515625 7.171875  z \" id=\"Consolas-122\"/>\n",
369
       "     </defs>\n",
370
       "     <use xlink:href=\"#Consolas-122\"/>\n",
371
       "    </g>\n",
372
       "   </g>\n",
373
       "   <g id=\"text_41\">\n",
374
       "    <!-- 2 -->\n",
375
       "    <g transform=\"translate(107.78571 36.552528)rotate(-90)scale(0.09 -0.09)\">\n",
376
       "     <defs>\n",
377
       "      <path d=\"M 50.34375 8.453125  L 50.34375 0  L 3.03125 0  Q 2.9375 3.171875 4.046875 6.109375  Q 5.859375 10.9375 9.828125 15.625  Q 13.8125 20.3125 21.34375 26.46875  Q 33.015625 36.03125 37.109375 41.625  Q 41.21875 47.21875 41.21875 52.203125  Q 41.21875 57.421875 37.46875 61  Q 33.734375 64.59375 27.734375 64.59375  Q 21.390625 64.59375 17.578125 60.78125  Q 13.765625 56.984375 13.71875 50.25  L 4.6875 51.171875  Q 5.609375 61.28125 11.65625 66.578125  Q 17.71875 71.875 27.9375 71.875  Q 38.234375 71.875 44.234375 66.15625  Q 50.25 60.453125 50.25 52  Q 50.25 47.703125 48.484375 43.546875  Q 46.734375 39.40625 42.65625 34.8125  Q 38.578125 30.21875 29.109375 22.21875  Q 21.1875 15.578125 18.9375 13.203125  Q 16.703125 10.84375 15.234375 8.453125  z \" id=\"ArialMT-50\"/>\n",
378
       "     </defs>\n",
379
       "     <use xlink:href=\"#ArialMT-50\"/>\n",
380
       "    </g>\n",
381
       "   </g>\n",
382
       "   <g id=\"text_42\">\n",
383
       "    <!-- 1 -->\n",
384
       "    <g transform=\"translate(109.683478 21.560896)scale(0.09 -0.09)\">\n",
385
       "     <defs>\n",
386
       "      <path d=\"M 37.25 0  L 28.46875 0  L 28.46875 56  Q 25.296875 52.984375 20.140625 49.953125  Q 14.984375 46.921875 10.890625 45.40625  L 10.890625 53.90625  Q 18.265625 57.375 23.78125 62.296875  Q 29.296875 67.234375 31.59375 71.875  L 37.25 71.875  z \" id=\"ArialMT-49\"/>\n",
387
       "     </defs>\n",
388
       "     <use xlink:href=\"#ArialMT-49\"/>\n",
389
       "    </g>\n",
390
       "   </g>\n",
391
       "   <g id=\"text_43\">\n",
392
       "    <!-- &lt;${\\mathit{int64}}$&gt; -->\n",
393
       "    <g transform=\"translate(99.0609 51.549784)scale(0.07 -0.07)\">\n",
394
       "     <defs>\n",
395
       "      <path d=\"M 5.46875 31.296875  L 5.46875 39.5  L 52.875 59.515625  L 52.875 50.78125  L 15.28125 35.359375  L 52.875 19.78125  L 52.875 11.03125  z \" id=\"ArialMT-60\"/>\n",
396
       "      <path d=\"M 18.3125 75.984375  L 27.296875 75.984375  L 25.09375 64.59375  L 16.109375 64.59375  z M 14.203125 54.6875  L 23.1875 54.6875  L 12.5 0  L 3.515625 0  z \" id=\"DejaVuSans-Oblique-105\"/>\n",
397
       "      <path d=\"M 55.71875 33.015625  L 49.3125 0  L 40.28125 0  L 46.6875 32.671875  Q 47.125 34.96875 47.359375 36.71875  Q 47.609375 38.484375 47.609375 39.5  Q 47.609375 43.609375 45.015625 45.890625  Q 42.4375 48.1875 37.796875 48.1875  Q 30.5625 48.1875 25.34375 43.375  Q 20.125 38.578125 18.5 30.328125  L 12.5 0  L 3.515625 0  L 14.109375 54.6875  L 23.09375 54.6875  L 21.296875 46.09375  Q 25.046875 50.828125 30.3125 53.40625  Q 35.59375 56 41.40625 56  Q 48.640625 56 52.609375 52.09375  Q 56.59375 48.1875 56.59375 41.109375  Q 56.59375 39.359375 56.375 37.359375  Q 56.15625 35.359375 55.71875 33.015625  z \" id=\"DejaVuSans-Oblique-110\"/>\n",
398
       "      <path d=\"M 42.28125 54.6875  L 40.921875 47.703125  L 23 47.703125  L 17.1875 18.015625  Q 16.890625 16.359375 16.75 15.234375  Q 16.609375 14.109375 16.609375 13.484375  Q 16.609375 10.359375 18.484375 8.9375  Q 20.359375 7.515625 24.515625 7.515625  L 33.59375 7.515625  L 32.078125 0  L 23.484375 0  Q 15.484375 0 11.546875 3.125  Q 7.625 6.25 7.625 12.59375  Q 7.625 13.71875 7.765625 15.0625  Q 7.90625 16.40625 8.203125 18.015625  L 14.015625 47.703125  L 6.390625 47.703125  L 7.8125 54.6875  L 15.28125 54.6875  L 18.3125 70.21875  L 27.296875 70.21875  L 24.3125 54.6875  z \" id=\"DejaVuSans-Oblique-116\"/>\n",
399
       "      <path d=\"M 33.015625 40.375  Q 26.375 40.375 22.484375 35.828125  Q 18.609375 31.296875 18.609375 23.390625  Q 18.609375 15.53125 22.484375 10.953125  Q 26.375 6.390625 33.015625 6.390625  Q 39.65625 6.390625 43.53125 10.953125  Q 47.40625 15.53125 47.40625 23.390625  Q 47.40625 31.296875 43.53125 35.828125  Q 39.65625 40.375 33.015625 40.375  z M 52.59375 71.296875  L 52.59375 62.3125  Q 48.875 64.0625 45.09375 64.984375  Q 41.3125 65.921875 37.59375 65.921875  Q 27.828125 65.921875 22.671875 59.328125  Q 17.53125 52.734375 16.796875 39.40625  Q 19.671875 43.65625 24.015625 45.921875  Q 28.375 48.1875 33.59375 48.1875  Q 44.578125 48.1875 50.953125 41.515625  Q 57.328125 34.859375 57.328125 23.390625  Q 57.328125 12.15625 50.6875 5.359375  Q 44.046875 -1.421875 33.015625 -1.421875  Q 20.359375 -1.421875 13.671875 8.265625  Q 6.984375 17.96875 6.984375 36.375  Q 6.984375 53.65625 15.1875 63.9375  Q 23.390625 74.21875 37.203125 74.21875  Q 40.921875 74.21875 44.703125 73.484375  Q 48.484375 72.75 52.59375 71.296875  z \" id=\"DejaVuSans-54\"/>\n",
400
       "      <path d=\"M 37.796875 64.3125  L 12.890625 25.390625  L 37.796875 25.390625  z M 35.203125 72.90625  L 47.609375 72.90625  L 47.609375 25.390625  L 58.015625 25.390625  L 58.015625 17.1875  L 47.609375 17.1875  L 47.609375 0  L 37.796875 0  L 37.796875 17.1875  L 4.890625 17.1875  L 4.890625 26.703125  z \" id=\"DejaVuSans-52\"/>\n",
401
       "      <path d=\"M 52.875 31.296875  L 5.46875 11.03125  L 5.46875 19.78125  L 43.015625 35.359375  L 5.46875 50.78125  L 5.46875 59.515625  L 52.875 39.5  z \" id=\"ArialMT-62\"/>\n",
402
       "     </defs>\n",
403
       "     <use transform=\"translate(0 0.015625)\" xlink:href=\"#ArialMT-60\"/>\n",
404
       "     <use transform=\"translate(58.398438 0.015625)\" xlink:href=\"#DejaVuSans-Oblique-105\"/>\n",
405
       "     <use transform=\"translate(86.181641 0.015625)\" xlink:href=\"#DejaVuSans-Oblique-110\"/>\n",
406
       "     <use transform=\"translate(149.560547 0.015625)\" xlink:href=\"#DejaVuSans-Oblique-116\"/>\n",
407
       "     <use transform=\"translate(188.769531 0.015625)\" xlink:href=\"#DejaVuSans-54\"/>\n",
408
       "     <use transform=\"translate(252.392578 0.015625)\" xlink:href=\"#DejaVuSans-52\"/>\n",
409
       "     <use transform=\"translate(316.015625 0.015625)\" xlink:href=\"#ArialMT-62\"/>\n",
410
       "    </g>\n",
411
       "   </g>\n",
412
       "   <g id=\"text_44\">\n",
413
       "    <!-- 2 -->\n",
414
       "    <g transform=\"translate(141.93531 36.552528)rotate(-90)scale(0.09 -0.09)\">\n",
415
       "     <use xlink:href=\"#ArialMT-50\"/>\n",
416
       "    </g>\n",
417
       "   </g>\n",
418
       "   <g id=\"text_45\">\n",
419
       "    <!-- 1 -->\n",
420
       "    <g transform=\"translate(143.833078 21.560896)scale(0.09 -0.09)\">\n",
421
       "     <use xlink:href=\"#ArialMT-49\"/>\n",
422
       "    </g>\n",
423
       "   </g>\n",
424
       "   <g id=\"text_46\">\n",
425
       "    <!-- &lt;${\\mathit{int64}}$&gt; -->\n",
426
       "    <g transform=\"translate(133.2105 51.549784)scale(0.07 -0.07)\">\n",
427
       "     <use transform=\"translate(0 0.015625)\" xlink:href=\"#ArialMT-60\"/>\n",
428
       "     <use transform=\"translate(58.398438 0.015625)\" xlink:href=\"#DejaVuSans-Oblique-105\"/>\n",
429
       "     <use transform=\"translate(86.181641 0.015625)\" xlink:href=\"#DejaVuSans-Oblique-110\"/>\n",
430
       "     <use transform=\"translate(149.560547 0.015625)\" xlink:href=\"#DejaVuSans-Oblique-116\"/>\n",
431
       "     <use transform=\"translate(188.769531 0.015625)\" xlink:href=\"#DejaVuSans-54\"/>\n",
432
       "     <use transform=\"translate(252.392578 0.015625)\" xlink:href=\"#DejaVuSans-52\"/>\n",
433
       "     <use transform=\"translate(316.015625 0.015625)\" xlink:href=\"#ArialMT-62\"/>\n",
434
       "    </g>\n",
435
       "   </g>\n",
436
       "  </g>\n",
437
       " </g>\n",
438
       " <defs>\n",
439
       "  <clipPath id=\"pf8405f6392\">\n",
440
       "   <rect height=\"41.491069\" width=\"310.3038\" x=\"0\" y=\"4.504646\"/>\n",
441
       "  </clipPath>\n",
442
       " </defs>\n",
443
       "</svg>"
444
      ],
445
      "text/plain": [
446
       "<IPython.core.display.SVG object>"
447
      ]
448
     },
449
     "metadata": {},
450
     "output_type": "display_data"
451
    },
452
    {
453
     "ename": "RuntimeError",
454
     "evalue": "1D tensors expected, but got 2D and 2D tensors\nCause: torch.dot(b,b) tensor arg b w/shape [2, 1], arg b w/shape [2, 1]",
455
     "output_type": "error",
456
     "traceback": [
457
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
458
      "\u001b[0;31mRuntimeError\u001b[0m                              Traceback (most recent call last)",
459
      "\u001b[0;32m/var/folders/93/9kzk2ccm8xj8k70059b28jk80000gp/T/ipykernel_55352/2660314622.py\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[1;32m     12\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m     13\u001b[0m \u001b[0;32mwith\u001b[0m \u001b[0mtsensor\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mclarify\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 14\u001b[0;31m     \u001b[0mW\u001b[0m \u001b[0;34m@\u001b[0m \u001b[0mtorch\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mdot\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mb\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0mb\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m+\u001b[0m \u001b[0mtorch\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0meye\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m2\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m2\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m@\u001b[0m\u001b[0mx\u001b[0m \u001b[0;34m+\u001b[0m \u001b[0mz\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m     15\u001b[0m \u001b[0;31m# W[33, 33] = 3\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m     16\u001b[0m \u001b[0mb\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mtorch\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mabs\u001b[0m\u001b[0;34m(\u001b[0m \u001b[0mW\u001b[0m \u001b[0;34m@\u001b[0m \u001b[0mb\u001b[0m \u001b[0;34m+\u001b[0m \u001b[0mx\u001b[0m \u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
460
      "\u001b[0;31mRuntimeError\u001b[0m: 1D tensors expected, but got 2D and 2D tensors\nCause: torch.dot(b,b) tensor arg b w/shape [2, 1], arg b w/shape [2, 1]"
461
     ]
462
    }
463
   ],
464
   "source": [
465
    "import torch\n",
466
    "import tsensor\n",
467
    "import sys\n",
468
    "\n",
469
    "W = torch.tensor([[1, 2], [3, 4]])\n",
470
    "b = torch.tensor([9, 10]).reshape(2, 1)\n",
471
    "x = torch.tensor([4, 5]).reshape(2, 1)\n",
472
    "h = torch.tensor([1,2])\n",
473
    "# z + z + W @ z\n",
474
    "# W @ z\n",
475
    "#torch.dot(b, 3)\n",
476
    "\n",
477
    "with tsensor.clarify():\n",
478
    "    W @ torch.dot(b,b)+ torch.eye(2,2)@x + z\n",
479
    "# W[33, 33] = 3\n",
480
    "b = torch.abs( W @ b + x )"
481
   ]
482
  },
483
  {
484
   "cell_type": "code",
485
   "execution_count": 4,
486
   "metadata": {},
487
   "outputs": [
488
    {
489
     "ename": "RuntimeError",
490
     "evalue": "1D tensors expected, got 2D, 2D tensors at /tmp/pip-req-build-as628lz5/aten/src/TH/generic/THTensorEvenMoreMath.cpp:83\nCause: torch.dot(b,b) tensor arg b w/shape [2, 1], arg b w/shape [2, 1]",
491
     "output_type": "error",
492
     "traceback": [
493
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
494
      "\u001b[0;31mRuntimeError\u001b[0m                              Traceback (most recent call last)",
495
      "\u001b[0;32m<ipython-input-4-fd44e2c68a52>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[1;32m      1\u001b[0m \u001b[0;32mwith\u001b[0m \u001b[0mtsensor\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mclarify\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m     \u001b[0mW\u001b[0m \u001b[0;34m@\u001b[0m \u001b[0mtorch\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mdot\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mb\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0mb\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m+\u001b[0m \u001b[0mtorch\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0meye\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m2\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m2\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m@\u001b[0m\u001b[0mx\u001b[0m \u001b[0;34m+\u001b[0m \u001b[0mz\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
496
      "\u001b[0;31mRuntimeError\u001b[0m: 1D tensors expected, got 2D, 2D tensors at /tmp/pip-req-build-as628lz5/aten/src/TH/generic/THTensorEvenMoreMath.cpp:83\nCause: torch.dot(b,b) tensor arg b w/shape [2, 1], arg b w/shape [2, 1]"
497
     ]
498
    }
499
   ],
500
   "source": [
501
    "with tsensor.clarify():\n",
502
    "    W @ torch.dot(b,b)+ torch.eye(2,2)@x + z"
503
   ]
504
  },
505
  {
506
   "cell_type": "markdown",
507
   "metadata": {},
508
   "source": [
509
    "## Graphviz"
510
   ]
511
  },
512
  {
513
   "cell_type": "code",
514
   "execution_count": 8,
515
   "metadata": {},
516
   "outputs": [
517
    {
518
     "name": "stdout",
519
     "output_type": "stream",
520
     "text": [
521
      "[<NAME:b>, <EQUAL:=>, <NAME:W>, <AT:@>, <NAME:h>, <PLUS:+>, <NAME:torch>, <DOT:.>, <NAME:abs>, <LPAR:(>, <NAME:x>, <RPAR:)>, <STAR:*>, <NAME:h>, <DOT:.>, <NAME:dot>, <LPAR:(>, <NAME:h>, <RPAR:)>, <ENDMARKER:>]\n"
522
     ]
523
    },
524
    {
525
     "data": {
526
      "text/plain": [
527
       "Assign(lhs=b,rhs=BinaryOp(op=<PLUS:+>,lhs=BinaryOp(op=<AT:@>,lhs=W,rhs=h),rhs=BinaryOp(op=<STAR:*>,lhs=Call(func=Member(obj=torch,member=abs),args=[x]),rhs=Call(func=Member(obj=h,member=dot),args=[h]))))"
528
      ]
529
     },
530
     "execution_count": 8,
531
     "metadata": {},
532
     "output_type": "execute_result"
533
    }
534
   ],
535
   "source": [
536
    "p = tsensor.parse.PyExprParser(\"b = W @ h + torch.abs(x) *h.dot(h)\")\n",
537
    "print(p.tokens)\n",
538
    "root = p.parse()\n",
539
    "root"
540
   ]
541
  },
542
  {
543
   "cell_type": "code",
544
   "execution_count": 9,
545
   "metadata": {},
546
   "outputs": [
547
    {
548
     "data": {
549
      "text/plain": [
550
       "tensor([[25, 31],\n",
551
       "        [30, 36]])"
552
      ]
553
     },
554
     "execution_count": 9,
555
     "metadata": {},
556
     "output_type": "execute_result"
557
    }
558
   ],
559
   "source": [
560
    "frame = sys._getframe()\n",
561
    "result = root.eval(frame)\n",
562
    "result"
563
   ]
564
  },
565
  {
566
   "cell_type": "code",
567
   "execution_count": 7,
568
   "metadata": {},
569
   "outputs": [],
570
   "source": [
571
    "def postorder(t):\n",
572
    "    nodes = []\n",
573
    "    _postorder(t, nodes)\n",
574
    "    return nodes\n",
575
    "def _postorder(t, nodes):\n",
576
    "    if t is None:\n",
577
    "        return\n",
578
    "    for sub in t.kids:\n",
579
    "        _postorder(sub, nodes)\n",
580
    "    nodes.append(t)"
581
   ]
582
  },
583
  {
584
   "cell_type": "code",
585
   "execution_count": null,
586
   "metadata": {},
587
   "outputs": [],
588
   "source": [
589
    "def leaves(t):\n",
590
    "    nodes = []\n",
591
    "    _leaves(t, nodes)\n",
592
    "    return nodes\n",
593
    "def _leaves(t, nodes):\n",
594
    "    if t is None:\n",
595
    "        return\n",
596
    "    if len(t.kids)==0:\n",
597
    "        nodes.append(t)\n",
598
    "        return\n",
599
    "    for sub in t.kids:\n",
600
    "        _leaves(sub, nodes)"
601
   ]
602
  },
603
  {
604
   "cell_type": "code",
605
   "execution_count": null,
606
   "metadata": {},
607
   "outputs": [],
608
   "source": [
609
    "nodes = postorder(root)\n",
610
    "atoms = leaves(root)\n",
611
    "atoms"
612
   ]
613
  },
614
  {
615
   "cell_type": "code",
616
   "execution_count": null,
617
   "metadata": {},
618
   "outputs": [],
619
   "source": [
620
    "# map tokens to nodes\n",
621
    "tok2node = {}\n",
622
    "for nd in atoms:\n",
623
    "    tok2node[nd.token] = nd"
624
   ]
625
  },
626
  {
627
   "cell_type": "code",
628
   "execution_count": null,
629
   "metadata": {},
630
   "outputs": [],
631
   "source": [
632
    "def walk(t, pre=lambda x:None, post=lambda x:None):\n",
633
    "    if t is None:\n",
634
    "        return\n",
635
    "    pre(t)\n",
636
    "    for sub in t.kids:\n",
637
    "        walk(sub, pre, post)\n",
638
    "    post(t)"
639
   ]
640
  },
641
  {
642
   "cell_type": "code",
643
   "execution_count": null,
644
   "metadata": {},
645
   "outputs": [],
646
   "source": [
647
    "import graphviz\n",
648
    "\n",
649
    "s = \"\"\"\n",
650
    "digraph G {\n",
651
    "    nodesep=.1;\n",
652
    "    ranksep=.3;\n",
653
    "    rankdir=BT;\n",
654
    "    ordering=out; # keep order of leaves\n",
655
    "    node [penwidth=\"0.5\", shape=plaintext, width=.1, height=.1];\n",
656
    "\"\"\"\n",
657
    "\n",
658
    "nodesS = set(nodes)\n",
659
    "atomsS = set(atoms)\n",
660
    "ops = nodesS.difference(atomsS)\n",
661
    "\n",
662
    "# s += f'{{ rank=same;'\n",
663
    "# for a in atoms:\n",
664
    "#     s += f' node{id(tok2node[a.token])};'\n",
665
    "# s += '}\\n'\n",
666
    "\n",
667
    "s += f'{{ rank=same;'\n",
668
    "for t in p.tokens:\n",
669
    "    if t.type!=tsensor.ENDMARKER:\n",
670
    "        x = tok2node[t] if t in tok2node else t\n",
671
    "        shape = \"\"\n",
672
    "        sh = tsensor._shape(x.value)\n",
673
    "        if x in atomsS and sh is not None:\n",
674
    "            shape = \"shape=box, fixedsize=shape \"\n",
675
    "            if len(sh)==1:\n",
676
    "                shape += f\"width={.15}, height={sh[0]/6.66}\"\n",
677
    "            elif len(sh)==2:\n",
678
    "                shape += f\"width={sh[1]/6.66}, height={sh[0]/6.66}\"\n",
679
    "#         print(sh, shape)\n",
680
    "        s += f'leaf{id(x)} [{shape} label=<<font face=\"Consolas\" color=\"#444443\" point-size=\"9\">{t.value}</font>>]\\n'\n",
681
    "s += '}\\n'\n",
682
    "\n",
683
    "# for nd in ops:\n",
684
    "#     s += f'leaf{id(nd)} [label=<<font face=\"Consolas\" color=\"#444443\" point-size=\"9\">{str(nd)}</font>>]'\n",
685
    "\n",
686
    "for nd in nodes:\n",
687
    "    if nd in ops:\n",
688
    "        text = str(nd)\n",
689
    "#         if isinstance(nd, tsensor.Atom):\n",
690
    "#             text = str(nd)\n",
691
    "#         else:\n",
692
    "#             text = nd.__class__.__name__\n",
693
    "#         text = str(nd)#+\"\\n\"+str(nd.value)\n",
694
    "#         text = \" \"\n",
695
    "        shape = \"\"\n",
696
    "        sh = tsensor._shape(nd.value)\n",
697
    "        if sh is not None:\n",
698
    "            shape = \"shape=box, fixedsize=shape \"\n",
699
    "            if len(sh)==1:\n",
700
    "                shape += f\"width={.15}, height={sh[0]/6.66}\"\n",
701
    "            elif len(sh)==2:\n",
702
    "                shape += f\"width={sh[1]/6.66}, height={sh[0]/6.66}\"\n",
703
    "        if sh is not None:\n",
704
    "            text += \"<br/>\"+'x'.join(str(s) for s in sh)\n",
705
    "#         else:\n",
706
    "#             text += \"=\"+str(nd.value)\n",
707
    "        s += f'node{id(nd)} [{shape} label=<<font face=\"Consolas\" color=\"#444443\" point-size=\"9\">{text}</font>>]\\n'\n",
708
    "\n",
709
    "# link leaves left to right\n",
710
    "for i in range(len(p.tokens)-2):\n",
711
    "    t = p.tokens[i]\n",
712
    "    t2 = p.tokens[i+1]\n",
713
    "    x = tok2node[t] if t in tok2node else t\n",
714
    "    x2 = tok2node[t2] if t2 in tok2node else t2\n",
715
    "    s += f'leaf{id(x)} -> leaf{id(x2)} [style=invis];\\n'\n",
716
    "    \n",
717
    "for nd in nodes:\n",
718
    "    kids = nd.kids\n",
719
    "#     if isinstance(nd, tsensor.Call) and isinstance(nd.kids[0], tsensor.Member):\n",
720
    "#         print('ignore', nd)\n",
721
    "#         kids = kids[1:]\n",
722
    "    for sub in kids:\n",
723
    "        if sub in atomsS:\n",
724
    "            s += f'node{id(nd)} -> leaf{id(sub)} [dir=back, penwidth=\"0.5\", color=\"#444443\", arrowsize=.4];\\n'\n",
725
    "        else:\n",
726
    "            s += f'node{id(nd)} -> node{id(sub)} [dir=back, penwidth=\"0.5\", color=\"#444443\", arrowsize=.4];\\n'\n",
727
    "s += \"}\\n\"\n",
728
    "graphviz.Source(s)"
729
   ]
730
  },
731
  {
732
   "cell_type": "code",
733
   "execution_count": null,
734
   "metadata": {},
735
   "outputs": [],
736
   "source": [
737
    "import graphviz\n",
738
    "\n",
739
    "s = \"\"\"\n",
740
    "digraph G {\n",
741
    "    nodesep=.1;\n",
742
    "    ranksep=.3;\n",
743
    "    rankdir=BT;\n",
744
    "    ordering=out; # keep order of leaves\n",
745
    "    node [penwidth=\"0.5\", shape=plaintext, width=.1, height=.1];\n",
746
    "\"\"\"\n",
747
    "\n",
748
    "nodesS = set(nodes)\n",
749
    "atomsS = set(atoms)\n",
750
    "ops = nodesS.difference(atomsS)\n",
751
    "\n",
752
    "s += f'{{ rank=same;'\n",
753
    "for a in atoms:\n",
754
    "    s += f'node{id(a)};'\n",
755
    "s += '}\\n'\n",
756
    "\n",
757
    "for nd in nodes:\n",
758
    "    if isinstance(nd, tsensor.Atom):\n",
759
    "        text = str(nd)\n",
760
    "    else:\n",
761
    "        text = nd.__class__.__name__\n",
762
    "    shape = \"\"\n",
763
    "    sh = _shape(nd.value)\n",
764
    "    if sh is not None:\n",
765
    "        shape = \"shape=box, fixedsize=shape \"\n",
766
    "        if len(sh)==1:\n",
767
    "            shape += f\"width={.15}, height={sh[0]/6.66}\"\n",
768
    "        elif len(sh)==2:\n",
769
    "            shape += f\"width={sh[1]/6.66}, height={sh[0]/6.66}\"\n",
770
    "    s += f'node{id(nd)} [{shape} label=<<font face=\"Consolas\" color=\"#444443\" point-size=\"9\">{text}</font>>]\\n'\n",
771
    "    \n",
772
    "for nd in nodes:\n",
773
    "    kids = nd.kids\n",
774
    "#     if isinstance(nd, tsensor.Call) and isinstance(nd.kids[0], tsensor.Member):\n",
775
    "#         print('ignore', nd)\n",
776
    "#         kids = kids[1:]\n",
777
    "    for sub in kids:\n",
778
    "        if sub in atomsS:\n",
779
    "            s += f'node{id(nd)} -> node{id(sub)} [dir=back, penwidth=\"0.5\", color=\"#444443\", arrowsize=.4];'\n",
780
    "        else:\n",
781
    "            s += f'node{id(nd)} -> node{id(sub)} [dir=back, penwidth=\"0.5\", color=\"#444443\", arrowsize=.4];'\n",
782
    "s += \"}\\n\"\n",
783
    "graphviz.Source(s)"
784
   ]
785
  },
786
  {
787
   "cell_type": "code",
788
   "execution_count": null,
789
   "metadata": {},
790
   "outputs": [],
791
   "source": [
792
    "W = torch.tensor([[1, 2], [3, 4], [5, 6]])\n",
793
    "b = torch.tensor([9, 10]).reshape(2, 1)\n",
794
    "x = torch.tensor([4, 5]).reshape(2, 1)\n",
795
    "h = torch.tensor([1,2])\n",
796
    "a = 3\n",
797
    "\n",
798
    "x = torch.tensor([4, 5]).reshape(2, 1)\n",
799
    "p = tsensor.PyExprParser(\"a = a\")\n",
800
    "p = tsensor.PyExprParser(\"b = W@b + torch.abs(x) + h.dot(h)\")\n",
801
    "p = tsensor.PyExprParser(\"b = W@b + h.dot(h)\")\n",
802
    "print(p.tokens)\n",
803
    "root = p.parse()\n",
804
    "nodes = postorder(root)\n",
805
    "atoms = leaves(root)\n",
806
    "# map tokens to nodes\n",
807
    "tok2node = {}\n",
808
    "for nd in atoms:\n",
809
    "    tok2node[nd.token] = nd\n",
810
    "frame = sys._getframe()\n",
811
    "result = root.eval(frame)\n",
812
    "result"
813
   ]
814
  },
815
  {
816
   "cell_type": "code",
817
   "execution_count": null,
818
   "metadata": {},
819
   "outputs": [],
820
   "source": [
821
    "import graphviz\n",
822
    "\n",
823
    "def matrix_html(nrows, ncols, label, fontsize=12, fontname=\"Consolas\", dimfontsize=9, color=\"#cfe2d4\"):\n",
824
    "    isvec = ncols==None\n",
825
    "    if isvec:\n",
826
    "        sz = str(nrows)\n",
827
    "        ncols=nrows\n",
828
    "        nrows=1\n",
829
    "    else:\n",
830
    "        sz = f\"{nrows}x{ncols}\"\n",
831
    "    w = ncols*20\n",
832
    "    h = nrows*20\n",
833
    "    if ncols==1:\n",
834
    "        w = 15\n",
835
    "    if nrows==1:\n",
836
    "        h = 15\n",
837
    "    html = f\"\"\"\n",
838
    "    <table fixedsize=\"true\" width=\"{w}\" height=\"{h+2*fontsize*1.3}\" BORDER=\"0\" CELLPADDING=\"0\" CELLBORDER=\"1\" CELLSPACING=\"0\">\n",
839
    "    <tr>\n",
840
    "    <td fixedsize=\"true\" width=\"{w}\" height=\"{fontsize*1.3}\" cellspacing=\"0\" cellpadding=\"0\" border=\"0\" valign=\"top\" align=\"center\">\n",
841
    "    <font face=\"{fontname}\" color=\"#444443\" point-size=\"{dimfontsize}\">{sz}</font>\n",
842
    "    </td>\n",
843
    "    </tr>\n",
844
    "    <tr>    \n",
845
    "    <td fixedsize=\"true\" width=\"{w}\" height=\"{h}\" cellspacing=\"0\" cellpadding=\"0\" bgcolor=\"{color}\" border=\"1\" sides=\"ltbr\" align=\"center\">\n",
846
    "    <font face=\"{fontname}\" color=\"#444443\" point-size=\"{fontsize}\">{label}</font>\n",
847
    "    </td>\n",
848
    "    </tr>\n",
849
    "    </table>\"\"\"\n",
850
    "    return html\n",
851
    "\n",
852
    "nodesS = set(nodes)\n",
853
    "atomsS = set(atoms)\n",
854
    "ops = nodesS.difference(atomsS)\n",
855
    "\n",
856
    "s = \"\"\"\n",
857
    "digraph G {\n",
858
    "    nodesep=.0;\n",
859
    "    ranksep=.3;\n",
860
    "    rankdir=BT;\n",
861
    "    ordering=out; # keep order of leaves\n",
862
    "\"\"\"\n",
863
    "\n",
864
    "fontname=\"Consolas\"\n",
865
    "fontsize=12\n",
866
    "spread = .2\n",
867
    "\n",
868
    "s += f'{{ rank=same; '\n",
869
    "for t in p.tokens:\n",
870
    "    if t.type!=tsensor.ENDMARKER:\n",
871
    "        x = tok2node[t] if t in tok2node else t\n",
872
    "        shape = \"\"\n",
873
    "        sh = tsensor._shape(x.value)\n",
874
    "        label = f'<font face=\"{fontname}\" color=\"#444443\" point-size=\"{fontsize}\">{t.value}</font>'\n",
875
    "        matrixcolor=\"#cfe2d4\"\n",
876
    "        vectorcolor=\"#fefecd\"\n",
877
    "        if x in atomsS and sh is not None:\n",
878
    "            if len(sh)==1:\n",
879
    "                label = matrix_html(sh[0],None,t.value,fontname=fontname,fontsize=fontsize,color=vectorcolor)\n",
880
    "            elif len(sh)==2:\n",
881
    "                label = matrix_html(sh[0],sh[1],t.value,fontname=fontname,fontsize=fontsize,color=matrixcolor)\n",
882
    "        # margin/width don't seem to do anything for shape=plain\n",
883
    "        if t.type==tsensor.DOT:\n",
884
    "            spread=.1\n",
885
    "        if t.type==tsensor.EQUAL:\n",
886
    "            spread=.25\n",
887
    "        if t.type in tsensor.ADDOP:\n",
888
    "            spread=.5\n",
889
    "        if t.type in tsensor.MULOP:\n",
890
    "            spread=.2\n",
891
    "        s += f'leaf{id(x)} [shape=box penwidth=0 margin=.001 width={spread} label=<{label}>]\\n'\n",
892
    "s += '}\\n'\n",
893
    "\n",
894
    "s += \"}\\n\"\n",
895
    "# print(s)\n",
896
    "graphviz.Source(s)"
897
   ]
898
  },
899
  {
900
   "cell_type": "code",
901
   "execution_count": null,
902
   "metadata": {},
903
   "outputs": [],
904
   "source": [
905
    "s = \"\"\"\n",
906
    "digraph foo {\n",
907
    "    rankdir=TB\n",
908
    "    subgraph {\n",
909
    "        node1;\n",
910
    "    }\n",
911
    "    subgraph {\n",
912
    "        node2;\n",
913
    "    }\n",
914
    "    node1 -> node2\n",
915
    "}\n",
916
    "\"\"\"\n",
917
    "display(graphviz.Source(s))\n",
918
    "display(graphviz.Source(s))"
919
   ]
920
  },
921
  {
922
   "cell_type": "markdown",
923
   "metadata": {},
924
   "source": [
925
    "## Get string size in matplotlib"
926
   ]
927
  },
928
  {
929
   "cell_type": "code",
930
   "execution_count": null,
931
   "metadata": {},
932
   "outputs": [],
933
   "source": [
934
    "import matplotlib\n",
935
    "import matplotlib.patches as patches\n",
936
    "from matplotlib import pyplot as plt\n",
937
    "def textdim(s, fontsize=11):\n",
938
    "    fig, ax = plt.subplots(1,1)\n",
939
    "    t = ax.text(0, 0, s, bbox={'lw':0}, fontsize=fontsize)\n",
940
    "    plt.savefig(\"/tmp/junk\")\n",
941
    "    plt.close()\n",
942
    "    bb = t.get_bbox_patch()\n",
943
    "    w, h = bb.get_width(), bb.get_height()\n",
944
    "    return w, h"
945
   ]
946
  },
947
  {
948
   "cell_type": "code",
949
   "execution_count": null,
950
   "metadata": {},
951
   "outputs": [],
952
   "source": [
953
    "textdim(\"test of foO\", fontsize=11)"
954
   ]
955
  },
956
  {
957
   "cell_type": "code",
958
   "execution_count": null,
959
   "metadata": {},
960
   "outputs": [],
961
   "source": [
962
    "import numpy as np\n",
963
    "a = [[1,2,3],[3,4,5]]\n",
964
    "objviz(a)"
965
   ]
966
  },
967
  {
968
   "cell_type": "code",
969
   "execution_count": null,
970
   "metadata": {},
971
   "outputs": [],
972
   "source": [
973
    "from lolviz import *\n",
974
    "print(objviz(a).source)"
975
   ]
976
  },
977
  {
978
   "cell_type": "code",
979
   "execution_count": null,
980
   "metadata": {},
981
   "outputs": [],
982
   "source": [
983
    "s = \"\"\"\n",
984
    "digraph G {\n",
985
    "    ranksep=0;\n",
986
    "    rankdir=BT;\n",
987
    "    ordering=out; # keep order of leaves\n",
988
    "    node1 [shape=plain, space=\"0.0\", margin=\"0.01\",label=<\n",
989
    "<table BORDER=\"0\" CELLPADDING=\"0\" CELLBORDER=\"1\" CELLSPACING=\"0\">\n",
990
    "<tr>\n",
991
    "<td  width=\"100\" height=\"12\" cellspacing=\"0\" cellpadding=\"0\" border=\"1\" sides=\"b\" valign=\"top\" align=\"center\">100x200</td>\n",
992
    "</tr>\n",
993
    "<tr>\n",
994
    "<td  width=\"100\" height=\"88\" cellspacing=\"0\" cellpadding=\"0\" border=\"1\" sides=\"lbr\" align=\"center\">self.W</td>\n",
995
    "</tr>\n",
996
    "</table>\n",
997
    "    >]\n",
998
    "}\n",
999
    "\"\"\"\n",
1000
    "graphviz.Source(s)"
1001
   ]
1002
  },
1003
  {
1004
   "cell_type": "markdown",
1005
   "metadata": {},
1006
   "source": [
1007
    "## CSS (yuck)"
1008
   ]
1009
  },
1010
  {
1011
   "cell_type": "code",
1012
   "execution_count": null,
1013
   "metadata": {},
1014
   "outputs": [],
1015
   "source": [
1016
    "from IPython.core.display import display, HTML\n",
1017
    "h = \"\"\"\n",
1018
    "<h6 style=\"color: Green; position:absolute; top: 20vh; left: 25vw; height: auto; margin: auto;\">Actief</h6>\n",
1019
    "<table>\n",
1020
    "<tr>\n",
1021
    "    <td>b = torch.abs( W @ b +</td>\n",
1022
    "    <td><div style=\"width:100px;height:150px;border:1px solid #000;\">\n",
1023
    "    <table style=\"width:100px;height:150px\" border='0' cellborder='0' cellpadding=\"0\" cellspacing=\"0\">\n",
1024
    "           <tr>\n",
1025
    "               <td height=\"50%\" width=\"33%\"></td>\n",
1026
    "               <td width=\"33%\" cellpadding=\"0\" cellspacing=\"0\" align=center valign=top>40</td><td width=\"33%\"></td>\n",
1027
    "           </tr>\n",
1028
    "           <tr>\n",
1029
    "               <td width=\"33%\" cellpadding=\"0\" cellspacing=\"0\">100</td>\n",
1030
    "               <td width=\"33%\" cellpadding=\"0\" cellspacing=\"0\" align=\"center\">self.W</td>\n",
1031
    "               <td width=\"33%\"></td>\n",
1032
    "           </tr>\n",
1033
    "         </table>\n",
1034
    "    </div>\n",
1035
    "    </td>\n",
1036
    "    <td>)</td>\n",
1037
    "</tr>\n",
1038
    "\"\"\"\n",
1039
    "t = HTML(h)\n",
1040
    "t"
1041
   ]
1042
  },
1043
  {
1044
   "cell_type": "code",
1045
   "execution_count": null,
1046
   "metadata": {},
1047
   "outputs": [],
1048
   "source": [
1049
    "def matrix(nrows,ncols,text,dim_fontsize=9,fontsize=12):\n",
1050
    "    h = f\"\"\"\n",
1051
    "    <div style=\"position:relative; width:{ncols}px;height:{nrows*1.2}px; padding:0px\">\n",
1052
    "     <div style=\"position:relative; width:{ncols}px;height:{nrows}px;border:1px solid #000; padding:0px\">\n",
1053
    "      <div style=\"position:absolute; width:{ncols}px; top: -3px; text-align:center; font-size:{dim_fontsize}px\">\n",
1054
    "        {ncols}\n",
1055
    "      </div>\n",
1056
    "      <div style=\"position:absolute; top: 40%; left: -3px; transform: rotate(-90deg); font-size:{dim_fontsize}px\">\n",
1057
    "        {nrows}\n",
1058
    "      </div>\n",
1059
    "     </div>\n",
1060
    "     <div style=\"position:absolute; width:{ncols}px; bottom: 5px; text-align:center; font-size:{fontsize}px\">\n",
1061
    "        {text}\n",
1062
    "     </div>\n",
1063
    "    </div>\n",
1064
    "    \"\"\"\n",
1065
    "    sp = f\"\"\"\n",
1066
    "    <div style=\"position:relative; width:{ncols}px;height:{nrows*1.2}px; padding:0px\">\n",
1067
    "     <span style=\"position:relative; width:{ncols}px;height:{nrows}px;border:1px solid #000; padding:0px\">\n",
1068
    "      <span style=\"position:absolute; width:{ncols}px; top: -3px; text-align:center; font-size:{dim_fontsize}px\">\n",
1069
    "        {ncols}\n",
1070
    "      </span>\n",
1071
    "      <span style=\"position:absolute; top: 40%; left: -3px; transform: rotate(-90deg); font-size:{dim_fontsize}px\">\n",
1072
    "        {nrows}\n",
1073
    "      </span>\n",
1074
    "     </span>\n",
1075
    "     <span style=\"position:absolute; width:{ncols}px; bottom: 5px; text-align:center; font-size:{fontsize}px\">\n",
1076
    "        {text}\n",
1077
    "     </span>\n",
1078
    "    </div>\n",
1079
    "    \"\"\"\n",
1080
    "\n",
1081
    "    return sp\n",
1082
    "\n",
1083
    "m = matrix(120,20,\"self.W\")\n",
1084
    "m = f\"torch.relu({m})\"\n",
1085
    "HTML(m)"
1086
   ]
1087
  },
1088
  {
1089
   "cell_type": "code",
1090
   "execution_count": null,
1091
   "metadata": {},
1092
   "outputs": [],
1093
   "source": []
1094
  },
1095
  {
1096
   "cell_type": "code",
1097
   "execution_count": null,
1098
   "metadata": {},
1099
   "outputs": [],
1100
   "source": []
1101
  }
1102
 ],
1103
 "metadata": {
1104
  "kernelspec": {
1105
   "display_name": "Python 3 (ipykernel)",
1106
   "language": "python",
1107
   "name": "python3"
1108
  },
1109
  "language_info": {
1110
   "codemirror_mode": {
1111
    "name": "ipython",
1112
    "version": 3
1113
   },
1114
   "file_extension": ".py",
1115
   "mimetype": "text/x-python",
1116
   "name": "python",
1117
   "nbconvert_exporter": "python",
1118
   "pygments_lexer": "ipython3",
1119
   "version": "3.8.8"
1120
  }
1121
 },
1122
 "nbformat": 4,
1123
 "nbformat_minor": 4
1124
}
1125

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.