Пример #1
0
def test_stick_transition():
    K = 3
    D = 2
    M = 0

    model = HMM(K=K, D=D, M=M, transition='sticky', observation='gaussian', transition_kwargs=dict(alpha=1, kappa=100))
    print("alpha = {}, kappa = {}".format(model.transition.alpha, model.transition.kappa))

    data = torch.tensor([[2,3], [4,5], [6, 7]], dtype=torch.float64)

    log_prob = model.log_likelihood(data)
    print("log_prob", log_prob)

    samples_z, samples_x = model.sample(10)

    samples_log_prob = model.log_probability(samples_x)
    print("sample log_prob", samples_log_prob)

    print("model transition\n", model.transition.stationary_transition_matrix)

    model1 = HMM(K=K, D=D, M=M, transition='sticky', observation='gaussian', transition_kwargs=dict(alpha=1, kappa=0.1))

    print("Before fit")
    print("model1 transition\n", model1.transition.stationary_transition_matrix)

    losses, opt = model1.fit(samples_x)
    print("After fit")
    print("model1 transition\n", model1.transition.stationary_transition_matrix)

    model2 = HMM(K=K, D=D, M=M, transition='sticky', observation='gaussian', transition_kwargs=dict(alpha=1, kappa=20))

    print("Before fit")
    print("model2 transition\n", model2.transition.stationary_transition_matrix)

    losses, opt = model2.fit(samples_x)
    print("After fit")
    print("model2 transition\n", model2.transition.stationary_transition_matrix)

    model3 = HMM(K=K, D=D, M=M, transition='sticky', observation='gaussian', transition_kwargs=dict(alpha=1, kappa=100))

    print("Before fit")
    print("model3 transition\n", model3.transition.stationary_transition_matrix)

    losses, opt = model3.fit(samples_x)
    print("After fit")
    print("model3 transition\n", model3.transition.stationary_transition_matrix)
Пример #2
0
    data[:-1])

out = model.log_likelihood(data,
                           momentum_vecs=momentum_vecs,
                           interaction_vecs=interaction_vecs)
print(out)

##################### training ############################

num_iters = 10
losses, opt = model.fit(data,
                        num_iters=num_iters,
                        lr=0.001,
                        momentum_vecs=momentum_vecs,
                        interaction_vecs=interaction_vecs)

##################### sampling ############################
print("start sampling")
sample_z, sample_x = model.sample(30)

#################### inference ###########################
print("inferiring most likely states...")
z = model.most_likely_states(data,
                             momentum_vecs=momentum_vecs,
                             interaction_vecs=interaction_vecs)

#print("k step prediction")
#x_predict = k_step_prediction_for_coupled_momentum_model(model, z, data, momentum_vecs=momentum_vecs, features=features)
#x_predict = k_step_prediction(model, z, data, 10)
# TODO: need to revise the k-step prediction, specifically the way to calculate the momentum
Пример #3
0
from hips.plotting.colormaps import gradient_cmap, white_to_color_cmap

color_names = [
    "windows blue", "red", "amber", "faded green", "dusty purple", "orange"
]

colors = sns.xkcd_palette(color_names)
cmap = gradient_cmap(colors)

npr.seed(0)
torch.manual_seed(0)

K = 3
D = 2
M = 1
T = 100

# Create an exogenous input
inpt = np.sin(2 * np.pi * np.arange(T) / 50)[:, None] + 1e-1 * npr.randn(T, M)
inpt = torch.tensor(inpt, dtype=torch.float64)

true_model = HMM(K=K,
                 D=D,
                 M=M,
                 transition='inputdriven',
                 observation='gaussian')

z, data = true_model.sample(T, input=inpt, return_np=True)

lls = true_model.log_likelihood(data, inpt)
Пример #4
0
import matplotlib.pyplot as plt

torch.manual_seed(0)
np.random.seed(0)

# test fitting

K = 3
D = 2
lags= 5

trans1 = LinearTransformation(K=K, D=D, lags=lags)
obs1 = ARGaussianObservation(K=K, D=D, transformation=trans1)
model1 = HMM(K=K,D=D, observation=obs1)

T = 100
sample_z, sample_x = model1.sample(T)

model2 = HMM(K=K, D=D, observation='gaussian', observation_kwargs=dict(lags=lags))

lls, opt = model2.fit(sample_x, num_iters=2000, lr=0.001)

