Exemplo n.º 1
0
def upaintbox_sample(log_res, hold, Y, held_out, ext, sig, sig_w, iterate, K,
                     data_run):
    print("Trial Number: " + str(data_run))
    N, T = Y.shape
    res = 1
    tree = gen_tree(K, res)
    ctree, ptree = tree
    Z = draw_Z_tree(tree, N)
    #W = sample_W(Y,Z,sig,sig_w)
    W = np.reshape(np.random.normal(0, sig_w, K * T), (K, T))
    ll_list = []
    iter_time = []
    f_count = []
    lapse_data = []
    pred_ll = []
    pred = 0
    for it in range(iterate):
        if it % hold == 0:
            if res < 2**log_res:
                res = res * 2

        start = time.time()
        N, K = Z.shape
        #sample Z
        Z, prob_matrix = sample_Z(Y, Z, W, sig, sig_w, tree)
        #        if it%10 == 0:
        #            print("iteration: " + str(it))
        #            print("Sparsity: " + str(np.sum(Z,axis=0)))
        #            print('predictive log likelihood: ' + str(pred))
        #sample paintbox
        tree, lapse = sample_pb(Z, tree, res)
        #sample W
        W = sample_W(Y, Z, sig, sig_w)
        #add new features
        ll_list.append(log_data_zw(Y, Z, W, sig))
        F, D = get_FD(tree)
        f_count.append(F)
        #predictive log likelihood
        #        if it%500 == 0:
        #            pred = pred_ll_paintbox(held_out, W, tree, sig)
        #            pred_ll.append(pred)
        #        if it%1000 == 0 and it > 0:
        #            display_W(W,3,3,3,3,'four')
        #handling last iteration edge case
        drop = 0
        if it == iterate - 1:
            drop = 1
        Z, W, tree = new_feature(Y, Z, W, tree, ext, K, res, sig, sig_w, drop)
        end = time.time()
        iter_time.append(end - start)
        lapse_data.append(lapse)
    iter_time = np.cumsum(iter_time)
    return (ll_list, iter_time, f_count, lapse_data, Z, W, prob_matrix,
            pred_ll, tree)
Exemplo n.º 2
0
def sample_recover(held,observe,W,tree,sig,obs_indices):
    N,obs = observe.shape 
    R,T = held.shape
    K,T = W.shape
    log_recover = 0
    vec = get_vec(tree)
    W_obs = W[:,obs_indices]
    #initialize Z
    Z = draw_Z_tree(tree,R)
    indices = [i for i in range(T)]
    hidden = [x for x in indices if x not in obs_indices]
    #print(hidden)
    iterate = 100
    for it in range(iterate):         
        N,K = Z.shape
        #sample Z
        Z,prob_matrix = sample_Z(held[:,obs_indices],Z,W_obs,sig,tree)
    #print("sampled Z")
    #print(Z)
    for row in Z:
        log_recover += np.log(Z_paintbox(row,vec))
    log_recover += log_data_zw(held[:,hidden],Z,W[:,hidden],sig) 
    return log_recover
Exemplo n.º 3
0
def ugibbs_sample(Y, ext, sig, sig_w, iterate, K, data_run):
    print("Trial Number: " + str(data_run))
    N, T = Y.shape
    tree = gen_tree(K, res)
    ctree, ptree = tree
    Z = draw_Z_tree(tree, N)
    #W = sample_W(Y,Z,sig,sig_w)
    W = np.reshape(np.random.normal(0, sig_w, K * T), (K, T))
    ll_list = []
    iter_time = []
    f_count = []
    for it in range(iterate):

        start = time.time()
        N, K = Z.shape
        #sample Z
        Z = sample_Z(Y, Z, W, sig, sig_w, tree)
        if it % 10 == 0:
            print("iteration: " + str(it))
            print("Sparsity: " + str(np.sum(Z, axis=0)))
        #sample paintbox
        tree = sample_pb(Z, tree, res)
        #ctree,ptree = tree
        #sample W
        W = sample_W(Y, Z, sig, sig_w)
        #add new features
        vec = get_vec(tree)
        #ll_list.append(log_data_zw(Y,Z,W,sig) + Z_vec(Z,vec) + log_w_sig(W,sig))
        ll_list.append(log_data_zw(Y, Z, W, sig))
        F, D = get_FD(tree)
        f_count.append(F)
        Z, W, tree = new_feature(Y, Z, W, tree, ext, K, res, sig, sig_w)
        end = time.time()
        iter_time.append(end - start)
    iter_time = np.cumsum(iter_time)
    return (ll_list, iter_time, f_count, Z, W)
