-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
78 lines (63 loc) · 2.38 KB
/
Copy pathutils.py
File metadata and controls
78 lines (63 loc) · 2.38 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import numpy as np
import matplotlib.pyplot as plt
def ReLU(unactivated_layer1_logits):
return np.maximum(unactivated_layer1_logits, 0)
def softmax(unactivated_layer2_logits):
unactivated_layer2_logits-=np.max(unactivated_layer2_logits, axis=0, keepdims=True)
exp_l2logits=np.exp(unactivated_layer2_logits)
return exp_l2logits / (np.sum(exp_l2logits, axis=0, keepdims=True)+1e-9)
def onehotencoder(labels):
labels = labels.astype(int)
num_classes = 10
encoded = np.zeros((labels.size, num_classes))
encoded[np.arange(labels.size), labels] = 1
encoded = encoded.T
return encoded
def ReLUderivative(unactivated_layer1_logits):
return unactivated_layer1_logits>0
def cross_entropy_loss(predictions, labels):
predictions = np.clip(predictions, 1e-15, 1-1e-15)
encoded_labels = onehotencoder(labels)
loss = -np.sum(encoded_labels*np.log(predictions))/encoded_labels.shape[1]
return loss
def accuracy(predictions, labels):
return (np.sum((np.argmax(predictions, 0))==labels))/labels.size
def plot_loss(training_loss):
plt.plot(training_loss, linewidth=2.5)
plt.title("Training Loss per Epoch")
plt.xlabel("Epoch")
plt.ylabel("Loss")
plt.grid(True)
plt.show()
def plot_accuracy(training_accuracy):
plt.plot(training_accuracy, linewidth=2.5)
plt.title("Training Accuracy per Epoch")
plt.xlabel("Epoch")
plt.ylabel("Accuracy")
plt.grid(True)
plt.show()
def create_confusion_matrix(true_labels, predicted_labels, classes=10, class_names=None):
cm = np.zeros((classes, classes), dtype=int)
for true, predicted in zip(true_labels, predicted_labels):
cm[true, predicted] +=1
plt.figure(figsize=(8, 6))
plt.imshow(cm, interpolation="nearest", cmap=plt.cm.viridis)
plt.title("Confusion Matrix")
plt.colorbar()
if class_names is None:
class_names = list(map(str, range(cm.shape[0])))
tick_marks = np.arange(len(class_names))
plt.xticks(tick_marks, class_names)
plt.yticks(tick_marks, class_names)
thresh = cm.max()/2
for i in range(cm.shape[0]):
for j in range(cm.shape[1]):
value = cm[i, j]
plt.text(j, i, str(value), horizontalalignment="center", color = "white" if value > thresh else "black")
plt.ylabel("True Label")
plt.xlabel("Predicted Label")
plt.tight_layout()
plt.show()
def shuffle_data(features, labels):
perm = np.random.permutation(features.shape[1])
return features[:, perm], labels[perm]