-
Notifications
You must be signed in to change notification settings - Fork 0
/
SpecialFunction.py
79 lines (71 loc) · 2.35 KB
/
SpecialFunction.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
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
79
import numpy as np
from scipy.special import expit
import matplotlib.pyplot as plt
def activation_function(activation) :
if activation == "binary" :
return lambda x: np.where(x<=0,0,1)
elif activation == "sigmoid":
return lambda x : expit(x)
elif activation == "tanh":
return lambda x : np.tanh(x)
elif activation == "relu" :
return lambda x: np.where(x<=0,0,x)
elif activation == 'leakyRelu' :
return lambda x: np.where(x<=0,0.01*x,x)
def activation_derivative(activation_function):
if activation_function == "binary":
return lambda x: np.where(x<=0,0,0)
elif activation_function == "sigmoid":
return lambda x: x*(1-x)
elif activation_function == "tanh" :
return lambda x : x * (1-x*x)
elif activation_function == "relu" :
return lambda x: np.where(x<=0,0,1)
elif activation_function == "leakyRelu" :
return lambda x: np.where(x<=0,0.01,1)
def weights_initializer(initializer,input_dim,output_dim):
if initializer == 'gaussian' :
return np.random.normal(0.0,pow(output_dim,-0.5),(input_dim,output_dim))
if initializer == 'uniform' :
return np.random.rand(input_dim,output_dim)-0.5
def confusion_matrix(y_test,y_pred):
correct = 0
incorrect = 0
for i in range(len(y_test)):
if y_test[i] == y_pred[i]:
correct = correct+1
else:
incorrect = incorrect+1
print("total correct = ",correct)
print("total incorrect = ",incorrect)
print("performence : ",(correct/(correct+incorrect))*100,"%")
def performence(y_test,y_pred):
correct = 0
incorrect = 0
for i in range(len(y_test)):
if y_test[i] == y_pred[i]:
correct = correct+1
else:
incorrect = incorrect+1
return round((correct/(correct+incorrect))*100,2)
def binary_confusion_matrix(y_test,y_pred):
zbuto = 0
obutz = 0
zbutz = 0
obuto = 0
for i in range(len(y_test)):
if y_test[i]==0 and y_pred[i] == 0 :
zbutz = zbutz + 1
if y_test[i]==0 and y_pred[i] == 1 :
zbuto = zbuto + 1
if y_test[i]==1 and y_pred[i]== 0 :
obutz = obutz + 1
if y_test[i]==1 and y_pred[i]== 1 :
obuto = obuto + 1
matrix = [[zbutz,zbuto],[obutz,obuto]]
return np.array(matrix)
def accuracy_map(neural_network,costom,lw=1.0):
accuracies = neural_network.accuracies
plt.plot([i for i in range(len(accuracies))],accuracies, costom,lw =lw)
plt.axis([0,len(accuracies), min(accuracies), max(accuracies)])
plt.show()