private void buttonTrain_Click(object sender, EventArgs e) { Tensor4 x = matlib.Convertor.Picture.BitmapInMatrix(pictX).ToTensor4() / 255.0; Tensor4 y = matlib.Convertor.Picture.BitmapInMatrix(pictY).ToTensor4() / 255.0; //pictureBox2.Image = matlib.Convertor.MatrixInBitmap(y.ToMatrix(0, 0) * 255); for (int i = 0; i < 200; i++) { net.TrainWithTeach(x, y, 0.002, 0, 0.0001, NeuralNet.Optimizer.Adam); } MessageBox.Show("Error: " + net.CalcErrRootMSE(new Tensor4[] { x }, new Tensor4[] { y })); }
public Tensor4 CalcOutp(Tensor4 inp) { this.input = inp; output = new Tensor4(inp.width, inp.height, inp.deep, inp.bs); for (int i = 0; i < input.elements.Length; i++) { output.elements[i] = (input.elements[i] > 0.0) ? input.elements[i] : 0.1 * input.elements[i]; } return(output); }
public int Calculation(Vector inp, double val, int layNum = 0, double thres = 0.4) { Tensor4 input = inp.ToTensor4(); for (int i = 0; i < layers.Count; i++) { if (layers[i] is Hand) { layers[i].output.elements[layers[i].output.elements.Length - 1] = val; break; } } return(CalculationBase(input, layNum, thres)[0]); }
public Tensor4 GetDeriv() { Tensor4 res = new Tensor4(input.width, input.height, input.deep, input.bs); for (int i = 0; i < input.elements.Length; i++) { if (output.elements[i] > 0.0) { res.elements[i] = 1.0; } } return(res); }
public ConvolutionalDistance(ILayer lastLayer, int neurNum) { this.lastLayer = lastLayer; int widthInp = lastLayer.output.width; int heightInp = lastLayer.output.height; int deepInp = lastLayer.output.deep; int bsInp = lastLayer.output.bs; input = new Tensor4(widthInp, heightInp, deepInp, bsInp); weights = new Tensor4(widthInp, heightInp, deepInp, neurNum); drop = new Tensor4(widthInp, heightInp, deepInp, neurNum) + 1.0; lastWeightsDelts = new Tensor4(widthInp, heightInp, deepInp, neurNum); output = new Tensor4(neurNum, 1, 1, bsInp); }
public ConvolutionalEuclidean(ILayer lastLayer, int widthFilt, int heightFilt, int neurNum, bool same) { this.lastLayer = lastLayer; int widthInp = lastLayer.output.width; int heightInp = lastLayer.output.height; int deepInp = lastLayer.output.deep; int bsInp = lastLayer.output.bs; this.same = same; input = new Tensor4(widthInp + Convert.ToInt32(same) * (widthFilt - 1), heightInp + Convert.ToInt32(same) * (heightFilt - 1), deepInp, bsInp); weights = new Tensor4(widthFilt, heightFilt, deepInp, neurNum); lastWeightsDelts = new Tensor4(widthFilt, heightFilt, deepInp, neurNum); output = new Tensor4(input.width - weights.width + 1, input.height - weights.height + 1, neurNum, bsInp); }
public void SaveModel(string path) { using (FileStream stream = new FileStream(path, FileMode.Create)) { Tensor4[] weights = new Tensor4[layers.Count]; for (int i = 0; i < weights.Length; i++) { weights[i] = layers[i].weights; } BinaryFormatter binForm = new BinaryFormatter(); binForm.Serialize(stream, architecture); binForm.Serialize(stream, weights); } }
private void Form1_Load(object sender, EventArgs e) { inp = new Tensor4(2, 1, 1, 1); net = new NeuralNet(new string[] { "input:" + inp.width + ":" + inp.height + ":" + inp.deep + ":" + inp.bs, //"conv:3:1:2", //"relu", //"direct:1", //"sin", //"kernel", "direct:1", //"sin" }); }
public Tensor3ToTensor3(ILayer lastLayer, int newWidth, int newHeight, int newDeep) { this.lastLayer = lastLayer; int width = lastLayer.output.width; int height = lastLayer.output.height; int deep = lastLayer.output.deep; int bs = lastLayer.output.bs; if (width * height * deep != newWidth * newHeight * newDeep) { throw new Exception("Размеры не совпадают"); } input = new Tensor4(width, height, deep, bs); output = new Tensor4(newWidth, newHeight, newDeep, bs); }
public void Train(Vector x, double norm = 0.01, double moment = 0.0) { Vector vect = new Vector(x); if (vect.EuclidNorm() != 1.0) { vect.Normalize(2.0); } Tensor4 inp = new Tensor4(vect.Length, 1, 1, 1); inp.elements = vect.elements; layer.CalcOutp(inp); layer.CalcGrads(0.0); layer.Train(norm, moment); }
public Polarization(ILayer lastLayer) { this.lastLayer = lastLayer; int width = lastLayer.output.width; int height = lastLayer.output.height; int deep = lastLayer.output.deep; int bs = lastLayer.output.bs; input = new Tensor4(width, height, deep, bs); if (input.height * input.deep != 1) { throw new Exception(); } output = new Tensor4(width + 1, height, deep, bs); }
public void Delt(Tensor4 <T> ideal) { Delts = ideal - Outputs; // Delts = new Tensor4<T>(delts.W, delts.H, delts.D, 1); // // for (int i = 0; i < Delts.W; i++) // for (int j = 0; j < Delts.H; j++) // for (int k = 0; k < Delts.D; k++) // for (int z = 0; z < delts.BS;z++) // { // Delts[i,j,k,0] += (delts[i,j,k,z] as dynamic); // } // // Delts /= delts.BS; }
public Tensor4 CalcDelts(ILayer lastLayer, ILayer nextLayer) { this.lastLayer = lastLayer; this.nextLayer = nextLayer; var a = nextLayer.Backward(); var b = CalcDerivs(); delts = new Tensor4(b.dhw, 1, 1, a.bs); for (int d = 0; d < b.bs; d++) { for (int i = 0; i < b.dhw; i++) { delts[d, 0, 0, i] = a[d, 0, 0, i] * b[d, 0, 0, i]; } } //delts = nextLayer.Backward() * CalcDerivs(); return(delts); }
public Tensor4 CalcOutp(Tensor4 inp) { this.input = inp; output = inp.VectorOnMatrix(weights); for (int i = 0; i < 3; i++) { output.elements[i] = matlib.Function.Func.SigmoidalFunc.HyperbolicTangent(output.elements[i]); } for (int i = 3; i < 6; i++) { output.elements[i] = Math.Sin(output.elements[i]); } output.elements[6] = Math.Sin(output.elements[6]) * Math.Log(output.elements[6]); output.elements[7] = -output.elements[7] / (1.0 + Math.Exp(Math.Sin(100 * output.elements[7]))); return(output); }
public int Run(Vector x) { Tensor4 inp = new Tensor4(x.Length, 1, 1, 1); inp.elements = x.elements; var outp = layer.CalcOutp(inp); int ind = 0; for (int i = 0; i < outp.width; i++) { if (outp[0, 0, 0, i] == 1) { ind = i; break; } } return(ind); }
public Tensor4 CalcOutp(Tensor4 inp) { this.input = inp; output = new Tensor4(inp.width + 1, inp.height, inp.deep, inp.bs); for (int d = 0; d < inp.bs; d++) { for (int x = 0; x < inp.width; x++) { output[d, 0, 0, x] = inp[d, 0, 0, x]; } } //for (int d = 0; d < inp.bs; d++) // output[d, 0, 0, output.width - 1] = 1.0; return(output); }
public double CalcErrRootMSE(Tensor4 x, Tensor4 y) { if (x.bs != y.bs) { throw new Exception(); } double err = 0; Calculation(x); for (int i = 0; i < x.bs; i++) { err += (output - y).ElementsPow(2.0).elements.Sum(); err /= Math.Sqrt((double)output.dhw); } err /= (double)x.bs; err = Math.Sqrt(err); return(err); }
public Tensor4 Backward() { Tensor4 lastDelts = new Tensor4(input.width, input.height, input.deep, input.bs); // if (input.width == 1) // output = new Tensor4(input.deep, input.height, 1, input.bs); // else output = new Tensor4(input.width, input.deep, 1, input.bs); if (input.width == 1) { for (int d = 0; d < input.bs; d++) { for (int z = 0; z < input.deep; z++) { for (int y = 0; y < input.height; y++) { lastDelts[d, z, y, 0] = delts[d, 0, y, z]; } } } } else { for (int d = 0; d < input.bs; d++) { for (int z = 0; z < input.deep; z++) { for (int x = 0; x < input.width; x++) { lastDelts[d, z, 0, x] = delts[d, 0, z, x]; } } } } /* * int ctr = 0; * for (int d = 0; d < input.bs; d++) * for (int z = 0; z < input.deep; z++) * for (int y = 0; y < input.height; y++) * for (int x = 0; x < input.width; x++) * lastDelts[d, z, y, x] = delts.elements[ctr++]; * //*/ return(lastDelts); }
public Tensor4 Backward() { Tensor4 lastDelts = new Tensor4(lastLayer.output.width, lastLayer.output.height, lastLayer.output.deep, lastLayer.output.bs); for (int d = 0; d < output.bs; d++) { for (int z = 0; z < output.deep; z++) { for (int y = 0; y < output.height; y += 2) { for (int x = 0; x < output.width; x += 2) { lastDelts[d, z, y / 2, x / 2] = delts[d, z, y + 1, x + 1]; } } } } return(lastDelts); }
public Tensor4 CalcOutp(Tensor4 inp) { this.input = inp; output = new Tensor4(inp.width, inp.height, inp.deep, inp.bs); for (int d = 0; d < inp.bs; d++) { for (int z = 0; z < inp.deep; z++) { for (int y = 0; y < inp.height; y++) { for (int x = 0; x < inp.width; x++) { output[d, z, y, x] = (input[d, z, y, x] >= thres) ? 1.0 : -1.0; } } } } return(output); }
public Tensor4 GetDeriv() { Tensor4 res = new Tensor4(input.width, input.height, input.deep, input.bs); for (int d = 0; d < input.bs; d++) { for (int z = 0; z < input.deep; z++) { for (int y = 0; y < input.height; y++) { for (int x = 0; x < input.width; x++) { res[d, z, y, x] = System.Math.Cos(input[d, z, y, x] * rad); } } } } return(res); }
public Tensor4 CalcDelts(Tensor4 y, ILayer lastLayer) { if (y.height != 1 || y.deep != 1) { throw new Exception("Чет браток не то у тебя."); } this.lastLayer = lastLayer; /* * Tensor4 Y = new Tensor4(y.width, y.height, y.deep, y.bs) + 1.0; * for (int i = 0; i < y.bs; i++ ) * for (int j = 0; j < y.width; j++) * if (y[i, 0, 0, j] == 1) { Y[i, 0, 0, j] = 0.0; break; } * //*/ delts = output - y; return(delts); }
public Tensor4 CalcOutp(Tensor4 inp) { this.input = inp; output = new Tensor4(input.width, input.height, input.deep, input.bs); for (int d = 0; d < inp.bs; d++) { for (int z = 0; z < inp.deep; z++) { for (int y = 0; y < inp.height; y++) { for (int x = 0; x < inp.width; x++) { output[d, z, y, x] = Math.Exp(-b * input[d, z, y, x] * input[d, z, y, x]); } } } } return(output); }
public Tensor4 GetDeriv() { double B = -2.0 * b; Tensor4 deriv = new Tensor4(output.width, output.height, output.deep, output.bs); for (int d = 0; d < deriv.bs; d++) { for (int z = 0; z < deriv.deep; z++) { for (int y = 0; y < deriv.height; y++) { for (int x = 0; x < deriv.width; x++) { deriv[d, z, y, x] = B * input[d, z, y, x] * output[d, z, y, x];// * Math.Exp(-b * input[d, z, y, x] * input[d, z, y, x]); } } } } return(deriv); }
public Tensor4 CalcOutp(Tensor4 inp) { this.input = inp; output = new Tensor4(output.width, output.height, output.deep, input.bs); for (int d = 0; d < output.bs; d++) { for (int z = 0; z < output.deep; z++) { for (int y = 0; y < output.height; y += 2) { for (int x = 0; x < output.width; x += 2) { output[d, z, y + 1, x + 1] = input[d, z, y / 2, x / 2]; } } } } return(output); }
public Tensor4 CalcGrads(double lambda) { grads = new Tensor4(weights.width, weights.height, weights.deep, weights.bs); int ind = 0; for (int i = 0; i < output.width; i++) { if (output[0, 0, 0, i] == 1) { ind = i; break; } } for (int i = 0; i < input.dhw; i++) { grads[0, 0, i, ind] = input.elements[i] - weights[0, 0, i, ind]; } return(grads); }
public Tensor4 CalcOutp(Tensor4 inp) { this.input = inp; int mul = output.height * output.width; for (int d = 0; d < output.bs; d++) { for (int z = 0; z < output.deep; z++) { for (int y = 0; y < output.height; y++) { for (int x = 0; x < output.width; x++) { output[d, 0, 0, z *mul + y * output.width + x] = input[d, z, y, x]; } } } } return(output); }
public Tensor4 Train(double norm, double moment) { var grads = new Tensor4(this.grads.width, this.grads.height, this.grads.deep, 1); for (int j = 0; j < grads.height; j++) { for (int k = 0; k < grads.width; k++) { for (int bs = 0; bs < this.grads.bs; bs++) { grads[0, 0, j, k] += this.grads[bs, 0, j, k]; } grads[0, 0, j, k] /= (double)this.grads.bs; } } grads += lambda * weights; lastWeightsDelts = norm * grads + lastWeightsDelts * moment; weights -= lastWeightsDelts * drop; return(null); }
public Tensor4 Backward() { Tensor4 lastDelts = new Tensor4(input.width, input.height, input.deep, input.bs); int mul = input.height * input.width; for (int d = 0; d < input.bs; d++) { for (int z = 0; z < input.deep; z++) { for (int y = 0; y < input.height; y++) { for (int x = 0; x < input.width; x++) { lastDelts[d, z, y, x] = delts[d, 0, 0, z *mul + y * input.width + x]; } } } } return(lastDelts); }
public Vector CalcErrRootMSEBase2(Tensor4 x, Tensor4 y) { if (x.bs != y.bs) { throw new Exception(); } Vector err = new Vector(x.bs); Calculation(x); for (int i = 0; i < x.bs; i++) { for (int j = 0; j < y.dhw; j++) { err[i] += Math.Pow((output.elements[i * output.dhw + j] - y.elements[i * y.dhw + j]), 2.0); } err[i] /= 2.0; } return(err); }