<jupyter_start><jupyter_text>Dataset splitsCreate a stratified 65-20-15 training, validation and test set split.<jupyter_code># Create a stratified training, validation and test set split (65-20-15)
X_train, X_val, X_test, y_train, y_val, y_test = data_split(X, y, (65, 20, 15), random_state=rng, stratify=True)
# MoCap dataset class counts (training set)
show_class_counts(y_train, gestures, title=None)
# Histogram of MoCap dataset gesture durations (training set)
show_durations(X_train, bins=75, title=None)<jupyter_output><empty_output><jupyter_text>Preprocessing<jupyter_code># Create a preprocessing pipeline
pre = Preprocess([
    Filter(window_size=10, method='median'),
    BinDownsample(bin_size=50, method='decimate'),
    Center()
])
pre.summary()
# Function for visualizing gesture signals
def plot_gesture(gesture, label, figsize=(7, 5), same_scale=True):
    labels = ['$\\varphi^{(t)}_x$', '$\\varphi^{(t)}_y$', '$\\varphi^{(t)}_z$']
    colors = ['blue', 'red', 'green']
    fig, axs = plt.subplots(3, 1, sharex=True, figsize=figsize)
    
    for i, ax in enumerate(axs.flat):
        ax.plot(gesture[:, i], color=colors[i])
        ax.set_ylabel(labels[i], rotation=0, labelpad=7, fontsize=14)
        if same_scale:
            pad = 0.05
            ax.set_ylim(gesture.min()-pad, gesture.max()+pad)
    ax.set_xlabel('Duration (frames)')
        
    fig.tight_layout()
#     plt.savefig('plots/example-rotation-signal.pdf')
    plt.show()
# Pick an example signal for visualization
x_sample, y_sample = X_train[1], y_train[1]
plot_gesture(x_sample, y_sample)
plot_gesture(pre.transform(x_sample), y_sample)
# Transform training data and plot histogram of MoCap dataset gesture durations (training set)
Xp_train = pre.fit_transform(X_train, verbose=True)
show_durations(Xp_train, bins=75, title=None)
# Apply the preprocessing pipeline to the other dataset splits
Xp_val, Xp_test = pre.transform(X_val, verbose=True), pre.transform(X_test, verbose=True)<jupyter_output><empty_output><jupyter_text>DTWKNN classifier  Fitting the model<jupyter_code>%%time
# Create and fit a DTWKNN classifier using the single nearest neighbor and a radius of 1
# NOTE: The radius parameter is a parameter that constrains the FastDTW algorithm.
clfs['knn'] = DTWKNN(k=1, radius=1)
clfs['knn'].fit(Xp_train, y_train)<jupyter_output>CPU times: user 3.6 ms, sys: 2.91 ms, total: 6.51 ms
Wall time: 6.38 ms<jupyter_text>Evaluating the model<jupyter_code>%%time
results['knn']['validation'] = clfs['knn'].evaluate(Xp_val, y_val, labels=gestures, n_jobs=-1)
show_results(*results['knn']['validation'], dataset='validation', labels=gestures)<jupyter_output><empty_output><jupyter_text>Hidden Markov Model classifierOne gesture model $\lambda_i=(A_i,B_i,\pi_i)$ is initialized and trained for each of the gestures: `nd`, `mnd`, `sh`, `fd`, `t`, `ti`, `fu`.  Training the model<jupyter_code>%%time

# Create HMMs to represent each class
#
# NumPy sometimes raises some errors as a result of instability during the Cholesky decomposition.
# According to issue #414 on Pomegranate's GitHub repository, this may be caused by:
# - Too many states in the HMMs
# - Too many dimensions in the input data, which leads to a large covariance matrix
# - Too few training examples
hmms = []
for g in tqdm(gestures, desc='Training HMMs'):
    hmm = HMM(label=g, n_states=8, random_state=rng)
    hmm.set_random_initial()
    hmm.set_random_transitions()
    hmm.fit([Xp_train[i] for i, label in enumerate(y_train) if label == g])
    hmms.append(hmm)
    
