TripletlossFn and train_model

Hello I am getting the wrong loss as 0.35175386 and train_model gives me the error like

Hi @yjewon121,

In your Ex 3, the variable triplet_loss is incorrectly implemented.

You are ignoring the instructions:
# add the two losses together and take the fastnp.sum of it

Cheers,
Mubsi

Hi @anjil,

After looking at your notebook, I see you have removed some variables which you are suppose to initialise. I’m sharing the template code with you here, please use this template and try to attempt the exercises again, and don’t remove any of the variables.

# UNQ_C3 (UNIQUE CELL IDENTIFIER, DO NOT EDIT)
# GRADED FUNCTION: TripletLossFn
def TripletLossFn(v1, v2, margin=0.25):
    """Custom Loss function.

    Args:
        v1 (numpy.ndarray): Array with dimension (batch_size, model_dimension) associated to Q1.
        v2 (numpy.ndarray): Array with dimension (batch_size, model_dimension) associated to Q2.
        margin (float, optional): Desired margin. Defaults to 0.25.

    Returns:
        jax.interpreters.xla.DeviceArray: Triplet Loss.
    """
    ### START CODE HERE (Replace instances of 'None' with your code) ###
    # use fastnp to take the dot product of the two batches (don't forget to transpose the second argument)
    scores = None # pairwise cosine sim    
    # calculate new batch size
    batch_size = len(scores)
    # use fastnp to grab all postive `diagonal` entries in `scores`
    positive = None  # the positive ones (duplicates)
    # subtract `fastnp.eye(batch_size)` out of 1.0 and do element-wise multiplication with `scores`
    negative_zero_on_duplicate = None
    # use `fastnp.sum` on `negative_zero_on_duplicate` for `axis=1` and divide it by `(batch_size - 1)`
    mean_negative = None
    # create a composition of two masks: 
    # the first mask to extract the diagonal elements, 
    # the second mask to extract elements in the negative_zero_on_duplicate matrix that are larger than the elements in the diagonal 
    mask_exclude_positives = (None)|(None)
    # multiply `mask_exclude_positives` with 2.0 and subtract it out of `negative_zero_on_duplicate`
    negative_without_positive = None
    # take the row by row `max` of `negative_without_positive`. 
    # Hint: negative_without_positive.max(axis = [?])  
    closest_negative = None
    # compute `fastnp.maximum` among 0.0 and `A`
    # where A = subtract `positive` from `margin` and add `closest_negative`
    # IMPORTANT: DO NOT create an extra variable 'A'
    triplet_loss1 = None
    # compute `fastnp.maximum` among 0.0 and `B`
    # where B = ubtract `positive` from `margin` and add `mean_negative`
    # IMPORTANT: DO NOT create an extra variable 'B'
    triplet_loss2 = None
    # add the two losses together and take the `fastnp.sum` of it    
    triplet_loss = None    
    ### END CODE HERE ###
    
    return triplet_loss

Best,
Mubsi

P.S I have added this in your notebook as well.

Hi @Mubsi , can you please check my exercise 4 where I am getting lr schedule variable as not defined and in exercise 3 , I am getting 5 Pass and 1 fail. Please help in this.

My lab id is hmhoenjefnhz

Hi @Riddhima_Sobti,

Did you copy/paste the solution from the internet ?

Here’s your Ex 3:

def TripletLossFn(v1, v2, margin=0.25):
    """Custom Loss function.

    Args:
        v1 (numpy.ndarray): Array with dimension (batch_size, model_dimension) associated to Q1.
        v2 (numpy.ndarray): Array with dimension (batch_size, model_dimension) associated to Q2.
        margin (float, optional): Desired margin. Defaults to 0.25.

    Returns:
        jax.interpreters.xla.DeviceArray: Triplet Loss.
    """
    ### START CODE HERE (Replace instances of 'None' with your code) ###
    # use fastnp to take the dot product of the two batches (don't forget to transpose the second argument)
    scores = ### YOUR CODE # pairwise cosine sim
    # calculate new batch size
    batch_size = ### YOUR CODE
    # use fastnp to grab all postive `diagonal` entries in `scores`
    positive = ### YOUR CODE
    # multiply `fastnp.eye(batch_size)` with 2.0 and subtract it out of `scores`
    negative_without_positive = ### YOUR CODE
    # take the row by row `max` of `negative_without_positive`. 
    # Hint: negative_without_positive.max(axis = [?])  
    closest_negative = ### YOUR CODE
    # subtract `fastnp.eye(batch_size)` out of 1.0 and do element-wise multiplication with `scores`
    negative_zero_on_duplicate = ### YOUR CODE
    # use `fastnp.sum` on `negative_zero_on_duplicate` for `axis=1` and divide it by `(batch_size - 1)` 
    mean_negative = ### YOUR CODE
    # compute `fastnp.maximum` among 0.0 and `A`
    # A = subtract `positive` from `margin` and add `closest_negative` 
    triplet_loss1 = ### YOUR CODE
    # compute `fastnp.maximum` among 0.0 and `B`
    # B = subtract `positive` from `margin` and add `mean_negative`
    triplet_loss2 = ### YOUR CODE
    # add the two losses together and take the `fastnp.sum` of it
    triplet_loss = ### YOUR CODE
    
    ### END CODE HERE ###
    
    return triplet_loss

