forked from Razvy000/ANN_Course
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ann_util.py
47 lines (31 loc) · 824 Bytes
/
ann_util.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
import math
import pickle
import random
def logistic(x):
return 1.0 / (1 + math.exp(-x))
def deriv_logistic(x):
lgst = logistic(x)
return (1 - lgst) * lgst
def hyperbolic_tangent(x):
return math.tanh(x)
def deriv_hyperbolic_tangent(x):
th = math.tanh(x)
return 1 - th * th
def between(min, max):
"""
Return a real random value between min and max.
"""
return random.random() * (max - min) + min
def make_matrix(N, M):
"""
Make an N rows by M columns matrix.
"""
return [[0 for i in range(M)] for i in range(N)]
def serialize(nn, fname):
with open(fname, 'wb') as f:
# use protocol 2, default 0 has problems
pickle.dump(nn, f, 2)
def deserialize(fname):
with open(fname, 'rb') as f:
nn = pickle.load(f)
return nn