예제 #1
0
        public unsafe static void XORExample()
        {
            //Hyperparameters
            Hyperparameters.LearningRate = 0.1f;
            Hyperparameters.Optimizer    = new SGD();

            //Model Creation
            var l1 = LayerBuilder.Dense(16, "sigmoid");
            var l2 = LayerBuilder.Dense(1, "sigmoid")[l1];


            var   x     = new Input(2);
            Layer model = l2[x];

            //Loss Function Creation
            var y    = new Input(1);
            var loss = LayerBuilder.SquaredError(model, y);


            //Data preparation
            Tensor x_train = new Tensor((1, 4, 2), DeviceConfig.Host_Float);
            Tensor y_train = new Tensor((1, 4, 1), DeviceConfig.Host_Float);

            float *xt = (float *)x_train.Array;
            float *yt = (float *)y_train.Array;

            // 1,1 = 0
            // 1,0 = 1
            // 0,1 = 1
            // 0,0 = 0

            xt[0] = 1; xt[1] = 1;
            xt[2] = 1; xt[3] = 0;
            xt[4] = 0; xt[5] = 1;
            xt[6] = 0; xt[7] = 0;

            yt[0] = 0;
            yt[1] = 1;
            yt[2] = 1;
            yt[3] = 0;

            //Give data to the model
            x.SetInput(x_train);
            y.SetInput(y_train);

            Stopwatch s = new Stopwatch();

            s.Start();
            //Minimizing
            loss.PreCheck();
            Index a = new Index(model.OuterShape);

            a.SetZero();

            for (int epoch = 0; epoch < 100000; epoch++)
            {
                loss.Minimize();
                if (epoch % 5000 == 0)
                {
                    float res = ((float *)loss.GetTerm(a).GetResult().Array)[0];
                    res += ((float *)loss.GetTerm(a).GetResult().Array)[1];
                    res += ((float *)loss.GetTerm(a).GetResult().Array)[2];
                    res += ((float *)loss.GetTerm(a).GetResult().Array)[3];
                    Console.WriteLine(res);
                }
            }
            s.Stop();
            Console.WriteLine("Time Elapsed: " + s.ElapsedMilliseconds);

            //Print Pools
            PrintPools();

            //Print the results

            var result = model.GetTerm(a).GetResult();

            Console.WriteLine("Results: " + result);


            //Print the results of clone model
            Input x2 = new Input(2);

            x2.SetInput(x_train);
            var clonemodel = l2[x2];

            clonemodel.PreCheck();
            var result2 = clonemodel.GetTerm(a).GetResult();

            Console.WriteLine("Results: " + result2);

            clonemodel.DeleteTerms();
            model.DeleteTerms();
        }
예제 #2
0
        public unsafe static void MNISTExample()
        {
            //Hyperparameters
            Hyperparameters.LearningRate = 0.001f;
            Hyperparameters.Optimizer    = new SGD();


            //Model Creation
            var x = new Input(784);
            //var dropout = new Dropout(x, 0.1f);
            //var model = LayerBuilder.Dense(500, x, "relu");
            var model = LayerBuilder.Dense(100, x, "relu");

            model = LayerBuilder.Dense(400, model, "relu");
            model = LayerBuilder.Dense(200, model, "relu");
            model = LayerBuilder.Dense(100, model, "relu");
            model = LayerBuilder.Dense(10, model, "softmax");


            //Loss Function Creation
            var y    = new Input(10);
            var loss = LayerBuilder.SquaredError(model, y);


            //Data preparation
            (float[,] traindata, float[,] labels) = LoadMNISTDataSet();
            int mnistsize = 42000;

            Tensor x_train = Tensor.LoadArrayToDisposedTensor(traindata, new Shape(mnistsize, 784), DeviceConfig.Host_Float);
            Tensor y_train = Tensor.LoadArrayToDisposedTensor(labels, new Shape(mnistsize, 10), DeviceConfig.Host_Float);

            //Training
            int batchsize = 100;
            int trainl    = 41000;

            Stopwatch s = new Stopwatch();

            for (int epoch = 0; epoch < 35; epoch++)
            {
                float l   = 0;
                float val = 0;

                s.Restart();
                Console.WriteLine("Epoch " + epoch + " başladı.");
                for (int batch = 0; batch < trainl / batchsize; batch++)
                {
                    Tensor batchx = Tensor.Cut(x_train, batch * (batchsize * 784), new Shape(1, batchsize, 784));
                    Tensor batchy = Tensor.Cut(y_train, batch * (batchsize * 10), new Shape(1, batchsize, 10));

                    x.SetInput(batchx);
                    y.SetInput(batchy);

                    loss.Minimize();

                    Index zero = new Index(loss.OuterShape);
                    zero.SetZero();

                    Tensor res = loss.GetTerm(zero).GetResult();
                    float *pp  = (float *)res.Array;

                    for (int i = 0; i < res.Shape.TotalSize; i++)
                    {
                        l += pp[i];
                    }
                }

                for (int batch = trainl / batchsize; batch < mnistsize / batchsize; batch++)
                {
                    Tensor batchx = Tensor.Cut(x_train, batch * (batchsize * 784), new Shape(1, batchsize, 784));
                    Tensor batchy = Tensor.Cut(y_train, batch * (batchsize * 10), new Shape(1, batchsize, 10));

                    model.DeleteTerms();

                    x.SetInput(batchx);
                    y.SetInput(batchy);

                    Index zero = new Index(model.OuterShape);
                    zero.SetZero();
                    model.PreCheck();
                    Tensor res = model.GetTerm(zero).GetResult();

                    for (int i = 0; i < batchsize; i++)
                    {
                        int myans      = MaxId((float *)res.Array + i * 10);
                        int correctres = MaxId((float *)batchy.Array + i * 10);
                        val += (myans == correctres ? 1 : 0);
                    }
                }
                s.Stop();

                Console.WriteLine("Epoch " + epoch + " biti.");
                Console.WriteLine("Loss: " + l / trainl);
                Console.WriteLine("Validation: " + val / (mnistsize - trainl));
                Console.WriteLine("Time: " + s.ElapsedMilliseconds + "ms");
            }

            PrintPools();

            while (true)
            {
                try
                {
                    float[] data   = LoadCurrentImage();
                    Tensor  x_test = Tensor.LoadArrayToDisposedTensor(data, new Shape(1, 1, 784), DeviceConfig.Host_Float);

                    model.DeleteTerms();

                    x.SetInput(x_test);

                    Index zero = new Index(model.OuterShape);
                    zero.SetZero();
                    model.PreCheck();
                    Tensor res = model.GetTerm(zero).GetResult();

                    Console.WriteLine("Result: " + res);
                    Console.WriteLine("Digit Prediction: " + MaxId((float *)res.Array));
                    Console.WriteLine("-----------");
                }
                catch (Exception)
                {
                }
                Thread.Sleep(500);
            }
        }