-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInitialization.py
83 lines (57 loc) · 1.88 KB
/
Initialization.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
"""
parameters initialize:
Gaussio distribute
Constant initialize
Xavier initialize
He_normal initialize
"""
import numpy as np
class Initialize:
def __init__(self, shape, withgrad=False, initializer=None):
self.shape = shape
self.data = None
self.grad = None
self.initializer = initializer
self.initialize(withgrad)
def initialize(self, withgrad=False):
self.data = self.initializer(self.shape)
if withgrad:
self.grad = np.zeros(self.shape)
def getdata(self):
return self.data
def getgrad(self):
return self.grad
def setdata(self, data):
self.data = data
def setgrad(self, grad):
self.grad = grad
class GaussionInitialize:
def __init__(self, mean=0, std=1e-2):
self.mean = mean
self.std = std
def __call__(self, shape):
return np.random.normal(self.mean, self.std, shape)
class ConstantInitialize:
def __init__(self, constant=0.1):
self.constant = constant
def __call__(self, shape):
return np.zeros(shape) + self.constant
# assume the active function attempt to be linear near the zero, also the active values is symmetric about 0 (like tanh)
class XavierInitialize:
def __init__(self, mode='uniform'):
self.mode = mode
def __call__(self, shape):
param = np.sum(shape)
if self.mode == 'uniform':
return np.random.uniform(-np.sqrt(6/param), np.sqrt(6/param), shape)
else:
return np.random.normal(0, np.sqrt(2/param), shape)
# used for relu
class HeInitialize:
def __init__(self, mode='normal'):
self.mode = mode
def __call__(self, shape):
if self.mode == 'normal':
return np.random.normal(0, np.sqrt(2/shape[0]), shape)
else:
return np.random.uniform(-np.sqrt(6/shape[0]), np.sqrt(6/shape[0]), shape)