-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcompute_kernel.py
More file actions
103 lines (76 loc) · 3.03 KB
/
Copy pathcompute_kernel.py
File metadata and controls
103 lines (76 loc) · 3.03 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
import numpy
import tensorflow as tf
import network
import Fisher
import linalg
# 4000 : 1.62 - 1.66 --- 0.0007
# 2000 : 1.68 - 1.76 --- 0.002
# 1000 : 1.32 - 1.99 --- 0.004
d = 2
#d1 = 500 #1000
d2 = 1
d1 = tf.placeholder(tf.int32)
X = tf.placeholder(tf.float32, [None, d])
Y = tf.placeholder(tf.float32, [None, 1])
batch_size = tf.shape(X)[0]
X0 = tf.placeholder(tf.float32, [1, d])
[YY, Y0], theta = network.affine_net([X, X0], [d, d1, d1, d1, d2], "NET", False
, tf.nn.relu, mult_b=0.1)
cost = tf.reduce_mean(tf.square(YY - Y))
d0 = tf.gradients(Y0, theta)
[K0] = Fisher.fwd_gradients([YY], theta, d0)
opt = tf.train.GradientDescentOptimizer(1.0).minimize(cost, var_list=theta)
norm_L2 = network.fix_weights(theta)
#calc_D = Fisher.derivative([YY], theta)
batch_size = 100
num = 100
num_sizes = 2
num_tries = 5
kernels_init = numpy.zeros([batch_size, num_tries, num_sizes])
kernels_train = numpy.zeros([batch_size, num_tries, num_sizes])
import matplotlib.pyplot as P
with tf.Session() as sess:
'''
const_x = numpy.linspace(-1.5, 1.5, batch_size)
const_x = numpy.reshape(const_x, [-1, 1])
#grid_x, grid_y = numpy.meshgrid(x, x)
#x = numpy.stack([numpy.reshape(grid_x, [-1]), numpy.reshape(grid_y, [-1])], 1)
'''
th = numpy.linspace(-numpy.pi, numpy.pi, batch_size)
const_x = numpy.stack([numpy.cos(th), numpy.sin(th)], 1)
#sess.run(tf.global_variables_initializer(), feed_dict={d1:1000})
#kernel = sess.run(K0, feed_dict={X:x, X0:[[1, 0]], d1:1000})
for i in range(num_sizes):
dd1 = [500, 10000][i]
for j in range(num_tries):
sess.run(tf.global_variables_initializer(), feed_dict={d1:dd1})
kernels_init[:, j:j+1, i] = sess.run(K0, feed_dict={X:const_x, X0:[[1.0, 0.0]], d1:dd1})
for t in range(num):
x = numpy.random.normal(numpy.zeros([batch_size, d]))
y = x[:, 0:1] * x[:, 1:2]
_, err = sess.run([opt, cost], feed_dict={X:x, Y:y, d1:dd1})
sess.run(norm_L2)
print(err)
kernels_train[:, j:j+1, i] = sess.run(K0, feed_dict={X:const_x, X0:[[1.0, 0.0]], d1:dd1})
#P.plot(x, kernel)
'''
P.plot(th, kernel)
P.figure()
P.semilogy(numpy.fft.fft(kernel, axis=0));
'''
'''
P.imshow(numpy.reshape(kernel, [batch_size, batch_size]))
P.contour(numpy.reshape(kernel, [batch_size, batch_size]))
'''
P.plot(th, kernels_init[:, 0, 0], "g-", alpha=0.5, label='$n=500, t=0$')
P.plot(th, kernels_init[:, 1:, 0], "g-", alpha=0.5)
P.plot(th, kernels_train[:, 0, 0], "g:", alpha=0.5, label='$n=500, t=20$')
P.plot(th, kernels_train[:, 1:, 0], "g:", alpha=0.5)
P.plot(th, kernels_init[:, 0, 1], "r-", alpha=0.5, label='$n=10000, t=0$')
P.plot(th, kernels_init[:, 1:, 1], "r-", alpha=0.5)
P.plot(th, kernels_train[:, 0, 1], "r:", alpha=0.5, label='$n=10000, t=20$')
P.plot(th, kernels_train[:, 1:, 1], "r:", alpha=0.5)
P.xlabel("$\gamma$")
P.xlim([-numpy.pi, numpy.pi])
P.legend()
P.show()