-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathfully_connected_PCA_MNIST.py
More file actions
56 lines (42 loc) · 1.72 KB
/
fully_connected_PCA_MNIST.py
File metadata and controls
56 lines (42 loc) · 1.72 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
import numpy as np
import DL
# change the directory
Label_Train, Features_Train, Label_Test, Features_Test = DL.ReadFile("F:\\eural\\project2\\Deep-Learning-framework-main\\MNISTcsv")
print(Features_Train.shape)
Features_Train_flattened=Features_Train.reshape(60000,28*28)
Features_Test_flattened=Features_Test.reshape(10000,28*28)
pca=DL.PCA(0.98)
pca.fit(Features_Train_flattened)
Features_Train_reduced = pca.transform(Features_Train_flattened)
print(Features_Train_reduced.shape)
#%% training
batch_size = 64
num_epochs = 10
num_classes = 10
hidden_units = 300
Label_Train_hotone = DL.hot_one(Label_Train, num_classes)
input_dimensions=Features_Train_reduced.shape[0]
print(input_dimensions)
model = DL.model()
model.input_dims(input_dimensions)
model.add('Relu', hidden_units)
model.add('Linear', num_classes)
optim = DL.optimizer('gd',0.2,0.2)
loss_fn = DL.loss_Function('SoftmaxCrossEntropy')
loss_fn.setLambda(0)
print('Features_Train_reduced',Features_Train_reduced.shape)
model.fit(Features_Train_reduced, Label_Train_hotone,batch_size, num_epochs, optim, loss_fn)
#%% testing
# test on the same trained data
print((Features_Train_reduced).shape)
predicted_labels = np.argmax(model.predict((Features_Train_reduced)), axis=0)
accuracy = DL.accuracy(predicted_labels, Label_Train)
print("Accuracy of training dataset = {:.2f}%".format(accuracy*100))
# test on the test data set
Features_Test_reduced = pca.transform(Features_Test_flattened)
predicted_labels = np.argmax(model.predict(Features_Test_reduced), axis=0)
accuracy = DL.accuracy(predicted_labels, Label_Test)
print("Model Accuracy = {:.2f}%".format(accuracy*100))
#%% store and load model
# DL.store(model, "FC MNIST model") # store
# model = DL.load("FC MNIST model") # load