public void EvalManagedConstantNetworkTest()
        {
            string modelDefinition = @"precision = ""float""
                traceLevel = 1
                run=NDLNetworkBuilder
                NDLNetworkBuilder=[
                v1 = Constant(1)
                v2 = Constant(2, tag=""output"")
                ol = Plus(v1, v2, tag=""output"")
                FeatureNodes = (v1)
                ]";

            using (var model = new ModelEvaluationExtendedF())
            {
                model.CreateNetwork(modelDefinition);

                VariableSchema outputSchema = model.GetOutputSchema();

                model.StartForwardEvaluation(outputSchema.Select(s => s.Name).ToList<string>());

                var outputBuffer = outputSchema.CreateBuffers<float>();
                var inputBuffer = new ValueBuffer<float>[0];

                // We can call the evaluate method and get back the results...
                model.ForwardPass(inputBuffer, outputBuffer);

                float[][] expected = { new float[] { 2 }, new float[] {3} };

                Assert.AreEqual(expected.Length, outputBuffer.Length);
                for (int idx = 0; idx < expected.Length; idx++)
                {
                    CollectionAssert.AreEqual(expected[idx], outputBuffer[idx].Buffer);
                }
            }
        }
Beispiel #2
0
        /// <summary>
        /// Evaluates an extended network (without a model and without input) and obtains a single layer output
        /// </summary>
        private static void EvaluateExtendedNetworkSingleLayerNoInput()
        {
            const string modelDefinition = @"precision = ""float""
                                     traceLevel = 1
                                     run=NDLNetworkBuilder
                                     NDLNetworkBuilder=[
                                     v1 = Constant(1)
                                     v2 = Constant(2, tag=""output"")
                                     ol = Plus(v1, v2, tag=""output"")
                                     FeatureNodes = (v1)
                                     ]";

            try
            {
                using (var model = new ModelEvaluationExtendedF())
                {
                    // Create the network
                    model.CreateNetwork(modelDefinition);

                    VariableSchema outputSchema = model.GetOutputSchema();

                    var outputNodeNames = outputSchema.Select(s => s.Name).ToList<string>();
                    model.StartForwardEvaluation(outputNodeNames);

                    var outputBuffer = outputSchema.CreateBuffers<float>();
                    var inputBuffer = new ValueBuffer<float>[0];

                    // We can call the evaluate method and get back the results...
                    model.ForwardPass(inputBuffer, outputBuffer);

                    // We expect two outputs: the v2 constant, and the ol Plus result
                    var expected = new float[][] { new float[] { 2 }, new float[] { 3 } };

                    Console.WriteLine("Expected values: {0}", string.Join(" - ", expected.Select(b => string.Join(", ", b)).ToList<string>()));
                    Console.WriteLine("Actual Values  : {0}", string.Join(" - ", outputBuffer.Select(b => string.Join(", ", b.Buffer)).ToList<string>()));
                }
            }
            catch (CNTKException ex)
            {
                OnCNTKException(ex);
            }
            catch (Exception ex)
            {
                OnGeneralException(ex);
            }
        }
