forked from shuyu-wang/DSC_data_analysis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
GradientDescent.py
169 lines (128 loc) · 5.42 KB
/
GradientDescent.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
import numpy as np
from copy import deepcopy
import matplotlib.pyplot as plt
###### Gradient descent ######
# Define the parameters in the gradient descent method: step factor, etc.
def DefineOptimizationParameters(minDecrease = 1e-8, maxIter = 1024, minIter = 0,
maxDampingIter = 32, dampingFactor = 7, increaseFactor = 2,
initialStepSize = 1):
params = { 'dampingFactor' : dampingFactor, 'increaseFactor': increaseFactor,
'minDecrease' : minDecrease, 'initialStepSize' : initialStepSize,
'maxIter': maxIter, 'minIter': minIter, 'maxDampingIter' : maxDampingIter}
return params
# Gradient descent
# Nonlinear conjugate gradient
# Stop optimization conditions
def GradientDescent(X, objective, gradient,
projection = (lambda x : x),
updateVariables = (lambda x, dx, s: x - s * dx),
params = DefineOptimizationParameters(),
isInDomain = lambda x : True):
X = projection(X)
# In the test, the calculated sum of the negative log likelihood function from PMF, the nll function
obj = objective(X)
stepSize = params['initialStepSize']
converged = False
iter = 0
objArr = np.zeros(params['maxIter']+1)
stepArr = np.zeros(params['maxIter']+1)
XTemp = deepcopy(X)
# Optimization loop
while not converged:
oldObj = obj
# Update iterative calculation
deltaX = gradient(X)
XTemp = updateVariables(X, deltaX, stepSize)
XTemp = projection(XTemp)
obj = objective(XTemp)
dampingIter = 0
while (obj > oldObj and dampingIter < params['maxDampingIter']) or not isInDomain(X):
stepSize /= params['dampingFactor']
XTemp = updateVariables(X, deltaX, stepSize)
XTemp = projection(XTemp)
obj = objective(XTemp)
dampingIter += 1
# Instead of increasing, could reset stepsize to 1 every few iterations
stepSize *= params['increaseFactor']
X = deepcopy(XTemp)
objArr[iter] = obj
stepArr[iter] = stepSize
iter += 1
if (iter > params['maxIter'] or oldObj - params['minDecrease'] < obj) and iter > params['minIter']:
converged = True
return X, objArr[0:iter], stepArr[0:iter]
# Newton Method
def GaussNewton(X, objective, gradient,
projection = (lambda x : x),
updateVariables = (lambda x, dx, s: x - s * dx),
params = DefineOptimizationParameters(),
isInDomain = lambda x : True):
X = projection(X)
obj = objective(X)
stepSize = params['initialStepSize']
converged = False
iter = 0
objArr = np.zeros(params['maxIter']+1)
stepArr = np.zeros(params['maxIter']+1)
XTemp = deepcopy(X)
while not converged:
oldObj = obj
deltaX = gradient(X)
XTemp = updateVariables(X, deltaX, stepSize)
XTemp = projection(XTemp)
obj = objective(XTemp)
dampingIter = 0
while (obj > oldObj and dampingIter < params['maxDampingIter']) or not isInDomain(X):
stepSize /= params['dampingFactor']
XTemp = updateVariables(X, deltaX, stepSize)
XTemp = projection(XTemp)
obj = objective(XTemp)
dampingIter += 1
stepSize *= params['increaseFactor']
X = deepcopy(XTemp)
objArr[iter] = obj
stepArr[iter] = stepSize
iter += 1
if (iter > params['maxIter'] or oldObj - params['minDecrease'] < obj) and iter > params['minIter']:
converged = True
return X, objArr[0:iter], stepArr[0:iter]
# Gradient descent method, fixed step size
def GradientDescentFixedStep(X, objective, gradient,
projection = (lambda x : x),
updateVariables = (lambda x, dx, s: x - s * dx),
params = DefineOptimizationParameters(),
isInDomain = lambda x : True):
X = projection(X)
obj = objective(X)
stepSize = params['initialStepSize']
converged = False
iter = 0
objArr = np.zeros(params['maxIter']+1)
stepArr = np.zeros(params['maxIter']+1)
XTemp = deepcopy(X)
while not converged:
oldObj = obj
deltaX = gradient(X)
XTemp = updateVariables(X, deltaX, stepSize)
XTemp = projection(XTemp)
obj = objective(XTemp)
X = deepcopy(XTemp)
stepArr[iter] = stepSize
objArr[iter] = obj
iter += 1
if (iter > params['maxIter'] or oldObj - params['minDecrease'] < obj) and iter > params['minIter']:
converged = True
return X, objArr[0:iter], stepArr[0:iter]
# Expectation maximization algorithm
def EM(p, expectationStep, maximizationStep, maxIter = 32, minDecrease = 1e-6):
converged = False
iter = 0
while not converged:
p = expectationStep(p)
p = maximizationStep(p)
iter += 1
# Convergence condition
# np.linalg.norm(). Norm solving
if np.linalg.norm(Z - oldZ) < minDecrease or iter > maxIter:
converged = True
return T, Z, iter