示例#1
0
        public void ComputeOutputs(byte[] buffer, int size, int height, int width, OutputVector result)
        {
            int osize;

            int[]    oclasses;
            double[] oenergies;
            ComputeOutputs(buffer, size, height, width, out oclasses, out oenergies, out osize);
            // fill result vector
            for (int i = 0; i < osize; i++)
            {
                result[oclasses[i]] = Convert.ToSingle(oenergies[i]);
            }
        }
示例#2
0
        private void DoTestRecognize(LenetClassifier classifier)
        {
            OutputVector ov = new OutputVector();
            Floatarray   v  = new Floatarray();
            Bytearray    ba = new Bytearray(1, 1);

            ImgIo.read_image_gray(ba, testPngFileName);
            NarrayUtil.Sub(255, ba);
            v.Copy(ba);
            v /= 255.0;
            classifier.XOutputs(ov, v);
            Console.WriteLine("Featured output class '{0}', score '{1}'", (char)ov.Key(ov.BestIndex), ov.Value(ov.BestIndex));
        }
示例#3
0
        protected override float Outputs(OutputVector result, Floatarray v)
        {
            result.Clear();
            if (v.Rank() == 1)
            {
                v.Reshape(csize, csize, 0, 0);
            }
            // byte array input
            StdInput vinput = new StdInput(v);

            byte[] buffer = vinput.GetDataBuffer();

            // char classifier compute outputs
            if (CharClass.AsciiTarget)
            {
                // net output 0..~ (lower - winner)
                CharClass.ComputeOutputs(buffer, vinput.Length, vinput.Height, vinput.Width, result);
            }
            else
            {
                // net output 0..1; (higher - winner)
                CharClass.ComputeOutputsRaw(buffer, vinput.Length, vinput.Height, vinput.Width, result);
            }

            // junk classifier
            if (PGetb("junk") && !DisableJunk && !JunkClass.IsEmpty)
            {
                OutputVector jv = new OutputVector();
                if (JunkClass.AsciiTarget)
                {
                    JunkClass.ComputeOutputs(buffer, vinput.Length, vinput.Height, vinput.Width, jv);
                    result[jc()] = jv.Value(1);
                }
                else
                {
                    //result.Normalize();
                    result.Normalize2();
                    JunkClass.ComputeOutputsRaw(buffer, vinput.Length, vinput.Height, vinput.Width, jv);
                    jv.Normalize2();
                    for (int i = 0; i < result.nKeys(); i++)
                    {
                        result.Values[i] *= jv.Value(0);
                    }
                    result[jc()] = jv.Value(1);
                }
            }

            return(0.0f);
        }
