def whiten(data, V, m0, s0, nprincomps, batchsize=500, use_gpu=True, verbose=True): data = data.astype(np.float32) ncases = data.shape[0] nbatches = (ncases - 1) / batchsize + 1 data_white = np.zeros((ncases, nprincomps), dtype=np.float32) for bidx in range(nbatches): start = bidx * batchsize end = min((bidx + 1) * batchsize, ncases) if use_gpu: data[start:end] = theano_subtract_m1(data[start:end]) data[start:end] = theano_divide_s1(data[start:end]) data[start:end] = theano_subtract_row(data[start:end], m0) data[start:end] = theano_divide_row(data[start:end], s0) data_white[start:end] = theano_dot(data[start:end], V[:nprincomps].T) else: data[start:end] -= data[start:end].mean(1)[:, None] s1 = data[start:end].std(1)[:, None] data[start:end] /= s1 + s1.mean() data[start:end] -= m0 data[start:end] /= s0 data_white[start:end] = np.dot(data[start:end], V[:nprincomps].T) return data_white
def whitenX(data, V, m0, s0, nprincomps, batchsize=500, contrast_norm=True, dataset_norm=True, use_gpu=True, verbose=True): data = data.astype(np.float32) ncases = data.shape[0] nbatches = (ncases - 1) / batchsize + 1 V = V[:nprincomps] V = np.concatenate((V,-1*V),0).T data_white = np.zeros((ncases, nprincomps*2), dtype=np.float32) for bidx in range(nbatches): start = bidx * batchsize end = min((bidx + 1) * batchsize, ncases) if use_gpu: if contrast_norm: data[start:end] = theano_subtract_m1(data[start:end]) data[start:end] = theano_divide_s1(data[start:end]) if dataset_norm: data[start:end] = theano_subtract_row(data[start:end], m0) data[start:end] = theano_divide_row(data[start:end], s0) data_white[start:end] = theano_dot(data[start:end], V) else: if contrast_norm: data[start:end] -= data[start:end].mean(1)[:, None] s1 = data[start:end].std(1)[:, None] data[start:end] /= s1 + s1.mean() if dataset_norm: data[start:end] -= m0 data[start:end] /= s0 data_white[start:end] = np.dot(data[start:end], V) return (data_white > 0.)*data_white
def zca_whiten(data, W, batchsize=500, use_gpu=True, verbose=True): data = data.astype(np.float32) ncases = data.shape[0] nbatches = (ncases - 1) / batchsize + 1 data_white = np.zeros((ncases, data.shape[1]), dtype=np.float32) for bidx in range(nbatches): start = bidx * batchsize end = min((bidx + 1) * batchsize, ncases) if use_gpu: data[start:end] = theano_subtract_m1(data[start:end]) data[start:end] = theano_divide_s1(data[start:end]) data_white[start:end] = theano_dot(data[start:end], W) else: data[start:end] -= data[start:end].mean(1)[:, None] s1 = data[start:end].std(1)[:, None] data[start:end] /= s1 + s1.mean() data_white[start:end] = np.dot(data[start:end], W) return data_white
def whitenX(data, V, m0, s0, nprincomps, batchsize=500, contrast_norm=True, dataset_norm=True, use_gpu=True, verbose=True): data = data.astype(np.float32) ncases = data.shape[0] nbatches = (ncases - 1) / batchsize + 1 V = V[:nprincomps] V = np.concatenate((V, -1 * V), 0).T data_white = np.zeros((ncases, nprincomps * 2), dtype=np.float32) for bidx in range(nbatches): start = bidx * batchsize end = min((bidx + 1) * batchsize, ncases) if use_gpu: if contrast_norm: data[start:end] = theano_subtract_m1(data[start:end]) data[start:end] = theano_divide_s1(data[start:end]) if dataset_norm: data[start:end] = theano_subtract_row(data[start:end], m0) data[start:end] = theano_divide_row(data[start:end], s0) data_white[start:end] = theano_dot(data[start:end], V) else: if contrast_norm: data[start:end] -= data[start:end].mean(1)[:, None] s1 = data[start:end].std(1)[:, None] data[start:end] /= s1 + s1.mean() if dataset_norm: data[start:end] -= m0 data[start:end] /= s0 data_white[start:end] = np.dot(data[start:end], V) return (data_white > 0.) * data_white