# Fit a HMM classifier with the HMMs
clfs['hmm'] = HMMClassifier()
clfs['hmm'].fit(hmms)<jupyter_output><empty_output><jupyter_text>Evaluating the model<jupyter_code>%%time
results['hmm']['validation'] = clfs['hmm'].evaluate(Xp_val, y_val, labels=gestures)
show_results(*results['hmm']['validation'], dataset='validation', labels=gestures)
with np.printoptions(precision=3, suppress=True):
    display(hmms[0].initial)
    display(hmms[0].transitions)<jupyter_output><empty_output><jupyter_text>Feedforward Neural Network classifier  Fitting the model<jupyter_code>%%time
clfs['ffnn'] = NNClassifier(epochs=114, batch_size=128, optimizer=Adam(learning_rate=0.002), classes=gestures)
hist = clfs['ffnn'].fit([
        Flatten(),
        Dense(300, activation='relu'),
        Dropout(0.5),
        Dense(300, activation='relu'),
        Dropout(0.5),
        Dense(len(gestures), activation='softmax')
    ], 
    Xp_train, y_train, validation_data=(Xp_val, y_val), verbose=1, return_history=True, early_stop=False
)
# Summarize the model
clfs['ffnn'].summary()
# Display accuracy and loss history during training
show_accuracy_history('FFNN', hist)
show_loss_history('FFNN', hist)<jupyter_output><empty_output><jupyter_text>Evaluating the model<jupyter_code>%%time
results['ffnn']['validation'] = clfs['ffnn'].evaluate(Xp_val, y_val)
show_results(*results['ffnn']['validation'], dataset='validation', labels=gestures)<jupyter_output><empty_output><jupyter_text>LSTM classifier  Fitting the model<jupyter_code>%%time
clfs['lstm'] = NNClassifier(epochs=175, batch_size=128, optimizer=Adam(learning_rate=0.002), classes=gestures)
hist = clfs['lstm'].fit([
    Bidirectional(LSTM(300, return_sequences=True, recurrent_dropout=0.5)),
    Bidirectional(LSTM(300, recurrent_dropout=0.5)),
    Dense(100, activation='relu'),
    Dropout(0.5),
    Dense(len(gestures), activation='softmax')
], Xp_train, y_train, validation_data=(Xp_val, y_val), return_history=True, verbose=1, early_stop=False)
# Summarize the model
clfs['lstm'].summary()
# Display accuracy and loss history during training
show_accuracy_history('LSTM', hist)
show_loss_history('LSTM', hist)<jupyter_output><empty_output><jupyter_text>Evaluating the model<jupyter_code>%%time
results['lstm']['validation'] = clfs['lstm'].evaluate(Xp_val, y_val)
show_results(*results['lstm']['validation'], dataset='validation', labels=gestures)<jupyter_output><empty_output><jupyter_text>Hyper-parameter searches  HMM<jupyter_code>for repeat in range(5):
    seed = repeat
    np.random.seed(seed)
    rng = np.random.RandomState(seed=seed)
    
    X_train, X_val, X_test, y_train, y_val, y_test = data_split(X, y, (65, 20, 15), random_state=rng, stratify=True)
    Xp_train = pre.fit_transform(X_train, verbose=True)
    Xp_val, Xp_test = pre.transform(X_val, verbose=True), pre.transform(X_test, verbose=True)
    
    for n_states in range(3, 11):
        clf = None
        running_stats = {'fit': {}, 'predict': {}}
        
        def fit():
            # Fit HMMs
            hmms = []
            for g in tqdm(gestures, desc='Training HMMs', disable=True):
                hmm = HMM(label=g, n_states=n_states, random_state=rng)
                hmm.set_random_initial()
                hmm.set_random_transitions()
                hmm.fit([Xp_train[i] for i, label in enumerate(y_train) if label == g])
                hmms.append(hmm)
                
            # Create and fit the classifier
            clf = HMMClassifier()
            clf.fit(hmms)
            return clf
            
        t0 = time.time()
        mem, clf = memory_profiler.memory_usage(fit, retval=True)
        running_stats['fit']['memory'] = max(mem)
        running_stats['fit']['time'] = time.time() - t0

        def evaluate():
            # Evaluate the classifier
            return clf.evaluate(Xp_val, y_val, labels=gestures)
            
        t0 = time.time()
        mem, res = memory_profiler.memory_usage(evaluate, retval=True)
        running_stats['predict']['memory'] = max(mem)
        running_stats['predict']['time'] = time.time() - t0
        results['hmm']['validation'] = res
        
        # Write the results
        write_hmm_results(results, running_stats, '{}states'.format(n_states), 'validation', number=repeat, save_cm=True)<jupyter_output><empty_output><jupyter_text>DTWKNN  Radius parameter<jupyter_code>for repeat in range(5):
    seed = repeat
    np.random.seed(seed)
    rng = np.random.RandomState(seed=seed)
    
    X_train, X_val, X_test, y_train, y_val, y_test = data_split(X, y, (65, 20, 15), random_state=rng, stratify=True)
    Xp_train = pre.fit_transform(X_train, verbose=True)
    Xp_val, Xp_test = pre.transform(X_val, verbose=True), pre.transform(X_test, verbose=True)
    
    for radius in (1, 25, 50, 75, 100):
        clf = None
        running_stats = {'fit': {}, 'predict': {}}
        
        def fit():
            clf = DTWKNN(k=1, radius=radius)
            clf.fit(Xp_train, y_train)
            return clf
            
        t0 = time.time()
        mem, clf = memory_profiler.memory_usage(fit, retval=True)
        running_stats['fit']['memory'] = max(mem)
        running_stats['fit']['time'] = time.time() - t0

        def evaluate():
            return clf.evaluate(Xp_val, y_val, labels=gestures, n_jobs=-1)
            
        t0 = time.time()
        mem, res = memory_profiler.memory_usage(evaluate, retval=True, include_children=True)
        running_stats['predict']['memory'] = max(mem)
        running_stats['predict']['time'] = time.time() - t0
        results['knn']['validation'] = res
        
        # Write the results
        write_knn_results(results, running_stats, '{}radius'.format(radius), 'validation', number=repeat, save_cm=True)<jupyter_output><empty_output><jupyter_text>Neighborhood parameter ($k$)<jupyter_code>for repeat in range(5):
    seed = repeat
    np.random.seed(seed)
    rng = np.random.RandomState(seed=seed)
    
    X_train, X_val, X_test, y_train, y_val, y_test = data_split(X, y, (65, 20, 15), random_state=rng, stratify=True)
    Xp_train = pre.fit_transform(X_train, verbose=True)
    Xp_val, Xp_test = pre.transform(X_val, verbose=True), pre.transform(X_test, verbose=True)
    
    for k in (1, 5, 10, 15, 20, 25, 30):
        clf = None
        running_stats = {'fit': {}, 'predict': {}}
        
        def fit():
            clf = DTWKNN(k=k, radius=1)
            clf.fit(Xp_train, y_train)
            return clf
            
        t0 = time.time()
        mem, clf = memory_profiler.memory_usage(fit, retval=True)
        running_stats['fit']['memory'] = max(mem)
        running_stats['fit']['time'] = time.time() - t0

        def evaluate():
            return clf.evaluate(Xp_val, y_val, labels=gestures, n_jobs=-1)
            
        t0 = time.time()
        mem, res = memory_profiler.memory_usage(evaluate, retval=True, include_children=True)
        running_stats['predict']['memory'] = max(mem)
        running_stats['predict']['time'] = time.time() - t0
        results['knn']['validation'] = res
        
        # Write the results
        write_knn_results(results, running_stats, '{}neighbors'.format(k), 'validation', number=repeat, save_cm=True)<jupyter_output><empty_output><jupyter_text>FFNN<jupyter_code>for repeat in range(5):
    seed = repeat
    np.random.seed(seed)
    rng = np.random.RandomState(seed=seed)
    
    X_train, X_val, X_test, y_train, y_val, y_test = data_split(X, y, (65, 20, 15), random_state=rng, stratify=True)
    Xp_train = pre.fit_transform(X_train, verbose=True)
    Xp_val, Xp_test = pre.transform(X_val, verbose=True), pre.transform(X_test, verbose=True)
    
    for n_layers in (1, 2, 3, 4):
        for n_units in (100, 150, 200, 250, 300):    
            clf = None
            running_stats = {'fit': {}, 'predict': {}}

            def fit():
                clf = NNClassifier(epochs=500, batch_size=128, optimizer=Adam(learning_rate=0.002), classes=gestures)
                
                arch = []
                arch.append(Flatten())
                for _ in range(n_layers):
                    arch += [Dense(n_units, activation='relu'), Dropout(0.5)]
                arch.append(Dense(len(gestures), activation='softmax'))
                
                hist = clf.fit(arch, Xp_train, y_train, validation_data=(Xp_val, y_val), return_history=True, verbose=2, patience=40)
                return clf, hist

            t0 = time.time()
            mem, out = memory_profiler.memory_usage(fit, retval=True)
            clf, hist = out
            running_stats['fit']['memory'] = max(mem)
            running_stats['fit']['time'] = time.time() - t0

            def evaluate():
                return clf.evaluate(Xp_val, y_val)

            t0 = time.time()
            mem, res = memory_profiler.memory_usage(evaluate, retval=True)
            running_stats['predict']['memory'] = max(mem)
            running_stats['predict']['time'] = time.time() - t0
            results['ffnn']['validation'] = res

            # Write the results
            write_network_results('ffnn', results, hist, running_stats, '{}layers {}units'.format(n_layers, n_units), 'validation', number=repeat)<jupyter_output><empty_output><jupyter_text>LSTM<jupyter_code>for repeat in range(5):
    seed = repeat
    np.random.seed(seed)
    rng = np.random.RandomState(seed=seed)
    
    X_train, X_val, X_test, y_train, y_val, y_test = data_split(X, y, (65, 20, 15), random_state=rng, stratify=True)
    Xp_train = pre.fit_transform(X_train, verbose=True)
    Xp_val, Xp_test = pre.transform(X_val, verbose=True), pre.transform(X_test, verbose=True)
    
    for n_layers in (1, 2, 3):
        for h_dim in (100, 200, 300):
    
            clf = None
            running_stats = {'fit': {}, 'predict': {}}

            def fit():
                clf = NNClassifier(epochs=500, batch_size=128, optimizer=Adam(learning_rate=0.002), classes=gestures)
                
                arch = []
                for _ in range(n_layers - 1):
                    arch.append(Bidirectional(LSTM(h_dim, return_sequences=True, recurrent_dropout=0.5)))
                arch.append(Bidirectional(LSTM(h_dim, recurrent_dropout=0.5)))
                arch.append(Dense(100, activation='relu'))
                arch.append(Dense(len(gestures), activation='softmax'))
                
                hist = clf.fit(arch, Xp_train, y_train, validation_data=(Xp_val, y_val), return_history=True, verbose=2, patience=40)
                return clf, hist

            t0 = time.time()
            mem, out = memory_profiler.memory_usage(fit, retval=True)
            clf, hist = out
            running_stats['fit']['memory'] = max(mem)
            running_stats['fit']['time'] = time.time() - t0

            def evaluate():
                return clf.evaluate(Xp_val, y_val)

            t0 = time.time()
            mem, res = memory_profiler.memory_usage(evaluate, retval=True)
            running_stats['predict']['memory'] = max(mem)
            running_stats['predict']['time'] = time.time() - t0
            results['lstm']['validation'] = res

            # Write the results
            write_network_results('lstm', results, hist, running_stats, '{}layers {}h_dim'.format(n_layers, h_dim), 'validation', number=repeat)<jupyter_output><empty_output><jupyter_text>Test results  DTWKNN<jupyter_code>for repeat in range(5):
    seed = repeat
    np.random.seed(seed)
    rng = np.random.RandomState(seed=seed)
    
    X_train, X_val, X_test, y_train, y_val, y_test = data_split(X, y, (65, 20, 15), random_state=rng, stratify=True)
    Xp_train = pre.fit_transform(X_train, verbose=True)
    Xp_val, Xp_test = pre.transform(X_val, verbose=True), pre.transform(X_test, verbose=True)
    
    clf = None
    running_stats = {'fit': {}, 'predict': {}}

    def fit():
        clf = DTWKNN(k=15, radius=1)
        clf.fit(Xp_train, y_train)
        return clf

    t0 = time.time()
    mem, clf = memory_profiler.memory_usage(fit, retval=True)
    running_stats['fit']['memory'] = max(mem)
    running_stats['fit']['time'] = time.time() - t0

    def evaluate():
        return clf.evaluate(Xp_test, y_test, labels=gestures, n_jobs=-1)

    t0 = time.time()
    mem, res = memory_profiler.memory_usage(evaluate, retval=True, include_children=True)
    running_stats['predict']['memory'] = max(mem)
    running_stats['predict']['time'] = time.time() - t0
    results['knn']['test'] = res

    # Write the results
    write_knn_results(results, running_stats, 'final', 'test', number=repeat, save_cm=True)<jupyter_output><empty_output><jupyter_text>HMM<jupyter_code>for repeat in range(5):
    seed = repeat
    np.random.seed(seed)
    rng = np.random.RandomState(seed=seed)
    
    X_train, X_val, X_test, y_train, y_val, y_test = data_split(X, y, (65, 20, 15), random_state=rng, stratify=True)
    Xp_train = pre.fit_transform(X_train, verbose=True)
    Xp_val, Xp_test = pre.transform(X_val, verbose=True), pre.transform(X_test, verbose=True)

    clf = None
    running_stats = {'fit': {}, 'predict': {}}

    def fit():
        # Fit HMMs
        hmms = []
        for g in tqdm(gestures, desc='Training HMMs', disable=True):
            hmm = HMM(label=g, n_states=8, random_state=rng)
            hmm.set_random_initial()
            hmm.set_random_transitions()
            hmm.fit([Xp_train[i] for i, label in enumerate(y_train) if label == g])
            hmms.append(hmm)

        # Create and fit the classifier
        clf = HMMClassifier()
        clf.fit(hmms)
        return clf

    t0 = time.time()
    mem, clf = memory_profiler.memory_usage(fit, retval=True)
    running_stats['fit']['memory'] = max(mem)
    running_stats['fit']['time'] = time.time() - t0

    def evaluate():
        # Evaluate the classifier
        return clf.evaluate(Xp_test, y_test, labels=gestures)

    t0 = time.time()
    mem, res = memory_profiler.memory_usage(evaluate, retval=True)
    running_stats['predict']['memory'] = max(mem)
    running_stats['predict']['time'] = time.time() - t0
    results['hmm']['test'] = res

    # Write the results
    write_hmm_results(results, running_stats, 'final', 'test', number=repeat, save_cm=True)<jupyter_output><empty_output><jupyter_text>FFNN<jupyter_code>epochs = None

