Turning classes into inputs

How to build ML models when classes are dynamic and ridiculously large

Let’s face it, we all have worked on an ML project where we had to predict a ridiculously high number of classes. Large enough to make the number of observations per class into an embarrassingly small subset. Most people model these tasks as a multiclass classification problem where, for each input observation, we must predict the most likely class (or the class probabilities).

Examples of such tasks are predicting the model of a car, the species of an animal, the intent of a user on a chat, the SIC/NAICS code of a company, and the product on a marketplace picture, among many others.

A dynamic number of classes also characterizes these examples. For example, let’s say we are training a Computer Vision model to recognize the item on a photo for an autonomous retail store. Every day, new products are launched to the market. If you go with the traditional approach, you must train a new model daily to keep up with the catalog.

This would make the model maintenance (and operations) go wild! You don’t want that!

Our recipe for cooking large Multiclass classification models

Our trick for this kind of model is converting classes into part of the question. So, instead of training a multiclass classification model that predicts:

What’s the class of the observation? –  a categorical question

we ask the question:

Is this observation from a given category? – a yes or no question.


I like to call this trick flipping your model upside down, making the outcome part of the inputs.


Technically, we transform our predictive model 

    $$F :: observation \rightarrow class$$


    $$F :: observation x class \rightarrow yes/no$$

Then, for any given observation, you just need to ask for all classes and take the one with the highest probability.

    $$argmax_{class} F(observation, class)$$

Is there a new class? Don’t worry; just ask an additional question next time you need to generate a prediction. No re-training is required. I like to call this trick flipping your model upside down, making the outcome part of the inputs.


Disclaimer: as long as your initial class subset is general enough. Otherwise, just re-train every now and then.

How can we encode classes as inputs? Multiclass classification as Binary classification 

Let’s say you have features describing the classes. Then, you just need to encode the class as the set of features that describe it. For example, in the retail product recognition example, you can characterize the item by its category, brand, weight, size, color, description, ingredients, etc.

    $$F :: observation \times classFeatures \rightarrow yes/no$$

However, it’s not so common to have features describing the classes. How would you describe a user’s intent on a chat? How would you describe a car model or an animal?

Yes, it would be possible to do it. But, my bet is that you won’t have access to such data.

What to do in such a situation?

A Card Up Your Sleeve

You must agree that you have the features of the entities belonging to that class, right? In that case, you can just get features about the distribution of the observations in that class. Statistical values like the average, minimum, maximum, and variance of the features for the observations in that class. Now you have features describing the class. You’re welcome.

Hey Kelwin, but you know, aren’t features old-fashioned? We all work with deep learning nowadays and leave the model to learn its own features. I’m glad you asked, young grasshopper!

Do you want to further discuss this idea?

Book a meeting with Kelwin Fernandes

Meet Kelwin Learn More

You can train a siamese neural network that answers the question:

Are these two observations from the same class?


Or, in a more formal language:

    $$F :: observation_1 \times observation_2 \rightarrow yes/no$$


Now, you can ask the question comparing your new test observation against all training data points, aggregate the probabilities by class (e.g., maximum, average) and return the class with the highest score. Basically, you can just transform a multiclass problem into a similarity learning one.

Are you crazy? That won’t scale at all. Well, it will. First of all, you just need to index all training observations. So, whenever new input arrives, you just run your neural network on the input instance to get its latent features plus a simple nearest neighbor comparison against all the other data points.

Still, can you imagine doing that over millions of observations? Of course not, but you can always choose pivots that represent your class properly using any technique, such as k-medoids on the latent space. Easy peasy.

Now, you have a scalable model that adjusts to new classes without the need for re-training.

We have used this trick in several industries and use cases, which always pays for itself.


The Machine Learning Spectrum

Learn more tricks like this in our ML course.

Learn More

You gain so much operational efficiency, plus mitigating the problem of classes with low frequency.

Is a class no longer relevant? Remove its observations from your index.

Is there any new class? Add new observations to your index.

As easy as that!


There are a couple of additional tricks we can teach you, but you will need to wait for another article. I have to leave. But you don’t. So, subscribe now to our newsletter below to stay tuned.

Like this story?

Subscribe to Our Newsletter

Special offers, latest news and quality content in your inbox once per month.

Signup single post

This field is for validation purposes and should be left unchanged.

Recommended Articles

Link to Leaders Awarded NILG.AI Startup of the Month

NILG.AI is Startup of the Month Link to Leaders awarded NILG.AI the Startup of the Month (check news). Beta-i nominated us after winning two of their Open Innovation challenges: VOXPOP Urban Mobility Initiatives and Re-Source. AI with Geospatial Data At VOXPOP, NILG.AI built an AI-based mobility index for wheelchair users for the municipality of Lisbon […]

Read More
Can Machine Learning Revolutionize Your Business?

Today, the buzz around machine learning (ML) is louder than ever. But what is it exactly, and more importantly, can it revolutionize your business? In essence, ML is a technology that empowers machines to learn from data, improve over time, and make predictive decisions. It has the potential to redefine how businesses operate. In this […]

Read More
Can ‘Old but Gold’ Predictions Minimize AI Costs?

There’s a common pattern in artificial intelligence (AI) where large corporations build massive infrastructures to support their AI use cases. The goal is to make quick predictions and constantly update with new data to scale up your infrastructure. However, this approach often overlooks the trade-off between infrastructure cost and the size of the opportunity that […]

Read More