<jupyter_start><jupyter_text>Now, we define some toy problem<jupyter_code># first demo -- gaussian mixture defined by the CRP

def build_static_dataset(N, beta=0.1):
    """ 
    2 events, modeled as gaussians with different means
    """
    
    pi = np.array([0.4, 0.6])
    mus = [[1, 1], [-1, -1]]
    x = np.zeros((N, 2), dtype=np.float32)
    for n in range(int(N/2)):
        x[n, :] = np.random.multivariate_normal(mus[0], np.eye(2) * beta)
    for n in range(int(N/2), N):
        x[n, :] = np.random.multivariate_normal(mus[1], np.eye(2) * beta)
        
    return x

x_train = build_static_dataset(50, 0.01)
y = np.concatenate([np.zeros((25), dtype=int), np.ones((25), dtype=int)])
plt.plot(x_train[:, 0], x_train[:, 1])
# SEM parameters
lmda = 10.0  # stickyness parameter
alfa = .01  # concentration parameter


# define plotting function
import seaborn as sns

def plot_segmentation(post, y):
    cluster_id = np.argmax(post, axis=1)
    cc = sns.color_palette('Dark2', post.shape[1])
    
    fig, axes = plt.subplots(1, 2, figsize=(12, 4), gridspec_kw=dict(width_ratios=[1, 2]))
    for clt in cluster_id:
        idx = np.nonzero(cluster_id == clt)[0]
        axes[0].scatter(x_train[idx, 0], x_train[idx, 1], color=cc[clt], alpha=.5)
    axes[0].set_xlabel(r'$\mathbf{x}_{s,1}$')
    axes[0].set_ylabel(r'$\mathbf{x}_{s,2}$')


    axes[1].plot(post)
    y_hat = np.argmax(post, axis=1)
    axes[1].set_xlabel('Time')
    axes[1].set_ylabel('Cluster ID')
    print("Adjusted Rand Score: {}\n".format(metrics.adjusted_rand_score(y, y_hat)))
    print(np.argmax(post, axis=1))<jupyter_output><empty_output><jupyter_text>Linear Events<jupyter_code># Initialize keras model
sem_kwargs2 = dict(
    lmda=lmda, alfa=alfa, f_class=LinearEvent, 
    f_opts=dict(l2_regularization=0.4)  
)
# using the f_opts kwargs, we can set options for the event model as well.  Here, 
# we've added l2 regularization to the network, but normally this can just be left empty


results = sem_run(x_train, sem_kwargs2)
plot_segmentation(results.post, y)<jupyter_output>Run SEM: 100%|██████████| 50/50 [00:06<00:00,  7.48it/s]<jupyter_text>Non-linear Events<jupyter_code># Initialize keras model
sem_kwargs3 = dict(
    lmda=lmda, alfa=alfa, f_class=NonLinearEvent,
    f_opts=dict()
)

results = sem_run(x_train, sem_kwargs3)
plot_segmentation(results.post, y)<jupyter_output>Run SEM: 100%|██████████| 50/50 [00:08<00:00,  6.17it/s]<jupyter_text>Recurrent Linear Events<jupyter_code># Initialize keras model
sem_kwargs4 = dict(
    lmda=lmda, alfa=alfa, f_class=RecurrentLinearEvent, 
    f_opts=dict(t=3)
)

results = sem_run(x_train, sem_kwargs4)
plot_segmentation(results.post, y)<jupyter_output>Run SEM: 100%|██████████| 50/50 [00:07<00:00,  7.01it/s]<jupyter_text>Recurrent Non-linear Events<jupyter_code>sem_kwargs5 = dict(
    lmda=lmda, alfa=alfa, f_class=RecurrentEvent,
    f_opts=dict(t=3)
)

results = sem_run(x_train, sem_kwargs5)
plot_segmentation(results.post, y)<jupyter_output>Run SEM: 100%|██████████| 50/50 [00:07<00:00,  6.97it/s]<jupyter_text>Gated Recurrent Unit Events Note: this is the default event type of the model<jupyter_code>sem_kwargs6 = dict(
    lmda=lmda, alfa=alfa, f_class=GRUEvent,
    f_opts=dict(t=3)
)

results = sem_run(x_train, sem_kwargs6)
plot_segmentation(results.post, y)

