static void ThirdNN() { 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); for (var i = 0; i < 10; i++) { var pred = seq.Forward(data); var diff = pred.Sub(target); var loss = diff.Mul(diff).Sum(AxisZero.vertical); loss.Backward(new Tensor(Matrix.Ones(loss.Data.X, loss.Data.Y))); sgd.Step(); Console.WriteLine($"Epoch: {i} Loss: {loss}"); } }
static void SecondNN() { 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 weights = new List <Tensor> (); weights.Add(new Tensor(Matrix.Random(2, 3, r), true)); weights.Add(new Tensor(Matrix.Random(3, 1, r), true)); var sgd = new StochasticGradientDescent(weights, 0.1f); for (var i = 0; i < 10; i++) { var pred = data.MatMul(weights[0]).Add(weights[1]); var diff = pred.Sub(target); var loss = diff.Mul(diff).Sum(AxisZero.vertical); loss.Backward(new Tensor(Matrix.Ones(loss.Data.X, loss.Data.Y))); sgd.Step(); Console.WriteLine($"Epoch: {i} Loss: {loss}"); } }
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}"); } }
// 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))); }
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(); }