# HG changeset patch # User James Bergstra # Date 1287686869 14400 # Node ID 0d55f8f0aedcb732a098a24860200412207f8bfc # Parent c8c30c675a4f5d64e3b664de5073674e91dda068 mcRBM - changes to creation of topo_P matrix diff -r c8c30c675a4f -r 0d55f8f0aedc pylearn/algorithms/mcRBM.py --- a/pylearn/algorithms/mcRBM.py Thu Oct 21 11:43:36 2010 -0400 +++ b/pylearn/algorithms/mcRBM.py Thu Oct 21 14:47:49 2010 -0400 @@ -507,13 +507,28 @@ rval._params = [rval.U, rval.W, rval.a, rval.b, rval.c] return rval -def topological_connectivity(out_shape=(12,12), window_shape=(3,3), window_stride=(2,2), - **kwargs): +def topological_connectivity(out_shape=(12,12), window_shape=(3,3), window_stride=(2,2), dtype=None): + """Return numpy R x C matrix with connection weights from R inputs (filters) to C output (covariance) units. + """ + if window_stride[0] != window_stride[1]: + raise ValueError() + if out_shape[0] != out_shape[1]: + raise ValueError() + if window_shape[0] != window_shape[1]: + raise ValueError() + + if dtype is None: + dtype = theano.config.floatX + + ker = numpy.asarray([1., 2., 1.], dtype=dtype) + ker = numpy.outer(ker,ker) + ker /= ker.sum() in_shape = (window_stride[0] * out_shape[0], window_stride[1] * out_shape[1]) - rval = numpy.zeros(in_shape + out_shape, dtype=theano.config.floatX) + tmp = numpy.zeros(in_shape, dtype=dtype) + rval = numpy.zeros(in_shape + out_shape, dtype=dtype) A,B,C,D = rval.shape # for each output position (out_r, out_c) @@ -523,17 +538,10 @@ for win_r in range(window_shape[0]): for win_c in range(window_shape[1]): # add 1 to the corresponding input location - in_r = out_r * window_stride[0] + win_r - in_c = out_c * window_stride[1] + win_c - rval[in_r%A, in_c%B, out_r%C, out_c%D] += 1 - - # This normalization algorithm is a guess, based on inspection of the matrix loaded from - # see CVPR2010paper_material/topo2D_3x3_stride2_576filt.mat - rval = rval.reshape((A*B, C*D)) - rval = (rval.T / rval.sum(axis=1)).T - - rval /= rval.sum(axis=0) - return rval + in_r = out_r * window_stride[0] + win_r - (window_shape[0]-1)//2 + in_c = out_c * window_stride[1] + win_c - (window_shape[1]-1)//2 + rval[(in_c+A)%A, (in_r+B)%B, out_r%C, out_c%D] = ker[win_r, win_c] + return rval.reshape((A*B,C*D)) class mcRBM_withP(mcRBM): """Light-weight class that provides the math related to inference @@ -598,8 +606,10 @@ @classmethod def alloc_topo_P(cls, n_I, n_J, p_out_shape=(12,12), p_win_shape=(3,3), p_win_stride=(2,2), **kwargs): + # the factor of 2 is for some degree of compatibility with Marc'Aurelio's code the 'w2' + # in his code is -0.5 * self.P return cls.alloc_with_P( - -topological_connectivity(p_out_shape, p_win_shape, p_win_stride), + -2*topological_connectivity(p_out_shape, p_win_shape, p_win_stride), n_I=n_I, n_J=n_J, **kwargs) @classmethod