private void forward(MnistImage image) { for (int i = 0; i < image.height; ++i) { for (int j = 0; j < image.width; ++j) { Net.layers[0].y[i * image.height + j] = image.pixels[i][j]; } } Net.layers[0].y[Net.layers[0].y.Length - 1] = 0.5; for (int l = 0; l < Net.l_number; ++l) { for (int i = 0; i < Net.layers[l].v.Length; ++i) { for (int j = 0; j < Net.layers[l].weight.Length; ++j) { Net.layers[l].v[i] += Net.layers[l].y[j] * Net.layers[l].weight[j][i]; } } for (int i = 0; i < Net.layers[l].v.Length; ++i) { Net.layers[l].v[i] = Net.layers[l].func(Net.layers[l].args, Net.layers[l].v[i]); } if (l != Net.l_number - 1) { Net.layers[l + 1].y = Net.layers[l].v; } } Net.Y = Net.layers[Net.l_number - 1].v; }
public static void LoadData(string pixelFile, string labelFile, out MnistImage[] Xout, out double[][] dout) { FileStream ifsPixels = new FileStream(pixelFile, FileMode.Open); FileStream ifsLabels = new FileStream(labelFile, FileMode.Open); BinaryReader brImages = new BinaryReader(ifsPixels); BinaryReader brLabels = new BinaryReader(ifsLabels); int magic1 = brImages.ReadInt32(); int numIm = brImages.ReadInt32(); int numRows = brImages.ReadInt32(); int numCols = brImages.ReadInt32(); int magic2 = brLabels.ReadInt32(); int numLabels = brLabels.ReadInt32(); int numImages = ReverseBytes(numIm); numImages = 60000; MnistImage[] result = new MnistImage[numImages]; double[][] labs = new double[numImages][]; for (int i = 0; i < labs.Length; ++i) { labs[i] = new double[10]; } double[][] pixels = new double[28][]; for (int i = 0; i < pixels.Length; ++i) { pixels[i] = new double[28]; } for (int mi = 0; mi < numImages; ++mi) { for (int i = 0; i < 28; ++i) { for (int j = 0; j < 28; ++j) { double b = (double)brImages.ReadByte(); //pixels[i][j] = b / 255.0 - 0.5; pixels[i][j] = b / 255.0; } } MnistImage mImage = new MnistImage(28, 28, pixels); result[mi] = mImage; byte lbl = brLabels.ReadByte(); // получаем маркеры и пиксели labs[mi][lbl] = 1; } // по каждому изображению ifsLabels.Close(); ifsPixels.Close(); brLabels.Close(); brImages.Close(); Xout = result; dout = labs; }
private void Load_test_Click(object sender, EventArgs e) { string pixelFile = openFileDialog4.FileName; string labelFile = openFileDialog5.FileName; if (pixelFile.Length == 0 || labelFile.Length == 0) { throw new Exception(); } else { MnistImage.LoadData(pixelFile, labelFile, out Net.T, out Net.dT); } Test.Visible = true; }
private void Load_input_Click(object sender, EventArgs e) { string pixelFile = openFileDialog1.FileName; string labelFile = openFileDialog2.FileName; if (pixelFile.Length == 0 || labelFile.Length == 0) { throw new Exception(); } else { MnistImage.LoadData(pixelFile, labelFile, out Net.X, out Net.d); } Net.Y = new double[Int32.Parse(Ylen.Text)]; Add_layers.Visible = true; Load_weights.Visible = true; Confirm.Visible = true; }
private void backward(MnistImage image) { double sum; for (int i = 0; i < Net.e.Length; ++i) { Net.layers[Net.l_number - 1].delta[i] = Net.e[i] * Net.layers[Net.l_number - 1].der(Net.layers[Net.l_number - 1].args, Net.layers[Net.l_number - 1].v[i]); } for (int l = Net.l_number - 2; l >= 0; --l) { for (int i = 0; i < Net.layers[l].v.Length; ++i) { sum = 0; for (int k = 0; k < Net.layers[l + 1].delta.Length; ++k) { sum += Net.layers[l + 1].delta[k] * Net.layers[l + 1].weight[i][k]; } Net.layers[l].delta[i] = Net.layers[l].der(Net.layers[l].args, Net.layers[l].v[i]) * sum; } } }