Skip to content

Commit 963f182

Browse files
committed
tf l2p2 student
1 parent 91808e1 commit 963f182

File tree

1 file changed

+23
-42
lines changed

1 file changed

+23
-42
lines changed

lab2/TF_Part2_Debiasing.ipynb

Lines changed: 23 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@
2727
" <td align=\"center\"><a target=\"_blank\" href=\"http://introtodeeplearning.com\">\n",
2828
" <img src=\"https://i.ibb.co/Jr88sn2/mit.png\" style=\"padding-bottom:5px;\" />\n",
2929
" Visit MIT Deep Learning</a></td>\n",
30-
" <td align=\"center\"><a target=\"_blank\" href=\"https://colab.research.google.com/github/aamini/introtodeeplearning/blob/master/lab2/solutions/TF_Part2_Debiasing_Solution.ipynb\">\n",
30+
" <td align=\"center\"><a target=\"_blank\" href=\"https://colab.research.google.com/github/aamini/introtodeeplearning/blob/master/lab2/TF_Part2_Debiasing.ipynb\">\n",
3131
" <img src=\"https://i.ibb.co/2P3SLwK/colab.png\" style=\"padding-bottom:5px;\" />Run in Google Colab</a></td>\n",
32-
" <td align=\"center\"><a target=\"_blank\" href=\"https://github.com/aamini/introtodeeplearning/blob/master/lab2/solutions/TF_Part2_Debiasing_Solution.ipynb\">\n",
32+
" <td align=\"center\"><a target=\"_blank\" href=\"https://github.com/aamini/introtodeeplearning/blob/master/lab2/TF_Part2_Debiasing.ipynb\">\n",
3333
" <img src=\"https://i.ibb.co/xfJbPmL/github.png\" height=\"70px\" style=\"padding-bottom:5px;\" />View Source on GitHub</a></td>\n",
3434
"</table>\n",
3535
"\n",
@@ -568,22 +568,19 @@
568568
"def vae_loss_function(x, x_recon, mu, logsigma, kl_weight=0.0005):\n",
569569
" # TODO: Define the latent loss. Note this is given in the equation for L_{KL}\n",
570570
" # in the text block directly above\n",
571-
" latent_loss = 0.5 * tf.reduce_sum(tf.exp(logsigma) + tf.square(mu) - 1.0 - logsigma, axis=1)\n",
572-
" # latent_loss = # TODO\n",
571+
" latent_loss = # TODO\n",
573572
"\n",
574573
" # TODO: Define the reconstruction loss as the mean absolute pixel-wise\n",
575574
" # difference between the input and reconstruction. Hint: you'll need to\n",
576575
" # use tf.reduce_mean, and supply an axis argument which specifies which\n",
577576
" # dimensions to reduce over. For example, reconstruction loss needs to average\n",
578577
" # over the height, width, and channel image dimensions.\n",
579578
" # https://www.tensorflow.org/api_docs/python/tf/math/reduce_mean\n",
580-
" reconstruction_loss = tf.reduce_mean(tf.abs(x-x_recon), axis=(1,2,3))\n",
581-
" # reconstruction_loss = # TODO\n",
579+
" reconstruction_loss = # TODO\n",
582580
"\n",
583581
" # TODO: Define the VAE loss. Note this is given in the equation for L_{VAE}\n",
584582
" # in the text block directly above\n",
585-
" vae_loss = kl_weight * latent_loss + reconstruction_loss\n",
586-
" # vae_loss = # TODO\n",
583+
" vae_loss = # TODO\n",
587584
"\n",
588585
" return vae_loss"
589586
],
@@ -637,8 +634,8 @@
637634
"\n",
638635
" # TODO: Define the reparameterization computation!\n",
639636
" # Note the equation is given in the text block immediately above.\n",
640-
" z = z_mean + tf.math.exp(0.5 * z_logsigma) * epsilon\n",
641-
" # z = # TODO\n",
637+
" z = # TODO\n",
638+
"\n",
642639
" return z"
643640
],
644641
"execution_count": null,
@@ -722,25 +719,19 @@
722719
"def debiasing_loss_function(x, x_pred, y, y_logit, mu, logsigma):\n",
723720
"\n",
724721
" # TODO: call the relevant function to obtain VAE loss\n",
725-
" vae_loss = vae_loss_function(x, x_pred, mu, logsigma)\n",
726-
" # vae_loss = vae_loss_function('''TODO''') # TODO\n",
722+
" vae_loss = vae_loss_function('''TODO''') # TODO\n",
727723
"\n",
728724
" # TODO: define the classification loss using sigmoid_cross_entropy\n",
729725
" # https://www.tensorflow.org/api_docs/python/tf/nn/sigmoid_cross_entropy_with_logits\n",
730-
" classification_loss = tf.nn.sigmoid_cross_entropy_with_logits(labels=y, logits=y_logit)\n",
731-
" # classification_loss = # TODO\n",
726+
" classification_loss = # TODO\n",
732727
"\n",
733728
" # Use the training data labels to create variable face_indicator:\n",
734729
" # indicator that reflects which training data are images of faces\n",
735730
" face_indicator = tf.cast(tf.equal(y, 1), tf.float32)\n",
736731
"\n",
737732
" # TODO: define the DB-VAE total loss! Use tf.reduce_mean to average over all\n",
738733
" # samples\n",
739-
" total_loss = tf.reduce_mean(\n",
740-
" classification_loss +\n",
741-
" face_indicator * vae_loss\n",
742-
" )\n",
743-
" # total_loss = # TODO\n",
734+
" total_loss = # TODO\n",
744735
"\n",
745736
" return total_loss, classification_loss"
746737
],
@@ -835,6 +826,7 @@
835826
"\n",
836827
" # classification prediction\n",
837828
" y_logit = tf.expand_dims(encoder_output[:, 0], -1)\n",
829+
"\n",
838830
" # latent variable distribution parameters\n",
839831
" z_mean = encoder_output[:, 1:self.latent_dim+1]\n",
840832
" z_logsigma = encoder_output[:, self.latent_dim+1:]\n",
@@ -844,15 +836,13 @@
844836
" # VAE reparameterization: given a mean and logsigma, sample latent variables\n",
845837
" def reparameterize(self, z_mean, z_logsigma):\n",
846838
" # TODO: call the sampling function defined above\n",
847-
" z = sampling(z_mean, z_logsigma)\n",
848-
" # z = # TODO\n",
839+
" z = # TODO\n",
849840
" return z\n",
850841
"\n",
851842
" # Decode the latent space and output reconstruction\n",
852843
" def decode(self, z):\n",
853844
" # TODO: use the decoder to output the reconstruction\n",
854-
" reconstruction = self.decoder(z)\n",
855-
" # reconstruction = # TODO\n",
845+
" reconstruction = # TODO\n",
856846
" return reconstruction\n",
857847
"\n",
858848
" # The call function will be used to pass inputs x through the core VAE\n",
@@ -861,12 +851,10 @@
861851
" y_logit, z_mean, z_logsigma = self.encode(x)\n",
862852
"\n",
863853
" # TODO: reparameterization\n",
864-
" z = self.reparameterize(z_mean, z_logsigma)\n",
865-
" # z = # TODO\n",
854+
" z = # TODO\n",
866855
"\n",
867856
" # TODO: reconstruction\n",
868-
" recon = self.decode(z)\n",
869-
" # recon = # TODO\n",
857+
" recon = # TODO\n",
870858
" return y_logit, z_mean, z_logsigma, recon\n",
871859
"\n",
872860
" # Predict face or not face logit for given input x\n",
@@ -956,8 +944,7 @@
956944
" print(\"Recomputing the sampling probabilities\")\n",
957945
"\n",
958946
" # TODO: run the input batch and get the latent variable means\n",
959-
" mu = get_latent_mu(images, dbvae)\n",
960-
" # mu = get_latent_mu('''TODO''') # TODO\n",
947+
" mu = get_latent_mu('''TODO''') # TODO\n",
961948
"\n",
962949
" # sampling probabilities for the images\n",
963950
" training_sample_p = np.zeros(mu.shape[0])\n",
@@ -976,8 +963,7 @@
976963
" # TODO: call the digitize function to find which bins in the latent distribution\n",
977964
" # every data sample falls in to\n",
978965
" # https://docs.scipy.org/doc/numpy-1.13.0/reference/generated/numpy.digitize.html\n",
979-
" bin_idx = np.digitize(latent_distribution, bin_edges)\n",
980-
" # bin_idx = np.digitize('''TODO''', '''TODO''') # TODO\n",
966+
" bin_idx = np.digitize('''TODO''', '''TODO''') # TODO\n",
981967
"\n",
982968
" # smooth the density function\n",
983969
" hist_smoothed_density = hist_density + smoothing_fac\n",
@@ -987,13 +973,11 @@
987973
" p = 1.0/(hist_smoothed_density[bin_idx-1])\n",
988974
"\n",
989975
" # TODO: normalize all probabilities\n",
990-
" p = p / np.sum(p)\n",
991-
" # p = # TODO\n",
976+
" p = # TODO\n",
992977
"\n",
993978
" # TODO: update sampling probabilities by considering whether the newly\n",
994979
" # computed p is greater than the existing sampling probabilities.\n",
995-
" training_sample_p = np.maximum(p, training_sample_p)\n",
996-
" # training_sample_p = # TODO\n",
980+
" training_sample_p = # TODO\n",
997981
"\n",
998982
" # final normalization\n",
999983
" training_sample_p /= np.sum(training_sample_p)\n",
@@ -1044,13 +1028,11 @@
10441028
" y_logit, z_mean, z_logsigma, x_recon = dbvae(x)\n",
10451029
"\n",
10461030
" '''TODO: call the DB_VAE loss function to compute the loss'''\n",
1047-
" loss, class_loss = debiasing_loss_function(x, x_recon, y, y_logit, z_mean, z_logsigma)\n",
1048-
" # loss, class_loss = debiasing_loss_function('''TODO arguments''') # TODO\n",
1031+
" loss, class_loss = debiasing_loss_function('''TODO arguments''') # TODO\n",
10491032
"\n",
10501033
" '''TODO: use the GradientTape.gradient method to compute the gradients.\n",
10511034
" Hint: this is with respect to the trainable_variables of the dbvae.'''\n",
1052-
" grads = tape.gradient(loss, dbvae.trainable_variables)\n",
1053-
" # grads = tape.gradient('''TODO''', '''TODO''') # TODO\n",
1035+
" grads = tape.gradient('''TODO''', '''TODO''') # TODO\n",
10541036
"\n",
10551037
" # apply gradients to variables\n",
10561038
" optimizer.apply_gradients(zip(grads, dbvae.trainable_variables))\n",
@@ -1070,8 +1052,7 @@
10701052
"\n",
10711053
" # Recompute data sampling proabilities\n",
10721054
" '''TODO: recompute the sampling probabilities for debiasing'''\n",
1073-
" p_faces = get_training_sample_probabilities(all_faces, dbvae)\n",
1074-
" # p_faces = get_training_sample_probabilities('''TODO''', '''TODO''') # TODO\n",
1055+
" p_faces = get_training_sample_probabilities('''TODO''', '''TODO''') # TODO\n",
10751056
"\n",
10761057
" # get a batch of training data and compute the training step\n",
10771058
" for j in tqdm(range(loader.get_train_size() // params[\"batch_size\"])):\n",
@@ -1163,4 +1144,4 @@
11631144
]
11641145
}
11651146
]
1166-
}
1147+
}

0 commit comments

Comments
 (0)