← AI Practitioner study path

AI and ML basics · 10 min

Model, algorithm, training and inference

In this lesson you will learn the difference between an algorithm and a model and between training and inference, what the ML cycle looks like in Amazon SageMaker AI and which of its four inference options fits each case.

The AIF-C01 exam guide lists "algorithm" and "model" as two different terms, even though in everyday use they are treated as the same thing, and it does the same with training and inference. Both differences apply in Amazon SageMaker AI, the AWS service for building, training and ML models, which also offers four ways to ask a model for predictions. A prediction is the answer the model gives about a piece of data it hadn't seen. For example, a bank shows the model a card purchase that just happened, and the model predicts whether it is fraud or not.

What is the difference between an algorithm and a model?

The algorithm is the procedure that looks for a relationship between the inputs and the outputs, while the model is what remains after training that algorithm with data. The inputs are the data you give it, and the outputs are the answers you expect. That is why the same algorithm with different data produces a different model. For example, two banks use the same algorithm with their own past purchases, each one marked as fraud or not fraud, and each bank ends up with a different model, because each one learned from different purchases.

The AWS page on ML explains it with numbers. You give the algorithm three input and output pairs, where 2 gives 10, 5 gives 19 and 9 gives 31, and the algorithm tries and adjusts until it finds that output = 3 × input + 4. That formula is the model, and the two numbers it learned, the 3 and the 4, are called parameters. If you then give it a 7, the model predicts 25.

This example has only two parameters, but a real model has thousands or millions, because it learns relationships far more complicated than a formula. A large language model (LLM), which is a model trained on enormous amounts of text to understand and write, reaches hundreds of billions of parameters. For example, the model behind a is an LLM.

What is the difference between training and inference?

From data to a prediction: on top, example data plus an algorithm are trained and give a model; below, a new input goes through the model and a prediction comes out

The SageMaker AI documentation sums up working with a model in two steps. First you use an algorithm and example data to train a model, and then you integrate that model into your application to generate inferences, that is, predictions on new data, in real time and at scale, that is, for many requests at once. For example, the bank trains its model with the purchases from previous years, and then the bank's app asks it for an inference every time a customer pays with their card.

The two steps happen at different times and that is why they cost different amounts. Training happens once, or each time the model is retrained, and it uses whatever computing capacity the data calls for, from a single to a cluster, which is a group of instances working together, with . Inference, on the other hand, happens every time the application asks for an answer, so its cost grows with use. For example, when you write to a chatbot you are requesting an inference from a model that was already trained before you arrived.

Training or inference?

0 of 4

Tap the correct column for each situation.

  1. A bank gives an algorithm five years of transactions marked as fraud or not fraud

  2. A card purchase comes in and the model says there is a 92% probability of fraud

  3. You ask a chatbot something and it answers you

  4. The team recalculates the parameters with last month's data

The ML cycle in Amazon SageMaker AI

ML cycle in Amazon SageMaker AI: generate example data, train the model and deploy it, in a circle that starts over with monitoring and retraining

The SageMaker AI documentation describes working with a model in three stages that form a circle:

  1. Generating example data, where you first fetch the data, then clean it (so that "United States" and "US" don't end up as two different countries) and then transform it, that is, change it into the form the model needs. In the AWS example, a model for aircraft de-icing combines temperature and humidity into a single attribute, which is one piece of input data for the model.
  2. Training the model, which includes evaluating it to see whether the accuracy of its inferences, that is, how often it gets them right, is acceptable.
  3. Deploying the model, which is kept separate from the application code, so the model can be changed without rewriting the application. For example, the bank can put in a new model without publishing another version of its app.

AWS calls this circle a continuous cycle, because after you deploy the model you monitor its inferences, that is, you watch its answers, collect new data and retrain it so it doesn't lose accuracy. For example, if fraudsters change the way they steal, the bank's model starts letting new frauds through, and that is why the bank trains it again with recent purchases.

Inference options in SageMaker AI

The four SageMaker AI inference options with their maximum payload and maximum time: real-time 25 MB and 60 s, serverless 4 MB and 60 s, asynchronous 1 GB and one hour, batch GB and days

Once the model is deployed, SageMaker AI gives you four ways to request inferences from it, and each one has its own limit on how large a request can be and how long it can take:

  • Real-time inference, which uses an always-on with the instance type you choose and is suited to low and steady traffic, that is, requests that arrive all the time. It accepts up to 25 per request and 60 seconds, or 8 minutes if the response is .
  • Serverless inference, which is suited to intermittent or unpredictable traffic, that is, requests that arrive every now and then, and where AWS takes care of the computers that make it work. With it you pay for what you use, without paying for idle time, and it accepts up to 4 MB and 60 seconds.
  • Asynchronous inference, which puts requests in a queue, that is, a waiting line, and is suited to large payloads and long-running processes, up to 1 GB and one hour. Its endpoint can scale down to zero when there are no requests.
  • Batch inference (batch transform), which processes a lot of data you already have in one go, several GB and over days, and which doesn't leave an endpoint running.

Each option solves a different case for the same business. For example, the bank uses real-time inference to check each purchase at the moment the customer pays, and uses batch inference to check all of the day's purchases every night.

Match each case with its inference option

0 of 4 pairs

Tap a case and then the option that fits it.

0 of 4 pairs.

Can you try it in your account?

SageMaker AI is in the free plan of the new experience account, which is fixed to the us-east-2 Region for Latin America, and also in a full account. In both account types you can open the at no cost and look at the service's options.

cloud path tip: don't deploy an endpoint just to try it. A real-time inference endpoint charges for every hour its instance is running, whether you use it or not, for as long as it exists. For example, if you create one for a five-minute test and forget to delete it, it keeps charging every day.

On the exam

In a question about inference options, the figures and conditions in the question decide the answer, because each option has its own limits. These words point to each option:

  • The words "milliseconds", "low latency" or "steady traffic", which point to real-time inference.
  • The words "intermittent", "unpredictable" or "not paying for idle time", which point to serverless inference.
  • The phrases "large payloads", "up to one hour", "queue" or "scale to zero", which point to asynchronous inference.
  • The phrases "the entire dataset", "every night" or "no endpoint", which point to batch inference.

For example, if a question says that a bank wants to check all of the day's purchases every night, the phrase "every night" tells you the answer is batch inference.

Review question

An insurance company receives crash photos of up to 200 MB. Each analysis takes about 10 minutes, and the customer receives the result by email. There are days with no requests at all, and the company doesn't want to pay for a running endpoint on those days. Which Amazon SageMaker AI inference option fits?

Next