|
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/2023/lab2/Part2_FaceDetection.ipynb\">\n", |
| 13 | + " <td align=\"center\"><a target=\"_blank\" href=\"https://colab.research.google.com/github/aamini/introtodeeplearning/blob/2023/lab2/solutions/Part2_FaceDetection_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/2023/lab2/Part2_FaceDetection.ipynb\">\n", |
| 15 | + " <td align=\"center\"><a target=\"_blank\" href=\"https://github.com/aamini/introtodeeplearning/blob/2023/lab2/solutions/Part2_FaceDetection_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", |
|
435 | 435 | "def vae_loss_function(x, x_recon, mu, logsigma, kl_weight=0.0005):\n", |
436 | 436 | " # TODO: Define the latent loss. Note this is given in the equation for L_{KL}\n", |
437 | 437 | " # in the text block directly above\n", |
438 | | - " latent_loss = 0.5 * tf.reduce_sum(tf.exp(logsigma) + tf.square(mu) - 1.0 - logsigma, axis=1)\n", |
439 | | - " # latent_loss = # TODO\n", |
| 438 | + " latent_loss = # TODO\n", |
440 | 439 | "\n", |
441 | 440 | " # TODO: Define the reconstruction loss as the mean absolute pixel-wise \n", |
442 | 441 | " # difference between the input and reconstruction. Hint: you'll need to \n", |
443 | 442 | " # use tf.reduce_mean, and supply an axis argument which specifies which \n", |
444 | 443 | " # dimensions to reduce over. For example, reconstruction loss needs to average \n", |
445 | 444 | " # over the height, width, and channel image dimensions.\n", |
446 | 445 | " # https://www.tensorflow.org/api_docs/python/tf/math/reduce_mean\n", |
447 | | - " reconstruction_loss = tf.reduce_mean(tf.abs(x-x_recon), axis=(1,2,3))\n", |
448 | | - " # reconstruction_loss = # TODO\n", |
| 446 | + " reconstruction_loss = # TODO\n", |
449 | 447 | "\n", |
450 | 448 | " # TODO: Define the VAE loss. Note this is given in the equation for L_{VAE}\n", |
451 | 449 | " # in the text block directly above\n", |
452 | | - " vae_loss = kl_weight * latent_loss + reconstruction_loss\n", |
453 | | - " # vae_loss = # TODO\n", |
| 450 | + " vae_loss = # TODO\n", |
454 | 451 | " \n", |
455 | 452 | " return vae_loss" |
456 | 453 | ] |
|
495 | 492 | "\n", |
496 | 493 | " # TODO: Define the reparameterization computation!\n", |
497 | 494 | " # Note the equation is given in the text block immediately above.\n", |
498 | | - " z = z_mean + tf.math.exp(0.5 * z_logsigma) * epsilon\n", |
499 | | - " # z = # TODO\n", |
| 495 | + " z = # TODO\n", |
| 496 | + " \n", |
500 | 497 | " return z" |
501 | 498 | ] |
502 | 499 | }, |
|
588 | 585 | "def ss_vae_loss_function(x, x_pred, y, y_logit, mu, logsigma):\n", |
589 | 586 | "\n", |
590 | 587 | " # TODO: call the relevant function to obtain VAE loss, defined earlier in the lab\n", |
591 | | - " vae_loss = vae_loss_function(x, x_pred, mu, logsigma)\n", |
592 | | - " # vae_loss = vae_loss_function('''TODO''') # TODO\n", |
| 588 | + " vae_loss = vae_loss_function('''TODO''') # TODO\n", |
593 | 589 | "\n", |
594 | 590 | " # TODO: define the classification loss using sigmoid_cross_entropy\n", |
595 | 591 | " # https://www.tensorflow.org/api_docs/python/tf/nn/sigmoid_cross_entropy_with_logits\n", |
596 | | - " classification_loss = tf.nn.sigmoid_cross_entropy_with_logits(labels=y, logits=y_logit)\n", |
597 | | - " # classification_loss = # TODO\n", |
| 592 | + " classification_loss = # TODO\n", |
598 | 593 | "\n", |
599 | 594 | " # Use the training data labels to create variable face_indicator:\n", |
600 | 595 | " # indicator that reflects which training data are images of faces\n", |
601 | 596 | " face_indicator = tf.cast(tf.equal(y, 1), tf.float32)\n", |
602 | 597 | "\n", |
603 | 598 | " # TODO: define the SS-VAE total loss! Use tf.reduce_mean to average over all\n", |
604 | 599 | " # samples\n", |
605 | | - " total_loss = tf.reduce_mean(\n", |
606 | | - " classification_loss + \n", |
607 | | - " face_indicator * vae_loss\n", |
608 | | - " )\n", |
609 | | - " # total_loss = # TODO\n", |
| 600 | + " total_loss = # TODO\n", |
610 | 601 | "\n", |
611 | 602 | " return total_loss, classification_loss, vae_loss" |
612 | 603 | ] |
|
708 | 699 | " # Decode the latent space and output reconstruction\n", |
709 | 700 | " def decode(self, z):\n", |
710 | 701 | " # TODO: use the decoder (self.decoder) to output the reconstruction\n", |
711 | | - " reconstruction = self.decoder(z)\n", |
712 | | - " # reconstruction = # TODO\n", |
| 702 | + " reconstruction = # TODO\n", |
713 | 703 | " return reconstruction\n", |
714 | 704 | "\n", |
715 | 705 | " # The call function will be used to pass inputs x through the core VAE\n", |
|
719 | 709 | "\n", |
720 | 710 | " # TODO: call the sampling function that you created above using \n", |
721 | 711 | " # z_mean and z_logsigma\n", |
722 | | - " z = sampling(z_mean, z_logsigma)\n", |
723 | | - " # z = # TODO\n", |
| 712 | + " z = # TODO\n", |
724 | 713 | "\n", |
725 | 714 | " # TODO: reconstruction\n", |
726 | | - " recon = self.decode(z)\n", |
727 | | - " # recon = # TODO\n", |
| 715 | + " recon = # TODO\n", |
| 716 | + " \n", |
728 | 717 | " return y_logit, z_mean, z_logsigma, recon\n", |
729 | 718 | "\n", |
730 | 719 | " # Predict face or not face logit for given input x\n", |
|
789 | 778 | " y_logit, z_mean, z_logsigma, x_recon = ss_vae(x)\n", |
790 | 779 | "\n", |
791 | 780 | " '''TODO: call the SS_VAE loss function to compute the loss'''\n", |
792 | | - " loss, class_loss, _ = ss_vae_loss_function(x, x_recon, y, y_logit, z_mean, z_logsigma)\n", |
793 | | - " # loss, class_loss = ss_vae_loss_function('''TODO arguments''') # TODO\n", |
| 781 | + " loss, class_loss = ss_vae_loss_function('''TODO arguments''') # TODO\n", |
794 | 782 | " \n", |
795 | 783 | " '''TODO: use the GradientTape.gradient method to compute the gradients.\n", |
796 | 784 | " Hint: this is with respect to the trainable_variables of the SS_VAE.'''\n", |
797 | | - " grads = tape.gradient(loss, ss_vae.trainable_variables)\n", |
798 | | - " # grads = tape.gradient('''TODO''', '''TODO''') # TODO\n", |
| 785 | + " grads = tape.gradient('''TODO''', '''TODO''') # TODO\n", |
799 | 786 | "\n", |
800 | 787 | " # apply gradients to variables\n", |
801 | 788 | " optimizer.apply_gradients(zip(grads, ss_vae.trainable_variables))\n", |
|
0 commit comments