Course 4 week 4 - grading 66/100

Hi! In my code everything works as asked. It passed all the tests and however I got that grade, I rechecked it and couldn´t find an error.
The grader wrote:
Code Cell UNQ_C1: Function ‘triplet_loss’ is correct.
Code Cell UNQ_C2: Function ‘verify’ is correct.
Code Cell UNQ_C3: Unexpected error (UnboundLocalError(“local variable ‘identity’ referenced before assignment”)) occurred during function check. We expected function who_is_it to return compute_layer_style_cost test 1 failed. Please check that this function is defined properly.
If you see many functions being marked as incorrect, try to trace back your steps & identify if there is an incorrect function that is being used in other steps.
This dependency may be the cause of the errors.

Has anyone experienced the same situation?

This is the code from exericse 3:

Step 1: Compute the target “encoding” for the image. Use img_to_encoding() see example above. ## (≈ 1 line)

{moderator edit - solution code removed}

Your code looks correct to me. There really aren’t that many moving parts here. One thing to realize is that the grader does not do an automatic “Save” for you in Course 4. So maybe the correct code you are looking at is not what the grader is actually seeing. Try doing a “Save” and then “Submit” again. Of course by now your notebook session has expired, so you’ll have to restart. Does the code still look the same as above?

I think this is a minor mistake on the deeplearning.ai’s part. I just initialized identity to an empty string right below where min_dist is initialized and everything worked.

@ttfoley, you’ve replied on thread that is 2 and 1/2 years old. The assignment as been updated since then.

There are no other recent reports of issues with the “identity” variable.

So I’m guessing your code originally had one error, and you added a second error to offset the first one.

I just had this error myself, today.

Interesting. It’s a good point that they don’t initialize that variable. But if you get that error it means you never saw a sample with a distance less than 100. Meaning that you never took the “if” clause. I didn’t have that problem, so it must be something in your code that is different.

Update: I felt like I was going crazy based on your and TMosh comments, so I commented out that initialization and still got a 100% score. The notebook was freshly opened so maybe there was some strange statefulness hidden in there? I don’t understand. To be clear, it passed without initializing identity. That was the only thing I changed previously to get it to pass. Should I delete my prior comments?

It’s ok, now that you confirm it’s not really a problem. There are several levels of statefulness that can cause problems:

  1. If you just type new code in a function cell and then run the test cell again, it calls the old function. You have to either click “Shift-Enter” or “Cell → Run” directly on the actual function cell itself in order to get the new code compiled. Or do “Kernel → Restart” and “Cell → Run All”.
  2. The “Submit” in Course 4 does not do an automatic “Save” for you, so the grader may be seeing different code than you are looking at.

Many of the notebooks use global variables (it’s especially common in Course 5 “Sequence Models”). So you need to be very careful if a global variable is actually a TensorFlow layer - because every time you modify one of those layers, you have to re-run all of the cells to create the object again.