-
Notifications
You must be signed in to change notification settings - Fork 18
/
util.py
82 lines (63 loc) · 3.78 KB
/
util.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
import numpy as np
def sample_Z(batch, z_dim , sampler = 'one_hot', num_class = 10, n_cat = 1, label_index = None):
if sampler == 'mul_cat':
if label_index is None:
label_index = np.random.randint(low = 0 , high = num_class, size = batch)
return np.hstack((0.10 * np.random.randn(batch, z_dim-num_class*n_cat),
np.tile(np.eye(num_class)[label_index], (1, n_cat))))
elif sampler == 'one_hot':
if label_index is None:
label_index = np.random.randint(low = 0 , high = num_class, size = batch)
return np.hstack((0.10 * np.random.randn(batch, z_dim-num_class), np.eye(num_class)[label_index]))
elif sampler == 'uniform':
return np.random.uniform(-1., 1., size=[batch, z_dim])
elif sampler == 'normal':
return 0.15*np.random.randn(batch, z_dim)
elif sampler == 'mix_gauss':
if label_index is None:
label_index = np.random.randint(low = 0 , high = num_class, size = batch)
return (0.1 * np.random.randn(batch, z_dim) + np.eye(num_class, z_dim)[label_index])
def sample_labelled_Z(batch, z_dim, sampler = 'one_hot', num_class = 10, n_cat = 1, label_index = None):
if sampler == 'mul_cat':
if label_index is None:
label_index = np.random.randint(low=0, high=num_class, size=batch)
return label_index, np.hstack((0.10 * np.random.randn(batch, z_dim - num_class*n_cat),
np.tile(np.eye(num_class)[label_index], (1, n_cat))))
elif sampler == 'one_hot':
if label_index is None:
label_index = np.random.randint(low=0, high=num_class, size=batch)
return label_index, np.hstack((0.10 * np.random.randn(batch, z_dim - num_class), np.eye(num_class)[label_index]))
elif sampler == 'mix_gauss':
if label_index is None:
label_index = np.random.randint(low=0, high=num_class, size=batch)
return label_index, (0.1 * np.random.randn(batch, z_dim) + np.eye(num_class, z_dim)[label_index])
def reshape_mnist(X):
return X.reshape(X.shape[0], 28, 28, 1)
def clus_sample_Z(batch, dim_gen=20, dim_c=2, num_class = 10, label_index = None):
if label_index is None:
label_index = np.random.randint(low=0, high=num_class, size=batch)
batch_mat = np.zeros((batch, num_class* dim_c))
for b in range(batch):
batch_mat[b, label_index[b] * dim_c:(label_index[b] + 1) * dim_c] = np.random.normal(loc = 1.0, scale = 0.05, size = (1, dim_c))
return np.hstack((0.10 * np.random.randn(batch, dim_gen), batch_mat))
def clus_sample_labelled_Z(batch, dim_gen=20, dim_c=2, num_class = 10, label_index = None):
if label_index is None:
label_index = np.random.randint(low=0, high=num_class, size=batch)
batch_mat = np.zeros((batch, num_class*dim_c))
for b in range(batch):
batch_mat[b, label_index[b] * dim_c:(label_index[b] + 1) * dim_c] = np.random.normal(loc=1.0, scale = 0.05, size = (1, dim_c))
return label_index, np.hstack((0.10 * np.random.randn(batch, dim_gen), batch_mat))
def sample_info(batch, z_dim, sampler = 'one_hot', num_class = 10, n_cat = 1, label_index = None):
if sampler == 'one_hot':
if label_index is None:
label_index = np.random.randint(low=0, high=num_class, size=batch)
return label_index, np.hstack(
(np.random.randn(batch, z_dim - num_class), np.eye(num_class)[label_index]))
elif sampler == 'mul_cat':
if label_index is None:
label_index = np.random.randint(low=0, high=num_class, size=batch)
return label_index, np.hstack((np.random.randn(batch, z_dim - num_class*n_cat),
np.tile(np.eye(num_class)[label_index], (1, n_cat))))
if __name__=='__main__':
l = sample_Z(10, 22, 'mul_cat', 10, 2)
print(l)