27
27
" <td align=\" center\" ><a target=\" _blank\" href=\" http://introtodeeplearning.com\" >\n " ,
28
28
" <img src=\" https://i.ibb.co/Jr88sn2/mit.png\" style=\" padding-bottom:5px;\" />\n " ,
29
29
" 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 " ,
31
31
" <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 " ,
33
33
" <img src=\" https://i.ibb.co/xfJbPmL/github.png\" height=\" 70px\" style=\" padding-bottom:5px;\" />View Source on GitHub</a></td>\n " ,
34
34
" </table>\n " ,
35
35
" \n " ,
568
568
" def vae_loss_function(x, x_recon, mu, logsigma, kl_weight=0.0005):\n " ,
569
569
" # TODO: Define the latent loss. Note this is given in the equation for L_{KL}\n " ,
570
570
" # 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 " ,
573
572
" \n " ,
574
573
" # TODO: Define the reconstruction loss as the mean absolute pixel-wise\n " ,
575
574
" # difference between the input and reconstruction. Hint: you'll need to\n " ,
576
575
" # use tf.reduce_mean, and supply an axis argument which specifies which\n " ,
577
576
" # dimensions to reduce over. For example, reconstruction loss needs to average\n " ,
578
577
" # over the height, width, and channel image dimensions.\n " ,
579
578
" # 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 " ,
582
580
" \n " ,
583
581
" # TODO: Define the VAE loss. Note this is given in the equation for L_{VAE}\n " ,
584
582
" # 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 " ,
587
584
" \n " ,
588
585
" return vae_loss"
589
586
],
637
634
" \n " ,
638
635
" # TODO: Define the reparameterization computation!\n " ,
639
636
" # 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 " ,
642
639
" return z"
643
640
],
644
641
"execution_count" : null ,
722
719
" def debiasing_loss_function(x, x_pred, y, y_logit, mu, logsigma):\n " ,
723
720
" \n " ,
724
721
" # 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 " ,
727
723
" \n " ,
728
724
" # TODO: define the classification loss using sigmoid_cross_entropy\n " ,
729
725
" # 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 " ,
732
727
" \n " ,
733
728
" # Use the training data labels to create variable face_indicator:\n " ,
734
729
" # indicator that reflects which training data are images of faces\n " ,
735
730
" face_indicator = tf.cast(tf.equal(y, 1), tf.float32)\n " ,
736
731
" \n " ,
737
732
" # TODO: define the DB-VAE total loss! Use tf.reduce_mean to average over all\n " ,
738
733
" # 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 " ,
744
735
" \n " ,
745
736
" return total_loss, classification_loss"
746
737
],
835
826
" \n " ,
836
827
" # classification prediction\n " ,
837
828
" y_logit = tf.expand_dims(encoder_output[:, 0], -1)\n " ,
829
+ " \n " ,
838
830
" # latent variable distribution parameters\n " ,
839
831
" z_mean = encoder_output[:, 1:self.latent_dim+1]\n " ,
840
832
" z_logsigma = encoder_output[:, self.latent_dim+1:]\n " ,
844
836
" # VAE reparameterization: given a mean and logsigma, sample latent variables\n " ,
845
837
" def reparameterize(self, z_mean, z_logsigma):\n " ,
846
838
" # TODO: call the sampling function defined above\n " ,
847
- " z = sampling(z_mean, z_logsigma)\n " ,
848
- " # z = # TODO\n " ,
839
+ " z = # TODO\n " ,
849
840
" return z\n " ,
850
841
" \n " ,
851
842
" # Decode the latent space and output reconstruction\n " ,
852
843
" def decode(self, z):\n " ,
853
844
" # TODO: use the decoder to output the reconstruction\n " ,
854
- " reconstruction = self.decoder(z)\n " ,
855
- " # reconstruction = # TODO\n " ,
845
+ " reconstruction = # TODO\n " ,
856
846
" return reconstruction\n " ,
857
847
" \n " ,
858
848
" # The call function will be used to pass inputs x through the core VAE\n " ,
861
851
" y_logit, z_mean, z_logsigma = self.encode(x)\n " ,
862
852
" \n " ,
863
853
" # TODO: reparameterization\n " ,
864
- " z = self.reparameterize(z_mean, z_logsigma)\n " ,
865
- " # z = # TODO\n " ,
854
+ " z = # TODO\n " ,
866
855
" \n " ,
867
856
" # TODO: reconstruction\n " ,
868
- " recon = self.decode(z)\n " ,
869
- " # recon = # TODO\n " ,
857
+ " recon = # TODO\n " ,
870
858
" return y_logit, z_mean, z_logsigma, recon\n " ,
871
859
" \n " ,
872
860
" # Predict face or not face logit for given input x\n " ,
956
944
" print(\" Recomputing the sampling probabilities\" )\n " ,
957
945
" \n " ,
958
946
" # 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 " ,
961
948
" \n " ,
962
949
" # sampling probabilities for the images\n " ,
963
950
" training_sample_p = np.zeros(mu.shape[0])\n " ,
976
963
" # TODO: call the digitize function to find which bins in the latent distribution\n " ,
977
964
" # every data sample falls in to\n " ,
978
965
" # 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 " ,
981
967
" \n " ,
982
968
" # smooth the density function\n " ,
983
969
" hist_smoothed_density = hist_density + smoothing_fac\n " ,
987
973
" p = 1.0/(hist_smoothed_density[bin_idx-1])\n " ,
988
974
" \n " ,
989
975
" # TODO: normalize all probabilities\n " ,
990
- " p = p / np.sum(p)\n " ,
991
- " # p = # TODO\n " ,
976
+ " p = # TODO\n " ,
992
977
" \n " ,
993
978
" # TODO: update sampling probabilities by considering whether the newly\n " ,
994
979
" # 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 " ,
997
981
" \n " ,
998
982
" # final normalization\n " ,
999
983
" training_sample_p /= np.sum(training_sample_p)\n " ,
1044
1028
" y_logit, z_mean, z_logsigma, x_recon = dbvae(x)\n " ,
1045
1029
" \n " ,
1046
1030
" '''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 " ,
1049
1032
" \n " ,
1050
1033
" '''TODO: use the GradientTape.gradient method to compute the gradients.\n " ,
1051
1034
" 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 " ,
1054
1036
" \n " ,
1055
1037
" # apply gradients to variables\n " ,
1056
1038
" optimizer.apply_gradients(zip(grads, dbvae.trainable_variables))\n " ,
1070
1052
" \n " ,
1071
1053
" # Recompute data sampling proabilities\n " ,
1072
1054
" '''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 " ,
1075
1056
" \n " ,
1076
1057
" # get a batch of training data and compute the training step\n " ,
1077
1058
" for j in tqdm(range(loader.get_train_size() // params[\" batch_size\" ])):\n " ,
1163
1144
]
1164
1145
}
1165
1146
]
1166
- }
1147
+ }
0 commit comments