-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathksd.py
174 lines (127 loc) · 5.45 KB
/
ksd.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
170
171
172
173
174
import numpy as np
from kernels import kernels, rbf_kernel, imq_kernel, poly_kernel
from tqdm import tqdm
"""
Kernelized Stein Discrepancy
"""
class ksd:
def __init__(self, name, params=dict(beta=1e-2)):
self.params = params
if name == 'rbf':
self.k_method = rbf_kernel(params)
if name == 'imq':
self.k_method = imq_kernel(params)
# if name == 'poly':
# self.k_method = poly_kernel(params)
self.k = self.k_method.value
self.grad_kx = self.k_method.grad_x
self.grad_ky = self.k_method.grad_y
self.grad_kxy = self.k_method.grad_xy
self.p = params['p']
self.q = params['q']
def h(self, x, y):
log_px = self.p.grad_log_density(x)
log_py = self.p.grad_log_density(y)
p1 = self.k(x,y) * np.dot(log_py, log_px)
p2 = np.dot(self.grad_kx(x,y), log_py)
p3 = np.dot(self.grad_ky(x,y), log_px)
p4 = np.sum(self.grad_kxy(x,y))
return p1 + p2 + p3 + p4
def Op_value(self, n, m=5):
x_samples = self.q.sampler(n)
y_samples = self.q.sampler(m)
stein_average = np.sum(np.array([np.array([self.h(y_i,x_j) for y_i in y_samples]) for x_j in tqdm(x_samples)]))
return stein_average/(n*m)
# class ksd:
# def __init__(self, params):
# self.k_method = kernels(params['name'])
# self.k = self.k_method.get_kernel()
# self.grad_kx = self.k_method.grad_kx
# self.grad_ky = self.k_method.grad_ky
# self.grad_kxy = self.k_method.grad_kxy
# self.p = params['p']
# self.q = params['q']
# def h(self, x, y):
# p1 = self.k(x,y) * np.dot(self.p.grad_log_density(y), self.p.grad_log_density(x))
# p2 = np.dot(self.grad_kx(x,y), self.p.grad_log_density(y))
# p3 = np.dot(self.grad_ky(x,y), self.p.grad_log_density(x))
# p4 = np.sum(self.grad_kxy(x,y))
# return p1 + p2 + p3 + p4
# def stein_Op(self, n, m=5):
# x_samples = self.q.sampler(n)
# y_samples = self.q.sampler(m)
# stein_average = np.sum(np.array([[self.h(y_i,x_j) for y_i in y_samples] for x_j in tqdm(x_samples)]))
# # stein_average = 0
# # for x in tqdm(x_samples):
# # stein_average += np.sum(np.array([self.h(x, y_) for y_ in y_samples]))
# return stein_average/(n*m)
# class ksd_rbf:
# def __init__(self, params=dict(beta=1e-2)):
# self.params = params
# self.k_method = rbf_kernel(params)
# self.k = self.k_method.value
# self.grad_kx = self.k_method.grad_x
# self.grad_ky = self.k_method.grad_y
# self.grad_kxy = self.k_method.grad_xy
# self.p = params['p']
# self.q = params['q']
# def h(self, x, y):
# p1 = self.k(x,y) * np.dot(self.p.grad_log_density(y), self.p.grad_log_density(x))
# p2 = np.dot(self.grad_kx(x,y), self.p.grad_log_density(y))
# p3 = np.dot(self.grad_ky(x,y), self.p.grad_log_density(x))
# p4 = np.sum(self.grad_kxy(x,y))
# return p1 + p2 + p3 + p4
# def stein_Op(self, n, m=5):
# x_samples = self.q.sampler(n)
# y_samples = self.q.sampler(m)
# stein_average = np.sum(np.array([[self.h(y_i,x_j) for y_i in y_samples] for x_j in tqdm(x_samples)]))
# return stein_average/(n*m)
# class ksd_imq:
# def __init__(self, params=dict(beta=1, c=1)):
# self.params = params
# self.k_method = imq_kernel(params)
# self.k = self.k_method.value
# self.grad_kx = self.k_method.grad_x
# self.grad_ky = self.k_method.grad_y
# self.grad_kxy = self.k_method.grad_xy
# self.p = params['p']
# self.q = params['q']
# def h(self, x, y):
# p1 = self.k(x,y) * np.dot(self.p.grad_log_density(y), self.p.grad_log_density(x))
# p2 = np.dot(self.grad_kx(x,y), self.p.grad_log_density(y))
# p3 = np.dot(self.grad_ky(x,y), self.p.grad_log_density(x))
# p4 = np.sum(self.grad_kxy(x,y))
# return p1 + p2 + p3 + p4
# def stein_Op(self, n, m=5):
# x_samples = self.q.sampler(n)
# y_samples = self.q.sampler(m)
# stein_average = np.sum(np.array([[self.h(y_i,x_j) for y_i in y_samples] for x_j in tqdm(x_samples)]))
# return stein_average/(n*m)
# class ksd_poly:
# def __init__(self, params=dict(degree=2, c=1)):
# self.params = params
# self.k_method = poly_kernel(params)
# self.k = self.k_method.value
# self.grad_kx = self.k_method.grad_x
# self.grad_ky = self.k_method.grad_y
# self.grad_kxy = self.k_method.grad_xy
# self.p = params['p']
# self.q = params['q']
# def h(self, x, y):
# p1 = self.k(x,y) * np.dot(self.p.grad_log_density(y), self.p.grad_log_density(x))
# p2 = np.dot(self.grad_kx(x,y), self.p.grad_log_density(y))
# p3 = np.dot(self.grad_ky(x,y), self.p.grad_log_density(x))
# p4 = np.sum(self.grad_kxy(x,y))
# return p1 + p2 + p3 + p4
# def stein_Op(self, n, m=5):
# x_samples = self.q.sampler(n)
# y_samples = self.q.sampler(m)
# stein_average = np.sum(np.array([[self.h(y_i,x_j) for y_i in y_samples] for x_j in tqdm(x_samples)]))
# return stein_average/(n*m)
# def ksd_operator(name, params):
# if name == 'rbf':
# return ksd_rbf(params)
# if name == 'imq':
# return ksd_imq(params)
# if name == 'poly':
# return ksd_poly(params)