Beispiel #3
0
        /// <summary>
        /// Evaluates an extended network (without a model and without input) and obtains a single layer output
        /// </summary>
        private static void EvaluateExtendedNetworkSingleLayerNoInput()
        {
            const string modelDefinition = @"precision = ""float"" 
                                     traceLevel = 1
                                     run=NDLNetworkBuilder
                                     NDLNetworkBuilder=[
                                     v1 = Constant(1)
                                     v2 = Constant(2, tag=""output"")
                                     ol = Plus(v1, v2, tag=""output"")
                                     FeatureNodes = (v1)
                                     ]";

            try
            {
                // The examples assume the executable is running from the data folder
                // We switch the current directory to the data folder (assuming the executable is in the <CNTK>/x64/Debug|Release folder
                string workingDirectory = Path.Combine(initialDirectory, @"..\..\Examples\Other\Simple2d\Config");
                Environment.CurrentDirectory = initialDirectory;

                using (var model = new ModelEvaluationExtendedF())
                {
                    // Create the network
                    // This network (AddOperatorConstantNoInput.cntk) is a simple network consisting of a single binary operator (Plus)
                    // operating over a two constants, therefore no input is necessary.
                    model.CreateNetwork(modelDefinition);

                    VariableSchema outputSchema = model.GetOutputSchema();

                    var outputNodeNames = outputSchema.Select(s => s.Name).ToList<string>();
                    model.StartForwardEvaluation(outputNodeNames);

                    var outputBuffer = outputSchema.CreateBuffers<float>();
                    var inputBuffer = new ValueBuffer<float>[0];

                    // We can call the evaluate method and get back the results...
                    model.ForwardPass(inputBuffer, outputBuffer);

                    // We expect two outputs: the v2 constant, and the ol Plus result
                    var expected = new float[][] { new float[] { 2 }, new float[] { 3 } };

                    Console.WriteLine("Expected values: {0}", string.Join(" - ", expected.Select(b => string.Join(", ", b)).ToList<string>()));
                    Console.WriteLine("Actual Values  : {0}", string.Join(" - ", outputBuffer.Select(b => string.Join(", ", b.Buffer)).ToList<string>()));
                }
            }
            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");
            }
        }
        public void EvalManagedValuesBufferTest()
        {
            int bufferSize = 2;
            int colIndicesSize = 5;
            var vb = new ValueBuffer<float>(bufferSize);
            Assert.AreEqual(bufferSize, vb.Buffer.Length);
            Assert.IsNull(vb.Indices);
            Assert.IsNull(vb.ColIndices);

            vb = new ValueBuffer<float>(bufferSize, colIndicesSize);
            Assert.AreEqual(bufferSize, vb.Buffer.Length);
            Assert.AreEqual(bufferSize, vb.Indices.Length);
            Assert.AreEqual(colIndicesSize, vb.ColIndices.Length);
        }
Beispiel #5
0
 public ArduinoListener(string portName, int baud)
 {
     LastState = new ValueBuffer()
     {
         DigitalPins = new bool[14],
         AnalogPins = new decimal[6]
     };
     CommandQueue = new Queue<ArduinoCommand>();
     ListeningThreadCancelToken = new CancellationTokenSource();
     SPort = new SerialPort(portName);
     SPort.BaudRate = baud;
     SPort.DataReceived += SPort_DataReceived;
     SPort.Open();
     ThreadPool.QueueUserWorkItem(new WaitCallback(delegate(object cToken)
     {
         CancellationToken token = (CancellationToken)cToken;
         while (!token.IsCancellationRequested)
         {
             lock (WriteMonitor)
             {
                 WaitsForACK = true;
                 SPort.Write(new byte[] { 0x42 }, 0, 1);
                 waitACKHandle.WaitOne();
                 if (!token.IsCancellationRequested)
                 {
                     WaitsForACK = false;
                     byte[] binaryCommand;
                     if (CommandQueue.Count > 0)
                     {
                         binaryCommand = new byte[3];
                         WaitsForPollData = false;
                         ArduinoCommand command = CommandQueue.Dequeue();
                         switch (command.Command)
                         {
                             case "setup":
                                 binaryCommand[0] = 0x01;
                                 break;
                             case "change_d":
                                 binaryCommand[0] = 0x02;
                                 break;
                             case "change_a":
                                 binaryCommand[0] = 0x03;
                                 break;
                             case "reset_all":
                                 binaryCommand[0] = 0x04;
                                 break;
                         }
                         binaryCommand[1] = command.PinNumber;
                         binaryCommand[2] = command.Parameter;
                     }
                     else
                     {
                         binaryCommand = new byte[] { 0x00, 0x00, 0x00 };
                         WaitsForPollData = true;
                         LastPollData = new byte[14];
                         LastPollDataPosition = 0;
                     }
                     SPort.Write(binaryCommand, 0, binaryCommand.Length);
                     waitOKHandle.WaitOne();
                     if (!token.IsCancellationRequested)
                     {
                         if (WaitsForPollData)
                         {
                             for (var i = 0; i < 8; i++) LastState.DigitalPins[i] = (LastPollData[0] & (1 << i - 1)) != 0;
                             for (var i = 0; i < 6; i++) LastState.DigitalPins[i + 8] = (LastPollData[1] & (1 << i - 1)) != 0;
                             for (var i = 0; i < 6; i++)
                             {
                                 int value = 256 * LastPollData[2 + (i * 2)] + LastPollData[3 + (i * 2)];
                                 LastState.AnalogPins[i] = value * 100m / 1023m;
                             }
                         }
                     }
                 }
             }
         }
     }), ListeningThreadCancelToken.Token);
 }