Exemplo n.º 4
0
def upaintbox_sample(log_res,hold,Y,held_out,ext,sig,sig_w,iterate,K,truncate,obs_indices,limit,Z_init=[],W_init=[],data_dim = [3,3,2,2],init=False,display=False):
    #print('time limit')
    #print(limit)
    small_x,small_y,big_x,big_y = data_dim
    N,T = Y.shape
    #technically this is a bug
    #generating tree with res = 1 is wrong.  
    #but you fixed it in tree paintbox hard coded 0.5 
    res = 1
    tree = gen_tree(K,res)
    ctree,ptree = tree
    if init:
        Z = draw_Z_tree(tree,N)
        #Z = Z_init
        W = W_init
    else:
        Z = draw_Z_tree(tree,N)
        W = np.reshape(np.random.normal(0,sig_w,K*T),(K,T))
    #Z = np.loadtxt('assignments.txt')
    #print(Z)
    #W = sample_W(Y,Z,sig,sig_w)
    #W = np.loadtxt('features.txt')
#    full = generate_gg_blocks()
#    W = np.zeros((3,T))
#    W[0,:] = full[0,:]
#    W[1,:] = full[2,:]
#    W[2,:] = full[0,:] + full[2,:]
#    display_W(W,'four')
    ll_list = [] 
    iter_time = [] 
    f_count = [] 
    lapse_data = [] 
    pred_ll = []
    pred = 0
    rec_ll = []
    rec = 0
    observe = held_out[:,obs_indices]
    for redo in range(1):
        if redo == 1:
            res = 1
            N,K = Z.shape
            tree = gen_tree(K,res)
            ctree,ptree = tree
        for it in range(iterate):
            if it == 0:
                start = time.time()
            if it > 0:
                #print(np.sum(iter_time))
                #print(limit)
                if np.sum(iter_time) > limit:
                    break     
            if it%hold == 0:
                if res < 2**log_res:
                    res = res*2     
            start = time.time()
            N,K = Z.shape
            #sample Z
            Z,prob_matrix = sample_Z(Y,Z,W,sig,tree)
            if it%10 == 0 and display:
                print("iteration: " + str(it))
                print("Sparsity: " + str(np.sum(Z,axis=0)))
                #print('predictive log likelihood: ' + str(pred))
                print('recover log likelihood: ' + str(rec))
            #sample paintbox
            tree,lapse = sample_pb(Z,tree,res)
            #sample W        
            W = sample_W(Y,Z,sig,sig_w)
            #add new features
            ll_list.append(log_data_zw(Y,Z,W,sig))
            F,D = get_FD(tree)
            f_count.append(F)
            #recovered log likelihood
            if it%50 == 49 and it > 0:
                #pred = pred_ll_paintbox(held_out, W, tree, sig)
                pred = 0
                pred_ll.append(pred)
                #rec = sample_recover(held_out,observe,W,tree,sig,obs_indices)
                rec = recover_paintbox(held_out,observe,W,tree,sig,obs_indices)
                rec_ll.append(rec)
                end = time.time()
                iter_time.append(end - start)
                start = time.time()
            #Auxiliary printouts
            #if it%500 == 0 and it > 0:
            #    print_paintbox(tree,W,data_dim,'four')
            #if it%200 == 0 and it > 0:
            #    display_W(W,data_dim,'nine')
            #handling last iteration edge case
            drop = 0
            if it == iterate - 1:
                drop = 1
            Z,W,tree = new_feature(Y,Z,W,tree,ext,K,res,sig,sig_w,drop,truncate)
            lapse_data.append(lapse)
    iter_time = np.cumsum(iter_time)
    return (ll_list,iter_time,f_count,lapse_data,Z,W,prob_matrix,pred_ll,rec_ll,tree)