# sem_model = SEM(**sem_kwargs6)
# sem_model.run(x_train)
# plot_segmentation(sem_model.results.post, y)<jupyter_output>Run SEM: 100%|██████████| 50/50 [00:12<00:00,  3.95it/s]<jupyter_text>LSTM Events<jupyter_code>sem_kwargs7 = dict(
    lmda=lmda, alfa=alfa, f_class=LSTMEvent,
    f_opts=dict(t=3)
               )

results = sem_run(x_train, sem_kwargs7)
plot_segmentation(results.post, y)<jupyter_output>Run SEM: 100%|██████████| 50/50 [00:13<00:00,  3.77it/s]<jupyter_text>Alternating between 2 static events<jupyter_code>def build_alternating_static_events(N, beta=0.1):
    """ 
    2 alternating events, modeled as gaussians with different means
    """
    
    pi = np.array([0.4, 0.6])
    mus = [[1, 1], [-1, -1]]
    stds = np.ones((2, 2)) * beta
    x = np.zeros((N, 2), dtype=np.float32)
    for n in range(int(N/4)):
        x[n, :] = np.random.multivariate_normal(mus[0], np.diag(stds[0]))
    for n in range(int(N/4), int(N/2)):
        x[n, :] = np.random.multivariate_normal(mus[1], np.diag(stds[1]))
    for n in range(int(N/2), int(N/4*3)):
        x[n, :] = np.random.multivariate_normal(mus[0], np.diag(stds[0]))
    for n in range(int(N/4*3), N):
        x[n, :] = np.random.multivariate_normal(mus[1], np.diag(stds[1]))
        
    return x

x_train = build_alternating_static_events(100, 0.01)
y = np.concatenate([np.zeros(25, dtype=int), np.ones(25, dtype=int), np.zeros(25, dtype=int), np.ones(25, dtype=int)])
plt.plot(x_train[:, 0], x_train[:, 1])<jupyter_output><empty_output><jupyter_text>Linear Events<jupyter_code>results = sem_run(x_train, sem_kwargs2)
plot_segmentation(results.post, y)<jupyter_output>Run SEM: 100%|██████████| 100/100 [00:12<00:00,  8.18it/s]<jupyter_text>Non-linear Events<jupyter_code>results = sem_run(x_train, sem_kwargs3)
plot_segmentation(results.post, y)<jupyter_output>Run SEM: 100%|██████████| 100/100 [00:13<00:00,  7.44it/s]<jupyter_text>Recurrent Linear Events<jupyter_code>results = sem_run(x_train, sem_kwargs4)
plot_segmentation(results.post, y)<jupyter_output>Run SEM: 100%|██████████| 100/100 [00:20<00:00,  4.95it/s]<jupyter_text>Recurrent Non-linear Events<jupyter_code>results = sem_run(x_train, sem_kwargs5)
plot_segmentation(results.post, y)<jupyter_output>Run SEM: 100%|██████████| 100/100 [00:16<00:00,  6.07it/s]<jupyter_text>GRU Events<jupyter_code>results = sem_run(x_train, sem_kwargs6)
plot_segmentation(results.post, y)<jupyter_output>Run SEM: 100%|██████████| 100/100 [00:17<00:00,  5.71it/s]<jupyter_text>LSTM Events<jupyter_code>results = sem_run(x_train, sem_kwargs7)
plot_segmentation(results.post, y)<jupyter_output>Run SEM: 100%|██████████| 100/100 [00:13<00:00,  7.42it/s]<jupyter_text>Example of four events where the events correspond to movement in vector space<jupyter_code>def build_moving_data(N, beta=0.01):
    pi = np.array([0.4, 0.6])
    bs = [[1, 1], [-1, -1]]  # starting point!
    ws = [np.array([[1, 0], [.5, 2]]), np.array([[-1, .2], [-.2, 1]])]
    stds = np.eye(2) * beta
    x = np.zeros((N, 2), dtype=np.float32)
    
    x[0, :] = np.random.multivariate_normal(bs[0], stds)
    for n in range(1, int(N/2)):
        _x = x[n-1, :] + np.array([0.05, 0.05])
        x[n, :] = np.random.multivariate_normal(_x, stds)
        
    x[int(N/2), :] = np.random.multivariate_normal(bs[1], stds)
    for n in range(int(N/2+1), N):
        _x = x[n-1, :] + np.array([-0.05, -0.05])
        x[n, :] = np.random.multivariate_normal(_x, stds)
    return x

