C1W4 A2: Having trouble in get_score function

    other_class_penalty = -1 * torch.mean(torch.tensor([
        torch.sqrt(
            torch.sum(
                torch.tensor([
                    (original_classifications[i, j] - current_classifications[i, j])**2 
                    for j in other_indices
                ])
            )
        ) for i in range(original_classifications.shape[0])
    ])) * penalty_weight
    # Take the mean of the current classifications for the target feature
    target_score = torch.mean(current_classifications[ : , target_indices])

I am having trouble in this function. Though the tests corresponding to this function are passing, but when I am running the next block of code, I am getting an error as follows:

ValueError: only one element tensors can be converted to Python scalars

I see you’re trying to calculate the L2 norm of the difference between original_classifications and current_classifications. PyTorch has a native function to calculate norms, maybe you should check it out: torch.norm. Be sure to choose the right dimension over which to take the norm.

Also, a quick refresher on array indexing: array[:,j] should return all elements of the j-th column, and is easier to type than [array[i,j] for i in range(array.shape[0])].

2 Likes

Hi @Elemento,

It seems that in the test cases, they are passing the index values (list of integers) as other_indices param. Check the highlighted part in Line #11 in the following screenshot:

However, they are passing Boolean values list as other_indices param while calling get_score function (where you are facing this issue) in the loop. Check the output of Line #5 in the following screenshot. Also, find a False in the second line because that is the cause of the error you are facing.

Now, Coming to error:
Since, in the test cases, ONLY the required indices were being passed, that’s why the line for j in other_indices of your code only considers the correct indices values.

However, the boolean list has a False value in it, and therefore, an erroneous tensor is getting appended in your list, and when you try to create a tensor from the list of tensors (the list contains the problematic tensor), they you are getting that ValueError. You can check the following screenshot to get the exact size and value of that problematic tensor.

Screenshot 2021-08-26 at 20.47.10

So, you have a boolean list coming and you know for which kind of list your code works, so I guess you know what to do now. Also, I would say that you should use torch.norm, because that will run your code not just faster but also you don’t have to worry about writing slow and complicated python for loops.

1 Like

Thanks a lot @28utkarsh @pedrorohde for your help. I was able to complete your assignment with your suggestions!

1 Like

That’s a great news @Elemento.

Just struggled through get_score, mostly for not understanding the shape/meaning of the classification matrices.

Take one of the inputs from the unit test below the function:

classification = torch.tensor([[1] * rows, [2] * rows, [3] * rows, [4] * rows]).T.float()
classification
>> tensor([[1., 2., 3., 4.],
        [1., 2., 3., 4.],
        [1., 2., 3., 4.],
        [1., 2., 3., 4.],
        [1., 2., 3., 4.],
        [1., 2., 3., 4.],
        [1., 2., 3., 4.],
        [1., 2., 3., 4.],
        [1., 2., 3., 4.],
        [1., 2., 3., 4.]])

In this case, each row is an example, each column is a feature. To select a list of examples:

classification[[1,2]]
>> tensor([[1., 2., 3., 4.],
        [1., 2., 3., 4.]])

To select a list of features for all examples:

classification[:, [1,2]]
>> tensor([[2., 3.],
        [2., 3.],
        [2., 3.],
        [2., 3.],
        [2., 3.],
        [2., 3.],
        [2., 3.],
        [2., 3.],
        [2., 3.],
        [2., 3.]])

Hope that helps someone else!

1 Like