-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathFunctions.py
More file actions
56 lines (36 loc) · 1.1 KB
/
Copy pathFunctions.py
File metadata and controls
56 lines (36 loc) · 1.1 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
import numpy as np
import math
def sigmoid(x):
return 1.0 / (1.0 + np.exp(-x))
def sigmoid_derivative(x):
return sigmoid(x) * (1 - sigmoid(x))
def tanh(x):
return np.tanh(x)
def tanh_derivate(X):
return 1 - (tanh(X) ** 2)
def relu(x):
return x * (x > 0)
def relu_derivative(X):
X[X <= 0.0] = 0.0
X[X > 0.0] = 1.0
return X
def softmax(X):
X = np.exp(X)
sum = np.sum(X, axis=0)
return X / sum
def squared_loss(y_hat, y, n_class, n_examples):
eIndicator = np.zeros((n_class, n_examples))
eIndicator[y, np.arange(n_examples)] = 1
return np.sum((y_hat - eIndicator)**2) / n_examples
# def cross_entropy(y, y_hat):
# s = 0.0
# for y_i, y_hat_i in zip(y, y_hat):
# s += y_i * math.log(y_hat_i + 1e-35)
# return 0 if s==0 else -s
def cross_entropy(yhat, y_train, n_class, n_examples):
eIndicator = np.zeros((n_class, n_examples))
eIndicator[y_train, np.arange(n_examples)] = 1
eIndicator = eIndicator * yhat
eIndicator = eIndicator.sum(axis=0)
eIndicator = np.log(eIndicator)
return -sum(eIndicator)/n_examples