示例#4
0
        public static void Main(string[] args)
        {
            /*
             * XorShift160 lol = new XorShift160();
             * double runningSum = 0;
             *
             * while (!Console.KeyAvailable)
             * {
             *  Console.WriteLine(lol.NextDouble());
             * }
             * return;
             */

            IStateMatrix A = new StateMatrix(2, 2);

            A.SetValue(0, 0, 0);
            A.SetValue(0, 1, 1);
            A.SetValue(1, 0, 0);
            A.SetValue(1, 0, 0);

            /*
             * var c = new CompiledMatrix<IStateMatrix>(A);
             * c.Compile();
             */

            IInputMatrix B = new InputMatrix(2, 1);

            B.SetValue(0, 0, 0);
            B.SetValue(1, 0, 1);

            IOutputMatrix C = new OutputMatrix(2, 2);

            C.SetValue(0, 0, 1);
            C.SetValue(0, 1, 0);
            C.SetValue(1, 0, 0);
            C.SetValue(1, 1, 1);

            IFeedthroughMatrix D = new FeedthroughMatrix(2, 1);

            D.SetValue(0, 0, 0);
            D.SetValue(1, 0, 0);

            ControlVector u = new ControlVector(1);

            u.SetValue(0, 1);
            // TODO: Construct vectors and matrices by factory and let the simulation driver compile them after every external change

            IStateVector x = new StateVector(2);

            /*
             * IStateVector dx = new StateVector(x.Length);
             * IStateVector dxu = new StateVector(x.Length);
             * IOutputVector y = new OutputVector(C.Rows);
             * IOutputVector yu = new OutputVector(C.Rows);
             */

            CancellationTokenSource cts = new CancellationTokenSource();
            CancellationToken       ct  = cts.Token;

            Semaphore startCalculation = new Semaphore(0, 2);
            Semaphore calculationDone  = new Semaphore(0, 2);

            SimulationTime simulationTime = new SimulationTime();

            IStateVector dx           = new StateVector(x.Length);
            Task         inputToState = new Task(() =>
            {
                IStateVector dxu = new StateVector(x.Length);
                while (!ct.IsCancellationRequested)
                {
                    startCalculation.WaitOne();
                    Thread.MemoryBarrier();

                    A.Transform(x, ref dx);
                    B.Transform(u, ref dxu);                                  // TODO: TransformAndAdd()
                    dx.AddInPlace(dxu);

                    Thread.MemoryBarrier();
                    calculationDone.Release();
                }
            });

            IOutputVector y             = new OutputVector(C.Rows);
            Task          stateToOutput = new Task(() =>
            {
                IOutputVector yu = new OutputVector(C.Rows);
                while (!ct.IsCancellationRequested)
                {
                    startCalculation.WaitOne();
                    Thread.MemoryBarrier();

                    C.Transform(x, ref y);
                    D.Transform(u, ref yu);                                   // TODO: TransformAndAdd()
                    y.AddInPlace(yu);

                    Thread.MemoryBarrier();
                    calculationDone.Release();
                }
            });

            Task control = new Task(() =>
            {
                Stopwatch watch = Stopwatch.StartNew();
                int steps       = 0;

                while (!ct.IsCancellationRequested)
                {
                    // wait for a new u to be applied
                    // TODO: apply control vector
                    startCalculation.Release(2);

                    // wait for y
                    calculationDone.WaitOne();
                    calculationDone.WaitOne();
                    Thread.MemoryBarrier();

                    // wait for state vector to be changeable
                    // TODO: perform real transformation
                    x.AddInPlace(dx);                             // discrete integration, T=1

                    // video killed the radio star
                    if (steps % 1000 == 0)
                    {
                        var localY    = y;
                        double thingy = steps / watch.Elapsed.TotalSeconds;
                        Trace.WriteLine(simulationTime.Time + " Position: " + localY.GetValue(0) + ", Velocity: " + localY.GetValue(1) + ", Acceleration: " + u.GetValue(0) + ", throughput: " + thingy);
                    }

                    // cancel out acceleration
                    if (steps++ == 10)
                    {
                        u.SetValue(0, 0);
                    }

                    // advance simulation time
                    simulationTime.Add(TimeSpan.FromSeconds(1));
                }
            });

            inputToState.Start();
            stateToOutput.Start();
            control.Start();

            Console.ReadKey(true);
            cts.Cancel();

            /*
             * while (!Console.KeyAvailable)
             * {
             *  A.Transform(x, ref dx);
             *  B.Transform(u, ref dxu); // TODO: TransformAndAdd()
             *  dx.AddInPlace(dxu);
             *
             *  // TODO: perform transformation
             *  x.AddInPlace(dx); // discrete integration, T=1
             *
             *  C.Transform(x, ref y);
             *  D.Transform(u, ref yu); // TODO: TransformAndAdd()
             *  y.AddInPlace(yu);
             *
             *  // video killed the radio star
             *  if (steps%1000 == 0)
             *  {
             *      Trace.WriteLine("Position: " + y[0] + ", Velocity: " + y[1] + ", Acceleration: " + u[0] + ", throughput: " + steps/watch.Elapsed.TotalSeconds);
             *  }
             *
             *  // cancel out acceleration
             *  if (steps++ == 10)
             *  {
             *      u[0] = 0;
             *  }
             * }
             */
        }
示例#5
0
        protected override float Outputs(OutputVector result, Floatarray v)
        {
            result.Clear();
            if (v.Rank() == 1)
                v.Reshape(csize, csize, 0, 0);
            // byte array input
            StdInput vinput = new StdInput(v);
            byte[] buffer = vinput.GetDataBuffer();

            // char classifier compute outputs
            if (CharClass.AsciiTarget)
                // net output 0..~ (lower - winner)
                CharClass.ComputeOutputs(buffer, vinput.Length, vinput.Height, vinput.Width, result);
            else
                // net output 0..1; (higher - winner)
                CharClass.ComputeOutputsRaw(buffer, vinput.Length, vinput.Height, vinput.Width, result);

            // junk classifier
            if (PGetb("junk") && !DisableJunk && !JunkClass.IsEmpty)
            {
                OutputVector jv = new OutputVector();
                if (JunkClass.AsciiTarget)
                {
                    JunkClass.ComputeOutputs(buffer, vinput.Length, vinput.Height, vinput.Width, jv);
                    result[jc()] = jv.Value(1);
                }
                else
                {
                    //result.Normalize();
                    result.Normalize2();
                    JunkClass.ComputeOutputsRaw(buffer, vinput.Length, vinput.Height, vinput.Width, jv);
                    jv.Normalize2();
                    for (int i = 0; i < result.nKeys(); i++)
                        result.Values[i] *= jv.Value(0);
                    result[jc()] = jv.Value(1);
                }
            }

            return 0.0f;
        }
示例#6
0
 public void ComputeOutputsRaw(byte[] buffer, int size, int height, int width, OutputVector result)
 {
     int osize;
     int[] oclasses;
     double[] oenergies;
     ComputeOutputsRaw(buffer, size, height, width, out oclasses, out oenergies, out osize);
     // fill result vector
     for (int i = 0; i < osize; i++)
         result[oclasses[i]] = Convert.ToSingle(oenergies[i]);
 }