And this is the actual code skeleton we have provided for Ex 3:

def TripletLossFn(v1, v2, margin=0.25):
    """Custom Loss function.

    Args:
        v1 (numpy.ndarray): Array with dimension (batch_size, model_dimension) associated to Q1.
        v2 (numpy.ndarray): Array with dimension (batch_size, model_dimension) associated to Q2.
        margin (float, optional): Desired margin. Defaults to 0.25.

    Returns:
        jax.interpreters.xla.DeviceArray: Triplet Loss.
    """
    ### START CODE HERE (Replace instances of 'None' with your code) ###
    # use fastnp to take the dot product of the two batches (don't forget to transpose the second argument)
    scores = None # pairwise cosine sim    
    # calculate new batch size
    batch_size = len(scores)
    # use fastnp to grab all postive `diagonal` entries in `scores`
    positive = None  # the positive ones (duplicates)
    # subtract `fastnp.eye(batch_size)` out of 1.0 and do element-wise multiplication with `scores`
    negative_zero_on_duplicate = None
    # use `fastnp.sum` on `negative_zero_on_duplicate` for `axis=1` and divide it by `(batch_size - 1)`
    mean_negative = None
    # create a composition of two masks: 
    # the first mask to extract the diagonal elements, 
    # the second mask to extract elements in the negative_zero_on_duplicate matrix that are larger than the elements in the diagonal 
    mask_exclude_positives = (None)|(None)
    # multiply `mask_exclude_positives` with 2.0 and subtract it out of `negative_zero_on_duplicate`
    negative_without_positive = None
    # take the row by row `max` of `negative_without_positive`. 
    # Hint: negative_without_positive.max(axis = [?])  
    closest_negative = None
    # compute `fastnp.maximum` among 0.0 and `A`
    # where A = subtract `positive` from `margin` and add `closest_negative`
    # IMPORTANT: DO NOT create an extra variable 'A'
    triplet_loss1 = None
    # compute `fastnp.maximum` among 0.0 and `B`
    # where B = ubtract `positive` from `margin` and add `mean_negative`
    # IMPORTANT: DO NOT create an extra variable 'B'
    triplet_loss2 = None
    # add the two losses together and take the `fastnp.sum` of it    
    triplet_loss = None    
    ### END CODE HERE ###
    
    return triplet_loss

Apart from the variable names and changed comments, one very distinctive feature in the code you are using from the skeleton we have provided is that in your code there are total 10 variables to be implemented and in our skeleton there are 11.

And in your Ex 4, this is the error you are getting:


It very straightforwardly tells you NameError: name 'lr_schedule' is not defined.

lr_schedule is no where defined in the code we have provided, then why are you using it ? Of course it is going to through the error as mentioned above.

As you know, using solutions that are not your own is against the honour code of this community, and I can’t help you when the only effort you are making is copy/pasting instead of actually trying to implement your code.

I’d suggest to get a fresh copy of the notebook by following the instructions and try again, and this time, pay close attention to all of the exercise instructions and hints provided in the notebook.

Best,
Mubsi

Hi, @Mubsi , Yes, I did refer to the online solution. This was the last week assignment of the course and I couldn’t get time to see all the videos of this week and like I said my course expires tomorrow. I thought of going through this assignment again in next couple of days. I myself know that there is no usage of looking at online solutions but just that I couldn’t purchase it again, I thought of just completing this week exercise like this. Thanks a lot for your suggestion. Would go through a fresh notebook.

Hi @Mubsi ,
can you please help me calculating “mask_exclude_positives = (None)|(None)”.I worked on this for an hour without success.