for repeat in range(5):
    seed = repeat
    np.random.seed(seed)
    rng = np.random.RandomState(seed=seed)
    
    X_train, X_val, X_test, y_train, y_val, y_test = data_split(X, y, (65, 20, 15), random_state=rng, stratify=True)
    Xp_train = pre.fit_transform(X_train, verbose=True)
    Xp_val, Xp_test = pre.transform(X_val, verbose=True), pre.transform(X_test, verbose=True)

    clf = None
    running_stats = {'fit': {}, 'predict': {}}

    def fit():
        e = 500 if epochs is None else epochs
        clf = NNClassifier(epochs=e, batch_size=128, optimizer=Adam(learning_rate=0.002), classes=gestures)

        arch = [
            Flatten(),
            Dense(300, activation='relu'), 
            Dropout(0.5),
            Dense(300, activation='relu'), 
            Dropout(0.5),
            Dense(len(gestures), activation='softmax')
        ]

        if epochs is None:
            hist = clf.fit(arch, Xp_train, y_train, validation_data=(Xp_val, y_val), return_history=True, verbose=2, patience=40)
        else:
            hist = clf.fit(arch, Xp_train, y_train, validation_data=(Xp_val, y_val), return_history=True, verbose=2, early_stop=False)
        return clf, hist

    t0 = time.time()
    mem, out = memory_profiler.memory_usage(fit, retval=True)
    clf, hist = out
    running_stats['fit']['memory'] = max(mem)
    running_stats['fit']['time'] = time.time() - t0
    
    if epochs is None:
        epochs = len(hist.history['loss'])

    def evaluate():
        return clf.evaluate(Xp_test, y_test)

    t0 = time.time()
    mem, res = memory_profiler.memory_usage(evaluate, retval=True)
    running_stats['predict']['memory'] = max(mem)
    running_stats['predict']['time'] = time.time() - t0
    results['ffnn']['test'] = res

    # Write the results
    write_network_results('ffnn', results, hist, running_stats, 'final', 'test', number=repeat, save_history=True, save_cm=True)<jupyter_output><empty_output><jupyter_text>LSTM<jupyter_code>epochs = None

