aboutsummaryrefslogtreecommitdiffhomepage
path: root/tensorflow/tools/docker/notebooks/1_hello_tensorflow.ipynb
blob: 0633b03259a06363d0d069eb479971f8b87f983e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {
        "colab_type": "text",
        "id": "a3bskVXPvchm"
      },
      "source": [
        "# Hello, TensorFlow\n",
        "## A beginner-level, getting started, basic introduction to TensorFlow"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "colab_type": "text",
        "id": "Rb5rSpcZvYbX"
      },
      "source": [
        "TensorFlow is a general-purpose system for graph-based computation. A typical use is machine learning. In this notebook, we'll introduce the basic concepts of TensorFlow using some simple examples.\n",
        "\n",
        "TensorFlow gets its name from [tensors](https://en.wikipedia.org/wiki/Tensor), which are arrays of arbitrary dimensionality. A vector is a 1-d array and is known as a 1st-order tensor. A matrix is a 2-d array and a 2nd-order tensor. The \"flow\" part of the name refers to computation flowing through a graph. Training and inference in a neural network, for example, involves the propagation of matrix computations through many nodes in a computational graph.\n",
        "\n",
        "When you think of doing things in TensorFlow, you might want to think of creating tensors (like matrices), adding operations (that output other tensors), and then executing the computation (running the computational graph). In particular, it's important to realize that when you add an operation on tensors, it doesn't execute immediately. Rather, TensorFlow waits for you to define all the operations you want to perform. Then, TensorFlow optimizes the computation graph, deciding how to execute the computation, before generating the data. Because of this, a tensor in TensorFlow isn't so much holding the data as a placeholder for holding the data, waiting for the data to arrive when a computation is executed."
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "colab_type": "text",
        "id": "E8FhiMivhcYB"
      },
      "source": [
        "## Adding two vectors in TensorFlow\n",
        "\n",
        "Let's start with something that should be simple. Let's add two length four vectors (two 1st-order tensors):\n",
        "\n",
        "$\\begin{bmatrix} 1. \u0026 1. \u0026 1. \u0026 1.\\end{bmatrix} + \\begin{bmatrix} 2. \u0026 2. \u0026 2. \u0026 2.\\end{bmatrix} = \\begin{bmatrix} 3. \u0026 3. \u0026 3. \u0026 3.\\end{bmatrix}$"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 0,
      "metadata": {
        "cellView": "both",
        "colab": {
          "autoexec": {
            "startup": false,
            "wait_interval": 0
          },
          "output_extras": [
            {
              "item_id": 1
            }
          ]
        },
        "colab_type": "code",
        "executionInfo": {
          "elapsed": 2922,
          "status": "ok",
          "timestamp": 1474675631337,
          "user": {
            "displayName": "",
            "photoUrl": "",
            "userId": ""
          },
          "user_tz": 420
        },
        "id": "2iv3XQ6k3eF1",
        "outputId": "7dbded62-91bc-4e38-9f25-53375c4c8dd8"
      },
      "outputs": [
        {
          "name": "stdout",
          "output_type": "stream",
          "text": [
            "result:  [ 3.  3.  3.  3.]\n"
          ]
        }
      ],
      "source": [
        "from __future__ import print_function\n",
        "\n",
        "import tensorflow as tf\n",
        "\n",
        "with tf.Session():\n",
        "    input1 = tf.constant([1.0, 1.0, 1.0, 1.0])\n",
        "    input2 = tf.constant([2.0, 2.0, 2.0, 2.0])\n",
        "    output = tf.add(input1, input2)\n",
        "    result = output.eval()\n",
        "    print(\"result: \", result)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "colab_type": "text",
        "id": "dqLV5GXT3wLy"
      },
      "source": [
        "What we're doing is creating two vectors, [1.0, 1.0, 1.0, 1.0] and [2.0, 2.0, 2.0, 2.0], and then adding them. Here's equivalent code in raw Python and using numpy:"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 0,
      "metadata": {
        "cellView": "both",
        "colab": {
          "autoexec": {
            "startup": false,
            "wait_interval": 0
          },
          "output_extras": [
            {
              "item_id": 1
            }
          ]
        },
        "colab_type": "code",
        "executionInfo": {
          "elapsed": 214,
          "status": "ok",
          "timestamp": 1474675631563,
          "user": {
            "displayName": "",
            "photoUrl": "",
            "userId": ""
          },
          "user_tz": 420
        },
        "id": "7DzDJ7sW79ao",
        "outputId": "588b573b-95d2-4587-849e-af6f3ec1303e"
      },
      "outputs": [
        {
          "name": "stdout",
          "output_type": "stream",
          "text": [
            "[3.0, 3.0, 3.0, 3.0]\n"
          ]
        }
      ],
      "source": [
        "print([x + y for x, y in zip([1.0] * 4, [2.0] * 4)])"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 0,
      "metadata": {
        "cellView": "both",
        "colab": {
          "autoexec": {
            "startup": false,
            "wait_interval": 0
          },
          "output_extras": [
            {
              "item_id": 1
            }
          ]
        },
        "colab_type": "code",
        "executionInfo": {
          "elapsed": 340,
          "status": "ok",
          "timestamp": 1474675631948,
          "user": {
            "displayName": "",
            "photoUrl": "",
            "userId": ""
          },
          "user_tz": 420
        },
        "id": "MDWJf0lHAF4E",
        "outputId": "bee09475-24dd-4331-fc46-692a07dae101"
      },
      "outputs": [
        {
          "name": "stdout",
          "output_type": "stream",
          "text": [
            "[ 1.  1.  1.  1.] + [ 2.  2.  2.  2.] = [ 3.  3.  3.  3.]\n"
          ]
        }
      ],
      "source": [
        "import numpy as np\n",
        "x, y = np.full(4, 1.0), np.full(4, 2.0)\n",
        "print(\"{} + {} = {}\".format(x, y, x + y))"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "colab_type": "text",
        "id": "I52jQOyO8vAn"
      },
      "source": [
        "## Details of adding two vectors in TensorFlow\n",
        "\n",
        "The example above of adding two vectors involves a lot more than it seems, so let's look at it in more depth.\n",
        "\n",
        "\u003e`import tensorflow as tf`\n",
        "\n",
        "This import brings TensorFlow's public API into our IPython runtime environment.\n",
        "\n",
        "\u003e`with tf.Session():`\n",
        "\n",
        "When you run an operation in TensorFlow, you need to do it in the context of a `Session`. A session holds the computation graph, which contains the tensors and the operations. When you create tensors and operations, they are not executed immediately, but wait for other operations and tensors to be added to the graph, only executing when finally requested to produce the results of the session. Deferring the execution like this provides additional opportunities for parallelism and optimization, as TensorFlow can decide how to combine operations and where to run them after TensorFlow knows about all the operations. \n",
        "\n",
        "\u003e\u003e`input1 = tf.constant([1.0, 1.0, 1.0, 1.0])`\n",
        "\n",
        "\u003e\u003e`input2 = tf.constant([2.0, 2.0, 2.0, 2.0])`\n",
        "\n",
        "The next two lines create tensors using a convenience function called `constant`, which is similar to numpy's `array` and numpy's `full`. If you look at the code for `constant`, you can see the details of what it is doing to create the tensor. In summary, it creates a tensor of the necessary shape and applies the constant operator to it to fill it with the provided values. The values to `constant` can be Python or numpy arrays. `constant` can take an optional shape parameter, which works similarly to numpy's `fill` if provided, and an optional name parameter, which can be used to put a more human-readable label on the operation in the TensorFlow operation graph.\n",
        "\n",
        "\u003e\u003e`output = tf.add(input1, input2)`\n",
        "\n",
        "You might think `add` just adds the two vectors now, but it doesn't quite do that. What it does is put the `add` operation into the computational graph. The results of the addition aren't available yet. They've been put in the computation graph, but the computation graph hasn't been executed yet.\n",
        "\n",
        "\u003e\u003e`result = output.eval()`\n",
        "\n",
        "\u003e\u003e`print result`\n",
        "\n",
        "`eval()` is also slightly more complicated than it looks. Yes, it does get the value of the vector (tensor) that results from the addition. It returns this as a numpy array, which can then be printed. But, it's important to realize it also runs the computation graph at this point, because we demanded the output from the operation node of the graph; to produce that, it had to run the computation graph. So, this is the point where the addition is actually performed, not when `add` was called, as `add` just put the addition operation into the TensorFlow computation graph."
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "colab_type": "text",
        "id": "H_5_2YY3ySr2"
      },
      "source": [
        "## Multiple operations\n",
        "\n",
        "To use TensorFlow, you add operations on tensors that produce tensors to the computation graph, then execute that graph to run all those operations and calculate the values of all the tensors in the graph.\n",
        "\n",
        "Here's a simple example with two operations:"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 0,
      "metadata": {
        "cellView": "both",
        "colab": {
          "autoexec": {
            "startup": false,
            "wait_interval": 0
          },
          "output_extras": [
            {
              "item_id": 1
            }
          ]
        },
        "colab_type": "code",
        "executionInfo": {
          "elapsed": 1203,
          "status": "ok",
          "timestamp": 1474675633108,
          "user": {
            "displayName": "",
            "photoUrl": "",
            "userId": ""
          },
          "user_tz": 420
        },
        "id": "-kQmn3U_yXX8",
        "outputId": "8ba14a4d-b0cd-4b90-8b95-790e77d35e70"
      },
      "outputs": [
        {
          "name": "stdout",
          "output_type": "stream",
          "text": [
            "[ 6.  6.  6.  6.]\n"
          ]
        }
      ],
      "source": [
        "import tensorflow as tf\n",
        "\n",
        "with tf.Session():\n",
        "    input1 = tf.constant(1.0, shape=[4])\n",
        "    input2 = tf.constant(2.0, shape=[4])\n",
        "    input3 = tf.constant(3.0, shape=[4])\n",
        "    output = tf.add(tf.add(input1, input2), input3)\n",
        "    result = output.eval()\n",
        "    print(result)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "colab_type": "text",
        "id": "Hod0zvsly8YT"
      },
      "source": [
        "This version uses `constant` in a way similar to numpy's `fill`, specifying the optional shape and having the values copied out across it.\n",
        "\n",
        "The `add` operator supports operator overloading, so you could try writing it inline as `input1 + input2` instead as well as experimenting with other operators."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 0,
      "metadata": {
        "cellView": "both",
        "colab": {
          "autoexec": {
            "startup": false,
            "wait_interval": 0
          },
          "output_extras": [
            {
              "item_id": 1
            }
          ]
        },
        "colab_type": "code",
        "executionInfo": {
          "elapsed": 350,
          "status": "ok",
          "timestamp": 1474675633468,
          "user": {
            "displayName": "",
            "photoUrl": "",
            "userId": ""
          },
          "user_tz": 420
        },
        "id": "yS2WElRfxz53",
        "outputId": "2e3efae6-3990-447c-e05d-56a9d9701e87"
      },
      "outputs": [
        {
          "name": "stdout",
          "output_type": "stream",
          "text": [
            "[ 3.  3.  3.  3.]\n"
          ]
        }
      ],
      "source": [
        "with tf.Session():\n",
        "    input1 = tf.constant(1.0, shape=[4])\n",
        "    input2 = tf.constant(2.0, shape=[4])\n",
        "    output = input1 + input2\n",
        "    print(output.eval())"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "colab_type": "text",
        "id": "zszjoYUjkUNU"
      },
      "source": [
        "##  Adding two matrices"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "colab_type": "text",
        "id": "EWNYBCB6kbri"
      },
      "source": [
        "Next, let's do something very similar, adding two matrices:\n",
        "\n",
        "$\\begin{bmatrix}\n",
        "  1. \u0026 1. \u0026 1. \\\\\n",
        "  1. \u0026 1. \u0026 1. \\\\\n",
        "\\end{bmatrix} + \n",
        "\\begin{bmatrix}\n",
        "  1. \u0026 2. \u0026 3. \\\\\n",
        "  4. \u0026 5. \u0026 6. \\\\\n",
        "\\end{bmatrix} = \n",
        "\\begin{bmatrix}\n",
        "  2. \u0026 3. \u0026 4. \\\\\n",
        "  5. \u0026 6. \u0026 7. \\\\\n",
        "\\end{bmatrix}$"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 0,
      "metadata": {
        "cellView": "both",
        "colab": {
          "autoexec": {
            "startup": false,
            "wait_interval": 0
          },
          "output_extras": [
            {
              "item_id": 1
            }
          ]
        },
        "colab_type": "code",
        "executionInfo": {
          "elapsed": 1327,
          "status": "ok",
          "timestamp": 1474675634683,
          "user": {
            "displayName": "",
            "photoUrl": "",
            "userId": ""
          },
          "user_tz": 420
        },
        "id": "tmWcCxSilYkg",
        "outputId": "8a135ccf-e706-457c-f4bc-2187039ffd92"
      },
      "outputs": [
        {
          "name": "stdout",
          "output_type": "stream",
          "text": [
            "[[ 2.  3.  4.]\n",
            " [ 5.  6.  7.]]\n"
          ]
        }
      ],
      "source": [
        "import tensorflow as tf\n",
        "import numpy as np\n",
        "\n",
        "with tf.Session():\n",
        "    input1 = tf.constant(1.0, shape=[2, 3])\n",
        "    input2 = tf.constant(np.reshape(np.arange(1.0, 7.0, dtype=np.float32), (2, 3)))\n",
        "    output = tf.add(input1, input2)\n",
        "    print(output.eval())"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "colab_type": "text",
        "id": "JuU3Bmglq1vd"
      },
      "source": [
        "Recall that you can pass numpy or Python arrays into `constant`.\n",
        "\n",
        "In this example, the matrix with values from 1 to 6 is created in numpy and passed into `constant`, but TensorFlow also has `range`, `reshape`, and `tofloat` operators. Doing this entirely within TensorFlow could be more efficient if this was a very large matrix.\n",
        "\n",
        "Try experimenting with this code a bit -- maybe modifying some of the values, using the numpy version, doing this using, adding another operation, or doing this using TensorFlow's `range` function."
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "colab_type": "text",
        "id": "gnXnpnuLrflb"
      },
      "source": [
        "##  Multiplying matrices"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "colab_type": "text",
        "id": "Ho-QNSOorj0y"
      },
      "source": [
        "Let's move on to matrix multiplication. This time, let's use a bit vector and some random values, which is a good step toward some of what we'll need to do for regression and neural networks."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 0,
      "metadata": {
        "cellView": "both",
        "colab": {
          "autoexec": {
            "startup": false,
            "wait_interval": 0
          },
          "output_extras": [
            {
              "item_id": 1
            }
          ]
        },
        "colab_type": "code",
        "executionInfo": {
          "elapsed": 2353,
          "status": "ok",
          "timestamp": 1474675637053,
          "user": {
            "displayName": "",
            "photoUrl": "",
            "userId": ""
          },
          "user_tz": 420
        },
        "id": "uNqMaFR8sIY5",
        "outputId": "b630554e-68b3-4904-c07d-f28a0a41bbd2"
      },
      "outputs": [
        {
          "name": "stdout",
          "output_type": "stream",
          "text": [
            "Input:\n",
            "[[ 1.  0.  0.  1.]]\n",
            "Weights:\n",
            "[[ 0.3949919  -0.83823347]\n",
            " [ 0.25941893 -1.58861065]\n",
            " [-1.11733329 -0.60435963]\n",
            " [ 1.04782867  0.18336453]]\n",
            "Output:\n",
            "[[ 1.44282055 -0.65486896]]\n"
          ]
        }
      ],
      "source": [
        "#@test {\"output\": \"ignore\"}\n",
        "import tensorflow as tf\n",
        "import numpy as np\n",
        "\n",
        "with tf.Session():\n",
        "    input_features = tf.constant(np.reshape([1, 0, 0, 1], (1, 4)).astype(np.float32))\n",
        "    weights = tf.constant(np.random.randn(4, 2).astype(np.float32))\n",
        "    output = tf.matmul(input_features, weights)\n",
        "    print(\"Input:\")\n",
        "    print(input_features.eval())\n",
        "    print(\"Weights:\")\n",
        "    print(weights.eval())\n",
        "    print(\"Output:\")\n",
        "    print(output.eval())"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "colab_type": "text",
        "id": "JDAVTPhb22AP"
      },
      "source": [
        "Above, we're taking a 1 x 4 vector [1 0 0 1] and multiplying it by a 4 by 2 matrix full of random values from a normal distribution (mean 0, stdev 1). The output is a 1 x 2 matrix.\n",
        "\n",
        "You might try modifying this example. Running the cell multiple times will generate new random weights and a new output. Or, change the input, e.g., to \\[0 0 0 1]), and run the cell again. Or, try initializing the weights using the TensorFlow op, e.g., `random_normal`, instead of using numpy to generate the random weights.\n",
        "\n",
        "What we have here is the basics of a simple neural network already. If we are reading in the input features, along with some expected output, and change the weights based on the error with the output each time, that's a neural network."
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "colab_type": "text",
        "id": "XhnBjAUILuy8"
      },
      "source": [
        "## Use of variables\n",
        "\n",
        "Let's look at adding two small matrices in a loop, not by creating new tensors every time, but by updating the existing values and then re-running the computation graph on the new data. This happens a lot with machine learning models, where we change some parameters each time such as gradient descent on some weights and then perform the same computations over and over again."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 0,
      "metadata": {
        "cellView": "both",
        "colab": {
          "autoexec": {
            "startup": false,
            "wait_interval": 0
          },
          "output_extras": [
            {
              "item_id": 1
            }
          ]
        },
        "colab_type": "code",
        "executionInfo": {
          "elapsed": 2561,
          "status": "ok",
          "timestamp": 1474675639610,
          "user": {
            "displayName": "",
            "photoUrl": "",
            "userId": ""
          },
          "user_tz": 420
        },
        "id": "vJ_AgZ8lLtRv",
        "outputId": "b8f19c28-a9b4-4fb3-9e90-6e432bf300a7"
      },
      "outputs": [
        {
          "name": "stdout",
          "output_type": "stream",
          "text": [
            "[[ -7.29560852e-05   8.01583767e-01]] [[ -7.29560852e-05   8.01583767e-01]]\n",
            "[[ 0.64477301 -0.03944111]] [[ 0.64470005  0.76214266]]\n",
            "[[-0.07470274 -0.76814342]] [[ 0.56999731 -0.00600076]]\n",
            "[[-0.34230471 -0.42372179]] [[ 0.2276926  -0.42972255]]\n",
            "[[ 0.67873812  0.65932178]] [[ 0.90643072  0.22959924]]\n"
          ]
        }
      ],
      "source": [
        "#@test {\"output\": \"ignore\"}\n",
        "import tensorflow as tf\n",
        "import numpy as np\n",
        "\n",
        "with tf.Session() as sess:\n",
        "    # Set up two variables, total and weights, that we'll change repeatedly.\n",
        "    total = tf.Variable(tf.zeros([1, 2]))\n",
        "    weights = tf.Variable(tf.random_uniform([1,2]))\n",
        "\n",
        "    # Initialize the variables we defined above.\n",
        "    tf.global_variables_initializer().run()\n",
        "\n",
        "    # This only adds the operators to the graph right now. The assignment\n",
        "    # and addition operations are not performed yet.\n",
        "    update_weights = tf.assign(weights, tf.random_uniform([1, 2], -1.0, 1.0))\n",
        "    update_total = tf.assign(total, tf.add(total, weights))\n",
        "  \n",
        "    for _ in range(5):\n",
        "        # Actually run the operation graph, so randomly generate weights and then\n",
        "        # add them into the total. Order does matter here. We need to update\n",
        "        # the weights before updating the total.\n",
        "        sess.run(update_weights)\n",
        "        sess.run(update_total)\n",
        "    \n",
        "        print(weights.eval(), total.eval())"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "colab_type": "text",
        "id": "kSYJr89aM_n0"
      },
      "source": [
        "This is more complicated. At a high level, we create two variables and add operations over them, then, in a loop, repeatedly execute those operations. Let's walk through it step by step.\n",
        "\n",
        "Starting off, the code creates two variables, `total` and `weights`. `total` is initialized to \\[0, 0\\] and `weights` is initialized to random values between -1 and 1.\n",
        "\n",
        "Next, two assignment operators are added to the graph, one that updates weights with random values from [-1, 1], the other that updates the total with the new weights. Again, the operators are not executed here. In fact, this isn't even inside the loop. We won't execute these operations until the `eval` call inside the loop.\n",
        "\n",
        "Finally, in the for loop, we run each of the operators. In each iteration of the loop, this executes the operators we added earlier, first putting random values into the weights, then updating the totals with the new weights. This call uses `eval` on the session; the code also could have called `eval` on the operators (e.g. `update_weights.eval`).\n",
        "\n",
        "It can be a little hard to wrap your head around exactly what computation is done when. The important thing to remember is that computation is only performed on demand.\n",
        "\n",
        "Variables can be useful in cases where you have a large amount of computation and data that you want to use over and over again with just a minor change to the input each time. That happens quite a bit with neural networks, for example, where you just want to update the weights each time you go through the batches of input data, then run the same operations over again."
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "colab_type": "text",
        "id": "fL3WfAbKzqr5"
      },
      "source": [
        "## What's next?\n",
        "\n",
        "This has been a gentle introduction to TensorFlow, focused on what TensorFlow is and the very basics of doing anything in TensorFlow. If you'd like more, the next tutorial in the series is Getting Started with TensorFlow, also available in the [notebooks directory](..)."
      ]
    }
  ],
  "metadata": {
    "colab": {
      "default_view": {},
      "name": "Untitled",
      "provenance": [],
      "version": "0.3.2",
      "views": {}
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}