z_infer = model2.most_likely_states(sample_x)

x_predict = k_step_prediction(model2, z_infer, sample_x)

plt.figure()
plt.plot(x_predict[:,0], label='prediction')
plt.plot(sample_x[:,0], label='truth')
plt.show()
Пример #5
0
def test_model():
    torch.manual_seed(0)
    np.random.seed(0)

    T = 100
    D = 4

    # data = np.array([[1.0, 1.0, 1.0, 6.0], [3.0, 6.0, 8.0, 6.0],
    #                [4.0, 7.0, 8.0, 5.0], [6.0, 7.0, 5.0, 6.0], [8.0, 2.0, 6.0, 1.0]])
    data = np.random.randn(T, D)
    data = torch.tensor(data, dtype=torch.float64)

    xmax = max(np.max(data[:, 0].numpy()), np.max(data[:, 2].numpy()))
    xmin = min(np.min(data[:, 0].numpy()), np.min(data[:, 2].numpy()))
    ymax = max(np.max(data[:, 1].numpy()), np.max(data[:, 3].numpy()))
    ymin = min(np.min(data[:, 1].numpy()), np.min(data[:, 3].numpy()))
    bounds = np.array([[xmin - 1, xmax + 1], [ymin - 1, ymax + 1],
                       [xmin - 1, xmax + 1], [ymin - 1, ymax + 1]])

    def toy_feature_vec_func(s):
        """
        :param s: self, (T, 2)
        :param o: other, (T, 2)
        :return: features, (T, Df, 2)
        """
        corners = torch.tensor([[0, 0], [0, 8], [10, 0], [10, 8]],
                               dtype=torch.float64)
        return feature_direction_vec(s, corners)

    K = 3

    Df = 4
    lags = 1

    tran = UniLSTMTransformation(K=K,
                                 D=D,
                                 Df=Df,
                                 feature_vec_func=toy_feature_vec_func,
                                 lags=lags,
                                 dh=10)

    # observation
    obs = ARTruncatedNormalObservation(K=K,
                                       D=D,
                                       lags=lags,
                                       bounds=bounds,
                                       transformation=tran)

    # model
    model = HMM(K=K, D=D, observation=obs)

    print("calculating log likelihood")
    feature_vecs_a = toy_feature_vec_func(data[:-1, 0:2])
    feature_vecs_b = toy_feature_vec_func(data[:-1, 2:4])
    feature_vecs = (feature_vecs_a, feature_vecs_b)
    packed_data = get_packed_data((data[:-1]), lags=lags)

    model.log_likelihood(data,
                         feature_vecs=feature_vecs,
                         packed_data=packed_data)

    # fit
    losses, _ = model.fit(data,
                          optimizer=None,
                          method="adam",
                          num_iters=50,
                          feature_vecs=feature_vecs,
                          packed_data=packed_data)

    plt.figure()
    plt.plot(losses)
    plt.show()

    # most-likely-z
    print("Most likely z...")
    z = model.most_likely_states(data,
                                 feature_vecs=feature_vecs,
                                 packed_data=packed_data)

    # prediction

    if data.shape[0] <= 1000:
        data_to_predict = data
    else:
        data_to_predict = data[-1000:]

    print("0 step prediction")
    if data.shape[0] <= 1000:
        data_to_predict = data
    else:
        data_to_predict = data[-1000:]
    x_predict = k_step_prediction_for_lstm_model(model,
                                                 z,
                                                 data_to_predict,
                                                 feature_vecs=feature_vecs)
    x_predict_err = np.mean(np.abs(x_predict - data_to_predict.numpy()),
                            axis=0)

    print("10 step prediction")
    x_predict_2 = k_step_prediction(model, z, data_to_predict, k=10)
    x_predict_2_err = np.mean(np.abs(x_predict_2 -
                                     data_to_predict[10:].numpy()),
                              axis=0)

    # samples
    print("sampling...")
    sample_T = 5
    sample_z, sample_x = model.sample(sample_T)
