|
10 | 10 | " <td align=\"center\"><a target=\"_blank\" href=\"http://introtodeeplearning.com\">\n", |
11 | 11 | " <img src=\"https://i.ibb.co/Jr88sn2/mit.png\" style=\"padding-bottom:5px;\" />\n", |
12 | 12 | " 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", |
14 | 14 | " <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", |
16 | 16 | " <img src=\"https://i.ibb.co/xfJbPmL/github.png\" height=\"70px\" style=\"padding-bottom:5px;\" />View Source on GitHub</a></td>\n", |
17 | 17 | "</table>\n", |
18 | 18 | "\n", |
|
62 | 62 | "outputs": [], |
63 | 63 | "source": [ |
64 | 64 | "# Import Tensorflow 2.0\n", |
| 65 | + "# !pip install tensorflow\n", |
65 | 66 | "import tensorflow as tf\n", |
66 | 67 | "\n", |
67 | 68 | "# MIT introduction to deep learning package\n", |
|
107 | 108 | "cell_type": "code", |
108 | 109 | "source": [ |
109 | 110 | "# 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", |
111 | 112 | "comet_model_1 = comet_ml.Experiment()" |
112 | 113 | ], |
113 | 114 | "metadata": { |
|
215 | 216 | " tf.keras.layers.Flatten(),\n", |
216 | 217 | "\n", |
217 | 218 | " # '''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", |
219 | 221 | "\n", |
220 | 222 | " # '''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", |
222 | 225 | "\n", |
223 | 226 | " ])\n", |
224 | 227 | " return fc_model\n", |
|
345 | 348 | "outputs": [], |
346 | 349 | "source": [ |
347 | 350 | "'''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", |
349 | 353 | "\n", |
350 | 354 | "print('Test accuracy:', test_acc)" |
351 | 355 | ] |
|
406 | 410 | " cnn_model = tf.keras.Sequential([\n", |
407 | 411 | "\n", |
408 | 412 | " # 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", |
410 | 415 | "\n", |
411 | 416 | " # 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", |
413 | 419 | "\n", |
414 | 420 | " # 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", |
416 | 423 | "\n", |
417 | 424 | " # 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", |
419 | 427 | "\n", |
420 | 428 | " tf.keras.layers.Flatten(),\n", |
421 | 429 | " tf.keras.layers.Dense(128, activation=tf.nn.relu),\n", |
422 | 430 | "\n", |
423 | 431 | " # TODO: Define the last Dense layer to output the classification\n", |
424 | 432 | " # probabilities. Pay attention to the activation needed a probability\n", |
425 | 433 | " # 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", |
427 | 436 | " ])\n", |
428 | 437 | "\n", |
429 | 438 | " return cnn_model\n", |
|
458 | 467 | "comet_model_2 = comet_ml.Experiment()\n", |
459 | 468 | "\n", |
460 | 469 | "'''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" |
462 | 474 | ] |
463 | 475 | }, |
464 | 476 | { |
|
479 | 491 | "outputs": [], |
480 | 492 | "source": [ |
481 | 493 | "'''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()" |
484 | 497 | ] |
485 | 498 | }, |
486 | 499 | { |
|
501 | 514 | "outputs": [], |
502 | 515 | "source": [ |
503 | 516 | "'''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", |
505 | 519 | "\n", |
506 | 520 | "print('Test accuracy:', test_acc)" |
507 | 521 | ] |
|
580 | 594 | "source": [ |
581 | 595 | "'''TODO: identify the digit with the highest confidence prediction for the first\n", |
582 | 596 | " image in the test dataset. '''\n", |
583 | | - "prediction = # TODO\n", |
| 597 | + "prediction = np.argmax(predictions[0])\n", |
| 598 | + "# prediction = # TODO\n", |
584 | 599 | "\n", |
585 | 600 | "print(prediction)" |
586 | 601 | ] |
|
710 | 725 | " # GradientTape to record differentiation operations\n", |
711 | 726 | " with tf.GradientTape() as tape:\n", |
712 | 727 | " #'''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", |
714 | 730 | "\n", |
715 | 731 | " #'''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", |
719 | 733 | " 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", |
720 | 735 | "\n", |
721 | 736 | " loss_history.append(loss_value.numpy().mean()) # append the loss to the loss_history record\n", |
722 | 737 | " plotter.plot(loss_history.get())\n", |
723 | 738 | "\n", |
724 | 739 | " # Backpropagation\n", |
725 | 740 | " '''TODO: Use the tape to compute the gradient against all parameters in the CNN model.\n", |
726 | 741 | " 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", |
728 | 744 | " optimizer.apply_gradients(zip(grads, cnn_model.trainable_variables))\n", |
729 | 745 | "\n", |
730 | 746 | "comet_model_3.log_figure(figure=plt)\n", |
|
0 commit comments