x_train = build_moving_data(100)
y = np.concatenate([np.zeros(50, dtype=int), np.ones(50, dtype=int)])
plt.plot(x_train[:, 0], x_train[:, 1])<jupyter_output><empty_output><jupyter_text>Linear Events<jupyter_code>results = sem_run(x_train, sem_kwargs2)
plot_segmentation(results.post, y)<jupyter_output>Run SEM: 100%|██████████| 100/100 [00:09<00:00, 10.35it/s]<jupyter_text>Non-linear Events<jupyter_code>results = sem_run(x_train, sem_kwargs3)
plot_segmentation(results.post, y)<jupyter_output>Run SEM: 100%|██████████| 100/100 [00:11<00:00,  8.67it/s]<jupyter_text>Recurrent Linear Events<jupyter_code>results = sem_run(x_train, sem_kwargs4)
plot_segmentation(results.post, y)<jupyter_output>Run SEM: 100%|██████████| 100/100 [00:14<00:00,  7.11it/s]<jupyter_text>Recurrent Non-linear Events<jupyter_code>results = sem_run(x_train, sem_kwargs5)
plot_segmentation(results.post, y)<jupyter_output>Run SEM: 100%|██████████| 100/100 [00:11<00:00,  8.74it/s]<jupyter_text>GRU Events<jupyter_code>results = sem_run(x_train, sem_kwargs6)
plot_segmentation(results.post, y)<jupyter_output>Run SEM: 100%|██████████| 100/100 [00:13<00:00,  7.63it/s]<jupyter_text>LSTM Events<jupyter_code>results = sem_run(x_train, sem_kwargs7)
plot_segmentation(results.post, y)<jupyter_output>Run SEM: 100%|██████████| 100/100 [00:13<00:00,  7.52it/s]<jupyter_text>Two alternating events with different levels of noise (10x)<jupyter_code>def build_alternating_static_events(N, beta=0.1):
    """ 
    2 alternating events, modeled as gaussians with different means
    """
    
    pi = np.array([0.4, 0.6])
    mus = [[1, 1], [-1, -1]]
    stds = np.array([beta, beta * 10.])
    x = np.zeros((N, 2), dtype=np.float32)
    for n in range(int(N/4)):
        x[n, :] = np.random.multivariate_normal(mus[0], np.eye(2) * stds[0])
    for n in range(int(N/4), int(N/2)):
        x[n, :] = np.random.multivariate_normal(mus[1], np.eye(2) * stds[1])
    for n in range(int(N/2), int(N/4*3)):
        x[n, :] = np.random.multivariate_normal(mus[0], np.eye(2) * stds[0])
    for n in range(int(N/4*3), N):
        x[n, :] = np.random.multivariate_normal(mus[1], np.eye(2) * stds[1])
        
    return x

x_train = build_alternating_static_events(100, 0.01)
y = np.concatenate([np.zeros(25, dtype=int), np.ones(25, dtype=int),
                    np.zeros(25, dtype=int), np.ones(25, dtype=int)])
