Machine Learning inside Containers

Abhishek Prasad Kesare
Nerd For Tech
Published in
3 min readMay 27, 2021

Many times ML engineers face issues in environmental conflicts due to the various versions of libraries.

Hello guys, If you are an ML engineer or in related fields, you have at least once faced the issue of environmental conflicts in a different version of python libraries. In Machine learning, we have various versions of libraries but some of the versions are only compatible. but this thing might happen that you know after installing it.

So to overcome this I have an idea, Can we train our model inside the container and the answer is yes

What is the container?

Containers or containerization is the technology that has sudden and extreme growth in the field of the IT sector in the last decade. Containerization is very similar to virtualization. We can launch any operating system on top of containers in CLI form. This means on top of containers we generally don’t launch GUI applications. Launching any operating system on VirtualBox or any other tool takes hours but not inside the containers. We can launch any container within a fraction of seconds.

How Run an ML application inside the container?

When we train our model save the model in a file and only transfer that file to the container. So that we can use it directly for prediction. also, you can download depending libraries so that they will work more properly

Let's start the practicle

step 1: install the docker-ce on your system

step2: After installing docker start and enable docker

systemctl start docker
systemctl enable docker

step3: You need an operating system image to launch the container to pull/download the image use below command here I am using centos image to give you demo

docker pull {osname:version}/ docker pull centos

step4: We need to launch the container.

docker run -it --name myos centos:latest
This will give you a terminal for a new centos image named salarypredictor

step5: We have to install packages and python3 inside the container

yum install python3 -y
pip3 install numpy
pip3 install pandas
pip3 install sklearn

step6: copy the trained model file inside the container

docker cp filename containername:/foldername
salarypredictor.pk1 is the model file we have to load and use now

step7: Write a python program that will load and use the model to predict the values.

For a demo purpose, I have used a simple linear regression model in the above program that will take years of experience as input and predict the salary of employees.

step8: Run the program file and see the output

That all for today see you in another article.

If you want code for the ml-model you can find it on GitHub

you can connect me on Linkedin

--

--

Abhishek Prasad Kesare
Nerd For Tech

Data science, , cloud computing, Artificial Intelligence, Cybersecurity,tech-blogger