Calculate the loss for class (column) 1

I would appreciate someone help me out with this “calculate the loss for class (column) 1

print(f"w_p[1]: {w_p[1]}“)
print(f"y_true[:,1]: {y_true[:, 1]}”)
print(f"y_pred[:,1]: {y_pred[:, 1]}")

This is the output I am getting:

w_p[1]: 0.8

IndexError Traceback (most recent call last)
Cell In[148], line 2
1 print(f"w_p[1]: {w_p[1]}“)
----> 2 print(f"y_true[:,1]: {y_true[:, 1]}”)
3 print(f"y_pred[:,1]: {y_pred[:, 1]}")

IndexError: index 1 is out of bounds for axis 1 with size 1

Hi @Odutola_Olujim,

It seems your y_true' has a size 1 for axis 1, so y_true[:,1]` would generate an error. You can check above this point in the exercise; in the cell right underneath the section “2. Weighted Loss for More than One Class”, you would have viewed the labels (true values). What does the output look like? What is the size of axis 1 of the output? That will probably point you to where the issue is. If not, you can DM me your code and we can take it from there.
Thank you.

Hello @Odutola_Olujim

If this error is from the test cell after you calculated weighted loss for y_true and y_pred.

I see you are calculating positive weight and negative weight separately and then trying to calculate loss, that would not be the right way.

What you need to do
You recall loss for multi-class by using for loop,
then under that loop using K.log you calculate both pos_weights and neg_weights and apply K.mean to this calculation. Refer the below image to calculate loss in a single equation.

Let us know if your issue still persist.

Regards
DP

Thank you Hassan for your reply. I followed all the steps outlined in the exercise.
y_true: array([[1, 0],
[1, 0],
[1, 0],
[1, 0],
[0, 1]])

using the axis:
using axis = 0 [4 1]
using axis = 1 [1 1 1 1 1]

w_p = np.sum(y_true == 0,axis=0) / y_true.shape[0]
w_p
output: array([0.2, 0.8])

w_n = np.sum(y_true == 1, axis=0) / y_true.shape[0]
w_n
output:
array([0.8, 0.2])

y_pred = np.ones(y_true.shape)
y_pred[:,0] = 0.3 * y_pred[:,0]
y_pred[:,1] = 0.7 * y_pred[:,1]
y_pred
output: array([[0.3, 0.7],
[0.3, 0.7],
[0.3, 0.7],
[0.3, 0.7],
[0.3, 0.7]])

print(f"w_p[0]: {w_p[0]}“)
print(f"y_true[:,0]: {y_true[:,0]}”)
print(f"y_pred[:,0]: {y_pred[:,0]}")

output: w_p[0]: 0.2
y_true[:,0]: [1 1 1 1 0]
y_pred[:,0]: [0.3 0.3 0.3 0.3 0.3]

loss_0_pos = -1 * np.sum(w_p[0] *
y_true[:, 0] *
np.log(y_pred[:, 0])
)
print(f"loss_0_pos: {loss_0_pos:.4f}")

output: loss_0_pos: 0.9632

print(f"w_n[0]: {w_n[0]}“)
print(f"y_true[:,0]: {y_true[:,0]}”)
print(f"y_pred[:,0]: {y_pred[:,0]}")

output: w_n[0]: 0.8
y_true[:,0]: [1 1 1 1 0]
y_pred[:,0]: [0.3 0.3 0.3 0.3 0.3]

loss_0_neg = -1 * np.sum(
w_n[0] *
(1 - y_true[:, 0]) *
np.log(1 - y_pred[:, 0])
)
print(f"loss_0_neg: {loss_0_neg:.4f}")

output: loss_0_neg: 0.2853

loss_0 = loss_0_neg + loss_0_pos
print(f"loss_0: {loss_0:.4f}")

output: loss_0: 1.2485

I am trying to do this next one for class 1 “Can you calculate the loss for class (column) 1” and I got this error:

1 print(f"w_p[1]: {w_p[1]}“)
----> 2 print(f"y_true[:,1]: {y_true[:, 1]}”)
3 print(f"y_pred[:,1]: {y_pred[:, 1]}")

IndexError: index 1 is out of bounds for axis 1 with size 1.

Kindly review my code and let me know the next step. Thank you.

Thank you, DP. I appreciate your reply. I started this week but my focus is still on Week 1. The lesson is yet to get to using a loop for me. I am trying to follow it step by step without jumping the ones in between.

Hello @Odutola_Olujim

is this not part of assignment?

You haven’t mentioned which grade cell you are talking about.

Anyways, seems you are getting followup from other mentor. If unable to resolve let me know.

Regards
DP

Thank you, @Odutola_Olujim. You can DM me the code you are using in that last cell. That is, how you are calculating the loss for class (column) 1.
Thank you.

Hi @Odutola_Olujim,

I did not see a DM from you. Were you able to solve the issue? If so, if there is a lesson learned that can be shared with others, please share - without pasting code.
Thank you.