TripletlossFn and train_model

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