Пример #1
0
    static void FourthNN()
    {
        var r = new Random();

        var data = new Tensor((Matrix) new double[, ] {
            { 0, 0 }, { 0, 1 }, { 1, 0 }, { 1, 1 }
        }, true);
        var target = new Tensor((Matrix) new double[, ] {
            { 0 }, { 1 }, { 0 }, { 1 }
        }, true);

        var seq = new Sequential();

        seq.Layers.Add(new Linear(2, 3, r));
        seq.Layers.Add(new Linear(3, 1, r));

        var sgd = new StochasticGradientDescent(seq.Parameters, 0.1f);

        var mse = new MeanSquaredError();

        for (var i = 0; i < 10; i++)
        {
            var pred = seq.Forward(data);

            var loss = mse.Forward(pred, target);

            loss.Backward(new Tensor(Matrix.Ones(loss.Data.X, loss.Data.Y)));
            sgd.Step();

            Console.WriteLine($"Epoch: {i} Loss: {loss}");
        }
    }
Пример #2
0
    // Start is called before the first frame update
    IEnumerator Start()
    {
        var r = new System.Random(2);

        var x = (Matrix) new double[1000, 1];

        Matrix.MatrixLoop((i, j) =>
        {
            x[i, 0] = i;
        }, x.X, x.Y);

        var y = (Matrix) new double[1000, 1];

        Matrix.MatrixLoop((i, j) =>
        {
            y[i, 0] = i * 12 + 15 + r.Next(10);
        }, x.X, x.Y);

        // var x = new double[,] { { 0, 0 }, { 0, 1 }, { 1, 0 }, { 1, 1 } };
        // var y = new double[,] { { 0 }, { 1 }, { 0 }, { 1 } };

        var X = new Tensor(x, true);
        var Y = new Tensor(y, true);

        var seq = new Sequential();

        seq.Layers.Add(new Linear(1, 1, r));

        var sgd = new StochasticGradientDescent(seq.Parameters, 0.001);

        var mse = new MeanSquaredError();

        for (var i = 0; i < 10000; i++)
        {
            yield return(null);

            var pred = seq.Forward(X);
            print(pred.Data.Size);
            var loss = mse.Forward(pred, Y);

            loss.Backward();
            sgd.Step();
            print($"Epoch: {i} Loss: {loss.Data[0, 0]}");
            print(Y);
            print(pred);
        }

        print(seq.Forward(new Tensor(x)));
    }
Пример #3
0
        static void Main(string[] args)
        {
            Operations K = new Operations();

            //Load array to the tensor
            NDArray x = new NDArray(3, 3);

            x.Load(2, 4, 6, 1, 3, 5, 2, 3, 5);
            x.Print("Load X Values");

            NDArray y = new NDArray(3, 1);

            y.Load(20, 15, 15);
            y.Print("Load Y Values");

            //Create two layers, one with 6 neurons and another with 1
            FullyConnected fc1 = new FullyConnected(3, 6, "relu");
            FullyConnected fc2 = new FullyConnected(6, 1, "relu");

            //Connect input by passing data from one layer to another
            fc1.Forward(x);
            fc2.Forward(fc1.Output);
            var preds = fc2.Output;

            preds.Print("Predictions");

            //Calculate the mean square error cost between the predicted and expected values
            BaseCost cost       = new MeanSquaredError();
            var      costValues = cost.Forward(preds, y);

            costValues.Print("MSE Cost");

            //Calculate the mean absolute metric value for the predicted vs expected values
            BaseMetric metric       = new MeanAbsoluteError();
            var        metricValues = metric.Calculate(preds, y);

            metricValues.Print("MAE Metric");

            Console.ReadLine();
        }
Пример #4
0
    private void Train()
    {
        var array = new int[totalInputList.Count];

        for (int i = 0; i < totalInputList.Count; i++)
        {
            array[i] = i;
        }

        System.Array.Sort <int>(array, new System.Comparison <int>(
                                    (i1, i2) => totalInputList[i2].Count.CompareTo(totalInputList[i1].Count)));

        var newArray = new int[getFirst];
        var regCount = 0;

        for (int i = 0; i < getFirst; i++)
        {
            newArray[i] = i;
            regCount   += totalOutputList[i].Count;
        }

        var MatrixX = new double[regCount, 4];
        var MatrixY = new double[regCount, 1];

        var x = 0;

        for (int i = 0; i < newArray.Length; i++)
        {
            for (int j = 0; j < totalInputList[i].Count; j++)
            {
                MatrixX[x, 0] = totalInputList[newArray[i]][j][0, 0];
                MatrixX[x, 1] = totalInputList[newArray[i]][j][0, 1];
                MatrixX[x, 2] = totalInputList[newArray[i]][j][0, 2];
                MatrixX[x, 3] = totalInputList[newArray[i]][j][0, 3];

                MatrixY[x, 0] = totalOutputList[newArray[i]][j][0, 0];

                x++;
            }
        }

        var X = new Tensor(MatrixX, true);
        var Y = new Tensor(MatrixY, true);

        for (var i = 0; i < fittingEpoch; i++)
        {
            var pred = seq.Forward(X);
            var loss = mse.Forward(pred, Y);
            loss.Backward();
            sgd.Step();

            if (double.IsNaN(loss.Data[0, 0]))
            {
                Debug.LogError("LOSS IS NAN");
                Debug.Break();
            }

            print("Epoch: " + i + " Loss: " + loss.Data[0, 0]);
        }

        totalInputList.Clear();
        totalOutputList.Clear();
    }