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$$

into 

    $$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.

Course

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

Consent(Required)
This field is for validation purposes and should be left unchanged.

Recommended Articles

Article
AI City Intelligence

Imagine being able to make better decisions about where to live, where to establish a new business, or how to understand the changing dynamics of urban neighborhoods. Access to detailed, up-to-date information about city environments allows us to answer these questions with greater confidence, but the challenge lies in accessing and analyzing the right data. […]

Read More
Article
EcoRouteAI: Otimização de Ecopontos com Inteligência Artificial

O Plano Estratégico para os Resíduos Urbanos (PERSU) 2030 definiu metas ambiciosas para a gestão de resíduos em Portugal, com o objetivo de aumentar a reciclagem e melhorar a sustentabilidade ambiental. No entanto, os atuais índices de reciclagem e separação de resíduos ainda estão aquém do necessário, tanto a nível nacional quanto europeu, criando desafios […]

Read More
Article
NILG.AI named Most-Reviewed AI Companies in Portugal by The Manifest

The artificial intelligence space has been showcasing many amazing technologies and solutions. AI is at its peak, and many businesses are using it to help propel their products and services to the top! You can do it, too, with the help of one of the best AI Companies in Portugal: NILG.AI. We focus on your […]

Read More