Skip to content

Commit 654e54e

Browse files
Shorna AlamShorna Alam
authored andcommitted
Merge branch '2024' of https://github.com/aamini/introtodeeplearning into 2024
adding image logging to lab 2
2 parents f4095df + 71470ce commit 654e54e

File tree

2 files changed

+271
-171
lines changed

2 files changed

+271
-171
lines changed

lab1/Part1_TensorFlow.ipynb

Lines changed: 18 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@
1010
" <td align=\"center\"><a target=\"_blank\" href=\"http://introtodeeplearning.com\">\n",
1111
" <img src=\"https://i.ibb.co/Jr88sn2/mit.png\" style=\"padding-bottom:5px;\" />\n",
1212
" Visit MIT Deep Learning</a></td>\n",
13-
" <td align=\"center\"><a target=\"_blank\" href=\"https://colab.research.google.com/github/aamini/introtodeeplearning/blob/2023/lab1/Part1_TensorFlow.ipynb\">\n",
13+
" <td align=\"center\"><a target=\"_blank\" href=\"https://colab.research.google.com/github/aamini/introtodeeplearning/blob/2024/lab1/Part1_TensorFlow.ipynb\">\n",
1414
" <img src=\"https://i.ibb.co/2P3SLwK/colab.png\" style=\"padding-bottom:5px;\" />Run in Google Colab</a></td>\n",
15-
" <td align=\"center\"><a target=\"_blank\" href=\"https://github.com/aamini/introtodeeplearning/blob/2023/lab1/Part1_TensorFlow.ipynb\">\n",
15+
" <td align=\"center\"><a target=\"_blank\" href=\"https://github.com/aamini/introtodeeplearning/blob/2024/lab1/Part1_TensorFlow.ipynb\">\n",
1616
" <img src=\"https://i.ibb.co/xfJbPmL/github.png\" height=\"70px\" style=\"padding-bottom:5px;\" />View Source on GitHub</a></td>\n",
1717
"</table>\n",
1818
"\n",
@@ -28,8 +28,8 @@
2828
},
2929
"outputs": [],
3030
"source": [
31-
"# Copyright 2023 MIT Introduction to Deep Learning. All Rights Reserved.\n",
32-
"# \n",
31+
"# Copyright 2024 MIT Introduction to Deep Learning. All Rights Reserved.\n",
32+
"#\n",
3333
"# Licensed under the MIT License. You may not use this file except in compliance\n",
3434
"# with the License. Use and/or modification of this code outside of MIT Introduction\n",
3535
"# to Deep Learning must reference:\n",
@@ -65,41 +65,16 @@
6565
},
6666
"outputs": [],
6767
"source": [
68-
"%tensorflow_version 2.x\n",
6968
"import tensorflow as tf\n",
7069
"\n",
7170
"# Download and import the MIT Introduction to Deep Learning package\n",
72-
"!pip install mitdeeplearning\n",
71+
"!pip install mitdeeplearning --quiet\n",
7372
"import mitdeeplearning as mdl\n",
7473
"\n",
7574
"import numpy as np\n",
7675
"import matplotlib.pyplot as plt"
7776
]
7877
},
79-
{
80-
"cell_type": "markdown",
81-
"metadata": {},
82-
"source": [
83-
"## 0.2 Set Up Comet\n",
84-
"\n",
85-
"When training models, it can be useful to visualize information about the model with plots. We can do this manually, but here, we'll show you how to do this using a tool called Comet, which generates loss and GPU usage curves for you.\n",
86-
"\n",
87-
"First, sign up for a Comet account [at this link](https://www.comet.com/signup?utm_source=mit_dl&utm_medium=partner&utm_content=github\n",
88-
") (you can use your Google or Github account). Running this cell will prompt you to enter your API Key (which you can find by pressing the '?' in the top right corner and then 'Quickstart Guide' - it is on the right hand side of the page)."
89-
]
90-
},
91-
{
92-
"cell_type": "code",
93-
"execution_count": null,
94-
"metadata": {},
95-
"outputs": [],
96-
"source": [
97-
"%pip install comet_ml\n",
98-
"import comet_ml\n",
99-
"comet_ml.init(project_name=\"6.s191lab1_part0\")\n",
100-
"comet_experiment = comet_ml.Experiment()"
101-
]
102-
},
10378
{
10479
"cell_type": "markdown",
10580
"metadata": {
@@ -189,7 +164,7 @@
189164
"outputs": [],
190165
"source": [
191166
"'''TODO: Define a 4-d Tensor.'''\n",
192-
"# Use tf.zeros to initialize a 4-d Tensor of zeros with size 10 x 256 x 256 x 3. \n",
167+
"# Use tf.zeros to initialize a 4-d Tensor of zeros with size 10 x 256 x 256 x 3.\n",
193168
"# You can think of this as 10 images where each image is RGB 256 x 256.\n",
194169
"images = # TODO\n",
195170
"\n",
@@ -335,7 +310,7 @@
335310
"## 1.3 Neural networks in TensorFlow\n",
336311
"We can also define neural networks in TensorFlow. TensorFlow uses a high-level API called [Keras](https://www.tensorflow.org/guide/keras) that provides a powerful, intuitive framework for building and training deep learning models.\n",
337312
"\n",
338-
"Let's first consider the example of a simple perceptron defined by just one dense layer: $ y = \\sigma(Wx + b)$, where $W$ represents a matrix of weights, $b$ is a bias, $x$ is the input, $\\sigma$ is the sigmoid activation function, and $y$ is the output. We can also visualize this operation using a graph: \n",
313+
"Let's first consider the example of a simple perceptron defined by just one dense layer: $ y = \\sigma(Wx + b)$, where $W$ represents a matrix of weights, $b$ is a bias, $x$ is the input, $\\sigma$ is the sigmoid activation function, and $y$ is the output. We can also visualize this operation using a graph:\n",
339314
"\n",
340315
"![alt text](https://raw.githubusercontent.com/aamini/introtodeeplearning/master/lab1/img/computation-graph-2.png)\n",
341316
"\n",
@@ -394,7 +369,7 @@
394369
"id": "Jt1FgM7qYZ3D"
395370
},
396371
"source": [
397-
"Conveniently, TensorFlow has defined a number of ```Layers``` that are commonly used in neural networks, for example a [```Dense```](https://www.tensorflow.org/api_docs/python/tf/keras/layers/Dense?version=stable). Now, instead of using a single ```Layer``` to define our simple neural network, we'll use the [`Sequential`](https://www.tensorflow.org/versions/r2.0/api_docs/python/tf/keras/Sequential) model from Keras and a single [`Dense` ](https://www.tensorflow.org/versions/r2.0/api_docs/python/tf/keras/layers/Dense) layer to define our network. With the `Sequential` API, you can readily create neural networks by stacking together layers like building blocks. "
372+
"Conveniently, TensorFlow has defined a number of ```Layers``` that are commonly used in neural networks, for example a [```Dense```](https://www.tensorflow.org/api_docs/python/tf/keras/layers/Dense?version=stable). Now, instead of using a single ```Layer``` to define our simple neural network, we'll use the [`Sequential`](https://www.tensorflow.org/versions/r2.0/api_docs/python/tf/keras/Sequential) model from Keras and a single [`Dense` ](https://www.tensorflow.org/versions/r2.0/api_docs/python/tf/keras/layers/Dense) layer to define our network. With the `Sequential` API, you can readily create neural networks by stacking together layers like building blocks."
398373
]
399374
},
400375
{
@@ -414,12 +389,12 @@
414389
"# Define the number of outputs\n",
415390
"n_output_nodes = 3\n",
416391
"\n",
417-
"# First define the model \n",
392+
"# First define the model\n",
418393
"model = Sequential()\n",
419394
"\n",
420395
"'''TODO: Define a dense (fully connected) layer to compute z'''\n",
421396
"# Remember: dense layers are defined by the parameters W and b!\n",
422-
"# You can read more about the initialization of W and b in the TF documentation :) \n",
397+
"# You can read more about the initialization of W and b in the TF documentation :)\n",
423398
"# https://www.tensorflow.org/api_docs/python/tf/keras/layers/Dense?version=stable\n",
424399
"dense_layer = # TODO\n",
425400
"\n",
@@ -479,7 +454,7 @@
479454
" # In __init__, we define the Model's layers\n",
480455
" def __init__(self, n_output_nodes):\n",
481456
" super(SubclassModel, self).__init__()\n",
482-
" '''TODO: Our model consists of a single Dense layer. Define this layer.''' \n",
457+
" '''TODO: Our model consists of a single Dense layer. Define this layer.'''\n",
483458
" self.dense_layer = '''TODO: Dense Layer'''\n",
484459
"\n",
485460
" # In the call function, we define the Model's forward pass.\n",
@@ -543,7 +518,7 @@
543518
" super(IdentityModel, self).__init__()\n",
544519
" self.dense_layer = tf.keras.layers.Dense(n_output_nodes, activation='sigmoid')\n",
545520
"\n",
546-
" '''TODO: Implement the behavior where the network outputs the input, unchanged, \n",
521+
" '''TODO: Implement the behavior where the network outputs the input, unchanged,\n",
547522
" under control of the isidentity argument.'''\n",
548523
" def call(self, inputs, isidentity=False):\n",
549524
" x = self.dense_layer(inputs)\n",
@@ -596,11 +571,11 @@
596571
"## 1.4 Automatic differentiation in TensorFlow\n",
597572
"\n",
598573
"[Automatic differentiation](https://en.wikipedia.org/wiki/Automatic_differentiation)\n",
599-
"is one of the most important parts of TensorFlow and is the backbone of training with \n",
600-
"[backpropagation](https://en.wikipedia.org/wiki/Backpropagation). We will use the TensorFlow GradientTape [`tf.GradientTape`](https://www.tensorflow.org/api_docs/python/tf/GradientTape?version=stable) to trace operations for computing gradients later. \n",
574+
"is one of the most important parts of TensorFlow and is the backbone of training with\n",
575+
"[backpropagation](https://en.wikipedia.org/wiki/Backpropagation). We will use the TensorFlow GradientTape [`tf.GradientTape`](https://www.tensorflow.org/api_docs/python/tf/GradientTape?version=stable) to trace operations for computing gradients later.\n",
601576
"\n",
602577
"When a forward pass is made through the network, all forward-pass operations get recorded to a \"tape\"; then, to compute the gradient, the tape is played backwards. By default, the tape is discarded after it is played backwards; this means that a particular `tf.GradientTape` can only\n",
603-
"compute one gradient, and subsequent calls throw a runtime error. However, we can compute multiple gradients over the same computation by creating a ```persistent``` gradient tape. \n",
578+
"compute one gradient, and subsequent calls throw a runtime error. However, we can compute multiple gradients over the same computation by creating a ```persistent``` gradient tape.\n",
604579
"\n",
605580
"First, we will look at how we can compute gradients using GradientTape and access them for computation. We define the simple function $ y = x^2$ and compute the gradient:"
606581
]
@@ -663,14 +638,13 @@
663638
"# Define the target value\n",
664639
"x_f = 4\n",
665640
"\n",
666-
"# We will run SGD for a number of iterations. At each iteration, we compute the loss, \n",
641+
"# We will run SGD for a number of iterations. At each iteration, we compute the loss,\n",
667642
"# compute the derivative of the loss with respect to x, and perform the SGD update.\n",
668643
"for i in range(500):\n",
669644
" with tf.GradientTape() as tape:\n",
670645
" '''TODO: define the loss as described above'''\n",
671646
" loss = # TODO\n",
672647
"\n",
673-
" comet_experiment.log_metric(\"loss\", loss, step=i) \n",
674648
" # loss minimization using gradient tape\n",
675649
" grad = tape.gradient(loss, x) # compute the derivative of the loss with respect to x\n",
676650
" new_x = x - learning_rate*grad # sgd update\n",
@@ -682,8 +656,7 @@
682656
"plt.plot([0, 500],[x_f,x_f])\n",
683657
"plt.legend(('Predicted', 'True'))\n",
684658
"plt.xlabel('Iteration')\n",
685-
"plt.ylabel('x value')\n",
686-
"comet_experiment.end()"
659+
"plt.ylabel('x value')"
687660
]
688661
},
689662
{
@@ -722,4 +695,4 @@
722695
},
723696
"nbformat": 4,
724697
"nbformat_minor": 0
725-
}
698+
}

0 commit comments

Comments
 (0)