forked from mingukkang/Adversarial-AutoEncoder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
prior.py
89 lines (73 loc) · 3.15 KB
/
prior.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
import numpy as np
from math import sin,cos,sqrt
## this code is borrowed from https://github.com/hwalsuklee/tensorflow-mnist-AAE/
def gaussian(batch_size, n_labels, n_dim, mean=0, var=1, use_label_info=False):
np.random.seed(0)
if use_label_info:
if n_dim != 2 or n_labels != 10:
raise Exception("n_dim must be 2 and n_labels must be 10.")
def sample(n_labels):
x, y = np.random.normal(mean, var, (2,))
angle = np.angle((x-mean) + 1j*(y-mean), deg=True)
dist = np.sqrt((x-mean)**2+(y-mean)**2)
# label 0
if dist <1.0:
label = 0
else:
label = ((int)((n_labels-1)*angle))//360
if label<0:
label+=n_labels-1
label += 1
return np.array([x, y]).reshape((2,)), label
z = np.empty((batch_size, n_dim), dtype=np.float32)
z_id = np.empty((batch_size), dtype=np.int32)
for batch in range(batch_size):
for zi in range((int)(n_dim/2)):
a_sample, a_label = sample(n_labels)
z[batch, zi*2:zi*2+2] = a_sample
z_id[batch] = a_label
return z, z_id
else:
z = np.random.normal(mean, var, (batch_size, n_dim)).astype(np.float32)
return z
def gaussian_mixture(batch_size, n_labels ,n_dim, x_var=0.5, y_var=0.1, label_indices=None):
np.random.seed(0)
if n_dim != 2:
raise Exception("n_dim must be 2.")
def sample(x, y, label, n_labels):
shift = 1.4
r = 2.0 * np.pi / float(n_labels) * float(label)
new_x = x * cos(r) - y * sin(r)
new_y = x * sin(r) + y * cos(r)
new_x += shift * cos(r)
new_y += shift * sin(r)
return np.array([new_x, new_y]).reshape((2,))
x = np.random.normal(0, x_var, (batch_size, (int)(n_dim/2)))
y = np.random.normal(0, y_var, (batch_size, (int)(n_dim/2)))
z = np.empty((batch_size, n_dim), dtype=np.float32)
for batch in range(batch_size):
for zi in range((int)(n_dim/2)):
if label_indices is not None:
z[batch, zi*2:zi*2+2] = sample(x[batch, zi], y[batch, zi], label_indices[batch], n_labels)
else:
z[batch, zi*2:zi*2+2] = sample(x[batch, zi], y[batch, zi], np.random.randint(0, n_labels), n_labels)
return z
def swiss_roll(batch_size, n_labels, n_dim, label_indices=None):
np.random.seed(0)
if n_dim != 2:
raise Exception("n_dim must be 2.")
def sample(label, n_labels):
uni = np.random.uniform(0.0, 1.0) / float(n_labels) + float(label) / float(n_labels)
r = sqrt(uni) * 3.0
rad = np.pi * 4.0 * sqrt(uni)
x = r * cos(rad)
y = r * sin(rad)
return np.array([x, y]).reshape((2,))
z = np.zeros((batch_size, n_dim), dtype=np.float32)
for batch in range(batch_size):
for zi in range((int)(n_dim/2)):
if label_indices is not None:
z[batch, zi*2:zi*2+2] = sample(label_indices[batch], n_labels)
else:
z[batch, zi*2:zi*2+2] = sample(np.random.randint(0, n_labels), n_labels)
return z