def test_model():
    torch.manual_seed(0)
    np.random.seed(0)

    T = 5
    x_grids = np.array([0.0, 5.0, 10.0])
    y_grids = np.array([0.0, 4.0, 8.0])

    data = np.array([[1.0, 1.0, 1.0, 6.0], [3.0, 6.0, 8.0, 6.0],
                     [4.0, 7.0, 8.0, 5.0], [6.0, 7.0, 5.0, 6.0],
                     [8.0, 2.0, 6.0, 1.0]])
    data = torch.tensor(data, dtype=torch.float64)

    def toy_feature_vec_func(s):
        """
        :param s: self, (T, 2)
        :param o: other, (T, 2)
        :return: features, (T, Df, 2)
        """
        corners = torch.tensor([[0, 0], [0, 8], [10, 0], [10, 8]],
                               dtype=torch.float64)
        return feature_direction_vec(s, corners)

    K = 3
    D = 4
    M = 0

    Df = 4

    bounds = np.array([[0.0, 10.0], [0.0, 8.0], [0.0, 10.0], [0.0, 8.0]])
    tran = LinearGridTransformation(K=K,
                                    D=D,
                                    x_grids=x_grids,
                                    y_grids=y_grids,
                                    Df=Df,
                                    feature_vec_func=toy_feature_vec_func)
    obs = ARTruncatedNormalObservation(K=K,
                                       D=D,
                                       M=0,
                                       lags=1,
                                       bounds=bounds,
                                       transformation=tran)

    model = HMM(K=K, D=D, M=M, transition="stationary", observation=obs)
    model.observation.mus_init = data[0] * torch.ones(
        K, D, dtype=torch.float64)

    # calculate memory
    gridpoints_idx_a = tran.get_gridpoints_idx_for_batch(data[:-1, 0:2])
    gridpoints_idx_b = tran.get_gridpoints_idx_for_batch(data[:-1, 2:4])
    gridpoints_a = tran.get_gridpoints_for_batch(gridpoints_idx_a)
    gridpoints_b = tran.get_gridpoints_for_batch(gridpoints_idx_b)
    feature_vecs_a = toy_feature_vec_func(data[:-1, 0:2])
    feature_vecs_b = toy_feature_vec_func(data[:-1, 2:4])

    gridpoints_idx = (gridpoints_idx_a, gridpoints_idx_b)
    gridpoints = (gridpoints_a, gridpoints_b)
    feature_vecs = (feature_vecs_a, feature_vecs_b)

    # fit
    losses, opt = model.fit(data,
                            optimizer=None,
                            method='adam',
                            num_iters=100,
                            lr=0.01,
                            pbar_update_interval=10,
                            gridpoints=gridpoints,
                            gridpoints_idx=gridpoints_idx,
                            feature_vecs=feature_vecs)

    plt.figure()
    plt.plot(losses)
    plt.show()

    # most-likely-z
    print("Most likely z...")
    z = model.most_likely_states(data,
                                 gridpoints_idx=gridpoints_idx,
                                 feature_vecs=feature_vecs)

    # prediction
    print("0 step prediction")
    if data.shape[0] <= 1000:
        data_to_predict = data
    else:
        data_to_predict = data[-1000:]
    x_predict = k_step_prediction_for_lineargrid_model(
        model,
        z,
        data_to_predict,
        gridpoints_idx=gridpoints_idx,
        feature_vecs=feature_vecs)
    x_predict_err = np.mean(np.abs(x_predict - data_to_predict.numpy()),
                            axis=0)

    print("2 step prediction")
    x_predict_2 = k_step_prediction(model, z, data_to_predict, k=2)
    x_predict_2_err = np.mean(np.abs(x_predict_2 -
                                     data_to_predict[2:].numpy()),
                              axis=0)

    # samples
    sample_T = 5
    sample_z, sample_x = model.sample(sample_T)
