#NEWLINE# @ no_debug_mode #NEWLINE# def check_sample_correctishness_channelwise( f ) : #NEWLINE# #INDENT# '\n    Tests that the sample mean converges to the conditional expectation given\n    by the function Tests that p really is the max of the samples tests that\n    at most one h in a group is on\n    ' #NEWLINE# batch_size = 27 #NEWLINE# pool_size = 4 #NEWLINE# n = ( pool_size * 21 ) #NEWLINE# rng = np . random . RandomState ( [ 2012 , 9 , 26 ] ) #NEWLINE# zv = ( ( rng . randn ( batch_size , n ) . astype ( config . floatX ) * 3.5 ) - 5.0 ) #NEWLINE# top_down_v = rng . randn ( batch_size , ( n / pool_size ) ) . astype ( config . floatX ) #NEWLINE# z_th = T . matrix ( ) #NEWLINE# z_th . tag . test_value = zv #NEWLINE# z_th . name = 'z_th' #NEWLINE# top_down_th = T . matrix ( ) #NEWLINE# top_down_th . tag . test_value = top_down_v #NEWLINE# top_down_th . name = 'top_down_th' #NEWLINE# theano_rng = MRG_RandomStreams ( rng . randint ( 2147462579 ) ) #NEWLINE# ( p_th , h_th , p_sth , h_sth ) = f ( z_th , pool_size , top_down_th , theano_rng ) #NEWLINE# prob_func = function ( [ z_th , top_down_th ] , [ p_th , h_th ] ) #NEWLINE# ( pv , hv ) = prob_func ( zv , top_down_v ) #NEWLINE# sample_func = function ( [ z_th , top_down_th ] , [ p_sth , h_sth ] ) #NEWLINE# acc_p = ( 0.0 * pv ) #NEWLINE# acc_h = ( 0.0 * hv ) #NEWLINE# buckets = 10 #NEWLINE# bucket_width = ( 1.0 / float ( buckets ) ) #NEWLINE# print ( pv . min ( ) , pv . max ( ) ) #NEWLINE# print ( hv . min ( ) , hv . max ( ) ) #NEWLINE# for i in xrange ( buckets ) : #NEWLINE# #INDENT# lower_lim = ( i * bucket_width ) #NEWLINE# upper_lim = ( ( i + 1 ) * bucket_width ) #NEWLINE# print ( lower_lim , upper_lim ) #NEWLINE# assert np . any ( ( ( pv >= lower_lim ) * ( pv < upper_lim ) ) ) #NEWLINE# assert np . any ( ( ( hv >= lower_lim ) * ( hv < upper_lim ) ) ) #NEWLINE# #UNINDENT# assert ( upper_lim == 1.0 ) #NEWLINE# for i in xrange ( 10000 ) : #NEWLINE# #INDENT# ( ps , hs ) = sample_func ( zv , top_down_v ) #NEWLINE# assert ( ps . shape == pv . shape ) #NEWLINE# assert ( k . shape == hv . shape ) #NEWLINE# acc_p += ps #NEWLINE# acc_h += hs #NEWLINE# #UNINDENT# est_p = ( acc_p / float ( ( i + 1 ) ) ) #NEWLINE# est_h = ( acc_h / float ( ( i + 1 ) ) ) #NEWLINE# pd = np . abs ( ( est_p - pv ) ) #NEWLINE# hd = np . abs ( ( est_h - hv ) ) #NEWLINE# '\n    # plot maps of the estimation error, this is to see if it has some\n    # spatial pattern this is useful for detecting bugs like not handling\n    # the border correctly, etc.\n    # from pylearn2.gui.patch_viewer import PatchViewer\n\n    pv = PatchViewer((pd.shape[0],pd.shape[3]),(pd.shape[1],pd.shape[2]),\n                                                            is_color = False)\n    for i in xrange(pd.shape[0]):\n    for j in xrange(pd.shape[3]):\n    pv.add_patch( (pd[i,:,:,j] / pd.max() )* 2.0 - 1.0, rescale = False)\n    pv.show()\n\n    pv = PatchViewer((hd.shape[0],hd.shape[3]),(hd.shape[1],hd.shape[2]),\n                                                            is_color = False)\n    for i in xrange(hd.shape[0]):\n    for j in xrange(hd.shape[3]):\n    pv.add_patch( (hd[i,:,:,j] / hd.max() )* 2.0 - 1.0, rescale = False)\n    pv.show()\n    ' #NEWLINE# '\n    plot expectation to estimate versus error in estimation\n    expect bigger errors for values closer to 0.5\n\n    from matplotlib import pyplot as plt\n\n    #nelem = reduce( lambda x, y : x*y, pd.shape)\n    #plt.scatter( pv.reshape(nelem), pd.reshape(nelem))\n    #plt.show()\n\n    nelem = reduce( lambda x, y : x*y, hd.shape)\n    plt.scatter( hv.reshape(nelem), hd.reshape(nelem))\n    plt.show()\n    ' #NEWLINE# assert ( max ( pd . max ( ) , hd . max ( ) ) < 0.17 ) #NEWLINE# assert np . all ( ( ( ps == 0 ) + ( ps == 1 ) ) ) #NEWLINE# assert np . all ( ( ( hs == 0 ) + ( hs == 1 ) ) ) #NEWLINE# for k in xrange ( batch_size ) : #NEWLINE# #INDENT# for i in xrange ( ps . shape [ 1 ] ) : #NEWLINE# #INDENT# p = ps [ ( k , i ) ] #NEWLINE# h = hs [ k , ( i * pool_size ) : ( ( i + 1 ) * pool_size ) ] #NEWLINE# assert ( h . shape == ( pool_size , ) ) #NEWLINE# assert ( p == h . max ( ) ) #NEWLINE# assert ( h . sum ( ) <= 1 ) #NEWLINE# #UNINDENT# #UNINDENT# ' If you made it to here, it\'s correctish\n     (cant tell if samples are perfectly "correct") '