Example #1
0
def generate_H_V_T(epsilon,L,vo,q_point):

    metrico = metric("unit_e",vo)
    Ho = Hamiltonian(vo, metrico)
    q = q_point.point_clone()
    p = Ho.T.generate_momentum(q)
    H_0_out = Ho.evaluate(q,p)
    H_list = [H_0_out["H"]]
    V_list = [H_0_out["V"]]
    T_list = [H_0_out["T"]]
    for _ in range(L):
        print("iter {}".format(_))
        q,p,stat = abstract_leapfrog_ult(q, p, epsilon, Ho)
        if stat["explode_grad"]:
            break

        # out = abstract_leapfrog_ult(q,p,epsilon,Ho)
        # out = abstract_NUTS(q,epsilon,Ham,abstract_leapfrog_ult,5)
        H_out = Ho.evaluate(q, p)
        current_H = H_out["H"]
        current_T = H_out["T"]
        current_V = H_out["V"]
        if abs(current_H - H_list[0]) > 1000:
            break
        print("current V is {}".format(current_V))
        print("current H {}".format(current_H))
        print("current T {}".format(current_T))
        # current_V, current_T, current_H = Ho.evaluate_all(q, p)
        # print("current H2 {}".format(Ho.evaluate(q,p)))
        H_list.append(current_H)
        V_list.append(current_V)
        T_list.append(current_T)

    out = {"H_list":H_list,"V_list":V_list,"T_list":T_list}
    return(out)
Example #2
0
    else:
        return((V(q)+T(p)))

# first verify they have the same Hamiltonian function
print("exact H {}".format(H(q,p,True)))

v_obj = V_pima_inidan_logit()
metric_obj = metric("unit_e",v_obj)
Ham = Hamiltonian(v_obj,metric_obj)
q_point = Ham.V.q_point.point_clone()
p_point = Ham.T.p_point.point_clone()

q_point.flattened_tensor.copy_(inputq)
p_point.flattened_tensor.copy_(inputp)

print("abstract H {}".format(Ham.evaluate(q_point,p_point)))

print("input q diff{}".format((q.data-q_point.flattened_tensor).sum()))
print("input p diff {}".format((p.data-p_point.flattened_tensor).sum()))

L=10
for i in range(L):
    outq,outp = leapfrog_ult(q,p,0.1,H)
    outq_a,outp_a,stat = abstract_leapfrog_ult(q_point,p_point,0.1,Ham)
    q, p = outq, outp
    q_point, p_point = outq_a, outp_a
diffq = ((outq.data - outq_a.flattened_tensor)*(outq.data - outq_a.flattened_tensor)).sum()
diffp = ((outp.data - outp_a.flattened_tensor)*(outp.data - outp_a.flattened_tensor)).sum()
print("diff outq {}".format(diffq))
print("diff outp {}".format(diffp))