How to Implement np.array - 1

{Using Keras’ Tokenizer yields values that start at 1 rather than at 0. This will present a problem when training since Keras usually expects the labels to start at 0. To work around this issue you could use an extra neuron in the last layer of your model. However this approach is rather hacky and not very clear. Instead you will substract 1 from every value of the labels that the function returns. Remember that when using numpy arrays you can simply do something like np.array - 1 to accomplish this since numpy allows for vectorized operations.}
I’m Getting error: TypeError: unsupported operand type(s) for -: ‘list’ and ‘int’
Method i’m implementing,

Convert labels to sequences

label_seq = tokenizer.texts_to_sequences(split_labels)

Convert sequences to a numpy array. Don’t forget to substact 1 from every entry in the array!

label_seq_np = np.asarray(label_seq)-1

Hi @Movlik_Suresh_Senjal ,
Your approach appears to be good and is an effective way to avoid any issues with the labels.
It’s possible that the problem lies in a previous function rather than the specific step you mentioned. Would it be possible for you to share your notebook with me directly so that I can take a closer look and provide some feedback?

Thanks,
Ado

Hi @Movlik_Suresh_Senjal
Please remove the notebook from this location. I will review it and provide direct feedback. Afterwards, we can discuss the issue to support other students facing the same problem.

I have the same issue

Hi @Skanda_Vyas
We were able to identify and resolve a few problems using the Movilik code. I would like to share some tips that may be helpful to you as well.

Firstly, in lines 200, Movlik has mixed up the labeling and sentence variables when splitting the database.

    # Split the sentences and labels into train/validation splits
    train_sentences = sentences[:train_size]
    train_labels = sentences[:train_size]

    validation_sentences = labels[train_size:]
    validation_labels = labels[train_size:]

This change ensures that the labels are assigned to the train_labels and validation_labels variables, while the sentences are assigned to train_sentences and validation_sentences , respectively.

I suggest modifying the code as follows:

    # Split the sentences and labels into train/validation splits
    train_sentences = sentences[:train_size]
    train_labels = labels[:train_size]

    validation_sentences = sentences[train_size:]
    validation_labels = labels[train_size:]

And the other issue was on line 398

label_seq_np = np.array(label_seq[])-1

It seems that you may have been testing and inadvertently included the square brackets [] in your code. The correct syntax should be:

    label_seq_np = np.array(label_seq) - 1

I have tested the code up to this point, and it appears to be functioning correctly.

If you’re experiencing the same issue as Skanda, I hope this helps. Alternatively, you can send me a direct message with your code and we can examine it in greater depth.

Ado,

1 Like

I’m afraid, the errors in my code aren’t similar to the ones faced by the OP.
Thank you, I’ll be sending you my notebook shortly.

1 Like

nevermind, i figured it out. Thank you though

I made a silly mistake

validation_labels = sentences[train_size:]

For people who are going to visit this thread later. Don’t do the same mistake. :sweat_smile:

you can always help others with your mistakes but do not provide a clear cut code in the community as it is against the community guidelines, kindly remove the code if it is a part of any graded cell.

Regards
DP

FYI, this code snippet has a bug and I even have not mentioned what’s the problem in it.
So, anybody who is going visit this thread and have done the same mistake will have to take his / her time and ponder about what’s wrong with this line and would know if he / she did the same mistake and will change his / her code.

I don’t think I have violated any community guidelines here.

Regards,

I do not mean to offend you, Even I have done wrong codes but I do not mention the codes on posts but only explain where I went wrong and how it could be corrected. I only mentioned this for your safety as they take action. Sorry if it offended you.

Regards
DP