Пример #7
0
def test_model():
    torch.manual_seed(0)
    np.random.seed(0)

    T = 5
    x_grids = np.array([0.0, 10.0])
    y_grids = np.array([0.0, 8.0])

    bounds = np.array([[0.0, 10.0], [0.0, 8.0], [0.0, 10.0], [0.0, 8.0]])
    data = np.array([[1.0, 1.0, 1.0, 6.0], [3.0, 6.0, 8.0, 6.0],
                     [4.0, 7.0, 8.0, 5.0], [6.0, 7.0, 5.0, 6.0],
                     [8.0, 2.0, 6.0, 1.0]])
    data = torch.tensor(data, dtype=torch.float64)

    K = 3
    D = 4
    M = 0

    obs = GPObservation(K=K,
                        D=D,
                        x_grids=x_grids,
                        y_grids=y_grids,
                        bounds=bounds,
                        train_rs=True)

    correct_kerneldist_gg = torch.tensor(
        [[0., 0., 64., 64., 100., 100., 164., 164.],
         [0., 0., 64., 64., 100., 100., 164., 164.],
         [64., 64., 0., 0., 164., 164., 100., 100.],
         [64., 64., 0., 0., 164., 164., 100., 100.],
         [100., 100., 164., 164., 0., 0., 64., 64.],
         [100., 100., 164., 164., 0., 0., 64., 64.],
         [164., 164., 100., 100., 64., 64., 0., 0.],
         [164., 164., 100., 100., 64., 64., 0., 0.]],
        dtype=torch.float64)
    assert torch.all(torch.eq(correct_kerneldist_gg,
                              obs.kernel_distsq_gg)), obs.kernel_distsq_gg

    log_prob_nocache = obs.log_prob(data)
    print("log_prob_nocache = {}".format(log_prob_nocache))

    kernel_distsq_xg_a = kernel_distsq_doubled(data[:-1, 0:2],
                                               obs.inducing_points)
    kernel_distsq_xg_b = kernel_distsq_doubled(data[:-1, 2:4],
                                               obs.inducing_points)

    correct_kernel_distsq_xg_a = torch.tensor(
        [[2., 2., 50., 50., 82., 82., 130., 130.],
         [2., 2., 50., 50., 82., 82., 130., 130.],
         [45., 45., 13., 13., 85., 85., 53., 53.],
         [45., 45., 13., 13., 85., 85., 53., 53.],
         [65., 65., 17., 17., 85., 85., 37., 37.],
         [65., 65., 17., 17., 85., 85., 37., 37.],
         [85., 85., 37., 37., 65., 65., 17., 17.],
         [85., 85., 37., 37., 65., 65., 17., 17.]],
        dtype=torch.float64)
    assert torch.all(torch.eq(correct_kernel_distsq_xg_a,
                              kernel_distsq_xg_a)), kernel_distsq_xg_a

    memory_kwargs = dict(kernel_distsq_xg_a=kernel_distsq_xg_a,
                         kernel_distsq_xg_b=kernel_distsq_xg_b)

    log_prob = obs.log_prob(data, **memory_kwargs)
    print("log_prob = {}".format(log_prob))

    assert torch.all(torch.eq(log_prob_nocache, log_prob))

    Sigma_a, A_a = obs.get_gp_cache(data[:-1, 0:2], 0, **memory_kwargs)
    Sigma_b, A_b = obs.get_gp_cache(data[:-1, 2:4], 1, **memory_kwargs)
    memory_kwargs_2 = dict(Sigma_a=Sigma_a, A_a=A_a, Sigma_b=Sigma_b, A_b=A_b)

    print("calculating log prob 2...")
    log_prob2 = obs.log_prob(data, **memory_kwargs_2)
    assert torch.all(torch.eq(log_prob, log_prob2))

    model = HMM(K=K, D=D, M=M, transition="stationary", observation=obs)
    model.observation.mus_init = data[0] * torch.ones(
        K, D, dtype=torch.float64)

    # fit
    losses, opt = model.fit(data,
                            optimizer=None,
                            method='adam',
                            num_iters=100,
                            lr=0.01,
                            pbar_update_interval=10,
                            **memory_kwargs)

    plt.figure()
    plt.plot(losses)
    plt.show()

    # most-likely-z
    print("Most likely z...")
    z = model.most_likely_states(data, **memory_kwargs)

    # prediction
    print("0 step prediction")
    if data.shape[0] <= 1000:
        data_to_predict = data
    else:
        data_to_predict = data[-1000:]
    x_predict = k_step_prediction_for_gpmodel(model, z, data_to_predict,
                                              **memory_kwargs)
    x_predict_err = np.mean(np.abs(x_predict - data_to_predict.numpy()),
                            axis=0)

    print("2 step prediction")
    x_predict_2 = k_step_prediction(model, z, data_to_predict, k=2)
    x_predict_2_err = np.mean(np.abs(x_predict_2 -
                                     data_to_predict[2:].numpy()),
                              axis=0)

    # samples
    sample_T = 5
    sample_z, sample_x = model.sample(sample_T)
Пример #8
0
        pbar.update(10)

x_reconstruct = model.sample_condition_on_zs(z, data[0])

