Example #1
0
def or_infer_with_shift(rbm, vis, x_shift, y_shift, iters, k, beta=1):

    n_samples = vis.shape[0]
    height = 28
    width = 28

    xi = vis.copy()[:, 0:height, 0:width]
    xf = 1 - xi

    yi = vis.copy()[:, y_shift : y_shift + height, x_shift : x_shift + width]
    yf = 1 - yi

    for i in range(iters):
        xs, _ = ml.rbm.gibbs_sample(flatten_samples(xi), k, vis_force=flatten_samples(xf), beta=beta)
        xs = unflatten_samples_like(xs, xi)
        xr, xrf = or_rest(vis[:, 0:height, 0:width], xs)

        yi_by_x = xr[:, y_shift : y_shift + height, x_shift : x_shift + width]
        yf_by_x = xrf[:, y_shift : y_shift + height, x_shift : x_shift + width]
        yi[:, 0 : height - y_shift, 0 : width - x_shift] = yi_by_x
        yf[:, 0 : height - y_shift, 0 : width - x_shift] = yf_by_x

        ys, _ = ml.rbm.gibbs_sample(flatten_samples(yi), k, vis_force=flatten_samples(yf), beta=beta)
        ys = unflatten_samples_like(ys, yi)
        yr, yrf = or_rest(vis[:, y_shift : y_shift + height, x_shift : x_shift + width], ys)

        xi_by_y = yr[:, 0 : height - y_shift, 0 : width - x_shift]
        xf_by_y = yf[:, 0 : height - y_shift, 0 : width - x_shift]
        xi[:, y_shift:, x_shift:] = xi_by_y

    return xs, ys
Example #2
0
File: orrbm.py Project: surban/ml
def or_infer_with_shift_iter(rbm, vis, x_shift, y_shift, k, beta=1):                                
    n_samples = vis.shape[0]
          
    x_sx = 0
    x_sy = base_y
    x_ex = x_sx + width
    x_ey = x_sy + height
    
    y_sx = x_shift
    y_sy = y_shift
    y_ex = y_sx + width
    y_ey = y_sy + height

    i_sx, i_sy, i_width, i_height = rect_intersection(x_sx, x_sy, width, height,
                                                      y_sx, y_sy, width, height)
    i_ex = i_sx + i_width
    i_ey = i_sy + i_height

    #print "intersection: ", i_sx, i_sy, i_width, i_height
    #print "fixed"
    
    xi = vis.copy()[:, x_sy:x_ey, x_sx:x_ex]
    xf = 1 - xi

    yi = vis.copy()[:, y_sy:y_ey, y_sx:y_ex]
    yf = 1 - yi

    while True:
        xs, _ = ml.rbm.gibbs_sample(flatten_samples(xi),
                                 k, vis_force=flatten_samples(xf), beta=beta)
        xs = unflatten_samples_like(xs, xi)

        xr, xrf = or_rest(vis[:, x_sy:x_ey, x_sx:x_ex], xs)
        yi_by_x = xr[:, i_sy-x_sy:i_ey-x_sy, i_sx-x_sx:i_ex-x_sx]
        yf_by_x = xrf[:, i_sy-x_sy:i_ey-x_sy, i_sx-x_sx:i_ex-x_sx]
        yi[:, i_sy-y_sy:i_ey-y_sy, i_sx-y_sx:i_ex-y_sx] = yi_by_x
        yf[:, i_sy-y_sy:i_ey-y_sy, i_sx-y_sx:i_ex-y_sx] = yf_by_x


        ys, _ = ml.rbm.gibbs_sample(flatten_samples(yi),
                                 k, vis_force=flatten_samples(yf), beta=beta)
        ys = unflatten_samples_like(ys, yi)

        yr, yrf = or_rest(vis[:, y_sy:y_ey, y_sx:y_ex], ys)
        xi_by_y = yr[:, i_sy-y_sy:i_ey-y_sy, i_sx-y_sx:i_ex-y_sx]
        xf_by_y = yrf[:, i_sy-y_sy:i_ey-y_sy, i_sx-y_sx:i_ex-y_sx] #fixed
        xi[:, i_sy-x_sy:i_ey-x_sy, i_sx-x_sx:i_ex-x_sx] = xi_by_y
        xf[:, i_sy-x_sy:i_ey-x_sy, i_sx-x_sx:i_ex-x_sx] = xf_by_y


        # BUG: yf instead of yrf, but worked anyway????

        yield xs, ys
Example #3
0
def cross_entropy(rbm, vis, points, x_shift, y_shift, iters, k, beta=1):

    n_samples = vis.shape[0]
    height = 28
    width = 28

    xi = vis.copy()[:, 0:height, 0:width]
    xf = 1 - xi

    yi = vis.copy()[:, y_shift : y_shift + height, x_shift : x_shift + width]
    yf = 1 - yi

    # print "old cross entropy"

    H = 0
    for n in range(points):
        for i in range(iters):
            xs, _ = ml.rbm.gibbs_sample(flatten_samples(xi), k, vis_force=flatten_samples(xf), beta=beta)
            xs = unflatten_samples_like(xs, xi)
            xr, xrf = or_rest(vis[:, 0:height, 0:width], xs)

            yi_by_x = xr[:, y_shift : y_shift + height, x_shift : x_shift + width]
            yf_by_x = xrf[:, y_shift : y_shift + height, x_shift : x_shift + width]
            yi[:, 0 : height - y_shift, 0 : width - x_shift] = yi_by_x
            yf[:, 0 : height - y_shift, 0 : width - x_shift] = yf_by_x

            ys, _ = ml.rbm.gibbs_sample(flatten_samples(yi), k, vis_force=flatten_samples(yf), beta=beta)
            ys = unflatten_samples_like(ys, yi)
            yr, yrf = or_rest(vis[:, y_shift : y_shift + height, x_shift : x_shift + width], ys)

            xi_by_y = yr[:, 0 : height - y_shift, 0 : width - x_shift]
            xf_by_y = yf[:, 0 : height - y_shift, 0 : width - x_shift]
            xi[:, y_shift:, x_shift:] = xi_by_y
            xf[:, y_shift:, x_shift:] = xf_by_y

        H += rbm.free_energy(flatten_samples(xs), beta=beta) + ml.rbm.free_energy(flatten_samples(ys), beta=beta)

    return H