In the last blog post, we saw how to save model state (checkpoint) in order to load it and train again. Also, we have seen how to save model for the evaluation and testing. In fact, we have seen how to prepare the model to be production ready.
Once we finish with the modeling process, we enter into production phase, to install the model on place where we can use it for solving real world problems. This blog post is going to describe the process how deployed CNTK model can be exported to Excel like an AddIn and be used like ordinary Excel formula.
Preparing and Deploying CNTK Model
From the previous posts, we saw how to train and save the model. This will be our starting point for this post.
Assume we trained and saved the model for evaluation from the previous blog post with file name as “IrisModel.model
”. The model calculates Iris flower based on 4 input parameters, as we saw earlier.
- The first step is to create a .NET Class Library and install the following Nugget packages:
- CNTK CPU Only ver. 2.3
- Excel Dna Addin
- Include saved IrisModel.model file in the project as Content and should be copied in Debug folder of the application.
As we can see, for this export, we need Excel Dna Addin, a fantastic library for making anything as Excel Addin. It can be installed as Nuget package, and more information can be found here.
The following picture shows the above 3 actions:
Once we prepare everything, we can start with the implementation of the Excel Addin.
- Change the Class.cs name into IrisModel.cs, and implement two methods:
public static string IrisEval(object arg)
and private static float EvaluateModel(float[] vector)
The first method is direct Excel function which will be called in the Excel, and the second method is similar to the previous blog post for model evaluation. The following code snippet shows the implementation for the methods:
[ExcelFunction(Description = "IrisEval - Prediction for the Iris flower based on 4 input values.")]
public static string IrisEval(object arg)
{
try
{
object[,] obj = (object[,])arg;
List<float> calculatedOutput = new List<float>();
foreach (var s in obj)
{
var ss = float.Parse(s.ToString(), CultureInfo.InvariantCulture);
calculatedOutput.Add(ss);
}
if (calculatedOutput.Count != 4)
throw new Exception("Incorrect number of input variables. It must be 4!");
return EvaluateModel(calculatedOutput.ToArray());
}
catch (Exception ex)
{
return ex.Message;
}
}
private static string EvaluateModel(float[] vector)
{
var ffnn_model = Function.Load(@"IrisModel.model", DeviceDescriptor.CPUDevice);
Variable feature = ffnn_model.Arguments[0];
Variable label = ffnn_model.Output;
Value xValues = Value.CreateBatch<float>(new int[] { feature.Shape[0] },
vector, DeviceDescriptor.CPUDevice);
var inputDataMap = new Dictionary<Variable, Value>();
inputDataMap.Add(feature, xValues);
var outputDataMap = new Dictionary<Variable, Value>();
outputDataMap.Add(label, null);
ffnn_model.Evaluate(inputDataMap, outputDataMap, DeviceDescriptor.CPUDevice);
var outputData = outputDataMap[label].GetDenseData<float>(label);
var actualLabels = outputData.Select(l => l.IndexOf(l.Max())).ToList();
var flower = actualLabels.FirstOrDefault();
var strFlower = flower == 0 ? "setosa" : flower == 1 ? "versicolor" : "virginica";
return strFlower;
}
That is all we need for model evaluation in Excel.
Notice that the project must be build with the x64 architecture, and also installed Excel must be in x64 version. This demo will not work in Excel 32 bits.
Rebuild the project and open Excel file with Iris Data set. You can use the file included in the demo project, especially prepared for this blog post.
- Got to Excel – > Options -> Addins,
- Install the ExportCNTKToExcel-AddIn64-packed addin file.
- Start typing the Excel formula:
IrisEval(A1:D1)
and press Enter. And the magic happens.
The complete source code for this blog post can be found here.
Filed under: .NET, C#, CNTK, CodeProject
Tagged: C#, CNTK, Code Project, CodeProject, Excel, Machine Learning, ML