private void SaveBestResults(MLApiAccuracyParameters parameters) { Dictionary <string, object> dict = new Dictionary <string, object>(); using (var mlapictx = new MLAPIEntities()) { try { var accparams = mlapictx.AccuracyParamters; var data = mlapictx.AccuracyParamters.FirstOrDefault(x => x.ModelId == parameters.ModelId); if (data != null) { data.Accuracy = parameters.Accuracy; data.NumberOfLayers = parameters.NumberOfLayers; data.Steps = parameters.Steps; data.LearningRate = parameters.LearningRate; mlapictx.Entry(data).State = System.Data.Entity.EntityState.Modified; mlapictx.SaveChanges(); } else { var newModel = new MLAPI.Domain.AccuracyParamter { ModelId = parameters.ModelId, Steps = parameters.Steps, NumberOfLayers = parameters.NumberOfLayers, Accuracy = parameters.Accuracy, LearningRate = parameters.LearningRate }; mlapictx.AccuracyParamters.Add(newModel); mlapictx.SaveChanges(); } } catch (Exception ex) { //Log the exception var res = string.Format($"Error occurred while saving the best results : {ex.Message}"); } } }
/* * Method to add the image path to the persistanct storage. * */ public HttpResponseMessage SaveImage(MLApiImage image) { Dictionary <string, object> dict = new Dictionary <string, object>(); if (string.IsNullOrEmpty(image.ImagePath)) { dict.Add("status", "failure"); dict.Add("error", "Image path is not added"); } else { using (var mlapictx = new MLAPIEntities()) { try { var images = mlapictx.Images; var newModel = new Image { ModelId = image.ModelId, ImagePath = image.ImagePath }; mlapictx.Images.Add(newModel); mlapictx.SaveChanges(); var responseMessage = string.Format("Image uploaded successfully."); dict.Add("status", "success"); dict.Add("message", responseMessage); return(Request.CreateResponse(HttpStatusCode.OK, dict));; } catch (Exception ex) { var res = string.Format($"Error Ocuurred while uploading the image : {ex.Message}"); dict.Clear(); dict.Add("status", "failure"); dict.Add("error", res); return(Request.CreateResponse(HttpStatusCode.InternalServerError, dict)); } } } HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.NoContent, dict); return(response); }
public HttpResponseMessage CreateModel(MLApiModel model) { Dictionary <string, object> dict = new Dictionary <string, object>(); if (string.IsNullOrEmpty(model.ModelName)) { dict.Add("error", "Model name has to be given"); } else { using (var mlapictx = new MLAPIEntities()) { try { var models = mlapictx.Models; var newModel = new Model { ModelName = model.ModelName }; Guid obj = Guid.NewGuid(); newModel.Id = obj; mlapictx.Models.Add(newModel); mlapictx.SaveChanges(); var responseMessage = string.Format("Model created successfully."); dict.Add("guid", newModel.Id); return(Request.CreateResponse(HttpStatusCode.OK, dict));; } catch (Exception ex) { var res = string.Format($"Error Ocuurred while creating the model : {ex.Message}"); dict.Clear(); dict.Add("error", res); return(Request.CreateResponse(HttpStatusCode.InternalServerError, dict)); } } } HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.NoContent, dict); return(response); }
public HttpResponseMessage GenerateExperiments(MLApiExperiment experiment) { ModelService modelservice = new ModelService(); Dictionary <string, object> dict = new Dictionary <string, object>(); if (experiment.ModelId == null || experiment.ModelId == Guid.Empty || !modelservice.IsValidModelId(experiment.ModelId ?? Guid.Empty)) { dict.Add("status", "failure"); dict.Add("error", "Valid model id is required"); HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.NoContent, dict); return(response); } else { double[] learningRates = new double[] { 0.001, 0.01, 0.1 }; double[] stepsArr = new double[] { 1000, 2000, 4000 }; double[] noOfLayers = new double[] { 1, 2, 4 }; double maxlr = 0, maxsteps = 0, maxlayers = 0, maxacc = 0; List <String> experimentErrors = new List <String>(); using (var mlapictx = new MLAPIEntities()) { for (int i = 0; i < learningRates.Length; i++) { for (int j = 0; j < stepsArr.Length; j++) { for (int k = 0; k < noOfLayers.Length; k++) { double acc = TrainModelAndReturnAccuracy(k, j, i); if (acc > maxacc) { maxlayers = noOfLayers[k]; maxlr = learningRates[i]; maxsteps = stepsArr[j]; maxacc = acc; } try { var models = mlapictx.Experiments; var newModel = new Experiment { ModelId = experiment.ModelId, Accuracy = (decimal)acc, LearningRate = (decimal?)learningRates[i], Steps = (decimal?)stepsArr[j], NumberOfLayers = (decimal?)noOfLayers[k] }; mlapictx.Experiments.Add(newModel); mlapictx.SaveChanges(); } catch (Exception ex) { var res = string.Format($"Error occurred for learning rate = ${i} , steps = ${j} , layers: ${k} , message = {ex.Message}"); experimentErrors.Add(res); //The errors while training for a specfic paramters combination should be logged in splunk } } } } } //Sending the error response only if all the 27 experiments are unsuccessful , even if some of them are successful sending //The response will be given with the best accuracy , steps , learning rate , no.of layers if (experimentErrors.Count != 27) { dict.Add("status", "success"); dict.Add("Accuracy", maxacc); dict.Add("Steps", maxsteps); dict.Add("LearningRate", maxlr); dict.Add("Layers", maxlayers); var accparam = new MLApiAccuracyParameters { ModelId = experiment.ModelId, Accuracy = (decimal?)maxacc, Steps = (decimal?)maxsteps, LearningRate = (decimal?)maxlr, NumberOfLayers = (decimal?)maxlayers }; SaveBestResults(accparam); return(Request.CreateResponse(HttpStatusCode.OK, dict)); } dict.Add("status", "failure"); dict.Add("error", experimentErrors); return(Request.CreateResponse(HttpStatusCode.InternalServerError, dict)); } }