Train machine learning models with multiple CPU cores

Contents

Overview

  • Introduction
  • reduce execution time
  • data set
  • read data set
  • management of categorical variables
  • dependent and independent characteristic
  • training models with CPU cores
  • Final note

Introduction

“The 65.536 processors were inside the connection machine”

~ Philip Emeagwali

We all have a background in computer science, we use a computer on a daily basis and have a better understanding of what the computer is. Computers have hearts like humans, it's called CPU.

We all know the CPU, If that is not the case, the UPC is the central processing unit in the computer, or is it an electronic circuit that executes the various instructions that comprise a computer program. The CPU performs basic arithmetic, logic, etc.

80576sep_cpu-8016078

Previous generations of CPUs were implemented as discrete components and numerous small ICs on one or more circuit boards. Over time, CPUs are changed, Are updated. They are implemented in an integrated circuit, with one or more CPUs on a single IC chip. There are many functions or operations that the computer performs, so it takes a long time, for that the scientist designed the multicore processors, it was a combination of microprocessor chips with multiple CPUs.

Multi-core processors are very fast, can work in a while. As a data scientist, we found that some of the python libraries are very slow and also long, slow down program execution, it takes a lot of time to run our machine learning models or deep learning. If you want to see how many cores are in your computer, just open your Panel Control and search system you will see all the information about your computer.

43098hourglass-800x500-9272954

If we talk about the python panda library that is used in machine learning for data manipulation and data analysis, if we analyze a small amount of data it will not take so long to perform the operations, but what if our data set is large? without knowing it, it will take a long time to perform calculations on a large amount of data. Then, This is a big problem, that all data scientists face in their careers. What if we reduce this time? it is beneficial for us?

we will discuss below:

Reduce execution time

Above we have a short discussion about the CPU, then, what does it mean? It means that we use cores from the central processing unit to train our machine learning model. There is one of the parameters within the machine learning algorithm that we normally use, but we have no knowledge about it or not. I know the exact meaning of that, This sounds amazing!

These CPU cores are very important parts of the training of any machine learning model., this is also important when working with deep learning, because these CPU cores will be possible parallel programming or parallel execution of the program. We cannot train machine learning models with the help of GPUs, Thus, CPUs are more useful in this condition.

For faster machine learning training on any machine learning project, you can use these CPU cores as long as you have a large amount of data in the dataset to train the machine learning model.

Now, we see how we train the machine learning model using CPU cores to improve performance over the machine learning model:

Data set

To train this particular problem statement, we have to take the Wine_Quality data set, the reference link for this dataset is here.

To better understand this data set, you can check this link: Click here

Remember if you have to solve this particular problem with CPU cores, so you must have a simple dataset with a large data size.

The reason behind using this dataset is that it is a simple dataset with a large data size present within this dataset..

Let's see what is the size of this data:

Read data set

First, we have to read the wine quality dataset using pandas.

#importing pandas
df = pd.read_csv('wine_quality.csv')
df.head()
87990screenshot202021-04-2920143059-6293808
df.shape()
43508screenshot202021-04-2920143757-7453715

After running this code, can you see we have 6497 rows Y 13 columns in the wine quality data set.

Now, we verify the unique categories that are present within a quality variable,

unique_points= df['quality'].unique()
unique_points
52301screenshot202021-04-2920144318-7342742

As we see, there is 6, 5, 7, 8, 4, 3, 9 unique data points in the quality variable, where these data points are the wine quality measures.

Handel's categorical variables

# catogerical vars 
next_df = pd.get_dummies(new_df,drop_first=True)
# display new dataframe
next_df
81024screenshot202021-04-2520143427-9124542

Dependent and independent characteristic

To apply the machine learning model, we have to divide the dependent and independent characteristics:

# independent features
x= next_df.drop(['quality','best quality'],axis=1)
# dependent feature
y= next_df['best quality']

Model training with CPU cores

Coming to execution now, we are doing it by applying some steps:

Paso 1: Using the RandomForestClassifier machine learning algorithm.

Paso 2: Using RepeatedStratifiedKFold for cross validation.

Paso 3: Train the model using the cross-validation score.

When we initialize all these things, time will be calculated based on this cross-validation score. Before checking the time we import the required modules:

Module import:

from time import time
# importing RepeatedStratifiedKFold
from sklearn.model_selection import RepeatedStratifiedKFold
from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import cross_val_score
# importing RandomForestClassifier

Now, we check the time using the different CPU cores:

Here we use the RandomForestClassifie machine learning algorithm, when you check the parameters of RandomForestClassifier, you find that there is a n_jobs parameter.

