-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExample.py
52 lines (45 loc) · 1.71 KB
/
Example.py
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
import numpy as np
import mirnet as mn
from mnist import MNIST
import matplotlib.pyplot as plt
from sklearn import preprocessing
from sklearn import metrics as mt
def main():
mndata = MNIST('datasets/')
# Preparing training data
X, Y_labels = mndata.load_training()
X = np.asanyarray(X) / 255
Y_labels = np.reshape(np.asanyarray(Y_labels), (len(Y_labels), 1))
binarizer = preprocessing.LabelBinarizer()
Y = binarizer.fit_transform(Y_labels)
# Preparing test data
X_test, Y_test_labels = mndata.load_testing()
X_test = np.asanyarray(X_test) / 255
Y_test_labels = np.reshape(np.asanyarray(Y_test_labels), (len(Y_test_labels), 1))
Y_test = binarizer.fit_transform(Y_test_labels)
# Setting up model parameters
depth = 1
neurons = 100
epochs = 100
patience = 5
sgd = int(len(X)/10)
layers = (neurons,) * depth
# Training the network
net = mn.MirNet(hidden_layers=layers, type="classifier", verbose=True)
net.fit(X, Y, sgd_init=sgd, max_epochs=epochs, patience=patience,
X_test=X_test, Y_test=Y_test)
Y_labels = binarizer.inverse_transform(Y)
train_prediction = binarizer.inverse_transform(net.predict(X))
best_train_loss = mt.accuracy_score(Y_labels,train_prediction)
test_prediction = binarizer.inverse_transform(net.predict(X_test))
best_test_loss = mt.accuracy_score(Y_test_labels, test_prediction)
# Plotting losses
plt.title("MirNet MNIST Training")
plt.ylabel('Loss')
plt.xlabel('Epoch')
plt.plot(net.losses_train,label="TRAIN ACCURACY %.4f" % best_train_loss)
plt.plot(net.losses_test, label="TEST ACCURACY%.4f" % best_test_loss)
plt.legend()
plt.show()
if __name__ == '__main__':
main()