Skip to content

Nuget Package for Evaluation

Wolfgang Manousek edited this page Aug 17, 2016 · 4 revisions

##Overview Starting with release 1.5, CNTK provides a NuGet package containing the necessary dlls for creating Visual Studio C++ and .Net projects for programmatic evaluation of CNTK models. The package contains the Debug and Release versions of the CPU-only (no GPU required) evaluation engine.

Note: The CNTK evaluation engine dll requires a x64 platform configuration

##Setup From Visual Studio (2013 or 2015) create a new C# Console Application

Open the Configuration Manager and first add an x64 configuration based on the AnyCPU configuration, then remove the AnyCPU configuration.

Open the solution NuGet package manager, search for the CNTK Nuget Package in the Nuget.org repository and install it. (You can also visit Package page at nuget.org)

Once installed, insert the code below into the body of the Main method:

        try
        {
            // The model is a simple Times operator with a constant and a single value input (o1 = 3 * i1)
            string modelDefinition = @"precision = ""float""  
                                    traceLevel = 1 
                                    run=NDLNetworkBuilder
                                    NDLNetworkBuilder=[ 
                                    i1 = Input(1)
                                    o1 = Times(Constant(3), i1, tag=""output"") 
                                    FeatureNodes = (i1)
                                    ]";

            Dictionary<string, List<float>> outputs;

            using (var model = new IEvaluateModelManagedF())
            {
                // Create the network
                model.CreateNetwork(modelDefinition, deviceId: -1);

                // We can call the evaluate method and get back the results (single layer)...
                var inputs = new Dictionary<string, List<float>>() { { "i1", new List<float> { 1 } } };
                outputs = new Dictionary<string, List<float>>() { { "o1", new List<float>(2) { 0 } } }; ;
                model.Evaluate(inputs, outputs);
            }

            Console.WriteLine("Output layer: o1");
            foreach (var entry in outputs["o1"])
            {
                Console.WriteLine(entry);
            }
        }
        catch (CNTKException ex)
        {
            Console.WriteLine("Error: {0}\nNative CallStack: {1}\n Inner Exception: {2}", ex.Message, ex.NativeCallStack, ex.InnerException != null ? ex.InnerException.Message : "No Inner Exception");
        }
        catch (Exception ex)
        {
            Console.WriteLine("Error: {0}\nCallStack: {1}\n Inner Exception: {2}", ex.Message, ex.StackTrace, ex.InnerException != null ? ex.InnerException.Message : "No Inner Exception");
        }

        Console.ReadLine();

Resolve the missing includes:

using Microsoft.MSR.CNTK.Extensibility.Managed;

Running the project should list the 3 as the output result at the bottom.

Clone this wiki locally