How to measure model training speed

AI Maverick
2 min readFeb 12, 2022

Have you ever thought about measuring your machine learning training time?
Is it useful to have the model training time?

Training time

It depends on your experiments, but it is better to know how long your ML model takes to train over the training dataset.

It does not matter which model you are using, you can measure all the models you like in python.

To measure the training time with python, you may use the time library. This library brings lots of useful time-oriented functions for the developers.

The first method I am going to write about is time().

import time
time.time()

According to the library documentation, this method returns the time in seconds as a float number. The epoch is January 1, 1970, 00:00:00 (UTC). You can check out the epoch by yourself:

gmtime(0)

How to use time.time()?

Imagine you have a Neural network model you want to train, it would be something as following

from sklearn.neural_network import MLPClassifier
from sklearn.datasets import make_classification
X, y = make_classification(n_samples=50)

model = MLPClassifier()
model.fit(X, y)

The main part you want to measure is the fit time, so you measure the difference before and after applying fit.

from sklearn.neural_network import MLPClassifier
from sklearn.datasets import make_classification
import time
X, y = make_classification(n_samples=50)

model = MLPClassifier()
t0 = time.time()
model.fit(X, y)
print("Training time:", time.time()-t0)

This will return a floating number, which would be a training time of the shallow neural network in seconds.

If you are looking for an integer value for your time calculation, you can use the time_ns() method.

But what if your computational resource uses workers for your applied job, such as SLURM?
In this case, you need a continuous and trustable value.

Here the library introduces the process_time()

import time
time.process_time()

This method only considers the aggregation of the user CPU time of the current process and ignores nonworking times during sleep.
Note that this method does not use the epoch to calculate the time; Therefore the only accurate experiment would be a difference between the two calls of the method.

To see some related codes, please refer here.

--

--