def test_whiteningnode(): line_x = numx.zeros((1000, 2), "d") line_y = numx.zeros((1000, 2), "d") line_x[:, 0] = numx.linspace(-1, 1, num=1000, endpoint=1) line_y[:, 1] = numx.linspace(-0.2, 0.2, num=1000, endpoint=1) mat = numx.concatenate((line_x, line_y)) utils.rotate(mat, uniform() * 2 * numx.pi) mat += uniform(2) mat -= mat.mean(axis=0) pca = CCIPCAWhiteningNode() for i in xrange(5): pca.train(mat) bpca = WhiteningNode() bpca.train(mat) bpca.stop_training() v = pca.get_projmatrix() bv = bpca.get_projmatrix() dcosines = numx.zeros(v.shape[1]) for dim in xrange(v.shape[1]): dcosines[dim] = numx.fabs(numx.dot(v[:, dim], bv[:, dim].T)) / ( numx.linalg.norm(v[:, dim]) * numx.linalg.norm(bv[:, dim])) assert_almost_equal(numx.ones(v.shape[1]), dcosines)
def test_mcanode_v2(): iterval = 30 t = numx.linspace(0, 4 * numx.pi, 500) x = numx.zeros([t.shape[0], 2]) x[:, 0] = numx.real(numx.sin(t) + numx.power(numx.cos(11 * t), 2)) x[:, 1] = numx.cos(11 * t) expnode = PolynomialExpansionNode(2) input_data = expnode(x) input_data = input_data - input_data.mean(axis=0) wtnnode = WhiteningNode() input_data = wtnnode(input_data) input_data = mdp.utils.timediff(input_data) ##Setup node/trainer output_dim = 4 node = MCANode(output_dim=output_dim, eps=0.05) bpcanode = PCANode() bpcanode(input_data) # bv = bpcanode.v / numx.linalg.norm(bpcanode.v, axis=0) bv = bpcanode.v / mdp.numx.sum(bpcanode.v**2, axis=0)**0.5 bv = bv[:, ::-1][:, :output_dim] _tcnt = time.time() v = [] for i in xrange(iterval * input_data.shape[0]): node.train(input_data[i % input_data.shape[0]:i % input_data.shape[0] + 1]) if (node.get_current_train_iteration() % 100 == 0): v.append(node.v) dcosines = numx.zeros([len(v), output_dim]) for i in xrange(len(v)): for dim in xrange(output_dim): dcosines[i, dim] = numx.fabs(numx.dot(v[i][:, dim], bv[:, dim].T)) / ( numx.linalg.norm(v[i][:, dim]) * numx.linalg.norm(bv[:, dim])) print('\nTotal Time for {} iterations: {}'.format(iterval, time.time() - _tcnt)) assert_almost_equal(numx.ones(output_dim), dcosines[-1], decimal=2)
def __init__(self, lags=1, sfa_ica_coeff=(1., 1.), icaweights=None, sfaweights=None, whitened=False, white_comp = None, white_parm = None, eps_contrast=1e-6, max_iter=10000, RP=None, verbose=False, input_dim=None, output_dim=None, dtype=None): """ Perform Independent Slow Feature Analysis. The notation is the same used in the paper by Blaschke et al. Please refer to the paper for more information. :Parameters: lags list of time-lags to generate the time-delayed covariance matrices (in the paper this is the set of \tau). If lags is an integer, time-lags 1,2,...,'lags' are used. Note that time-lag == 0 (instantaneous correlation) is always implicitly used. sfa_ica_coeff a list of float with two entries, which defines the weights of the SFA and ICA part of the objective function. They are called b_{SFA} and b_{ICA} in the paper. sfaweights weighting factors for the covariance matrices relative to the SFA part of the objective function (called \kappa_{SFA}^{\tau} in the paper). Default is [1., 0., ..., 0.] For possible values see the description of icaweights. icaweights weighting factors for the cov matrices relative to the ICA part of the objective function (called \kappa_{ICA}^{\tau} in the paper). Default is 1. Possible values are: - an integer ``n``: all matrices are weighted the same (note that it does not make sense to have ``n != 1``) - a list or array of floats of ``len == len(lags)``: each element of the list is used for weighting the corresponding matrix - ``None``: use the default values. whitened ``True`` if input data is already white, ``False`` otherwise (the data will be whitened internally). white_comp If whitened is false, you can set ``white_comp`` to the number of whitened components to keep during the calculation (i.e., the input dimensions are reduced to ``white_comp`` by keeping the components of largest variance). white_parm a dictionary with additional parameters for whitening. It is passed directly to the WhiteningNode constructor. Ex: white_parm = { 'svd' : True } eps_contrast Convergence is achieved when the relative improvement in the contrast is below this threshold. Values in the range [1E-4, 1E-10] are usually reasonable. max_iter If the algorithms does not achieve convergence within max_iter iterations raise an Exception. Should be larger than 100. RP Starting rotation-permutation matrix. It is an input_dim x input_dim matrix used to initially rotate the input components. If not set, the identity matrix is used. In the paper this is used to start the algorithm at the SFA solution (which is often quite near to the optimum). verbose print progress information during convergence. This can slow down the algorithm, but it's the only way to see the rate of improvement and immediately spot if something is going wrong. output_dim sets the number of independent components that have to be extracted. Note that if this is not smaller than input_dim, the problem is solved linearly and SFA would give the same solution only much faster. """ # check that the "lags" argument has some meaningful value if isinstance(lags, (int, int)): lags = list(range(1, lags+1)) elif isinstance(lags, (list, tuple)): lags = numx.array(lags, "i") elif isinstance(lags, numx.ndarray): if not (lags.dtype.char in ['i', 'l']): err_str = "lags must be integer!" raise NodeException(err_str) else: pass else: err_str = ("Lags must be int, list or array. Found " "%s!" % (type(lags).__name__)) raise NodeException(err_str) self.lags = lags # sanity checks for weights if icaweights is None: self.icaweights = 1. else: if (len(icaweights) != len(lags)): err = ("icaweights vector length is %d, " "should be %d" % (str(len(icaweights)), str(len(lags)))) raise NodeException(err) self.icaweights = icaweights if sfaweights is None: self.sfaweights = [0]*len(lags) self.sfaweights[0] = 1. else: if (len(sfaweights) != len(lags)): err = ("sfaweights vector length is %d, " "should be %d" % (str(len(sfaweights)), str(len(lags)))) raise NodeException(err) self.sfaweights = sfaweights # store attributes self.sfa_ica_coeff = sfa_ica_coeff self.max_iter = max_iter self.verbose = verbose self.eps_contrast = eps_contrast # if input is not white, insert a WhiteningNode self.whitened = whitened if not whitened: if white_parm is None: white_parm = {} if output_dim is not None: white_comp = output_dim elif white_comp is not None: output_dim = white_comp self.white = WhiteningNode(input_dim=input_dim, output_dim=white_comp, dtype=dtype, **white_parm) # initialize covariance matrices self.covs = [ DelayCovarianceMatrix(dt, dtype=dtype) for dt in lags ] # initialize the global rotation-permutation matrix # if not set that we'll eventually be an identity matrix self.RP = RP # initialize verbose structure to print nice and useful progress info if verbose: info = { 'sweep' : max(len(str(self.max_iter)), 5), 'perturbe': max(len(str(self.max_iter)), 5), 'float' : 5+8, 'fmt' : "%.5e", 'sep' : " | "} f1 = "Sweep".center(info['sweep']) f1_2 = "Pertb". center(info['perturbe']) f2 = "SFA part".center(info['float']) f3 = "ICA part".center(info['float']) f4 = "Contrast".center(info['float']) header = info['sep'].join([f1, f1_2, f2, f3, f4]) info['header'] = header+'\n' info['line'] = len(header)*"-" self._info = info # finally call base class constructor super(ISFANode, self).__init__(input_dim, output_dim, dtype)