-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
58 lines (53 loc) · 2.28 KB
/
Copy pathmain.py
File metadata and controls
58 lines (53 loc) · 2.28 KB
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
import numpy as np
from datetime import datetime
import NeuralNetwork as nn
import gui
start = datetime.now()
# This first bit is getting the training data to work with.
# image sizing
imageSize = 28
numberOfLabels = 10
numberOfPixels = 784
# base path
dataPath = "data/"
#
# Load training data, uses this for loop because otherwise it will crash the shell
trainingData = np.empty([60000, 785])
row = 0
for line in open(dataPath + "train.csv"):
trainingData[row] = np.fromstring(line, sep=",")
row += 1
# Load testing data
testData = np.empty([10000, 785])
row = 0
for line in open(dataPath + "test.csv"):
testData[row] = np.fromstring(line, sep=",")
row += 1
# This is used to adjust the MNIST dataset and map values into an interval between 0.01 and 1
adjust = 0.99 / 255
# Getting all the images in the correct interval
trainingImages = np.asfarray(trainingData[:, 1:]) * adjust + 0.01
testImages = np.asfarray(testData[:, 1:]) * adjust + 0.01
# Getting all the labels in the correct order
trainingLabels = np.asfarray(trainingData[:, :1])
testLabels = np.asfarray(testData[:, :1])
# Organising data in a one-hot representation. This will use 0.01s and 0.99 as this is better for calculation
lr = np.arange(numberOfLabels)
trainingLabelsOneHot = (lr == trainingLabels).astype(np.float)
testLabelsOneHot = (lr == testLabels).astype(np.float)
# making sure 0.01s and 0.99s are used
# trainingLabelsOneHot[trainingLabelsOneHot == 0] = 0.01
# trainingLabelsOneHot[trainingLabelsOneHot == 1] = 0.99
# testLabelsOneHot[testLabelsOneHot == 0] = 0.01
# testLabelsOneHot[testLabelsOneHot == 1] = 0.99
timeTaken = datetime.now() - start
print("Time taken:", timeTaken)
# Here we will implement the neural network code and use it to train.
# The second value determines whether the network will be loaded from a file. To make it do a proper training session
# change it to False
neuralNetwork = nn.NeuralNetwork([784, 24, 24, 10], True, "data/weights[1].txt")
# Loss: 0.016759857738949845 for most trained one
# To edit the learning paces change the values in the learningPace.txt
neuralNetwork.trainNetwork(trainingImages, trainingLabelsOneHot, 100, 0.1, 0.016759857738949845)
neuralNetwork.testNetwork(testImages, testLabelsOneHot, 1)
gui.start(neuralNetwork)