Some question about inception_block.py in Face Recognition


why the stride of X_pool is 2 ?? Andrew said the stride should be 1 in the course.
the output of X_pool match the size of X_3x3, X_5x5 ???

Hi scut_xu,

This is in line with the design of Inception-v2. The aim is to arrive at an efficient grid size reduction. You can find the description and explanation here:

1 Like

thanks a lot, I have got some brilliant answers!

Well, for starters, your code is incorrect: you are not specifying the “axis” parameter on the reduce_sum calls for pos_dist and neg_dist. As a result, you are getting scalar values for those, where the logic expects a vector output with one element per sample.

Also if you want to debug the intermediate values, it would be a better idea to add the print statements inside the triplet_loss function to make sure what you are seeing is not some artifact of not understanding how TF graphs work.

Also where in the logic does it say that loss should equal basic_loss? With your negative value of basic_loss and the code as written, loss should be 0, right?

Once you figure this out, please remove the source code from your post. We aren’t supposed to leave solutions sitting out on the forums. Even incorrect ones :nerd_face:

Also while we’re at it, here’s a style point: you could make your code more readable by using the variables anchor, positive and negative instead of indexing into y_pred as you do. They went to the trouble of giving you those in the template. Sometimes readability is an important value in code beyond mere correctness. :nerd_face:

Thanks for your birilliant answer !!! I will remove my code, but before that, I still have some questions. Firstly, could you please give me an example if I want to debug the intermediate values?
i.e. I want to konw post_dist, shoud I add print(post_dist), right? Secondly, why the incorrect code can get the corrent answer? it is just a conincidence?

The test cases sometimes don’t catch all possible errors.

My suggestion for using debugging print statements in this case is to put them within the triplet_loss function source itself. For example if you want to see all the intermediate values, you can add these statements to the body of the function:

print(f"pos_dist = {pos_dist}")
print(f"neg_dist = {neg_dist}")
print(f"basic_loss = {basic_loss}")

I added those statement to my triplet_loss function and then ran the standard test cell. Here’s what I get for the output:

pos_dist = [3122.1926 3377.6548 3365.9343]
neg_dist = [3905.9553 2850.595  3434.0498]
basic_loss = [-783.5627   527.2598   -67.91548]
loss = tf.Tensor(527.2598, shape=(), dtype=float32)
pos_dist = 0.0
neg_dist = 0.0
basic_loss = 5.0
pos_dist = 0.0
neg_dist = 2.0
basic_loss = 1.0
pos_dist = 2.0
neg_dist = 0.0
basic_loss = 2.0
pos_dist = 0.0
neg_dist = 0.0
basic_loss = -2.0
pos_dist = [0. 0.]
neg_dist = [2. 2.]
basic_loss = [1. 1.]

You can see that for most of the test cases, there is only one sample so that is why omitting the “axis” parameter does not fail the test cases.

when I specify the axis parameter, the answer is 528.1417 instead of 350.026

Oh, sorry, it looks like you are still on the old version of the course. Things may be different there. I’ll have to go back and look. Everything I have showed is for the new version of the course (first published April 7, 2021).

Ok, I just opened my old notebook and the “Expected Value” for the cost in that test is 528.143. So it looks like your new answer is very close to the correct value. What does 350.xxx have to do with any of this?

my neg_dist is different from mentor’s. but why??? :frowning:

You have a very old version of the notebook, even for the old version of the course. I checked my version from the previous version of the course and here is my screenshot showing the expected value for the test for triplet_loss.


You can see it is different than yours. I don’t know what version of the course you are in or are you perhaps using a local copy of a notebook? In either case, I suggest you update to the current version of the course.

I agree that the numbers you are showing do not make any sense. I think this is some kind of problem with your environment. You are not working with a current version of the notebook, even for the “old” version of the course.

Your code looks correct to me, but the issues with printing are all to do with how TensorFlow version 1 works. The print code I gave you only works in TensorFlow 2 with “Eager execution” mode activated. In TF 1, you have to do as you show and actually use “session.run” or “tensor.eval” to actually get the value of the tensor. So try adding the “eval” to the print statements inside the function. I think that’s the easiest way.

Or just upgrade to the new version of the course so that you can use TF2. TF2 is a lot easier to deal with. The one important thing to know before you do the upgrade is that you will lose all your work and your grading progress. It is essential that you save copies of all your notebooks by downloading them first. Do “File → Download (as notebook)” on every assignment that you have completed. Note that there is quite a bit of new material in the new version of the course so you will have more work to do if you go that route.

Here’s a StackExchange article about printing values of tensors in TF1.