Advanced Computing Platform for Theoretical Physics

Commit 38341fe4 authored by kosklain's avatar kosklain
Browse files

Added random state information to sparsify and minibatch generation

parent 1f09e2e3
......@@ -10,7 +10,7 @@ import numpy as np
import climin.mathadapt as ma
def sparsify_columns(arr, n_non_zero, keep_diagonal=False):
def sparsify_columns(arr, n_non_zero, keep_diagonal=False, random_state=None):
"""Set all but ``n_non_zero`` entries to zero for each column of ``arr``.
This is a common technique to find better starting points for learning
......@@ -28,6 +28,10 @@ def sparsify_columns(arr, n_non_zero, keep_diagonal=False):
keep_diagonal : boolean, optional [default: False]
If set to True and ``arr`` is square, do keep the diagonal.
random_state : numpy.random.RandomState object, optional [default : None]
If set, random number generator that will generate the indices
corresponding to the zero-valued columns.
Examples
--------
......@@ -45,16 +49,17 @@ def sparsify_columns(arr, n_non_zero, keep_diagonal=False):
# In case it's gnumpy, copy to numpy array first. The sparsifying loop will
# run in numpy.
arr_np = arr if isinstance(arr, np.ndarray) else arr.as_numpy_array()
mask = np.ones_like(arr_np)
for i in range(arr.shape[1]):
idxs = xrange(colsize)
zeros = random.sample(idxs, colsize - n_non_zero)
if random_state is None:
zeros = random.sample(idxs, colsize - n_non_zero)
else:
zeros = random_state.choice(idxs, colsize - n_non_zero,
replace=False)
mask[zeros, i] *= 0
if keep_diagonal and arr.shape[0] == arr.shape[1]:
mask += np.eye(arr.shape[0])
arr *= mask
......@@ -91,7 +96,7 @@ def bound_spectral_radius(arr, bound=1.2):
arr[...] = np.dot(vecs, np.dot(np.diag(vals), np.linalg.inv(vecs)))
def randomize_normal(arr, loc=0, scale=1):
def randomize_normal(arr, loc=0, scale=1, random_state=None):
"""Populate an array with random numbers from a normal distribution with
mean `loc` and standard deviation `scale`.
......@@ -107,6 +112,9 @@ def randomize_normal(arr, loc=0, scale=1):
scale : float
Standard deviation of the random numbers.
random_state : np.random.RandomState object, optional [default : None]
Random number generator that shall generate the random numbers.
Examples
--------
......@@ -124,7 +132,8 @@ def randomize_normal(arr, loc=0, scale=1):
[ 9.99867829, 9.99410111, 9.8242318 ],
[ 9.9383779 , 9.94880091, 10.03179085]])
"""
sample = np.random.normal(loc, scale, arr.shape)
rng = np.random if random_state is None else random_state
sample = rng.normal(loc, scale, arr.shape)
if isinstance(arr, np.ndarray):
arr[...] = sample.astype(arr.dtype)
else:
......
......@@ -305,7 +305,7 @@ def minibatches(arr, batch_size, d=0):
return res
def iter_minibatches(lst, batch_size, dims, n_cycles=False):
def iter_minibatches(lst, batch_size, dims, n_cycles=False, random_state=None):
"""Return an iterator that successively yields tuples containing aligned
minibatches of size `batch_size` from slicable objects given in `lst`, in
random order without replacement.
......@@ -333,6 +333,9 @@ def iter_minibatches(lst, batch_size, dims, n_cycles=False):
Number of cycles after which to stop the iterator. If ``False``, will
yield forever.
random_state : a numpy.random.RandomState object, optional [default : None]
Random number generator that will act as a seed for the minibatch order
Returns
-------
......@@ -346,6 +349,8 @@ def iter_minibatches(lst, batch_size, dims, n_cycles=False):
if any(len(i) != len(batches[0]) for i in batches[1:]):
raise ValueError("containers to be batched have different lengths")
counter = itertools.count()
if random_state is not None:
random.seed(random_state.normal())
while True:
indices = [i for i, _ in enumerate(batches[0])]
while True:
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment