-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
87 lines (77 loc) · 2.99 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace MNIST_S
{
class Program
{
private static String setFileName()
{
String FileDirName = "C:\\Users\\AFOC\\Documents\\Visual Studio 2008\\Projects\\MNIST_S\\MNIST_S\\TrainData\\";
Random r = new Random();
int cuN = r.Next(10);
int cuI = r.Next(1280);
String FileName = cuN.ToString() + "-" + cuI.ToString() + ".txt";
return FileDirName + FileName;
}
private static void ShowAsPixel(double[] SingleData, StreamWriter sw)
{
for (int i = 0; i < 28; i++)
{
for (int j = i * 28; j < i * 28 + 28; j++)
{
sw.Write(SingleData[j]);
}
sw.WriteLine();
}
sw.Write(SingleData[784]);
sw.WriteLine();
}
private static void getPixelData(double[] SingleData, StreamReader reader)
{
for (int i = 0; i < 785; i++)
{
SingleData[i] = reader.Read() - '0';
reader.Read();
}
}
static void Main(string[] args)
{
StreamReader TestDataReader;
StreamWriter ResultWriter = new StreamWriter("C:\\Users\\AFOC\\Documents\\Visual Studio 2008\\Projects\\MNIST_S\\MNIST_S\\result.txt");
double[] SingleData = new double[785];
int data_amount = 100000;
MNIST_Train trainer = new MNIST_Train(ResultWriter, data_amount);
for (int i = 0; i < data_amount; i++)
{
if (i >= 50000) { trainer.flag = 1; }
ResultWriter.WriteLine("[Train Set : "+(i+1).ToString()+"]");
TestDataReader = new StreamReader(setFileName());
getPixelData(SingleData, TestDataReader);
// Console.WriteLine();
// Console.WriteLine("================================");
// ShowAsPixel(SingleData);
// Console.WriteLine("================================");
// Console.WriteLine();
System.Threading.Thread.Sleep(1);
// Initialize model
trainer.setInput(SingleData);
ShowAsPixel(SingleData, ResultWriter);
trainer.MNIST_TrainActivity();
if (i % 100 == 0)
{
Console.WriteLine("train idx : " + (i + 1));
Console.WriteLine("Loss function : " + trainer.objValue);
Console.WriteLine();
}
trainer.clearValue();
}
double accurate = (double)trainer.getAnsCnt() / data_amount * 100;
ResultWriter.WriteLine("Train end");
ResultWriter.WriteLine("Accurate : "+accurate+"%");
ResultWriter.Close();
}
}
}