@no_debug_mode
def check_sample_correctishness_channelwise(f):

    batch_size = 27
    pool_size = 4
    n = pool_size * 21

    rng = np.random.RandomState([2012, 9, 26])
    zv = rng.randn(batch_size, n).astype(config.floatX) * 3.5 - 5.
    top_down_v = rng.randn(batch_size, n / pool_size).astype(config.floatX)

    z_th = T.matrix()
    z_th.tag.test_value = zv
    z_th.name = 'z_th'

    top_down_th = T.matrix()
    top_down_th.tag.test_value = top_down_v
    top_down_th.name = 'top_down_th'

    theano_rng = MRG_RandomStreams(rng.randint(2147462579))
    p_th, h_th, p_sth, h_sth = f(z_th, pool_size, top_down_th, theano_rng)

    prob_func = function([z_th, top_down_th], [p_th, h_th])
    pv, hv = prob_func(zv, top_down_v)

    sample_func = function([z_th, top_down_th], [p_sth, h_sth])

    acc_p = 0. * pv
    acc_h = 0. * hv

    # make sure the test gets good coverage, ie, that it includes
    # many different activation probs for both detector and pooling layer
    buckets = 10
    bucket_width = 1. / float(buckets)
    print(pv.min(), pv.max())
    print(hv.min(), hv.max())
    for i in xrange(buckets):
        lower_lim = i * bucket_width
        upper_lim = (i+1) * bucket_width
        print(lower_lim, upper_lim)

        assert np.any((pv >= lower_lim) * (pv < upper_lim))
        assert np.any((hv >= lower_lim) * (hv < upper_lim))

    assert upper_lim == 1.

    for i in xrange(10000):
        ps, hs = sample_func(zv, top_down_v)

        assert ps.shape == pv.shape
        assert hs.shape == hv.shape

        acc_p += ps
        acc_h += hs

    est_p = acc_p / float(i+1)
    est_h = acc_h / float(i+1)

    pd = np.abs(est_p-pv)
    hd = np.abs(est_h-hv)

    """
    # plot maps of the estimation error, this is to see if it has some
    # spatial pattern this is useful for detecting bugs like not handling
    # the border correctly, etc.
    # from pylearn2.gui.patch_viewer import PatchViewer

    pv = PatchViewer((pd.shape[0],pd.shape[3]),(pd.shape[1],pd.shape[2]),
                                                            is_color = False)
    for i in xrange(pd.shape[0]):
    for j in xrange(pd.shape[3]):
    pv.add_patch( (pd[i,:,:,j] / pd.max() )* 2.0 - 1.0, rescale = False)
    pv.show()

    pv = PatchViewer((hd.shape[0],hd.shape[3]),(hd.shape[1],hd.shape[2]),
                                                            is_color = False)
    for i in xrange(hd.shape[0]):
    for j in xrange(hd.shape[3]):
    pv.add_patch( (hd[i,:,:,j] / hd.max() )* 2.0 - 1.0, rescale = False)
    pv.show()
    """

    """
    plot expectation to estimate versus error in estimation
    expect bigger errors for values closer to 0.5

    from matplotlib import pyplot as plt

    #nelem = reduce( lambda x, y : x*y, pd.shape)
    #plt.scatter( pv.reshape(nelem), pd.reshape(nelem))
    #plt.show()

    nelem = reduce( lambda x, y : x*y, hd.shape)
    plt.scatter( hv.reshape(nelem), hd.reshape(nelem))
    plt.show()
    """

    # don't really know how tight this should be
    # but you can try to pose an equivalent problem
    # and implement it in another way
    # using a numpy implementation in softmax_acc.py
    # I got a max error of .17
    assert max(pd.max(), hd.max()) < .17

    # Do exhaustive checks on just the last sample
    assert np.all((ps == 0) + (ps == 1))
    assert np.all((hs == 0) + (hs == 1))

    for k in xrange(batch_size):
        for i in xrange(ps.shape[1]):
            p = ps[k, i]
            h = hs[k, i*pool_size:(i+1)*pool_size]
            assert h.shape == (pool_size,)
            assert p == h.max()
            assert h.sum() <= 1

    """ If you made it to here, it's correctish
     (cant tell if samples are perfectly "correct") """
Wait until specific OFP message received
dataset/ETHPy150Open lisa-lab/pylearn2/pylearn2/expr/tests/test_probabilistic_max_pooling.py/check_sample_correctishness_channelwise--crossover with--dataset/ETHPy150Open osrg/ryu/ryu/tests/switch/tester.py/OfTester._wait