96476screenshot202021-04-2920155143-2952558

n_jobs it is the parameter that will really help you to basically assign how many cores a particular system workout should take.

For instance, if we want to take n_jobs = 1 then will take 1 CPU core, yes take 2, it will take 2 CPU cores, and so on. When you want to use all CPU cores for training and you don't know how many cores are in the system, just use n_jobs = -1.

1 CPU Cores:

## CPU cores we use n_jobs
random = RandomForestClassifier(n_estimators = 100)
# creating object of RepeatedStratifiedKFold
cv = RepeatedStratifiedKFold(n_splits=5, n_repeats=3, random_state=4)
# starting execution time
start_time=time()
n_scores =cross_val_score(random,x,Y,scoring='accuracy', cv=cv, n_jobs=1)
# ending time of execution
end_time=time()
final_time = end_time-start_time
# display execution time
print('final execution time is : {}.format(final_time))
33266screenshot202021-04-2920152121-9489604

Here we see that with 1 core are needed around 10 seconds to run, and this is a huge time.

We do not write the n_jobs inside the RandomForestClassifier instead we write it in the cross_val_score because it helps us to do cross validation using RepeatedStratifiedKFold

Now, we use this same code with a little change for more cores:

2 CPU cores:

## CPU cores we use n_jobs
random = RandomForestClassifier(n_estimators = 100)
# creating object of RepeatedStratifiedKFold
cv = RepeatedStratifiedKFold(n_splits=5, n_repeats=3, random_state=4)
# starting execution time
start_time=time()
n_scores =cross_val_score(random,x,Y,scoring='accuracy', cv=cv, n_jobs=2) # 2 cores
# ending time of execution
end_time=time()
final_time = end_time-start_time
# display execution time
print('final execution time is : {}.format(final_time))
65558screenshot202021-04-2920160956-9156826

Yes, here you can see that what is the difference between 1 core and 2 cores, execution time is very different from 1 core.

3 CPU cores:

## CPU cores we use n_jobs
random = RandomForestClassifier(n_estimators = 100)
# creating object of RepeatedStratifiedKFold
cv = RepeatedStratifiedKFold(n_splits=5, n_repeats=3, random_state=4)
# starting execution time
start_time=time()
n_scores =cross_val_score(random,x,Y,scoring='accuracy', cv=cv, n_jobs=3)   # 3 cores
# ending time of execution
end_time=time()
final_time = end_time-start_time
# display execution time
print('final execution time is : {}.format(final_time))
31028screenshot202021-04-2920161326-7219513

Take all the cores:

## CPU cores we use n_jobs
random = RandomForestClassifier(n_estimators = 100)
# creating object of RepeatedStratifiedKFold
cv = RepeatedStratifiedKFold(n_splits=5, n_repeats=3, random_state=4)
# starting execution time
start_time=time()
n_scores =cross_val_score(random,x,Y,scoring='accuracy', cv=cv, n_jobs= -1) # all cores
# ending time of execution
end_time=time()
final_time = end_time-start_time
# display execution time
print('final execution time is : {}.format(final_time))
48775screenshot202021-04-2920161839-5422602

Core timing comparison:

## CPU cores we use n_jobs
for core in [1,2,3,4,5,6,7,8,9,10]:
    random = RandomForestClassifier(n_estimators = 100)
    # creating object of RepeatedStratifiedKFold
    cv = RepeatedStratifiedKFold(n_splits=5, n_repeats=3, random_state=4)
    # starting execution time
    start_time=time()
    n_scores =cross_val_score(random,x,Y,scoring='accuracy', cv=cv, n_jobs = core)
    # ending time of execution
    end_time=time()
    final_time = end_time-start_time
    # display execution time
    print('final execution time of core {} is : {}.format(core,final_time))
21077screenshot202021-04-2920162514-7769768

You can see that there is a big difference when we use 1 core to train our ML model and 10 cores for training the ML model.

Final notes

Hello there, in this article you learned the training of ML models using CPU cores, now is the time to implement this technique in your machine learning model to reduce execution time.

Hope you enjoy this article., Share it with your friends.

You can connect with me on LinkedIn: www.linkedin.com/in/mayur-badole-189221199

Check out my other articles: https://www.analyticsvidhya.com/blog/author/mayurbadole2407/

Thanks.

The media shown in this article is not the property of DataPeaker and is used at the author's discretion.

Subscribe to our Newsletter

We will not send you SPAM mail. We hate it as much as you.

Datapeaker