-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmultiprocessing.py
More file actions
167 lines (138 loc) · 5.09 KB
/
multiprocessing.py
File metadata and controls
167 lines (138 loc) · 5.09 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
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
"""
DISREGARD THIS FOR NOW
I'm just trying to improve performance by using multiple concurrent processes
"""
import numpy as np
import random
import math
import numdifftools as nd
import matplotlib.pyplot as plt
import matplotlib.animation as animation
import multiprocessing
e = math.e
pi = math.pi
def distance(z1, z2):
sum = 0
for i in range(0,len(z1)-1):
sum += (z1[i] - z2[i]) ** 2
return math.sqrt(sum)
def F_1(z):
r = distance(z, center)
alpha = 1.5 # Consistent with the density (As this gets larger, less samples are moved close to 0)
return r * math.erf(r/alpha) + (alpha/math.sqrt(pi)) * math.pow(e, -(r/alpha) ** 2)
def F_2(z):
r = distance(z, center)
alpha = 1.5
return alpha + r - alpha * math.log(abs(alpha + r))
def Beta_1Calculation(q):
Proportion = 0.5
xSummationDerivative = 0
ySummationDerivative = 0
for j in range(0, len(StandardNormal)):
ySummationDerivative += F_1(StandardNormal[j])
for i in range(0, len(MixtureSample)):
xSummationDerivative += F_1(MixtureSample[i])
Beta = (-1/len(MixtureSample)) * xSummationDerivative + \
(1/len(StandardNormal)) * ySummationDerivative
# return Beta * Proportion
q.put(Beta * Proportion)
def Beta_2Calculation(q):
Proportion = 0.5
xSummationDerivative = 0
ySummationDerivative = 0
for j in range(0, len(StandardNormal)):
ySummationDerivative += F_2(StandardNormal[j])
for i in range(0, len(MixtureSample)):
xSummationDerivative += F_2(MixtureSample[i])
Beta = (-1/len(MixtureSample)) * xSummationDerivative + \
(1/len(StandardNormal)) * ySummationDerivative
# return Beta * Proportion
q.put(Beta * Proportion)
def u(x, Beta_1, Beta_2):
return (((x[0] ** 2) + (x[1] ** 2)) / 2) + Beta_1 * F_1(x) + Beta_2 * F_2(x)
def uConjugate(y, Beta_1, Beta_2):
ConvexCandidate = []
for i in range(0, len(MixtureSample)):
ConvexCandidate.append(np.dot(MixtureSample[i], y) - u(MixtureSample[i], Beta_1, Beta_2))
return max(ConvexCandidate)
def D(Beta_1, Beta_2):
xSummation = 0
ySummation = 0
for i in range(0, len(MixtureSample)):
xSummation += u(MixtureSample[i], Beta_1, Beta_2)
for j in range(0, len(StandardNormal)):
ySummation += uConjugate(StandardNormal[j], Beta_1, Beta_2)
LL = 1/len(MixtureSample) * xSummation + 1 / \
len(StandardNormal) * ySummation
return LL
def SamplesUpdate(OldMixtureSample):
NewMixtureSample = []
for i in range(0, len(OldMixtureSample)):
xval = OldMixtureSample[i][0] + Beta_1 * nd.Gradient(F_1)(OldMixtureSample[i])[0] + Beta_2 * nd.Gradient(F_2)(OldMixtureSample[i])[0]
yval = OldMixtureSample[i][1] + Beta_1 * nd.Gradient(F_1)(OldMixtureSample[i])[1] + Beta_2 * nd.Gradient(F_2)(OldMixtureSample[i])[1]
NewMixtureSample.append([xval,yval])
NewMixtureSample = np.array(NewMixtureSample)
return NewMixtureSample
def MixtureSampleGenerator():
mean1 = [1, -1]
cov1 = [[0.5, 0], [0, 0.5]]
mean2 = [-1, 1]
cov2 = [[0.5, 0], [0, 0.5]]
x = np.random.multivariate_normal(mean1, cov1, 500)
y = np.random.multivariate_normal(mean2, cov2, 500)
MixtureSample = []
for i in range(500):
RandomSelector = random.random()
if RandomSelector > 0.7:
MixtureSample.append(x[i])
else:
MixtureSample.append(y[i])
MixtureSample = np.array(MixtureSample)
return MixtureSample
def StandardNormalGenerator():
Sample = []
x = np.random.standard_normal(500)
y = np.random.standard_normal(500)
for i in range(500):
Sample.append([x[i], y[i]])
return Sample
#------------------------------------------------------------------ TESTING (change to heatmap, add animtation)------------------------------------------------------------
MixtureSample = MixtureSampleGenerator()
StandardNormal = StandardNormalGenerator()
CenterGeneratorList = MixtureSample + StandardNormal
plt.subplot(1,3,3)
plt.title("Target")
plt.scatter(*zip(*StandardNormal), color = 'r', alpha = 0.2)
plt.xlim(-4, 4)
plt.ylim(-4, 4)
plt.subplot(1,3,1)
plt.title("Initial")
plt.scatter(*zip(*MixtureSample), color = 'b', alpha = 0.2)
plt.xlim(-4, 4)
plt.ylim(-4, 4)
DValue = 0
while True: # Maybe there is a problem of overfitting
#print("Iteration " + str(i))
center = CenterGeneratorList[random.randint(0, len(CenterGeneratorList) - 1)]
# q = multiprocessing.Queue()
if __name__ == '__main__':
process1 = multiprocessing.Process(target=Beta_1Calculation, args=[q])
process2 = multiprocessing.Process(target=Beta_2Calculation, args=[q])
process1.start()
process2.start()
process1.join()
process2.join()
# Beta_1 = Beta_1Calculation()
# Beta_2 = Beta_2Calculation()
OldD = DValue
DValue = D(q.get(), q.get())
print(DValue)
MixtureSample = SamplesUpdate(MixtureSample)
if abs(DValue - OldD) < 0.001:
break
plt.subplot(1,3,2)
plt.title("Optimal Transport")
plt.scatter(*zip(*MixtureSample), color = 'g', alpha = 0.2)
plt.xlim(-4, 4)
plt.ylim(-4, 4)
plt.show()