ML+devops
Automated Hyper-Parameter Tuning:
Have you done hyperparameter tuning in the ML model i.e. have you tried to change parameters again and again to improve the accuracy of your ML model. Doing it manually again-and-again running the script may lead to tiredness and mistakes. What will happen if you can choose all the parameters initially in a smooth and well-defined way and let the machine do everything (like changing the values, training the model, saving it in some cases and also maintaining a dataset of accuracy for all the dataset you gave)
Wouldn't that be fun, if you let the machine do all this training stuff while you rest? Because that’s what this project here does for you.
You need just to push your code and at GitHub, rest of the things will be taken care by Jenkins
Technologies used: Jenkins, docker
Programming language: python
The above things can be integrated using 4 jobs in jenkins.
Job1:
This job pulls the code from github and copies in the /root/mlops folder which is used in the container.
the job will check every minute if any new commits are made in the ml code using the poll scm build triggers.
Job2:
This job is to start a container where the ml code will run.The container is build from a dockerfile.The job will watch for job1 for building.This enables pipelining the jobs.
Job3:-
This job is one which runs the python file in the container using docker exec command which is used to execute command inside the container without opening the container.This job watches job 2 as to run the command the container must be running.
Job4:
The ml code when runs save the accuracy in the output.txt file and the job takes that data to check if the accuracy is more than 95%,if not it will trigger the job3 which will run the ml code again with the latest output.
The change in hyper parameters is done inside the ml code using the output.txt file which is updated everytime the code runs thereby changing the parameters as the code executes from time to time.
- exit 1 is used in the code so that when accuracy is greater than 95 it will mark the build as failed and the job loop will stop.We can use email notification plugin to notify the developer that the required accuracy is achieved.
- Now comes hidden part of the code which you can implement in any machine learning code.
- The part is to change the parameters as you build the model again and again.
out=open('data.txt','r+')
n=out.read().split('\n')
opt=int(n[0])
epoch=int(n[1])
layers=int(n[2])
act=int(n[3])
out.close()
if opt%3 == 1: //using 3 as no. of options are three
epoch=epoch+10
elif opt%3 == 2:
layers=layers+1
else:
act=act+1
act=act%3
.
. //the machine learning code where you can use the variables
. //given above as parametrs for model building
. //the data is taken from a file data.txt which we will update after every
. // build and use it for next build until best accuracy is achieved
out=open('data.txt','w') //saving the data again in data.txt
opt=opt+1 //so that next time it uses changed data
out.write(str(opt)+'\n')
out.write(str(epoch)+'\n')
out.write(str(layers)+'\n')
out.write(str(act))
- Here is the data.txt file which the ml code takes input from as parameters and save the new parameters.
8 30 5
2
- Now you must be thinking what is this data, the first line will tell the code which option should be changed in this build check the above code to see how
- you can change the no. of options but you have to also make some changes in ml code like opt%4 will be used if you have 4 options
- the first option(second line) is the no. of epochs,second option is the no. of layers which you can use a loop variable for building architecture of model , third option is the activation function which can be used as a position of a list which contains the different activation function we can use.
This is how you can change the parameters of a code during build again and again taking all kinds of permutations and finding the best accuracy.The above code is not limited to any one to any specific kind of ml code ,you can use it anywhere where you need to change the parameters.