"""

# test momentum_lags

true_observation = ARGaussianObservation(K=K,
                                         D=D,
                                         M=0,
                                         lags=5,
                                         transformation='linear')
true_model = HMM(K=K, D=D, M=0, observation=true_observation)

z, data = true_model.sample(T, return_np=True)

# fit to a model
observation = ARGaussianObservation(K=K,
                                    D=D,
                                    M=0,
                                    lags=5,
                                    transformation='linear')
model = HMM(K=K, D=D, M=0, observation=observation)

num_iters = 10000

pbar = tqdm(total=num_iters, file=sys.stdout)

optimizer = torch.optim.Adam(model.params, lr=0.0001)
bounds = np.array([[0, 20], [0, 40]])
thetas = np.linspace(0, 2 * np.pi, K, endpoint=False)
mus_init = 3 * np.column_stack((np.cos(thetas), np.sin(thetas))) + 5

true_tran = LinearTransformation(K=K, D=D, lags=lags)
true_observation = ARTruncatedNormalObservation(K=K,
                                                D=D,
                                                M=0,
                                                lags=lags,
                                                transformation=true_tran,
                                                bounds=bounds,
                                                mus_init=mus_init,
                                                train_sigma=False)
true_model = HMM(K=K, D=D, M=0, observation=true_observation)

z, x = true_model.sample(T, return_np=False)
true_ll = true_model.log_likelihood(x)

print(true_ll)

print("\n # model parameters: \n", len(true_model.params))

print("\n # model trainable parameters: \n", len(true_model.trainable_params))

sample_z, sample_x = true_model.sample(T)
"""
# learning

tran = LinearTransformation(K=K, D=D, momentum_lags=1, As=As)
observation = ARTruncatedNormalObservation(K=K, D=D, M=0, momentum_lags=1, with_noise=tran, bounds=bounds, mus_init=mus_init)
model = HMM(K=K, D=D, M=0, observation=true_observation)
# model
model = HMM(K=K, D=D, M=M, observation=obs)

# log like
log_prob = model.log_probability(data)
print("log probability = ", log_prob)

# training
print("start training...")
num_iters = 10
losses, opt = model.fit(data, num_iters=num_iters, lr=0.001)

# sampling
samplt_T = T
print("start sampling")
sample_z, sample_x = model.sample(samplt_T)

print("start sampling based on with_noise")
sample_z2, sample_x2 = model.sample(T, with_noise=True)

# inference
print("inferiring most likely states...")
z = model.most_likely_states(data)

print("0 step prediction")
x_predict = k_step_prediction(model, z, data)

print("k step prediction")
x_predict_10 = k_step_prediction(model, z, data, 2)
D = 2
T = 100

As = [random_rotation(D) for _ in range(K)]
true_tran = LinearTransformation(K=K, d_in=D, D=D, As=As)

bounds = np.array([[0, 20], [-5, 25]])
true_observation = ARLogitNormalObservation(K=K,
                                            D=D,
                                            M=0,
                                            transformation=true_tran,
                                            bounds=bounds)

true_model = HMM(K=K, D=D, M=0, observation=true_observation)

z, data = true_model.sample(T, return_np=False)

# Define a model to fit the data

tran = LinearTransformation(K=K, d_in=D, D=D)
observation = ARLogitNormalObservation(K=K,
                                       D=D,
                                       M=0,
                                       transformation=tran,
                                       bounds=bounds)
model = HMM(K=K, D=D, M=0, observation=observation)

# Model fitting

num_iters = 10  #9000
                                   M=M,
                                   lags=1,
                                   bounds=bounds,
                                   transformation=tran)

# model
model = HMM(K=K, D=D, M=M, observation=obs)

#model.observation.mus_init = data[0] * torch.ones(K, D, dtype=torch.float64)

params = joblib.load(
    "/Users/leah/Columbia/courses/19summer/SocialBehavior/SocialBehaviorptc/project_notebooks/gridmodel/model_k2"
)

model.params = params

obs.log_sigmas = torch.log(torch.ones((2, 4), dtype=torch.float64) * 0.01)

center_z = torch.tensor([0], dtype=torch.int)
center_x = torch.tensor([[150, 190, 200, 200]], dtype=torch.float64)

sample_z_center, sample_x_center = model.sample(20,
                                                prefix=(center_z, center_x))

diff = np.diff(sample_x_center, axis=0)

print("diff in dim 0")
print(diff[:, 0])

print("\ndiff in dim 1")
print(diff[:, 1])