1
+ from tqdm import tqdm
1
2
import numpy as np
2
3
import json
3
- import os
4
- import mnist
5
4
import sys
6
- from tqdm import tqdm
5
+ import os
7
6
8
7
import activations
8
+ import mnist
9
9
import utils
10
10
11
11
12
- def feed_forward (X_input , weights , activation_fn ) :
12
+ def feed_forward (X_input : np . ndarray , weights : list , activation_fn : list ) -> np . ndarray :
13
13
"""Feed fordward the network
14
14
15
15
X_input => input layer
@@ -34,7 +34,7 @@ def feed_forward(X_input, weights, activation_fn):
34
34
return x
35
35
36
36
37
- def grads (x , y_expected , weights , activations_fn , activations_prime ) :
37
+ def grads (x : np . ndarray , y_expected : np . ndarray , weights : list , activations_fn : list , activations_prime : list ) -> np . ndarray :
38
38
"""Calculate errors corrections with backward propagation
39
39
40
40
x => input layer
@@ -54,7 +54,7 @@ def grads(x, y_expected, weights, activations_fn, activations_prime):
54
54
delta = y [- 1 ] - y_expected
55
55
56
56
# Calculate error of output weights layer
57
- grads = np .empty_like (weights )
57
+ grads : np . ndarray = np .empty_like (weights )
58
58
grads [- 1 ] = y [- 2 ].T .dot (delta )
59
59
60
60
# Backward loop
@@ -69,64 +69,91 @@ def grads(x, y_expected, weights, activations_fn, activations_prime):
69
69
return grads / len (x )
70
70
71
71
72
- def train (weights , trX , trY , teX , teY , filename , epochs , batch , learning_rate , save_timeout , reduce_output , activations_fn , activations_prime ) :
72
+ def train (weights : list , trX : np . ndarray , trY : np . ndarray , teX : np . ndarray , teY : np . ndarray , activations_fn : list , activations_prime : list , filename : np . ndarray , epochs : int , batch : int , learning_rate : float , save_timeout : int , reduce_output : int ) -> dict :
73
73
path = os .path .dirname (__file__ )
74
- accuracy = {}
75
- prediction = np .argmax (feed_forward (
74
+ accuracy = []
75
+
76
+ # Make prediction with the untrained network
77
+ prediction : np .ndarray = np .argmax (feed_forward (
76
78
teX , weights , activations_fn )[- 1 ], axis = 1 )
77
- accuracy [0 ] = np .mean (prediction == np .argmax (teY , axis = 1 ))
78
- if reduce_output < 2 :
79
+ accuracy .append (np .mean (prediction == np .argmax (teY , axis = 1 )))
80
+
81
+ if reduce_output <= 1 :
79
82
print ('Accuracy of epoch 0 :' , accuracy [0 ])
80
- if reduce_output == 2 :
83
+ elif reduce_output == 2 :
81
84
print (0 , accuracy [0 ])
85
+
82
86
if epochs < 0 :
83
87
epochs = 99999999999
88
+
89
+ # Epochs loop
84
90
for i in range (epochs ):
85
91
if reduce_output < 1 :
92
+
86
93
pbar = tqdm (range (0 , len (trX ), batch ))
87
94
else :
88
95
pbar = range (0 , len (trX ), batch )
96
+
97
+ # Batches loop
89
98
for j in pbar :
90
99
if reduce_output < 1 :
91
100
pbar .set_description ("Processing epoch %s" % (i + 1 ))
92
101
102
+ # Select training data
93
103
X , Y = trX [j :j + batch ], trY [j :j + batch ]
104
+
105
+ # Correct the network
94
106
weights -= learning_rate * \
95
107
grads (X , Y , weights , activations_fn , activations_prime )
108
+
109
+ # Make prediction for epoch
96
110
prediction = np .argmax (feed_forward (
97
111
teX , weights , activations_fn )[- 1 ], axis = 1 )
98
- accuracy [i + 1 ] = np .mean (prediction == np .argmax (teY , axis = 1 ))
112
+ accuracy .append (np .mean (prediction == np .argmax (teY , axis = 1 )))
113
+
99
114
if reduce_output < 2 :
100
115
print ('Accuracy of epoch' , i + 1 , ':' , accuracy [i + 1 ])
101
116
if reduce_output == 2 :
102
117
print (i + 1 , accuracy [i + 1 ])
118
+
119
+ # Save temp file if set so
103
120
if filename :
104
121
if save_timeout > 0 :
105
122
if i % save_timeout == 0 :
106
123
temp_filename = '../trains/temp/' + \
107
124
filename + '_epoch_' + str (i ) + '.npy'
108
125
temp_filename = os .path .join (path , temp_filename )
109
126
utils .save (weights , temp_filename , reduce_output )
127
+
128
+ # Save final file
110
129
if filename :
111
130
filename = os .path .join (path , '../trains/' + filename + '.npy' )
112
131
utils .save (weights , filename , reduce_output )
132
+
113
133
return accuracy
114
134
115
135
116
- def runTrain (params , architecture , file = None ):
117
- params = json .loads (params )
118
- epochs = params ['epochs' ]
119
- batch = params ['batch' ]
120
- learning_rate = params ['learning_rate' ]
121
- save_timeout = params ['save_timeout' ]
122
- reduce_output = params ['reduce_output' ]
136
+ def runTrain (params : dict , architecture : list , file = None ) -> dict :
137
+ params : dict = json .loads (params )
138
+ epochs : int = params ['epochs' ]
139
+ batch : int = params ['batch' ]
140
+ learning_rate : float = params ['learning_rate' ]
141
+ save_timeout : int = params ['save_timeout' ]
142
+ reduce_output : int = params ['reduce_output' ]
123
143
activations_arch , primes_arch = activations .listToActivations (
124
144
params ['activations' ], architecture )
125
145
146
+ # Print network visualization
126
147
if reduce_output < 1 :
127
148
utils .print_network_visualization (
128
149
architecture , activations_arch , epochs , batch , learning_rate )
129
150
151
+ # Load data
152
+ # TODO do not load arbitrary data
130
153
trX , trY , teX , teY = mnist .load_data ()
154
+
155
+ # Init weights
131
156
weights = [np .random .randn (* w ) * 0.1 for w in architecture ]
132
- return train (weights , trX , trY , teX , teY , file , epochs , batch , learning_rate , save_timeout , reduce_output , activations_arch , primes_arch )
157
+
158
+ # Train network
159
+ return train (weights , trX , trY , teX , teY , activations_arch , primes_arch , file , epochs , batch , learning_rate , save_timeout , reduce_output )
0 commit comments