num_classes vs num_tokens #42

Closed
opened 2025-11-02 00:02:14 -05:00 by GiteaMirror · 1 comment
Owner

Originally created by @Aman0807 on GitHub (Nov 24, 2022).

The following padding function used in https://madewithml.com/courses/foundations/convolutional-neural-networks/ refers to num_classes which in the example used comes up to 500. I was wondering if it should be referred as num_tokens (as used in other functions). Just getting confused since as per my understanding num_classes = 4.

def pad_sequences(sequences, max_seq_len=0):
      """Pad sequences to max length in sequence."""
      max_seq_len = max(max_seq_len, max(len(sequence) for sequence in sequences))
      num_classes = sequences[0].shape[-1]
      padded_sequences = np.zeros((len(sequences), max_seq_len, num_classes))
      for i, sequence in enumerate(sequences):
          padded_sequences[i][:len(sequence)] = sequence
      return padded_sequences

Originally created by @Aman0807 on GitHub (Nov 24, 2022). The following padding function used in [https://madewithml.com/courses/foundations/convolutional-neural-networks/](url) refers to `num_classes `which in the example used comes up to 500. I was wondering if it should be referred as `num_tokens` (as used in other functions). Just getting confused since as per my understanding `num_classes = 4.` ``` def pad_sequences(sequences, max_seq_len=0): """Pad sequences to max length in sequence.""" max_seq_len = max(max_seq_len, max(len(sequence) for sequence in sequences)) num_classes = sequences[0].shape[-1] padded_sequences = np.zeros((len(sequences), max_seq_len, num_classes)) for i, sequence in enumerate(sequences): padded_sequences[i][:len(sequence)] = sequence return padded_sequences ```
Author
Owner

@GokuMohandas commented on GitHub (Nov 25, 2022):

Hi @Aman0807, this is a great question and is actually a major source of bugs even in very mature companies. Typically, when I write a function, I want to make it as modular and reusable as possible. So that I can use it for my current context but also for future contexts for my team. In our task here, the number of classes happens to be the same as the number of tokens but in someone else's task, they may still require padding but tokens may not make any sense to them if they're not doing exactly our task. So when writing functions, it's a good practice to use names that are universal (but still descriptive).

I've often seen functions written using specific variables that exist for the current context it's been written for. And then when others reuse the same function (copy/paste or from an internal ML library) they get errors because that specific variable may not exist or it doesn't make sense for their context.

@GokuMohandas commented on GitHub (Nov 25, 2022): Hi @Aman0807, this is a great question and is actually a major source of bugs even in very mature companies. Typically, when I write a function, I want to make it as modular and reusable as possible. So that I can use it for my current context but also for future contexts for my team. In our task here, the number of classes happens to be the same as the number of tokens but in someone else's task, they may still require padding but tokens may not make any sense to them if they're not doing exactly our task. So when writing functions, it's a good practice to use names that are universal (but still descriptive). I've often seen functions written using specific variables that exist for the current context it's been written for. And then when others reuse the same function (copy/paste or from an internal ML library) they get errors because that specific variable may not exist or it doesn't make sense for their context.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/Made-With-ML#42