Skip to content

Commit c3e97ee

Browse files
committed
base for tf student versions
1 parent dcb03a5 commit c3e97ee

3 files changed

Lines changed: 1992 additions & 21 deletions

File tree

lab2/Part1_MNIST.ipynb

Lines changed: 37 additions & 21 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/master/lab2/Part1_MNIST.ipynb\">\n",
13+
" <td align=\"center\"><a target=\"_blank\" href=\"https://colab.research.google.com/github/aamini/introtodeeplearning/blob/master/lab2/solutions/Part1_MNIST_Solution.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/master/lab2/Part1_MNIST.ipynb\">\n",
15+
" <td align=\"center\"><a target=\"_blank\" href=\"https://github.com/aamini/introtodeeplearning/blob/master/lab2/solutions/Part1_MNIST_Solution.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",
@@ -62,6 +62,7 @@
6262
"outputs": [],
6363
"source": [
6464
"# Import Tensorflow 2.0\n",
65+
"# !pip install tensorflow\n",
6566
"import tensorflow as tf\n",
6667
"\n",
6768
"# MIT introduction to deep learning package\n",
@@ -107,7 +108,7 @@
107108
"cell_type": "code",
108109
"source": [
109110
"# start a first comet experiment for the first part of the lab\n",
110-
"comet_ml.init(project_name=\"6S191lab2_part1_NN\")\n",
111+
"comet_ml.init(project_name=\"6S191_lab2_part1_NN\")\n",
111112
"comet_model_1 = comet_ml.Experiment()"
112113
],
113114
"metadata": {
@@ -215,10 +216,12 @@
215216
" tf.keras.layers.Flatten(),\n",
216217
"\n",
217218
" # '''TODO: Define the activation function for the first fully connected (Dense) layer.'''\n",
218-
" tf.keras.layers.Dense(128, activation= '''TODO'''),\n",
219+
" tf.keras.layers.Dense(128, activation=tf.nn.relu),\n",
220+
" # tf.keras.layers.Dense(128, activation= '''TODO'''),\n",
219221
"\n",
220222
" # '''TODO: Define the second Dense layer to output the classification probabilities'''\n",
221-
" [TODO Dense layer to output classification probabilities]\n",
223+
" tf.keras.layers.Dense(10, activation=tf.nn.softmax)\n",
224+
" # [TODO Dense layer to output classification probabilities]\n",
222225
"\n",
223226
" ])\n",
224227
" return fc_model\n",
@@ -345,7 +348,8 @@
345348
"outputs": [],
346349
"source": [
347350
"'''TODO: Use the evaluate method to test the model!'''\n",
348-
"test_loss, test_acc = # TODO\n",
351+
"test_loss, test_acc = model.evaluate(test_images, test_labels) # TODO\n",
352+
"# test_loss, test_acc = # TODO\n",
349353
"\n",
350354
"print('Test accuracy:', test_acc)"
351355
]
@@ -406,24 +410,29 @@
406410
" cnn_model = tf.keras.Sequential([\n",
407411
"\n",
408412
" # TODO: Define the first convolutional layer\n",
409-
" tf.keras.layers.Conv2D('''TODO''')\n",
413+
" tf.keras.layers.Conv2D(filters=24, kernel_size=(3,3), activation=tf.nn.relu),\n",
414+
" # tf.keras.layers.Conv2D('''TODO''')\n",
410415
"\n",
411416
" # TODO: Define the first max pooling layer\n",
412-
" tf.keras.layers.MaxPool2D('''TODO''')\n",
417+
" tf.keras.layers.MaxPool2D(pool_size=(2,2)),\n",
418+
" # tf.keras.layers.MaxPool2D('''TODO''')\n",
413419
"\n",
414420
" # TODO: Define the second convolutional layer\n",
415-
" tf.keras.layers.Conv2D('''TODO''')\n",
421+
" tf.keras.layers.Conv2D(filters=36, kernel_size=(3,3), activation=tf.nn.relu),\n",
422+
" # tf.keras.layers.Conv2D('''TODO''')\n",
416423
"\n",
417424
" # TODO: Define the second max pooling layer\n",
418-
" tf.keras.layers.MaxPool2D('''TODO''')\n",
425+
" tf.keras.layers.MaxPool2D(pool_size=(2,2)),\n",
426+
" # tf.keras.layers.MaxPool2D('''TODO''')\n",
419427
"\n",
420428
" tf.keras.layers.Flatten(),\n",
421429
" tf.keras.layers.Dense(128, activation=tf.nn.relu),\n",
422430
"\n",
423431
" # TODO: Define the last Dense layer to output the classification\n",
424432
" # probabilities. Pay attention to the activation needed a probability\n",
425433
" # output\n",
426-
" [TODO Dense layer to output classification probabilities]\n",
434+
" tf.keras.layers.Dense(10, activation=tf.nn.softmax)\n",
435+
" # [TODO Dense layer to output classification probabilities]\n",
427436
" ])\n",
428437
"\n",
429438
" return cnn_model\n",
@@ -458,7 +467,10 @@
458467
"comet_model_2 = comet_ml.Experiment()\n",
459468
"\n",
460469
"'''TODO: Define the compile operation with your optimizer and learning rate of choice'''\n",
461-
"cnn_model.compile(optimizer='''TODO''', loss='''TODO''', metrics=['accuracy']) # TODO"
470+
"cnn_model.compile(optimizer=tf.keras.optimizers.Adam(learning_rate=1e-3),\n",
471+
" loss='sparse_categorical_crossentropy',\n",
472+
" metrics=['accuracy'])\n",
473+
"# cnn_model.compile(optimizer='''TODO''', loss='''TODO''', metrics=['accuracy']) # TODO"
462474
]
463475
},
464476
{
@@ -479,8 +491,9 @@
479491
"outputs": [],
480492
"source": [
481493
"'''TODO: Use model.fit to train the CNN model, with the same batch_size and number of epochs previously used.'''\n",
482-
"cnn_model.fit('''TODO''')\n",
483-
"# comet_model_2.end() ## uncomment this line to end the comet experiment"
494+
"cnn_model.fit(train_images, train_labels, batch_size=BATCH_SIZE, epochs=EPOCHS)\n",
495+
"# cnn_model.fit('''TODO''')\n",
496+
"# comet_model_2.end()"
484497
]
485498
},
486499
{
@@ -501,7 +514,8 @@
501514
"outputs": [],
502515
"source": [
503516
"'''TODO: Use the evaluate method to test the model!'''\n",
504-
"test_loss, test_acc = # TODO\n",
517+
"test_loss, test_acc = cnn_model.evaluate(test_images, test_labels)\n",
518+
"# test_loss, test_acc = # TODO\n",
505519
"\n",
506520
"print('Test accuracy:', test_acc)"
507521
]
@@ -580,7 +594,8 @@
580594
"source": [
581595
"'''TODO: identify the digit with the highest confidence prediction for the first\n",
582596
" image in the test dataset. '''\n",
583-
"prediction = # TODO\n",
597+
"prediction = np.argmax(predictions[0])\n",
598+
"# prediction = # TODO\n",
584599
"\n",
585600
"print(prediction)"
586601
]
@@ -710,21 +725,22 @@
710725
" # GradientTape to record differentiation operations\n",
711726
" with tf.GradientTape() as tape:\n",
712727
" #'''TODO: feed the images into the model and obtain the predictions'''\n",
713-
" logits = # TODO\n",
728+
" logits = cnn_model(images)\n",
729+
" # logits = # TODO\n",
714730
"\n",
715731
" #'''TODO: compute the categorical cross entropy loss\n",
716-
" loss_value = tf.keras.backend.sparse_categorical_crossentropy('''TODO''', '''TODO''') # TODO\n",
717-
"\n",
718-
" # log the loss to comet\n",
732+
" loss_value = tf.keras.backend.sparse_categorical_crossentropy(labels, logits)\n",
719733
" comet_model_3.log_metric(\"loss\", loss_value.numpy().mean(), step=idx)\n",
734+
" # loss_value = tf.keras.backend.sparse_categorical_crossentropy('''TODO''', '''TODO''') # TODO\n",
720735
"\n",
721736
" loss_history.append(loss_value.numpy().mean()) # append the loss to the loss_history record\n",
722737
" plotter.plot(loss_history.get())\n",
723738
"\n",
724739
" # Backpropagation\n",
725740
" '''TODO: Use the tape to compute the gradient against all parameters in the CNN model.\n",
726741
" Use cnn_model.trainable_variables to access these parameters.'''\n",
727-
" grads = # TODO\n",
742+
" grads = tape.gradient(loss_value, cnn_model.trainable_variables)\n",
743+
" # grads = # TODO\n",
728744
" optimizer.apply_gradients(zip(grads, cnn_model.trainable_variables))\n",
729745
"\n",
730746
"comet_model_3.log_figure(figure=plt)\n",

0 commit comments

Comments
 (0)