plt.plot(x_train[:, 0], x_train[:, 1])<jupyter_output><empty_output><jupyter_text>Linear Events<jupyter_code>results = sem_run(x_train, sem_kwargs2)
plot_segmentation(results.post, y)<jupyter_output>Run SEM: 100%|██████████| 100/100 [00:09<00:00, 10.15it/s]<jupyter_text>Non-linear Events<jupyter_code>results = sem_run(x_train, sem_kwargs3)
plot_segmentation(results.post, y)<jupyter_output>Run SEM: 100%|██████████| 100/100 [00:10<00:00,  9.87it/s]<jupyter_text>Recurrent Linear Events<jupyter_code>results = sem_run(x_train, sem_kwargs4)
plot_segmentation(results.post, y)<jupyter_output>Run SEM: 100%|██████████| 100/100 [00:12<00:00,  8.09it/s]<jupyter_text>Recurrent Non-linear Events<jupyter_code>results = sem_run(x_train, sem_kwargs5)
plot_segmentation(results.post, y)<jupyter_output>Run SEM: 100%|██████████| 100/100 [00:11<00:00,  8.76it/s]<jupyter_text>GRU Events<jupyter_code>results = sem_run(x_train, sem_kwargs6)
plot_segmentation(results.post, y)<jupyter_output>Run SEM: 100%|██████████| 100/100 [00:13<00:00,  7.38it/s]<jupyter_text>LSTM Events<jupyter_code>results = sem_run(x_train, sem_kwargs7)
plot_segmentation(results.post, y)<jupyter_output>Run SEM: 100%|██████████| 100/100 [00:22<00:00,  4.53it/s]<jupyter_text>4 events where the events correspond to movement in vector space<jupyter_code>def build_alternating_moving_events(N, beta=0.01):
    pi = np.array([0.4, 0.6])
    bs = [[1, 1], [-1, -1], [1, -1], [-1, 1]]  # starting point!
    ws = [np.array([[1, 0], [.5, 2]]), np.array([[-1, .2], [-.2, 1]])]
    Sigmas = [np.eye(2) * beta* 5.,
            np.eye(2) * beta / 5.,
            np.eye(2) * beta * 5.,
            np.eye(2) * beta / 5.]
    x = np.zeros((N, 2), dtype=np.float32)
    
    x[0, :] = np.random.multivariate_normal(bs[0], Sigmas[0])
    for n in range(1, int(N/4)):
        _x = x[n-1, :] + np.array([0.05, 0.05])
        x[n, :] = np.random.multivariate_normal(_x, Sigmas[0])
        
    x[int(N/4), :] = np.random.multivariate_normal(bs[1], Sigmas[1])
    for n in range(int(N/4+1), int(N/2)):
        _x = x[n-1, :] + np.array([-0.05, -0.05])
        x[n, :] = np.random.multivariate_normal(_x, Sigmas[1])
            
    x[int(N/2), :] = np.random.multivariate_normal(bs[2], Sigmas[2])
    for n in range(int(N/2+1), int(N/4*3)):
        _x = x[n-1, :] + np.array([-0.05, -0.05])
        x[n, :] = np.random.multivariate_normal(_x, Sigmas[2])
        
    x[int(N/4*3), :] = np.random.multivariate_normal(bs[3], Sigmas[3])
    for n in range(int(N/4*3+1), N):
        _x = x[n-1, :] + np.array([0.05, 0.05])
        x[n, :] = np.random.multivariate_normal(_x, Sigmas[3])      
        
    return x

x_train = build_alternating_moving_events(100)
y = np.concatenate([np.zeros(25, dtype=int), np.ones(25, dtype=int), 
                    np.ones(25, dtype=int)*2, np.ones(25, dtype=int)*3])

plt.plot(x_train[:, 0], x_train[:, 1])<jupyter_output><empty_output><jupyter_text>Linear Events<jupyter_code>results = sem_run(x_train, sem_kwargs2)
plot_segmentation(results.post, y)<jupyter_output>Run SEM: 100%|██████████| 100/100 [00:23<00:00,  4.25it/s]<jupyter_text>Non-linear Events<jupyter_code>results = sem_run(x_train, sem_kwargs3)
plot_segmentation(results.post, y)<jupyter_output>Run SEM: 100%|██████████| 100/100 [00:24<00:00,  4.10it/s]<jupyter_text>Recurrent Linear Events<jupyter_code>results = sem_run(x_train, sem_kwargs4)
plot_segmentation(results.post, y)<jupyter_output>Run SEM: 100%|██████████| 100/100 [00:25<00:00,  3.85it/s]<jupyter_text>Recurrent Non-linear Events<jupyter_code>results = sem_run(x_train, sem_kwargs5)
plot_segmentation(results.post, y)<jupyter_output>Run SEM: 100%|██████████| 100/100 [00:24<00:00,  4.04it/s]<jupyter_text>GRU Events<jupyter_code>results = sem_run(x_train, sem_kwargs6)
plot_segmentation(results.post, y)<jupyter_output>Run SEM:  50%|█████     | 50/100 [00:13<00:09,  5.51it/s]<jupyter_text>LSTM Events<jupyter_code>results = sem_run(x_train, sem_kwargs7)
plot_segmentation(results.post, y)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.19064611646799787, 'fraction_numerical': 0.07114114342115253, 'mean_word_length': 4.8829834747655205, 'pattern_counts': {'":': 0, '<': 141, '<?xml version=': 0, '>': 112, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '35', 'dir': 'jupyter-structured-clean-dedup', 'id': '659668', 'license': 'MIT', 'n_tokens_mistral': 6375, 'n_tokens_neox': 5587, 'n_words': 911, 'path': 'Tutorials and Demos/Demo - Toy Data (Segmentation).ipynb', 'repo_name': 'sophiexingsu/SEM2', 'size': '13171'}