-
Notifications
You must be signed in to change notification settings - Fork 0
/
Gaussian_Sample.py
195 lines (162 loc) · 6.74 KB
/
Gaussian_Sample.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
from __future__ import (division, print_function, )
from collections import OrderedDict
from scipy.stats import multivariate_normal
import numpy as np
import numpy.random as npr
from fuel import config
from fuel.datasets import H5PYDataset, IndexableDataset
from fuel.transformers.defaults import uint8_pixels_to_floatX
from fuel.utils import find_in_data_path
import itertools
from fuel.streams import DataStream
from fuel.schemes import ShuffledScheme
import numpy.random as npr
import torch
import torch.nn as nn
import torch.autograd as autograd
import torch.optim as optim
import torchvision
import torchvision.datasets as dsets
import torchvision.transforms as transforms
from torch.autograd import Variable
import torch.nn.functional as F
import scipy.misc
import imageio
import matplotlib.pyplot as plt
import matplotlib.gridspec as gridspec
import os, time
from itertools import *
from torch.utils.data import Dataset
from fuel import config
from fuel.datasets import H5PYDataset, IndexableDataset
from functools import reduce
import matplotlib
matplotlib.use('agg')
import matplotlib.pyplot as plt
plt.switch_backend('agg')
NUM_EPOCHS = 200
LEARNING_RATE = 1e-4
BETA1 = 0.8
BATCH_SIZE = 128
MONITORING_BATCH_SIZE = 500
PRIORS = None
MEANS = [np.array([i, j]) for i, j in itertools.product(range(-4, 5, 2),
range(-4, 5, 2))]
VARIANCES = [0.05 ** 2 * np.eye(len(mean)) for mean in MEANS]
class GaussianMixture():
""" Toy dataset containing points sampled from a gaussian mixture distribution.
The dataset contains 3 sources:
* features
* label
* densities
"""
def __init__(self, num_examples, means=None, variances=None, priors=None,
**kwargs):
rng = kwargs.pop('rng', None)
if rng is None:
seed = kwargs.pop('seed', config.default_seed)
rng = np.random.RandomState(seed)
gaussian_mixture = GaussianMixtureDistribution(means=means,
variances=variances,
priors=priors,
rng=rng)
self.means = gaussian_mixture.means
self.variances = gaussian_mixture.variances
self.priors = gaussian_mixture.priors
features, labels = gaussian_mixture.sample(nsamples=num_examples)
densities = gaussian_mixture.pdf(x=features)
self.data = {
'features': features,
'label': labels,
'density': densities
}
def get_data(self):
return self.data
def create_gaussian_mixture_data(batch_size, monitoring_batch_size,
means=None, variances=None, priors=None,
rng=None, num_examples=100000,
sources=('features', )):
train_set = GaussianMixture(num_examples=num_examples, means=means,
variances=variances, priors=priors,
rng=rng, sources=sources)
valid_set = GaussianMixture(num_examples=num_examples,
means=means, variances=variances,
priors=priors, rng=rng, sources=sources)
return train_set, valid_set
def as_array(obj):
"""Converts to ndarray of specified dtype"""
return np.asarray(obj)
class GaussianMixtureDistribution(object):
""" Gaussian Mixture Distribution
Parameters
----------
means : tuple of ndarray.
Specifies the means for the gaussian components.
variances : tuple of ndarray.
Specifies the variances for the gaussian components.
priors : tuple of ndarray
Specifies the prior distribution of the components.
"""
def __init__(self, means=None, variances=None, priors=None, rng=None, seed=None):
if means is None:
means = map(lambda x: 10.0 * as_array(x), [[0, 0],
[1, 1],
[-1, -1],
[1, -1],
[-1, 1]])
# Number of components
self.ncomponents = len(means)
self.dim = means[0].shape[0]
self.means = means
# If prior is not specified let prior be flat.
if priors is None:
priors = [1.0/self.ncomponents for _ in range(self.ncomponents)]
self.priors = priors
# If variances are not specified let variances be identity
if variances is None:
variances = [np.eye(self.dim) for _ in range(self.ncomponents)]
self.variances = variances
assert len(means) == len(variances), "Mean variances mismatch"
assert len(variances) == len(priors), "prior mismatch"
if rng is None:
rng = npr.RandomState(seed=seed)
self.rng = rng
def _sample_prior(self, nsamples):
return self.rng.choice(a=self.ncomponents,
size=(nsamples, ),
replace=True,
p=self.priors)
def sample(self, nsamples):
# Sampling priors
samples = []
fathers = self._sample_prior(nsamples=nsamples).tolist()
for father in fathers:
samples.append(self._sample_gaussian(self.means[father],
self.variances[father]))
return as_array(samples), as_array(fathers)
def _sample_gaussian(self, mean, variance):
# sampling unit gaussians
epsilons = self.rng.normal(size=(self.dim, ))
return mean + np.linalg.cholesky(variance).dot(epsilons)
def _gaussian_pdf(self, x, mean, variance):
return multivariate_normal.pdf(x, mean=mean, cov=variance)
def pdf(self, x):
"Evaluates the the probability density function at the given point x"
pdfs = map(lambda m, v, p: p * self._gaussian_pdf(x, m, v),
self.means, self.variances, self.priors)
return reduce(lambda x, y: x + y, pdfs, 0.0)
class Gaussian_Data(Dataset):
def __init__(self, dataset):
self.x = torch.from_numpy(np.array(dataset['features'])).type(torch.FloatTensor)
self.y = torch.from_numpy(np.array(dataset['label']))
def __len__(self):
return len(self.x)
def __getitem__(self, idx):
return self.x[idx], self.y[idx]
def main():
data = create_gaussian_mixture_data(
batch_size=BATCH_SIZE, monitoring_batch_size=MONITORING_BATCH_SIZE,
means=MEANS, variances=VARIANCES, priors=PRIORS)
train_data = data[0].get_data()
valid_data = data[1].get_data()
return train_data, valid_data