This repository has been archived by the owner on Oct 3, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Commands.cs
329 lines (291 loc) · 11.1 KB
/
Commands.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
using ErrorChecks;
using MNIST;
using NeuralNetworking;
using NetworkTraining;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using UserPrompts;
namespace Actions
{
public static class Commands
{
public static void Train(List<NeuralNetwork> networks, List<string> networkNames)
{
if(networks.Count == 0)
{
Console.WriteLine("There are no networks to train");
return;
}
//Get the name of the network to be trained
string nameToTrain = Prompts.NetworkNameTrainPrompt(networkNames);
if(nameToTrain == "exit") { return; }
//Get the learning rate
string learningRateString = Prompts.LearningRatePrompt();
if(learningRateString == "exit") { return; }
double learningRate = Convert.ToDouble(learningRateString);
//Find the network in the list and train it
for(int i = 0; i < networks.Count; i++)
{
if(networks[i].name == nameToTrain)
{
NeuralNetwork network = Training.TrainNetwork(networks[i], learningRate);
return;
}
}
}
public static void Test(List<NeuralNetwork> networks, List<string> networkNames)
{
if(networks.Count == 0)
{
Console.WriteLine("There are no networks to test");
return;
}
//Get the name of the network to be tested
string nameToTest = Prompts.NetworkNameTestPrompt(networkNames);
if(nameToTest == "exit") { return; }
//Find the network in the list
NeuralNetwork networkToTest = new NeuralNetwork("placeholder");
for(int i = 0; i < networks.Count; i++)
{
if(networks[i].name == nameToTest)
{
networkToTest = networks[i];
}
}
string exit = "";
Random random = new Random();
byte[] labels = MNISTFileHandler.GetLabels("/home/runner/NeuralNetwork/MNIST_Test_Database/labels");
byte[,,] images = MNISTFileHandler.GetImages("/home/runner/NeuralNetwork/MNIST_Test_Database/images");
int imageIndex = 0;
while(exit != "exit")
{
imageIndex = random.Next(0, labels.Length);
MNISTFileHandler.WriteImage(imageIndex, images, labels);
Console.Write($"{networkToTest.name} guessed that the image was of a {networkToTest.FeedForwardAndGetGuess(networkToTest, MNISTFileHandler.ImageToByteArray(images, imageIndex))}.\nEnter 'exit' to exit, or enter anything else to continue: ");
exit = Console.ReadLine();
}
}
public static void PrintError(List<NeuralNetwork> networks, List<string> networkNames)
{
if(networks.Count == 0)
{
Console.WriteLine("There are no networks to get the error of");
return;
}
//Get the name of the network to be deleted
string nameToGetErrorOf = Prompts.NetworkNameErrorPrompt(networkNames);
if(nameToGetErrorOf == "exit") { return; }
//Find the network in the list and print the error of it
NeuralNetwork networkToGetErrorOf = new NeuralNetwork("placeholder");
for(int i = 0; i < networks.Count; i++)
{
if(networks[i].name == nameToGetErrorOf)
{
networkToGetErrorOf = networks[i];
}
}
//Finds the average error of the network
byte[] labels = MNISTFileHandler.GetLabels("/home/runner/NeuralNetwork/MNIST_Test_Database/labels");
byte[,,] images = MNISTFileHandler.GetImages("/home/runner/NeuralNetwork/MNIST_Test_Database/images");
double[] errors = new double[labels.Length];
for(int i = 0; i < labels.Length; i++)
{
byte[] imageBytes = MNISTFileHandler.ImageToByteArray(images, i);
errors[i] = networkToGetErrorOf.Error(networkToGetErrorOf, imageBytes, labels[i]);
}
//Prints the average error
double averageError = 0;
for(int i = 0; i < errors.Length; i++)
{
averageError += errors[i];
}
averageError /= errors.Length;
//Finds the percent that the network guesses correctly
double correctGuesses = 0;
for(int i = 0; i < labels.Length; i++)
{
if(networkToGetErrorOf.FeedForwardAndGetGuess(networkToGetErrorOf, MNISTFileHandler.ImageToByteArray(images, i)) == labels[i])
{
correctGuesses++;
}
}
double percent = correctGuesses / Convert.ToDouble(labels.Length);
Console.WriteLine($"The percent the network guessed correctly was {percent * 100}%");
Console.WriteLine($"The error of {networkToGetErrorOf.name} is: {averageError}");
}
public static void PrintOverfittedError(NeuralNetwork networkToGetErrorOf, byte[] labels, byte[,,] images)
{
//Finds the average error of the network
int numberOfTests = labels.Length;
double error = 0;
byte[] imageBytes = MNISTFileHandler.ImageToByteArray(images, 0);
error = networkToGetErrorOf.Error(networkToGetErrorOf, imageBytes, labels[0]);
//Prints the average error
double averageError = error;
//Finds the percent that the network guesses correctly
double correctGuesses = 0;
if(networkToGetErrorOf.FeedForwardAndGetGuess(networkToGetErrorOf, MNISTFileHandler.ImageToByteArray(images, 0)) == labels[0])
{
correctGuesses = 1;
}
double percent = correctGuesses;
Console.WriteLine($"The percent the network guessed correctly was {percent * 100}%");
Console.WriteLine($"The error of {networkToGetErrorOf.name} is: {averageError}");
}
public static void Overfit(List<NeuralNetwork> networks, List<string> networkNames)
{
if(networks.Count == 0)
{
Console.WriteLine("There are no networks to overfit");
return;
}
//Get the name of the network to be overfitted
string nameToOverfit = Prompts.NetworkNameOverfitPrompt(networkNames);
if(nameToOverfit == "exit") { return; }
//Get the learning rate
string learningRateString = Prompts.LearningRatePrompt();
if(learningRateString == "exit") { return; }
double learningRate = Convert.ToDouble(learningRateString);
//Find the network in the list
NeuralNetwork network = new NeuralNetwork("placeholder");
for(int i = 0; i < networks.Count; i++)
{
if(networks[i].name == nameToOverfit)
{
network = networks[i];
}
}
//Overfits the network and shows the overfitted error
network = Training.OverfitNetwork(network, learningRate);
}
public static void Delete(List<NeuralNetwork> networks, List<string> networkNames)
{
if(networks.Count == 0)
{
Console.WriteLine("There are no networks to delete");
return;
}
//Get the name of the network to be deleted
string nameToDelete = Prompts.NetworkNameDeletePrompt(networkNames);
if(nameToDelete == "exit") { return; }
//Find the network in the list and delete it
for(int i = 0; i < networks.Count; i++)
{
if(networks[i].name == nameToDelete)
{
networks.RemoveAt(i);
networkNames.RemoveAt(i);
}
}
Console.WriteLine($"\nDeleted {nameToDelete}");
}
public static void Make(List<NeuralNetwork> networks, List<string> networkNames)
{
if(networks.Count == 20)
{
Console.WriteLine("You know what? No. You don't get anymore than 20 networks because no sane human being would ever need that many.");
return;
}
string useSaveDataString = Prompts.SaveDataPrompt();
if(useSaveDataString == "exit") { return; }
bool useSaveData = useSaveDataString == "y";
if(useSaveData)
{
//Get save data from user
Console.Write("Enter the network save data: ");
string networkJson = Console.ReadLine();
if(networkJson == "exit") { return; }
//Convert to a NeuralNetwork
NeuralNetwork network = JsonToNetwork(networkJson);
if(network == null) { return; }
//If there is already a network with the name, make the user make a new one
if(Checks.NetworkListContainsName(networkNames, network.name))
{
Console.WriteLine("There is already a network with that name");
string name = Prompts.NetworkNamePrompt(networkNames);
if(name == "exit") { return; }
network.name = name;
}
//Add the network to list of networks
networkNames.Add(network.name);
networks.Add(network);
Console.WriteLine($"{network.name} created from save data");
}
else
{
//Gets name
string name = Prompts.NetworkNamePrompt(networkNames);
if(name == "exit") { return; }
//Creates a randomized network with the name
NeuralNetwork network = new NeuralNetwork(name);
//Add the network to list of networks
networks.Add(network);
networkNames.Add(name);
Console.WriteLine($"\nCreated {network.name}");
}
}
public static void Store(List<NeuralNetwork> networks, List<string> networkNames)
{
if(networks.Count == 0)
{
Console.WriteLine("There are no networks to store");
return;
}
string nameToStore = Prompts.NetworkNameStorePrompt(networkNames);
if(nameToStore == "exit") { return; }
for(int i = 0; i < networks.Count; i++)
{
if(networks[i].name == nameToStore)
{
//Serialize
string json = networks[i].ToString();
File.WriteAllText("/home/runner/NeuralNetwork/NetworkFile.txt", json);
Console.WriteLine($"Saved {nameToStore} in /home/runner/NeuralNetwork/NetworkFile.txt");
return;
}
}
}
public static void ShowCommands()
{
Console.WriteLine("Type commands to do stuff:");
Console.WriteLine("Show list of neural networks: show");
Console.WriteLine("Train neural network: train");
Console.WriteLine("Test the network: test");
Console.WriteLine("Get the error of the network: error");
Console.WriteLine("Overfit the network: overfit");
Console.WriteLine("Store neural network: store");
Console.WriteLine("Make new neural network: make");
Console.WriteLine("Delete neural network: delete");
Console.WriteLine("Exits out of a prompt: exit");
Console.WriteLine("Clear the screen: clear");
Console.WriteLine("Show this dialogue again: help");
}
public static void ShowNetworks(List<NeuralNetwork> neuralNetworks)
{
Console.WriteLine("\nSaved Neural Networks:\n");
if(neuralNetworks.Count == 0)
{
Console.WriteLine("No saved networks here");
return;
}
for(int i = 0; i < neuralNetworks.Count; i++)
{
Console.WriteLine(neuralNetworks[i].name);
}
}
private static NeuralNetwork JsonToNetwork(string networkJson)
{
try
{
return JsonConvert.DeserializeObject<NeuralNetwork>(networkJson);
}
catch
{
Console.WriteLine("Error reading save data");
return null;
}
}
}
}