for repeat in range(5):
    seed = repeat
    np.random.seed(seed)
    rng = np.random.RandomState(seed=seed)
    
    X_train, X_val, X_test, y_train, y_val, y_test = data_split(X, y, (65, 20, 15), random_state=rng, stratify=True)
    Xp_train = pre.fit_transform(X_train, verbose=True)
    Xp_val, Xp_test = pre.transform(X_val, verbose=True), pre.transform(X_test, verbose=True)

    clf = None
    running_stats = {'fit': {}, 'predict': {}}

    def fit():
        e = 500 if epochs is None else epochs
        clf = NNClassifier(epochs=e, batch_size=128, optimizer=Adam(learning_rate=0.002), classes=gestures)
                
        arch = [
            Bidirectional(LSTM(300, return_sequences=True, recurrent_dropout=0.5)),
            Bidirectional(LSTM(300, recurrent_dropout=0.5)),
            Dense(100, activation='relu'),
            Dense(len(gestures), activation='softmax')
        ]

        if epochs is None:
            hist = clf.fit(arch, Xp_train, y_train, validation_data=(Xp_val, y_val), return_history=True, verbose=2, patience=40)
        else:
            hist = clf.fit(arch, Xp_train, y_train, validation_data=(Xp_val, y_val), return_history=True, verbose=2, early_stop=False)
        return clf, hist

    t0 = time.time()
    mem, out = memory_profiler.memory_usage(fit, retval=True)
    clf, hist = out
    running_stats['fit']['memory'] = max(mem)
    running_stats['fit']['time'] = time.time() - t0
    
    if epochs is None:
        epochs = len(hist.history['loss'])

    def evaluate():
        return clf.evaluate(Xp_test, y_test)

    t0 = time.time()
    mem, res = memory_profiler.memory_usage(evaluate, retval=True)
    running_stats['predict']['memory'] = max(mem)
    running_stats['predict']['time'] = time.time() - t0
    results['lstm']['test'] = res

    # Write the results
    write_network_results('lstm', results, hist, running_stats, 'final', 'test', number=repeat, save_history=True, save_cm=True)<jupyter_output><empty_output><jupyter_text>Preprocessing experiments  Filtering<jupyter_code>for repeat in range(5):
    seed = repeat
    np.random.seed(seed)
    rng = np.random.RandomState(seed=seed)
    
    X_train, X_val, X_test, y_train, y_val, y_test = data_split(X, y, (65, 20, 15), random_state=rng, stratify=True)
    
    for window_size in (1, 5, 10, 15, 20):
        pre = Preprocess([
            Filter(window_size=window_size, method='median'),
            Center()
        ])

        Xp_train = pre.fit_transform(X_train, verbose=False)
        Xp_val, Xp_test = pre.transform(X_val, verbose=False), pre.transform(X_test, verbose=False)
        
        running_stats = {
            'hmm': {'time_fit': None, 'time_predict': None}, 
            'ffnn': {'time_fit': None, 'time_predict': None}
        }
        
        # HMM classifier (fit)
        t0 = time.time()
        hmms = []
        for g in tqdm(gestures, desc='Training HMMs'):
            hmm = HMM(label=g, n_states=8, random_state=rng)
            hmm.set_random_initial()
            hmm.set_random_transitions()
            hmm.fit([Xp_train[i] for i, label in enumerate(y_train) if label == g])
            hmms.append(hmm)
        hmm_clf = HMMClassifier()
        hmm_clf.fit(hmms)
        running_stats['hmm']['time_fit'] = time.time() - t0
        
        # HMM classifier (predict)
        t0 = time.time()
        running_stats['hmm']['acc'], _ = hmm_clf.evaluate(Xp_val, y_val, labels=gestures)
        running_stats['hmm']['time_predict'] = time.time() - t0
        
        # FFNN classifier (fit)
        t0 = time.time()
        ffnn = NNClassifier(epochs=114, batch_size=128, optimizer=Adam(learning_rate=0.002), classes=gestures)
        ffnn.fit([
                Flatten(),
                Dense(300, activation='relu'),
                Dropout(0.5),
                Dense(300, activation='relu'),
                Dropout(0.5),
                Dense(len(gestures), activation='softmax')
            ], 
            Xp_train, y_train, validation_data=(Xp_val, y_val), verbose=0, return_history=False, early_stop=False
        )
        running_stats['ffnn']['time_fit'] = time.time() - t0
        
        # FFNN classifier (predict)
        t0 = time.time()
        running_stats['ffnn']['acc'], _ = ffnn.evaluate(Xp_val, y_val)
        running_stats['ffnn']['time_predict'] = time.time() - t0
        
        # Write results
        write_filter_results(running_stats, '{}filter'.format(window_size), 'validation', number=repeat)<jupyter_output><empty_output><jupyter_text>Downsampling (standard)<jupyter_code>for repeat in range(5):
    seed = repeat
    np.random.seed(seed)
    rng = np.random.RandomState(seed=seed)
    
    X_train, X_val, X_test, y_train, y_val, y_test = data_split(X, y, (65, 20, 15), random_state=rng, stratify=True)
    
    for factor in (1, 5, 10, 15, 20):
        pre = Preprocess([
            Filter(window_size=10, method='median'),
            Downsample(factor=factor, method='decimate'),
            Center()
        ])

        Xp_train = pre.fit_transform(X_train, verbose=False)
        Xp_val, Xp_test = pre.transform(X_val, verbose=False), pre.transform(X_test, verbose=False)
        
        running_stats = {
            'hmm': {'time_fit': None, 'time_predict': None}, 
            'ffnn': {'time_fit': None, 'time_predict': None}
        }
        
        # HMM classifier (fit)
        t0 = time.time()
        hmms = []
        for g in tqdm(gestures, desc='Training HMMs'):
            hmm = HMM(label=g, n_states=8, random_state=rng)
            hmm.set_random_initial()
            hmm.set_random_transitions()
            hmm.fit([Xp_train[i] for i, label in enumerate(y_train) if label == g])
            hmms.append(hmm)
        hmm_clf = HMMClassifier()
        hmm_clf.fit(hmms)
        running_stats['hmm']['time_fit'] = time.time() - t0
        
        # HMM classifier (predict)
        t0 = time.time()
        running_stats['hmm']['acc'], _ = hmm_clf.evaluate(Xp_val, y_val, labels=gestures)
        running_stats['hmm']['time_predict'] = time.time() - t0
        
        # FFNN classifier (fit)
        t0 = time.time()
        ffnn = NNClassifier(epochs=114, batch_size=128, optimizer=Adam(learning_rate=0.002), classes=gestures)
        ffnn.fit([
                Flatten(),
                Dense(300, activation='relu'),
                Dropout(0.5),
                Dense(300, activation='relu'),
                Dropout(0.5),
                Dense(len(gestures), activation='softmax')
            ], 
            Xp_train, y_train, validation_data=(Xp_val, y_val), verbose=0, return_history=False, early_stop=False
        )
        running_stats['ffnn']['time_fit'] = time.time() - t0
        
        # FFNN classifier (predict)
        t0 = time.time()
        running_stats['ffnn']['acc'], _ = ffnn.evaluate(Xp_val, y_val)
        running_stats['ffnn']['time_predict'] = time.time() - t0
        
        # Write results
        write_downsample_results(running_stats, '{}decimate'.format(factor), 'validation', number=repeat)<jupyter_output><empty_output><jupyter_text>Downsampling (bin)<jupyter_code>for repeat in range(5):
    seed = repeat
    np.random.seed(seed)
    rng = np.random.RandomState(seed=seed)
    
    X_train, X_val, X_test, y_train, y_val, y_test = data_split(X, y, (65, 20, 15), random_state=rng, stratify=True)
    
    for bin_size in (25, 50, 75, 100, 200):
        pre = Preprocess([
            Filter(window_size=10, method='median'),
            BinDownsample(bin_size=bin_size, method='decimate'),
            Center()
        ])

        Xp_train = pre.fit_transform(X_train, verbose=False)
        Xp_val, Xp_test = pre.transform(X_val, verbose=False), pre.transform(X_test, verbose=False)
        
        running_stats = {
            'hmm': {'time_fit': None, 'time_predict': None}, 
            'ffnn': {'time_fit': None, 'time_predict': None}
        }
        
        # HMM classifier (fit)
        t0 = time.time()
        hmms = []
        for g in tqdm(gestures, desc='Training HMMs'):
            hmm = HMM(label=g, n_states=8, random_state=rng)
            hmm.set_random_initial()
            hmm.set_random_transitions()
            hmm.fit([Xp_train[i] for i, label in enumerate(y_train) if label == g])
            hmms.append(hmm)
        hmm_clf = HMMClassifier()
        hmm_clf.fit(hmms)
        running_stats['hmm']['time_fit'] = time.time() - t0
        
        # HMM classifier (predict)
        t0 = time.time()
        running_stats['hmm']['acc'], _ = hmm_clf.evaluate(Xp_val, y_val, labels=gestures)
        running_stats['hmm']['time_predict'] = time.time() - t0
        
        # FFNN classifier (fit)
        t0 = time.time()
        ffnn = NNClassifier(epochs=114, batch_size=128, optimizer=Adam(learning_rate=0.002), classes=gestures)
        ffnn.fit([
                Flatten(),
                Dense(300, activation='relu'),
                Dropout(0.5),
                Dense(300, activation='relu'),
                Dropout(0.5),
                Dense(len(gestures), activation='softmax')
            ], 
            Xp_train, y_train, validation_data=(Xp_val, y_val), verbose=0, return_history=False, early_stop=False
        )
        running_stats['ffnn']['time_fit'] = time.time() - t0
        
        # FFNN classifier (predict)
        t0 = time.time()
        running_stats['ffnn']['acc'], _ = ffnn.evaluate(Xp_val, y_val)
        running_stats['ffnn']['time_predict'] = time.time() - t0
        
        # Write results
        write_downsample_results(running_stats, '{}bin'.format(bin_size), 'validation', number=repeat)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.13837032017618162, 'fraction_numerical': 0.017008300863967474, 'mean_word_length': 3.0460589444825223, 'pattern_counts': {'":': 0, '<': 88, '<?xml version=': 0, '>': 88, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '22', 'dir': 'jupyter-structured-clean-dedup', 'id': '481411', 'license': 'CC-BY-4.0', 'n_tokens_mistral': 10461, 'n_tokens_neox': 9838, 'n_words': 1944, 'path': 'notebooks/MoCap Recognition.ipynb', 'repo_name': 'eonu/inf4-hons', 'size': '29515'}