Ejemplo n.º 1
0
def sample_slds_model():
    mu_init = np.zeros(D_latent)
    mu_init[0] = 2.0
    sigma_init = 0.01 * np.eye(D_latent)

    def random_rotation(n, theta):
        rot = 0.99 * np.array([[np.cos(theta), -np.sin(theta)],
                               [np.sin(theta), np.cos(theta)]])
        out = np.zeros((n, n))
        out[:2, :2] = rot
        q = np.linalg.qr(np.random.randn(n, n))[0]
        return q.dot(out).dot(q.T)

    def random_dynamics(n):
        A = np.random.randn(n,n)
        A = A.dot(A.T)
        U,S,V = np.linalg.svd(A)
        A_stable = U.dot(np.diag(S/(1.1*np.max(S)))).dot(V.T)
        # A_stable = U.dot(0.99 * np.eye(n)).dot(V.T)
        return A_stable

    ths = np.linspace(0, np.pi/8., K)
    As = [random_rotation(D_latent, ths[k]) for k in range(K)]
    # As = [random_dynamics(D_latent) for k in range(K)]
    bs = [np.zeros((D_latent, 1))] + [.25 * np.random.randn(D_latent, 1) for k in range(K-1)]

    C = np.random.randn(D_obs, D_latent)
    d = np.zeros((D_obs, 1))
    sigma_obs = 0.5 * np.ones(D_obs)

    ###################
    #  generate data  #
    ###################
    init_dynamics_distns = [Gaussian(mu=mu_init, sigma=sigma_init) for _ in range(K)]
    dynamics_distns = [Regression(A=np.hstack((A, b)), sigma=0.01 * np.eye(D_latent)) for A,b in zip(As, bs)]
    emission_distns = DiagonalRegression(D_obs, D_latent+1, A=np.hstack((C, d)), sigmasq=sigma_obs)

    slds = HMMSLDS(
        dynamics_distns=dynamics_distns,
        emission_distns=emission_distns,
        init_dynamics_distns=init_dynamics_distns,
        alpha=3., init_state_distn='uniform')

    #### MANUALLY CREATE DATA
    P = np.ones((K,K)) + 1 * np.eye(K)
    P = P / np.sum(P,1,keepdims=True)
    z = np.zeros(T//D, dtype=np.int32)
    for t in range(1,T//D):
        z[t] = np.random.choice(np.arange(K), p=P[z[t-1]])
    z = np.repeat(z, D)

    statesobj = slds._states_class(model=slds, T=z.size, stateseq=z, inputs=np.ones((z.size, 1)))
    statesobj.generate_gaussian_states()
    y = statesobj.data = statesobj.generate_obs()
    x = statesobj.gaussian_states
    slds.states_list.append(statesobj)

    return z,x,y,slds
Ejemplo n.º 2
0
def sample_slds_model():
    mu_init = np.zeros(D_latent)
    mu_init[0] = 2.0
    sigma_init = 0.01 * np.eye(D_latent)

    def random_rotation(n, theta):
        rot = 0.99 * np.array([[np.cos(theta), -np.sin(theta)],
                               [np.sin(theta), np.cos(theta)]])
        out = np.zeros((n, n))
        out[:2, :2] = rot
        q = np.linalg.qr(np.random.randn(n, n))[0]
        return q.dot(out).dot(q.T)

    def random_dynamics(n):
        A = np.random.randn(n,n)
        A = A.dot(A.T)
        U,S,V = np.linalg.svd(A)
        A_stable = U.dot(np.diag(S/(1.1*np.max(S)))).dot(V.T)
        # A_stable = U.dot(0.99 * np.eye(n)).dot(V.T)
        return A_stable

    ths = np.linspace(0, np.pi/8., K)
    As = [random_rotation(D_latent, ths[k]) for k in range(K)]
    # As = [random_dynamics(D_latent) for k in range(K)]
    bs = [np.zeros((D_latent, 1))] + [.25 * np.random.randn(D_latent, 1) for k in range(K-1)]

    C = np.random.randn(D_obs, D_latent)
    d = np.zeros((D_obs, 1))
    sigma_obs = 0.5 * np.ones(D_obs)

    ###################
    #  generate data  #
    ###################
    init_dynamics_distns = [Gaussian(mu=mu_init, sigma=sigma_init) for _ in range(K)]
    dynamics_distns = [Regression(A=np.hstack((A, b)), sigma=0.01 * np.eye(D_latent)) for A,b in zip(As, bs)]
    emission_distns = DiagonalRegression(D_obs, D_latent+1, A=np.hstack((C, d)), sigmasq=sigma_obs)

    slds = HMMSLDS(
        dynamics_distns=dynamics_distns,
        emission_distns=emission_distns,
        init_dynamics_distns=init_dynamics_distns,
        alpha=3., init_state_distn='uniform')

    #### MANUALLY CREATE DATA
    P = np.ones((K,K)) + 1 * np.eye(K)
    P = P / np.sum(P,1,keepdims=True)
    z = np.zeros(T//D, dtype=np.int32)
    for t in range(1,T//D):
        z[t] = np.random.choice(np.arange(K), p=P[z[t-1]])
    z = np.repeat(z, D)

    statesobj = slds._states_class(model=slds, T=z.size, stateseq=z, inputs=np.ones((z.size, 1)))
    y = statesobj.data = statesobj.generate_obs()
    x = statesobj.gaussian_states
    slds.states_list.append(statesobj)

    return z,x,y,slds
Ejemplo n.º 3
0
def fit_slds(inputs, z_init, x_init, y, mask, C_init):
    print("Fitting standard SLDS")
    init_dynamics_distns, dynamics_distns, emission_distns = \
        make_rslds_parameters(C_init)

    slds = HMMSLDS(init_state_distn='uniform',
                   init_dynamics_distns=init_dynamics_distns,
                   dynamics_distns=dynamics_distns,
                   emission_distns=emission_distns,
                   alpha=3.)

    slds.add_data(y, inputs=inputs, mask=mask)

    # Initialize states
    slds.states_list[0].stateseq = z_init.copy().astype(np.int32)
    slds.states_list[0].gaussian_states = x_init.copy()

    # Initialize dynamics
    print("Initializing dynamics with Gibbs sampling")
    for _ in tqdm(range(100)):
        slds.resample_dynamics_distns()

    # Fit the model
    lps = []
    z_smpls = []
    for itr in tqdm(range(args.N_samples)):
        slds.resample_model()
        lps.append(slds.log_likelihood())
        z_smpls.append(slds.stateseqs[0].copy())

    x_test = slds.states_list[0].gaussian_states
    z_smpls = np.array(z_smpls)
    lps = np.array(lps)

    return slds, lps, z_smpls, x_test
Ejemplo n.º 4
0
      0.99 * random_rotation(D_latent, np.pi/12.)]

C = np.random.randn(D_obs, D_latent)
sigma_obs = 0.5 * np.ones(D_obs)


###################
#  generate data  #
###################
init_dynamics_distns = [Gaussian(mu=mu_init, sigma=sigma_init) for _ in range(K)]
dynamics_distns = [Regression(A=A, sigma=np.eye(D_latent)) for A in As]
emission_distns = DiagonalRegression(D_obs, D_latent, A=C, sigmasq=sigma_obs)

truemodel = HMMSLDS(
    dynamics_distns=dynamics_distns,
    emission_distns=emission_distns,
    init_dynamics_distns=init_dynamics_distns,
    alpha=3., init_state_distn='uniform')

# Manually create the states object with the mask
T = 1000
stateseq = np.repeat(np.arange(T//100) % 2, 100).astype(np.int32)
statesobj = truemodel._states_class(model=truemodel, T=stateseq.size, stateseq=stateseq)
statesobj.generate_gaussian_states()
data = statesobj.data = statesobj.generate_obs()
gaussian_states = statesobj.gaussian_states
truemodel.states_list.append(statesobj)

# Mask off a chunk of data
# mask = npr.rand(*data.shape) < 0.5
mask = np.ones_like(data, dtype=bool)
Ejemplo n.º 5
0
Archivo: vbem.py Proyecto: zqwei/pyslds
              for _ in range(Kmax)]  # Emission noise covariances

model = HMMSLDS(init_dynamics_distns=[
    Gaussian(nu_0=5,
             sigma_0=3. * np.eye(D_latent),
             mu_0=np.zeros(D_latent),
             kappa_0=0.01,
             mu=np.zeros(D_latent),
             sigma=np.eye(D_latent)) for _ in range(Kmax)
],
                dynamics_distns=[
                    Regression(
                        A=np.hstack(
                            (np.eye(D_latent), np.zeros((D_latent, D_input)))),
                        sigma=np.eye(D_latent),
                        nu_0=D_latent + 3,
                        S_0=D_latent * np.eye(D_latent),
                        M_0=np.hstack(
                            (np.eye(D_latent), np.zeros((D_latent, D_input)))),
                        K_0=D_latent * np.eye(D_latent + D_input),
                    ) for _ in range(Kmax)
                ],
                emission_distns=DiagonalRegression(
                    D_obs,
                    D_latent + D_input,
                    alpha_0=2.0,
                    beta_0=1.0,
                ),
                alpha=3.,
                init_state_distn='uniform')

model.add_data(data, inputs=np.ones((T, D_input)))
Ejemplo n.º 6
0
#%%
###################
#  generate data  #
###################
# K: x_0 ~ Normal(mu_init, sigma_init)
init_dynamics_distns = [
    Gaussian(mu=mu_init, sigma=sigma_init) for _ in range(K)
]
# K : x_t ~ Normal( A_{z=k} x_t + b_{z=K}, I)
dynamics_distns = [Regression(A=A, sigma=np.eye(D_latent)) for A in As]
#K: y_t | x_t ~ Normal (C_{z=k} x_t + d_z{k=K}, diag(Sigma_obs))
emission_distns = DiagonalRegression(D_obs, D_latent, A=C, sigmasq=sigma_obs)

truemodel = HMMSLDS(dynamics_distns=dynamics_distns,
                    emission_distns=emission_distns,
                    init_dynamics_distns=init_dynamics_distns,
                    alpha=3.,
                    init_state_distn='uniform')

#%%
# Manually create the states object with the mask
T = 1000
# get z is T x 1
stateseq = np.repeat(np.arange(T // 100) % 2, 100).astype(np.int32)
statesobj = truemodel._states_class(model=truemodel,
                                    T=stateseq.size,
                                    stateseq=stateseq)
#statesobj.generate_gaussian_states()
# class HMMSLDSStatesEigen(
#     _SLDSStatesMaskedData,
#     _SLDSStatesGibbs,
Ejemplo n.º 7
0
    AutoRegression(A=np.eye(P), sigma=np.eye(P), nu_0=3, S_0=3.0 * np.eye(P), M_0=np.eye(P), K_0=10.0 * np.eye(P))
    for _ in xrange(Nmax)
]

emission_distns = [
    Regression(A=np.eye(D), sigma=0.05 * np.eye(D), nu_0=5.0, S_0=np.eye(P), M_0=np.eye(P), K_0=10.0 * np.eye(P))
    for _ in xrange(Nmax)
]


init_dynamics_distns = [Gaussian(nu_0=4, sigma_0=4.0 * np.eye(P), mu_0=np.zeros(P), kappa_0=0.1) for _ in xrange(Nmax)]

model = HMMSLDS(
    dynamics_distns=dynamics_distns,
    emission_distns=emission_distns,
    init_dynamics_distns=init_dynamics_distns,
    alpha=3.0,
    init_state_distn="uniform",
)

model.add_data(data)
model.resample_states()
for _ in progprint_xrange(10):
    model.resample_model()
model.states_list[0]._init_mf_from_gibbs()


####################
#  run mean field  #
####################
Ejemplo n.º 8
0
def trainModel(fileName, unit_trial, K=8, xDim=5):
    # unit_trial -- training data
    # randomization
    np.random.seed()

    numTrial, numUnit, numTime = unit_trial.shape

    # factor analysis for initialization
    factor_unit_trial = unit_trial.transpose([0, 2, 1])
    factor_unit_trial = factor_unit_trial.reshape([-1, factor_unit_trial.shape[2]])
    yDim = numUnit
    inputDim = 1 # some constants
    inputs = np.ones((numTime, inputDim))
    estimator = factan(n_components=xDim, tol=0.00001, copy=True,
                       max_iter=10000, noise_variance_init=None,
                       svd_method='randomized', iterated_power=3,
                       random_state=None)
    estimator.fit(factor_unit_trial)
    C_init = estimator.components_.T
    D_init = estimator.mean_.reshape([-1, 1])

    # SLDS fit
    init_dynamics_distns = [Gaussian(nu_0=xDim+3,
                                     sigma_0=3.*np.eye(xDim),
                                     mu_0=np.zeros(xDim),
                                     kappa_0=0.01)
                            for _ in range(K)]

    dynamics_distns = [Regression(nu_0=xDim + 1,
                                  S_0=xDim * np.eye(xDim),
                                  M_0=np.hstack((.99 * np.eye(xDim), np.zeros((xDim, inputDim)))),
                                  K_0=xDim * np.eye(xDim + inputDim))
                       for _ in range(K)]

    As = [np.eye(xDim) for _ in range(K)]
    if inputDim > 0:
        As = [np.hstack((A, np.zeros((xDim, inputDim))))
              for A in As]
    for dd, A in zip(dynamics_distns, As):
        dd.A = A

    sigma_statess = [np.eye(xDim) for _ in range(K)]
    for dd, sigma in zip(dynamics_distns, sigma_statess):
        dd.sigma = sigma

    emission_distns = [DiagonalRegression(yDim, xDim + inputDim,
                                          mu_0=None, Sigma_0=None,
                                          alpha_0=3.0, beta_0=2.0,
                                          A=np.hstack((C_init.copy(), D_init.copy())),
                                          sigmasq=None, niter=1)
                       for _ in range(K)]

    train_model = HMMSLDS(
        init_dynamics_distns= init_dynamics_distns,
        dynamics_distns= dynamics_distns,
        emission_distns= emission_distns,
        alpha=3., init_state_distn='uniform')

    # Adding training data
    for trial in range(numTrial):
        train_model.add_data(unit_trial[trial].T, inputs=inputs)

    print("Initializing with Gibbs")
    N_gibbs_samples = 2000
    def initialize(model):
        model.resample_model()
        return model.log_likelihood()

    gibbs_lls = [initialize(train_model) for _ in progprint_xrange(N_gibbs_samples)]

    print("Fitting with VBEM")
    N_vbem_iters = 100
    def update(model):
        model.VBEM_step()
        return model.log_likelihood()

    train_model._init_mf_from_gibbs()
    vbem_lls = [update(train_model) for _ in progprint_xrange(N_vbem_iters)]

    np.save(fileName + '_gibbs_lls', gibbs_lls)
    np.save(fileName + '_vbem_lls', vbem_lls)
    np.save(fileName + '_train_model', train_model)

    return train_model